/* 

	Wyndham Hotel Group - Homepage Javascript functions

*/
// set image list
images = new Array();
images[0]="/images/tabs/3tabs_1_on.png" // Tabs
images[1]="/images/tabs/3tabs_2_on.png" // Tabs
images[2]="/images/tabs/3tabs_3_on.png" // Tabs
images[3]="/images/mainnav/portfolio-back.png" // Main nav dropdowns
images[4]="/images/mainnav/support-back.png" // Main nav dropdowns


var home_background_carousel_itemList = [
    {url: '/images/homepage/homepage_hero_image_1.jpg', title: 'Wyndham Home 1'},
    {url: '/images/homepage/homepage_hero_image_2.jpg', title: 'Wyndham Home 2'},
    {url: '/images/homepage/homepage_hero_image_3.jpg', title: 'Wyndham Home 3'},
    {url: '/images/homepage/homepage_hero_image_4.jpg', title: 'Wyndham Home 4'},
    {url: '/images/homepage/homepage_hero_image_5.jpg', title: 'Wyndham Home 5'},
    {url: '/images/homepage/homepage_hero_image_6.jpg', title: 'Wyndham Home 6'}
];

// Preload the images in the carousel for smoother playback
for (var i=0; i < home_background_carousel_itemList.length; i++) {
	preloadImage = new Image();
	preloadImage.src = home_background_carousel_itemList[i].url;
}

// ******************* //
// Hero Image Carousel //
// ******************* //
function home_background_carousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    jQuery('#home-background-carousel-next').bind('click', function() {
        carousel.prev();
        return false;
    });

    jQuery('#home-background-carousel-prev').bind('click', function() {
        carousel.next();
        return false;
    });
};

function home_background_carousel_itemVisibleInCallback(carousel, item, i, state, evt) {
    var idx = carousel.index(i, home_background_carousel_itemList.length);
    carousel.add(i, home_background_carousel_getItemHTML(home_background_carousel_itemList[idx - 1]));
};

function home_background_carousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
};

function home_background_carousel_getItemHTML(item) {
    return '<img src="' + item.url + '" width="943" height="495" alt="' + item.title + '" />';
};

//Global Carousel variables so we can access them in tab handler
var featuresCarousel;
var brandCarousel;
var corporateCarousel;
// ********************* //
// Features RSS Carousel //
// ********************* //
function featuresCarousel_initCallback(carousel) {
   	featuresCarousel = carousel;
    jQuery('#features-carousel-next').bind('click', function() {
        carousel.prev();
        return false;
    });

    jQuery('#features-carousel-prev').bind('click', function() {
        carousel.next();
        return false;
    });
};


// *********************** //
// Brand News RSS Carousel //
// *********************** //
function brandCarousel_initCallback(carousel) {
	brandCarousel = carousel;
    jQuery('#brand-carousel-next').bind('click', function() {
        carousel.prev();
        return false;
    });

    jQuery('#brand-carousel-prev').bind('click', function() {
        carousel.next();
        return false;
    });
};


// *************************** //
// Corporate News RSS Carousel //
// *************************** //
function corporateCarousel_initCallback(carousel) {
	corporateCarousel = carousel;
    jQuery('#corporate-carousel-next').bind('click', function() {
        carousel.prev();
        return false;
    });

    jQuery('#corporate-carousel-prev').bind('click', function() {
        carousel.next();
        return false;
    });
};


jQuery(document).ready(function() {
    jQuery('#home-background-carousel').jcarousel({
        wrap: 'circular',
        animation: 2000,
        auto: 25,
        scroll: 1,
        buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: home_background_carousel_initCallback,
        itemVisibleInCallback: {onBeforeAnimation: home_background_carousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: home_background_carousel_itemVisibleOutCallback}
    });

   jQuery('#featuresCarousel ul').jcarousel({
        wrap: 'last',
		animation: 2000,
        auto: 0,
        scroll: 1,		
        initCallback: featuresCarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

   jQuery('#brandCarousel').jcarousel({
        wrap: 'last',
        animation: 2000,
        auto: 0,
        scroll: 1,
        initCallback: brandCarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

	jQuery('#corporateCarousel').jcarousel({
        wrap: 'last',
        animation: 2000,
        auto: 0,
        scroll: 1,
        initCallback: corporateCarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	/* IE6 creates 2 carousels for each id, this deletes the second one if it exists */
	if($('#featuresCarousel .jcarousel-container').length > 1) {
		$('#featuresCarousel .jcarousel-container').eq(1).remove();
	}
	if($('#brandCarousel .jcarousel-container').length > 1) {
		$('#featuresCarousel .jcarousel-container').eq(1).remove();
	}
	if($('#corporateCarousel .jcarousel-container').length > 1) {
		$('#featuresCarousel .jcarousel-container').eq(1).remove();
	}

    
	function handleTabSelect(event, tab) {
		if (tab.index == 0) {
			// tab 1 selected
			$('.ui-tabs-nav').css('background-image','url(/images/tabs/3tabs_1_on.png)');
			$('#imgTab1').attr({ src: "/images/homepage/features.png" });
			$('#imgTab2').attr({ src: "/images/homepage/brand_news.png" });
			$('#imgTab3').attr({ src: "/images/homepage/corporate_news.png" });
			featuresCarousel.startAuto(5);
			brandCarousel.stopAuto();
			corporateCarousel.stopAuto();
		} else if (tab.index == 1) {
			 // tab 2 selected
			 $('.ui-tabs-nav').css('background-image','url(/images/tabs/3tabs_2_on.png)');
			 $('#imgTab1').attr({ src: "/images/homepage/features.png" });
			 $('#imgTab2').attr({ src: "/images/homepage/brand_news.png" });
			 $('#imgTab3').attr({ src: "/images/homepage/corporate_news.png" });
			 featuresCarousel.stopAuto();
			 brandCarousel.startAuto(5);
			 corporateCarousel.stopAuto();
		} else if (tab.index == 2) {
			// tab 2 selected
			$('.ui-tabs-nav').css('background-image','url(/images/tabs/3tabs_3_on.png)');
			$('#imgTab1').attr({ src: "/images/homepage/features.png" });
			$('#imgTab2').attr({ src: "/images/homepage/brand_news.png" });
			$('#imgTab3').attr({ src: "/images/homepage/corporate_news.png" });
			featuresCarousel.stopAuto();
			brandCarousel.stopAuto();
			corporateCarousel.startAuto(5);
		} else {
			$('.ui-tabs-nav').css('background-image','url(/images/tabs/3tabs_1_on.png)');
        }
	}

	// Set Tab Constructor values & initialize tabs
	$('#tabs').tabs({ select: handleTabSelect });
	$('#tabs').tabs('select',0);
	$('.ui-tabs-nav').css('background-image','url(/images/tabs/3tabs_1_on.png)');
	featuresCarousel.startAuto(5);
});
