﻿var homepageCarousel = new function()
{
	this.slides;
	this.backgrounds;
	this.loader;
	this.navigator;
	this.images = new Array();
	this._imagesUrls;
	this.intervalId;
	this.timeoutId;
	this.leftArrow;
	this.rightArrow;
	
	this.init = function(urls)
	{
		this._imagesUrls = urls;
		this.slides = $(".hp-carousel .slides");
		this.navigator = $("<ul/>").appendTo($(".hp-carousel .navigator"));
		this.backgrounds = $("#homepage-background .image div");
		this.loader = $(".hp-carousel .loader");
	//	this.leftArrow = $(".hp-carousel .left-arrow");
	//	this.rightArrow = $(".hp-carousel .right-arrow");
		var navigatorWidth = 0;
		
		if (this.slides.children().length > 1)
		{
			this.slides.children().each
			(
				function(i)
				{
					homepageCarousel.navigator.append(String.format("<li{0}><div></div></li>", (i == homepageCarousel.slides.children().length - 1) ? " class=\"last\"" : ""));
					navigatorWidth += homepageCarousel.navigator.children("li:eq(" + i + ")").outerWidth();
				}
			);
			
			this.navigator.width(navigatorWidth);
			
			this.navigator.find("div").bind
			(
				"click",
				function()
				{
					if (!$(this).parent().hasClass("selected"))
					{
						var index = $(this).parent().index();
						homepageCarousel.navigateSlide(index);
						
						linkTrack("chrysler_homepage", "navigation" + (index + 1) + "_" + homepageCarousel.slides.children().eq(index).attr("id"));
						
						
					}
				}
			);
		
		/*	$([this.leftArrow[0], this.rightArrow[0]]).mouseenter
			(
				function()
				{
					$(this).children().fadeIn(500);
				}
			);
			
			$([this.leftArrow[0], this.rightArrow[0]]).mouseleave
			(
				function()
				{
					$(this).children().fadeOut(500);
				}
			);
			
			$([this.leftArrow[0], this.rightArrow[0]]).mousedown
			(
				function()
				{
					$(this).children().addClass("click");
				}
			);
			
			$([this.leftArrow[0], this.rightArrow[0]]).mouseup
			(
				function()
				{
					$(this).children().removeClass("click");
				}
			);
			
			this.rightArrow.click
			(
				function()
				{
					var index = homepageCarousel.navigator.children(".selected").index() + 1;
					
					if (index > homepageCarousel.navigator.children().length - 1)
					{
						index = 0;
					}
					
					homepageCarousel.navigateSlide(index);
					linkTrack("chrysler_homepage", "right_arrow");
				}
			);
			
			this.leftArrow.click
			(
				function()
				{
					var index = homepageCarousel.navigator.children(".selected").index() - 1;
					
					if (index < 0)
					{
						index = homepageCarousel.navigator.children().length - 1;
					}
					
					homepageCarousel.navigateSlide(index);
					linkTrack("chrysler_homepage", "left_arrow");
				}
			);*/
		
			this.navigator.children().eq(0).addClass("selected");
			this.showBackground(0);
			this.autoPlay();
		}
		else
		{
			this.showBackground(0);
		}
	};
	
	this.navigateSlide = function(index)
	{
		this.navigator.children(".selected").removeClass("selected");
		this.navigator.children().eq(index).addClass("selected");
		this.showBackground(index);
		this.pause();
		this.resume(1500);
	};
	
	this.showBackground = function(index)
	{
		if (this.images[index])
		{
			this.fadeInBackground(index);
		}
		else
		{
			this.loader.fadeIn(200);
			
			chrysler.lazy.loadImage
			(
				homepageCarousel._imagesUrls[index],
				function(index, homepageCarousel, img)
				{
					homepageCarousel.loader.fadeOut(500);
					img.id = "for-" + homepageCarousel.slides.children().eq(index).attr("id");
					homepageCarousel.images[index] = img;
					homepageCarousel.backgrounds.append($(img));
					homepageCarousel.fadeInBackground(index);
				},
				index,
				homepageCarousel
			);
		}
	};
	
	this.fadeInBackground = function(index)
	{
		this.slides.children(".selected").stop(true, true).fadeOut(1000).removeClass("selected");
		this.backgrounds.children(".selected").stop(true, true).fadeOut(1500).removeClass("selected");
		
		this.slides.children().eq(index).stop(true, true).fadeIn(1500).addClass("selected").find("img[lazysrc]").each
		(
			function()
			{
				if (!$(this).parents(".baseball-container")[0])
				{
					chrysler.lazy.showPreloader(this);
					chrysler.lazy.loadContent(this);
				}
			}
		);
		
		this.backgrounds.children("[id='for-" + this.slides.children().eq(index).attr("id") + "']").stop(true, true).fadeIn(1500).addClass("selected");
	};
		
	this.autoPlay = function()
	{
		this.intervalId = setInterval
		(
			function()
			{
				var index = homepageCarousel.navigator.children(".selected").index() + 1;
				
				if (index >= homepageCarousel.navigator.children().length)
				{
					index = 0;
				}
				
				if (homepageCarousel.images[index])
				{
					homepageCarousel.navigator.children(".selected").removeClass("selected");
					homepageCarousel.navigator.children().eq(index).addClass("selected");
					homepageCarousel.fadeInBackground(index);
				}
				else
				{
					chrysler.lazy.loadImage
					(
						homepageCarousel._imagesUrls[index],
						function(img)
						{
							img.id = "for-" + homepageCarousel.slides.children().eq(index).attr("id");
							homepageCarousel.images[index] = img;
							homepageCarousel.backgrounds.append($(img));
							homepageCarousel.navigator.children(".selected").removeClass("selected");
							homepageCarousel.navigator.children().eq(index).addClass("selected");
							homepageCarousel.fadeInBackground(index);
						}
					);
				}
			},
			15000
		);
	};
	
	this.pause = function()
	{
		clearInterval(this.intervalId);
		clearTimeout(this.timeoutId);
	};
	
	this.resume = function(delay)
	{
		if (this.slides.children().length > 1)
		{
			this.pause();
			this.timeoutId = setTimeout
			(
				function()
				{
					homepageCarousel.autoPlay();
				},
				delay
			);
		}
	};
	
	this.toString = function()
	{
		return "[object homepageCarousel]";
	};
};

$(document).ready
(
	function()
	{
		var homepagePromoCarousel = new promoCarousel($(".promo-carousel"), 2);
	}
);

function playvideo(id)
{
	homepageCarousel.pause();
	modal.open($("<div id=\"" + id + "\" />"));
	modal.onClose = function()
	{
		homepageCarousel.resume();
	};
	
	var player = new videoPlayer(id);
	var youtubeSize = getYouTubeSize();
	
	switch (id)
	{
		case "homepage_300_ad":
			player.video.id = "sMey6AL_SRE";
			player.tracking.mediaPath = "http://www.youtube.com/watch?v=sMey6AL_SRE";
			player.tracking.vehicle = "homepage_300_ad";
			player.tracking.mediaId = "eight_speed_if_your_gonna";
			break;
		
		case "homepage_200_ad":
			player.video.id = "WjAv9WqBlFQ";
			player.tracking.mediaPath = "http://www.youtube.com/watch?v=WjAv9WqBlFQ";
			player.tracking.vehicle = "homepage_200_ad";
			player.tracking.mediaId = "ifd_born_of _fire";
			break;
		
		case "homepage_tnc_ad":
			player.video.id = "ZwU4fJpXcJ8";
			player.tracking.mediaPath = "http://www.youtube.com/watch?v=ZwU4fJpXcJ8";
			player.tracking.vehicle = "homepage_town_country_ad";
			player.tracking.mediaId = "boxing_beaty_beast";
			break;
	}
	
	player.video.type = videoType.YouTube;
	player.video.autoPlay = true;
	player.height = youtubeSize.height;
	player.width = youtubeSize.width;
	player.tracking.brand = "chrysler";
	player.tracking.mediaTitle = "homepage_video";
	player.init();
}

function getYouTubeSize()
{
	var width = $(window).width();
	var height = $(window).height();
	
	if (width < 640 || height < 360)
	{
		return {width: 560, height: 315};
	}
	else if (width < 853 || height < 480)
	{
		return {width: 640, height: 360};
	}
	else if (width < 1280 || height < 720)
	{
		return {width: 853, height: 480};
	}
	else
	{
		return {width: 1280, height: 720};
	}
}
