/*
 * 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();
    });
};

function prevNextClick(carousel, liElement, index, state) {

	var totalItems = j(carousel.list.children()).length;

	// if there is less than or equal to 8 items, exit the function as we don't need to change any visibility classes
	if (totalItems <= 8) {
		return;
	}

	// store the visible items
	var visibleItems = [];

	// by default, set the first and last visible to 5 and 9, which is the default that requires the flipdrop
	var firstVisible = 5;
	var lastVisible = 9;

	// if the current index is > 9, then we need to reset the numbers to the visible items only.
	if (index > 9) {
		// set the last visible as the index and first visible as 4 items before it
		lastVisible = index;
		firstVisible = lastVisible - 4;
	}

	// Loop through each of the elements
	j.each(carousel.list.children(), function (objIndex, obj) {

		// remove the flipDrop from them all, as we will decide which elements to add it to next
		j(obj).removeClass('flipDrop');

		// incremement the index to match the carousel index.
		var elementIndex = objIndex + 1

		// if the element index falls within the first and last visible (inclusive), add the flipDrop class
		if (elementIndex > firstVisible && elementIndex <= lastVisible) {
			j(obj).addClass('flipDrop');
		}

	});

	/*$.each($(liElement).parent().children().slice((itemIndex - 1), (itemIndex + num_items) - 1), function (index, value) { 
	var itemURL = $(value).children('a:last-child').attr('href'); 
	urlItems.push(itemURL);
	});*/

}


j(document).ready(function() {
    j('#parksCarousel').jcarousel({
        auto: 0.01,
		scroll: 1,
		start: 11,
		direction: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: prevNextClick
    });
    j('#parksCarouselMelb').jcarousel({
        auto: 0.01,
		scroll: 1,
		start: 11,
		direction: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: prevNextClick
    });
    j('#parksCarouselGC').jcarousel({
        auto: 0.01,
		scroll: 1,
		start: 11,
		direction: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: prevNextClick
    });
    j('#parksCarouselSyd').jcarousel({
        auto: 0.01,
		scroll: 1,
		start: 11,
		direction: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: prevNextClick
    });
	
	// Detect items that are to the right of scroller in order to flip the drop down
	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");
	j("#parksCarousel li:nth-child(12)").addClass("flipDrop");
	j("#parksCarousel li:nth-child(13)").addClass("flipDrop");
	j("#parksCarousel li:nth-child(14)").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(5)").addClass("flipDrop");
	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)

	// Reset the overflow setting to allow the dropdowns.
	j("#parksCarousel").css('overflow', 'visible');
	j(".js-enabled #hero-carousel").css('overflow', 'visible');

});

/* --------------------------------------------------------- */

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(100);
		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(100);
		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(100);
		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(100);
		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()", 8000 );


//	*/

/* --------------------------------------------------------- */
/* Booking Engine */
/* --------------------------------------------------------- */


j(document).ready(function () {
							
	j("#parksTix").msDropDown();
	j("#parksAdult").msDropDown();
	j("#parksKid").msDropDown();	
	j("#parksPensioner").msDropDown();	
	
	j("#resortAdult").msDropDown();
	j("#resortkids").msDropDown();	
	j("#resortInfants").msDropDown();	
	j("#resortRooms").msDropDown();	
	
	
	j("#secondaryPromos .bookTickets div.formBlock").hide();
	
	j("#secondaryPromos .bookTickets div.formBlock:first").show();
	j("#secondaryPromos .bookTickets div.formBlock:first").addClass("expanded");
	j("#secondaryPromos .bookTickets h3 a:first").addClass("expanded");
	
	
    j("#secondaryPromos .bookTickets h3 a").click(function() {
														   
		if (!j(this).hasClass('expanded')) {
				j("#secondaryPromos .bookTickets div.expanded").slideToggle(500);
				j("#secondaryPromos .bookTickets div.expanded").toggleClass("expanded");
				j("#secondaryPromos .bookTickets h3 a.expanded").toggleClass("expanded");
				
				j(this).parents("div").children("div.formBlock").slideToggle(500);												   	
				j(this).parents("div").children("div.formBlock").toggleClass("expanded");
				j(this).toggleClass("expanded");
				return false;
       	} else {
			return false;
       		}												   	
	});
	
							
});						


/* --------------------------------------------------------- */
/* Calendar */
/* --------------------------------------------------------- */

j(document).ready(function () {

	j(".dateInput").datepicker({
			showOn: 'button',
			buttonImage: '/resources/img/myfuncomau/secondaryPromos/calendar_icon.gif',
			buttonImageOnly: true,
			dateFormat : 'dd/mm/yy'
	});
	
});

