/*
 * 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();

/* --------------------------------------------------------- */
/* Set Scrolling for Homepage GlobalNav Attraction Logos */
/* --------------------------------------------------------- */

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


j(document).ready(function() {
    j('#parksCarousel').jcarousel({
        auto: 0.01,
		scroll: 1,
		start: 11,
		direction: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
    j('#parksCarouselMelb').jcarousel({
        auto: 0.01,
		scroll: 1,
		start: 11,
		direction: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
    j('#parksCarouselGC').jcarousel({
        auto: 0.01,
		scroll: 1,
		start: 11,
		direction: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
    j('#parksCarouselSyd').jcarousel({
        auto: 0.01,
		scroll: 1,
		start: 11,
		direction: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });
	
	// Detect items that are to the right of scroller in order to flip the drop down
	j("#parksCarousel li:nth-child(6)").addClass("flipDrop");
	j("#parksCarousel li:nth-child(7)").addClass("flipDrop");
	j("#parksCarousel li:nth-child(8)").addClass("flipDrop");
	j("#parksCarousel li:nth-child(9)").addClass("flipDrop");
	j("#parksCarousel li:nth-child(10)").addClass("flipDrop");
	j("#parksCarousel li:nth-child(11)").addClass("flipDrop");
	// Detect items that are to the right of scroller in order to flip the drop down - Melbourne
	j("#parksCarouselMelb li:nth-child(6)").addClass("flipDrop");
	j("#parksCarouselMelb li:nth-child(7)").addClass("flipDrop");
	j("#parksCarouselMelb li:nth-child(8)").addClass("flipDrop");
	j("#parksCarouselMelb li:nth-child(9)").addClass("flipDrop");
	j("#parksCarouselMelb li:nth-child(10)").addClass("flipDrop");
	j("#parksCarouselMelb li:nth-child(11)").addClass("flipDrop");
	// Detect items that are to the right of scroller in order to flip the drop down - Gold Coast
	j("#parksCarouselGC li:nth-child(6)").addClass("flipDrop");
	j("#parksCarouselGC li:nth-child(7)").addClass("flipDrop");
	j("#parksCarouselGC li:nth-child(8)").addClass("flipDrop");
	j("#parksCarouselGC li:nth-child(9)").addClass("flipDrop");
	j("#parksCarouselGC li:nth-child(10)").addClass("flipDrop");
	j("#parksCarouselGC li:nth-child(11)").addClass("flipDrop");
	// Detect items that are to the right of scroller in order to flip the drop down - Sydney
	j("#parksCarouselSyd li:nth-child(6)").addClass("flipDrop");
	j("#parksCarouselSyd li:nth-child(7)").addClass("flipDrop");
	j("#parksCarouselSyd li:nth-child(8)").addClass("flipDrop");
	j("#parksCarouselSyd li:nth-child(9)").addClass("flipDrop");
	j("#parksCarouselSyd li:nth-child(10)").addClass("flipDrop");
	j("#parksCarouselSyd li:nth-child(11)").addClass("flipDrop"); 

	// Remove scroll if less items than 8
	if (j('#parksCarousel li').length <= 8) {
  		j('.jcarousel-prev, .jcarousel-next').addClass("noScroll");	
  	}
	
	// Detect when list has less than 8 and add specific class
	if (j('#parksCarousel li').length == 7) {
		j('#parkScroller .jcarousel-clip').addClass("parks-7");
  	}
	else if (j('#parksCarousel li').length == 6) {
		j('#parkScroller .jcarousel-clip').addClass("parks-6");
  	}
	else if (j('#parksCarousel li').length == 5) {
		j('#parkScroller .jcarousel-clip').addClass("parks-5");
	}
	else if (j('#parksCarousel li').length == 4) {
		j('#parkScroller .jcarousel-clip').addClass("parks-4");
  	}
	else if (j('#parksCarousel li').length == 3) {
		j('#parkScroller .jcarousel-clip').addClass("parks-3");
  	}
	else if (j('#parksCarousel li').length == 2) {
		j('#parkScroller .jcarousel-clip').addClass("parks-2");
  	}
	else if (j('#parksCarousel li').length == 1) {
		j('#parkScroller .jcarousel-clip').addClass("parks-1");
  	}
	// Detect when list has less than 8 and add specific class - Melbourne
	if (j('#parksCarouselMelb li').length == 7) {
		j('#parkScrollerMelb .jcarousel-clip').addClass("parks-7");
  	}
	else if (j('#parksCarouselMelb li').length == 6) {
		j('#parkScrollerMelb .jcarousel-clip').addClass("parks-6");
  	}
	else if (j('#parksCarouselMelb li').length == 5) {
		j('#parkScrollerMelb .jcarousel-clip').addClass("parks-5");
	}
	else if (j('#parksCarouselMelb li').length == 4) {
		j('#parkScrollerMelb .jcarousel-clip').addClass("parks-4");
  	}
	else if (j('#parksCarouselMelb li').length == 3) {
		j('#parkScrollerMelb .jcarousel-clip').addClass("parks-3");
  	}
	else if (j('#parksCarouselMelb li').length == 2) {
		j('#parkScrollerMelb .jcarousel-clip').addClass("parks-2");
  	}
	else if (j('#parksCarouselMelb li').length == 1) {
		j('#parkScrollerMelb .jcarousel-clip').addClass("parks-1");
  	}
	// Detect when list has less than 8 and add specific class - Gold Coast
	if (j('#parksCarouselGC li').length == 7) {
		j('#parkScrollerGC .jcarousel-clip').addClass("parks-7");
  	}
	else if (j('#parksCarouselGC li').length == 6) {
		j('#parkScrollerGC .jcarousel-clip').addClass("parks-6");
  	}
	else if (j('#parksCarouselGC li').length == 5) {
		j('#parkScrollerGC .jcarousel-clip').addClass("parks-5");
	}
	else if (j('#parksCarouselGC li').length == 4) {
		j('#parkScrollerGC .jcarousel-clip').addClass("parks-4");
  	}
	else if (j('#parksCarouselGC li').length == 3) {
		j('#parkScrollerGC .jcarousel-clip').addClass("parks-3");
  	}
	else if (j('#parksCarouselGC li').length == 2) {
		j('#parkScrollerGC .jcarousel-clip').addClass("parks-2");
  	}
	else if (j('#parksCarouselGC li').length == 1) {
		j('#parkScrollerGC .jcarousel-clip').addClass("parks-1");
  	}
	// Detect when list has less than 8 and add specific class - Sydney
	if (j('#parksCarouselSyd li').length == 7) {
		j('#parkScrollerSyd .jcarousel-clip').addClass("parks-7");
  	}
	else if (j('#parksCarouselSyd li').length == 6) {
		j('#parkScrollerSyd .jcarousel-clip').addClass("parks-6");
  	}
	else if (j('#parksCarouselSyd li').length == 5) {
		j('#parkScrollerSyd .jcarousel-clip').addClass("parks-5");
	}
	else if (j('#parksCarouselSyd li').length == 4) {
		j('#parkScrollerSyd .jcarousel-clip').addClass("parks-4");
  	}
	else if (j('#parksCarouselSyd li').length == 3) {
		j('#parkScrollerSyd .jcarousel-clip').addClass("parks-3");
  	}
	else if (j('#parksCarouselSyd li').length == 2) {
		j('#parkScrollerSyd .jcarousel-clip').addClass("parks-2");
  	}
	else if (j('#parksCarouselSyd li').length == 1) {
		j('#parkScrollerSyd .jcarousel-clip').addClass("parks-1");
  	} 
	
	// Flag the last item in the list with a class - All
	j('#parksCarousel li:last').addClass("jcarousel-item-last");
	// Flag the last item in the list with a class - Melbourne
	j('#parksCarouselMelb li:last').addClass("jcarousel-item-last");
	// Flag the last item in the list with a class - Gold Coast
	j('#parksCarouselGC li:last').addClass("jcarousel-item-last");
	// Flag the last item in the list with a class - Syndey
	j('#parksCarouselSyd li:last').addClass("jcarousel-item-last"); 

});


/* --------------------------------------------------------- */
/* Homepage parks dropdowns */
/* --------------------------------------------------------- */

j(document).ready(function() {

	j("ul#parksCarousel li div.desc").hide();
	j("ul#parksCarousel li span.dropArrow").hide();

	j("ul#parksCarousel").mouseout(function () {
		j("ul#parksCarousel li div.desc").hide();			
	});
	
	function expandDesc(){  
		j("div.desc").hide();
		j(this).find("div.desc").slideDown(300);
		j(this).find("span.dropArrow").show();
		j("#parkScroller .jcarousel-clip-horizontal").addClass("expand")
	}
	function makeHidden(){
		j("div.desc").hide();
		//j("div.desc").slideUp(1);
		j("span.dropArrow").hide();
		j("#parkScroller .jcarousel-clip-horizontal").removeClass("expand")
	}

	//Initiate Dropdown using the hoverIntent.js plugin to delay action
	j("#parksCarousel li").hoverIntent( expandDesc, makeHidden )
	
});

/* For GC version of header drop downs */
j(document).ready(function() {

	j("ul#parksCarouselGC li div.desc").hide();
	j("ul#parksCarouselGC li span.dropArrow").hide();

	j("ul#parksCarouselGC").mouseout(function () {
		j("ul#parksCarouselGC li div.desc").hide();			
	});
	
	function expandDesc(){  
		j("div.desc").hide();
		j(this).find("div.desc").slideDown(300);
		j(this).find("span.dropArrow").show();
		j("#parkScrollerGC .jcarousel-clip-horizontal").addClass("expand")
	}
	function makeHidden(){
		j("div.desc").hide();
		//j("div.desc").slideUp(1);
		j("span.dropArrow").hide();
		j("#parkScrollerGC .jcarousel-clip-horizontal").removeClass("expand")
	}

	//Initiate Dropdown using the hoverIntent.js plugin to delay action
	j("#parksCarouselGC li").hoverIntent( expandDesc, makeHidden )
	
});

/* For Syd version of header drop downs */
j(document).ready(function() {

	j("ul#parksCarouselSyd li div.desc").hide();
	j("ul#parksCarouselSyd li span.dropArrow").hide();

	j("ul#parksCarouselSyd").mouseout(function () {
		j("ul#parksCarouselSyd li div.desc").hide();			
	});
	
	function expandDesc(){  
		j("div.desc").hide();
		j(this).find("div.desc").slideDown(300);
		j(this).find("span.dropArrow").show();
		j("#parkScrollerSyd .jcarousel-clip-horizontal").addClass("expand")
	}
	function makeHidden(){
		j("div.desc").hide();
		//j("div.desc").slideUp(1);
		j("span.dropArrow").hide();
		j("#parkScrollerSyd .jcarousel-clip-horizontal").removeClass("expand")
	}

	//Initiate Dropdown using the hoverIntent.js plugin to delay action
	j("#parksCarouselSyd li").hoverIntent( expandDesc, makeHidden )
	
});
/* For Melb version of header drop downs */
j(document).ready(function() {

	j("ul#parksCarouselMelb li div.desc").hide();
	j("ul#parksCarouselMelb li span.dropArrow").hide();

	j("ul#parksCarouselMelb").mouseout(function () {
		j("ul#parksCarouselMelb li div.desc").hide();			
	});
	
	function expandDesc(){  
		j("div.desc").hide();
		j(this).find("div.desc").slideDown(300);
		j(this).find("span.dropArrow").show();
		j("#parkScrollerMelb .jcarousel-clip-horizontal").addClass("expand")
	}
	function makeHidden(){
		j("div.desc").hide();
		//j("div.desc").slideUp(1);
		j("span.dropArrow").hide();
		j("#parkScrollerMelb .jcarousel-clip-horizontal").removeClass("expand")
	}

	//Initiate Dropdown using the hoverIntent.js plugin to delay action
	j("#parksCarouselMelb li").hoverIntent( expandDesc, makeHidden )
	
});





/* --------------------------------------------------------- */
/* 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 );


														   
		
