/*
 * myfuncomau.js
 * Javascript control for the Parks Scroller on top of MyFun.com.au home page
 * Uses the juery library on majority of functions
 * 
 * Last Updated 10/06/2010
 * Author: GF
 */


// Switches to No-Conflict mode by re-writing all '$' as 'j' to avoid conflict with other Javascript libraries.
var j = jQuery.noConflict();


/* --------------------------------------------------------- */
/* Hero Scroller */
/* --------------------------------------------------------- */

j(document).ready(function () {
		
	//jCarousel Plugin
	j('#heroScroller').jcarousel({
		vertical: true,
		scroll: 1,
		auto: 0,
		direction: 1,
		wrap: 'last'	
	});
		

	//Front page Carousel - Initial Setup
   	j('div#hero-main li:first').addClass("active");
	j('div#hero-carousel a img').css({'opacity': '0.5'});
   	j('div#hero-carousel a img:first').css({'opacity': '1.0'});
   	j('div#hero-carousel li a:first').append('<span class="arrow"></span>')


	// Hide arrows if no more than 4 heros
  	if (j('#hero-carousel li').length <= 4) {
  		j('#hero-carousel .jcarousel-prev, #hero-carousel .jcarousel-next').hide();	
  	}
	
	
  
  	//Combine jCarousel with Image Display
    j('div#hero-carousel li a').hover(
       	function () {
       		if (!j(this).has('span').length) {
        		//j('div#hero-carousel li a img').stop(true, true).css({'opacity': '0.4'});
   	    		j(this).stop(true, true).children('img').css({'opacity': '1.0'});
       		}		
       	},
       	function () { 		
       		j('div#hero-carousel li a img').stop(true, true).css({'opacity': '0.5'});
       		j('div#hero-carousel li a').each(function () {
       			if (j(this).has('span').length) j(this).children('img').css({'opacity': '1.0'});
       		});	
       	
       	}
	).click(function () {
		
		if(!j(this).has('span').length) {
            
	     j('div#hero-carousel li a').has('span.arrow').children("img").css({'opacity': '0.5'})
    	 j('div#hero-carousel li a span.arrow').remove();        
	     j(this).append('<span class="arrow"></span>');
 
    	var nextHero = j('div#hero-main li.' + j(this).attr('rel')); 
	    var shownHero = j('div#hero-main li.active');    
    	 nextHero.css({opacity: 0.0})
	     nextHero.addClass('active')
    	 nextHero.css({'z-index':'10'})
	     shownHero.css({'z-index':'9'})
    	 nextHero.animate({opacity: 1.0}, 400, function() {
        	  shownHero.removeClass('active');
	     });
                
	     clearInterval(slideshow);
		}
		return false;

	});
	
	
});


// -- Auto rotate through heros -- 	
	function slideSwitch() {
	
		var shownHero = j('div#hero-main li.active');
		if ( shownHero.length == 0 ) shownHero = j('div#hero-main ul li:last');
	
		var nextHero =  shownHero.next().length ? shownHero.next()
			: j('div#hero-main li:first'); 
					
		
			size = j('div#hero-main li').length;
			index = j('div#hero-main li.active').index();
			current = j('div#hero-carousel li:eq(' + (index + 1) + ')');
			
			j('div#hero-carousel li a').has('span.arrow').children("img").css({'opacity': '0.5'})
		    j('div#hero-carousel li a span.arrow').remove();        
			current.find('a').append('<span class="arrow"></span>');
			current.find('img').css({'opacity' : '1.0'});
			
			/*	
			console.log(size);
			console.log(j('div#hero-main li.active').index());
			console.log(shownHero.next().length);
			console.log(j('div#hero-carousel li:eq(' + index + ')').html());
			*/
			
			if ((index+1) % 4 == 0 || (index+1)>4) j('#hero-carousel .jcarousel-next').click();
			
			shownHero.addClass('last-active');
			nextHero.css({opacity: 0.0})
				.addClass('active')
				.animate({opacity: 1.0}, 300, function() {
					shownHero.removeClass('active last-active');
				});
		
		if (index == j('div#hero-main li:last').index()) {                
    	 j('div#hero-carousel li:first a').append('<span class="arrow"></span>');
	     j('div#hero-carousel li:first a img').css({'opacity' : '1.0'});                    
		}
					
	}
	

		var slideshow = setInterval( "slideSwitch()", 5000 );


//	*/
