
// Speed of the automatic slideshow
var slideshowSpeed = 900;

// Variable to store the images
var photos = [ {
		"title" : "Mazda 2",
		"image" : "images/banners/home-bgcar1.jpg"
	}, {
		"title" : "Mazda 3",
		"image" : "images/banners/home-bgcar2.jpg"
	}, {
		"title" : "Mazda 6",
		"image" : "images/banners/home-bgcar3.jpg"
	}, {
		"title" : "Mazda 6 ultra",
		"image" : "images/banners/home-bgcar4.jpg"
	}, {
		"title" : "Mazda CX 7",
		"image" : "images/banners/home-bgcar5.jpg"
	}, {
		"title" : "Mazda CX 9",
		"image" : "images/banners/home-bgcar6.jpg"
	}, {
		"title" : "Mazda Mx 5",
		"image" : "images/banners/home-bgcar7.jpg"
	}, {
		"title" : "Mazda Bt 50",
		"image" : "images/banners/home-bgcar8.jpg"
	}, {
		"title" : "Mazda 6",
		"image" : "images/banners/home-bgcar9.jpg"
	}, {
		"title" : "Mazda 6 Ultra",
		"image" : "images/banners/home-bgcar10.jpg"
	}, {
		"title" : "Mazda CX-7",
		"image" : "images/banners/home-bgcar11.jpg"
	}, {
		"title" : "Mazda CX-9",
		"image" : "images/banners/home-bgcar12.jpg"
	}
];

$(document).ready(function() {

	var interval;
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var currentContainer;
	var currentZindex = -1;
	/******* BG Image Circle **********************/
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			//if(currentImg == photos.length + 1) {
			if(currentImg == 9) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = 8;//photos.length
			}
		}
		
		// Check which container we need to use
		currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};

	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url("+ photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		$("#headerimg" + currentContainer).fadeOut(2000,function() {
			setTimeout(function() {
				animating = false;
			}, 1000);
		});
	};
	// We should statically set the first image
	navigate("next");
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);

	/* End ****** BG Image Circle **********************/
	/************ Main Menu ****************************/
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo(400, 1).show();
		$(this).find(".sub2").stop().fadeTo(400, 1).show();
		$(this).find(".sub3").stop().fadeTo(400, 1).show();
		$(this).find(".sub4").stop().fadeTo(400, 1).show();
		$(this).find(".sub5").stop().fadeTo(400, 1).show();
		$(this).find(".sub6").stop().fadeTo(400, 1).show();
		$(this).find(".sub7").stop().fadeTo(400, 1).show();
		$(this).find(".sub8").stop().fadeTo(400, 1).show();
	}
	function megaHoverOut(){ 
		$(this).find(".sub").stop().fadeTo(500, 0, function() {
		  $(this).hide(); 
		});
		$(this).find(".sub2").stop().fadeTo(500, 0, function() {
		  $(this).hide(); 
		});
		$(this).find(".sub3").stop().fadeTo(500, 0, function() {
		  $(this).hide(); 
		});
		$(this).find(".sub4").stop().fadeTo(500, 0, function() {
		  $(this).hide(); 
		});
		$(this).find(".sub5").stop().fadeTo(500, 0, function() {
		  $(this).hide(); 
		});
		$(this).find(".sub6").stop().fadeTo(500, 0, function() {
		  $(this).hide(); 
		});
		$(this).find(".sub7").stop().fadeTo(500, 0, function() {
		  $(this).hide(); 
		});
		$(this).find(".sub8").stop().fadeTo(500, 0, function() {
		  $(this).hide(); 
		});
	}

	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
	
	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
	/* End************ Main Menu ************************/
	
	/***************** Mazda2 inside Page****************/
	$(".mazda2menu").click(function(){

		dispMainBGImage('images/banners/home-bgcar1.jpg');
		$(".sub").css({"display" : "none"});	
		$(".sub").stop().hide();
		
		loadinsidepage('mazda2inside.html');
		
	});
	$(".mazda3menu").click(function(){
		dispMainBGImage('images/banners/home-bgcar15.jpg');
		$(".sub2").css({"display" : "none"});	
		$(".sub2").stop().hide();
		
		loadinsidepage('mazda3inside.html');
	});
	$(".mazda6menu").click(function(){
		dispMainBGImage('images/banners/home-bgcar9.jpg');
		$(".sub3").css({"display" : "none"});	
		$(".sub3").stop().hide();
		
		loadinsidepage('mazda6inside.html');
		
	});
	$(".mazda6ultramenu").click(function(){
		dispMainBGImage('images/banners/home-bgcar10.jpg');
		$(".sub4").css({"display" : "none"});	
		$(".sub4").stop().hide();
		
		loadinsidepage('mazda6ultrainside.html');
	});
	$(".mazdacx7menu").click(function(){
		dispMainBGImage('images/banners/home-bgcar11.jpg');
		$(".sub5").css({"display" : "none"});	
		$(".sub5").stop().hide();
		
		loadinsidepage('mazdacx7inside.html');
	});
	$(".mazdacx9menu").click(function(){
		dispMainBGImage('images/banners/home-bgcar12.jpg');
		$(".sub6").css({"display" : "none"});	
		$(".sub6").stop().hide();
		
		loadinsidepage('mazdacx9inside.html');
	});
	$(".mazdamx5menu").click(function(){
		dispMainBGImage('images/banners/home-bgcar13.jpg');
		$(".sub7").css({"display" : "none"});	
		$(".sub7").stop().hide();
		
		loadinsidepage('mazdamx5inside.html');
	});
	$(".mazdabt50menu").click(function(){
		dispMainBGImage('images/banners/home-bgcar14.jpg');
		$(".sub8").css({"display" : "none"});	
		$(".sub8").stop().hide();
		
		loadinsidepage('mazdabt50inside.html');
	});
	$("#getShowrooms").click(function(){
		//dispMainBGImage('images/banners/home-bgcar14.jpg');
		//$(".sub8").css({"display" : "none"});	
		//$(".sub8").stop().hide();
		$("#headerimg1").fadeTo("slow",0.2);
		$("#headerimg2").fadeTo("slow",0.2);
		loadinsidepage('showrooms.html');
	});
	$("#getServiceCentre").click(function(){
		$("#headerimg1").fadeTo("slow",0.2);
		$("#headerimg2").fadeTo("slow",0.2);
		loadinsidepage('showrooms.html');
	});
	
	$("#thinkAbtBuying").click(function(){
		dispMainBGImage('images/banners/home-bgcar1.jpg');
		$("#headerimg1").fadeTo("slow",1);
		$("#headerimg2").fadeTo("slow",1);
		loadinsidepage('abtbuying.html');
	});
	$("#owningaMazda").click(function(){
		dispMainBGImage('images/banners/home-bgcar13.jpg');
		loadinsidepage('owningmazda.html');
	});
	$("#getZoomZoomLife").click(function(){
		dispMainBGImage('images/banners/home-bgcar16.jpg');
		loadinsidepage('zoomhome.html');
	});
	$("#showAboutMazda").click(function(){
		dispMainBGImage('images/banners/home-bgcar16.jpg');
		loadinsidepage('aboutmazda.html');
	});
	//
	$("#mazda2DispColors").click(function(){
		$(".sub").css({"display" : "none"});	
		$(".sub").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar1.jpg', 'mazda2inside.html','mazda2colors.html');
	});
	$("#mazda2DispSpecs").click(function(){
		$(".sub").css({"display" : "none"});	
		$(".sub").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar1.jpg', 'mazda2inside.html','mazda2spec.html');
	});
	$("#mazda2DispGallery").click(function(){
		$(".sub").css({"display" : "none"});	
		$(".sub").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar1.jpg', 'mazda2inside.html','mazda2gallery.html');
	});
	$("#mazda3DispColors").click(function(){
		$(".sub2").css({"display" : "none"});	
		$(".sub2").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar15.jpg', 'mazda3inside.html','mazda3colors.html');
	});
	$("#mazda3DispSpecs").click(function(){
		$(".sub2").css({"display" : "none"});	
		$(".sub2").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar15.jpg', 'mazda3inside.html','mazda3spec.html');
	});
	$("#mazda3DispGallery").click(function(){
		$(".sub2").css({"display" : "none"});	
		$(".sub2").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar15.jpg', 'mazda3inside.html','mazda3gallery.html');
	});
	$("#mazda6DispColors").click(function(){
		$(".sub3").css({"display" : "none"});	
		$(".sub3").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar9.jpg', 'mazda6inside.html','mazda6colors.html');
	});
	$("#mazda6DispSpecs").click(function(){
		$(".sub3").css({"display" : "none"});	
		$(".sub3").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar9.jpg', 'mazda6inside.html','mazda6spec.html');
	});
	$("#mazda6DispGallery").click(function(){
		$(".sub3").css({"display" : "none"});	
		$(".sub3").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar9.jpg', 'mazda6inside.html','mazda6gallery.html');
	});
	$("#mazda6UltraDispColors").click(function(){
		$(".sub4").css({"display" : "none"});	
		$(".sub4").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar10.jpg', 'mazda6ultrainside.html','mazda6ultracolors.html');
	});
	$("#mazda6UltraDispSpecs").click(function(){
		$(".sub4").css({"display" : "none"});	
		$(".sub4").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar10.jpg', 'mazda6ultrainside.html','mazda6ultraspec.html');
	});
	$("#mazda6UltraDispGallery").click(function(){
		$(".sub4").css({"display" : "none"});	
		$(".sub4").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar10.jpg', 'mazda6ultrainside.html','mazda6ultragallery.html');
	});
	$("#mazdaCx7DispColors").click(function(){
		$(".sub5").css({"display" : "none"});	
		$(".sub5").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar11.jpg', 'mazdacx7inside.html','mazdacx7colors.html');
	});
	$("#mazdaCx7DispSpecs").click(function(){
		$(".sub5").css({"display" : "none"});	
		$(".sub5").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar11.jpg', 'mazdacx7inside.html','mazdacx7spec.html');
	});
	$("#mazdaCx7DispGallery").click(function(){
		$(".sub5").css({"display" : "none"});	
		$(".sub5").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar11.jpg', 'mazdacx7inside.html','mazdacx7gallery.html');
	});
	$("#mazdaCx9DispColors").click(function(){
		$(".sub6").css({"display" : "none"});	
		$(".sub6").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar12.jpg', 'mazdacx9inside.html','mazdacx9colors.html');
	});
	$("#mazdaCx9DispSpecs").click(function(){
		$(".sub6").css({"display" : "none"});	
		$(".sub6").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar12.jpg', 'mazdacx9inside.html','mazdacx9spec.html');
	});
	$("#mazdaCx9DispGallery").click(function(){
		$(".sub6").css({"display" : "none"});	
		$(".sub6").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar12.jpg', 'mazdacx9inside.html','mazdacx9gallery.html');
	});
	$("#mazdaMx5DispColors").click(function(){
		$(".sub7").css({"display" : "none"});	
		$(".sub7").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar13.jpg', 'mazdamx5inside.html','mazdamx5colors.html');
	});
	$("#mazdaMx5DispSpecs").click(function(){
		$(".sub7").css({"display" : "none"});	
		$(".sub7").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar13.jpg', 'mazdamx5inside.html','mazdamx5spec.html');
	});
	$("#mazdaMx5DispGallery").click(function(){
		$(".sub7").css({"display" : "none"});	
		$(".sub7").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar13.jpg', 'mazdamx5inside.html','mazdamx5gallery.html');
	});
	$("#mazdabt50DispColors").click(function(){
		$(".sub8").css({"display" : "none"});	
		$(".sub8").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar14.jpg', 'mazdabt50inside.html','mazdabt50colors.html');
	});
	$("#mazdabt50DispSpecs").click(function(){
		$(".sub8").css({"display" : "none"});	
		$(".sub8").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar14.jpg', 'mazdabt50inside.html','mazdabt50spec.html');
	});
	$("#mazdabt50DispGallery").click(function(){
		$(".sub8").css({"display" : "none"});	
		$(".sub8").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar14.jpg', 'mazdabt50inside.html','mazdabt50gallery.html');
	});
	$("#testDriveClick").click(function(){
		$(".sub8").css({"display" : "none"});	
		$(".sub8").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar1.jpg', 'abtbuying.html','testdrive.html');
	});
	$("#brochureReqClick").click(function(){
		$(".sub8").css({"display" : "none"});	
		$(".sub8").stop().hide();
		displaySelectedInsidePage('images/banners/home-bgcar1.jpg', 'abtbuying.html','brochure.html');
	});
	//
	var dispMainBGImage = function(bgFileName){
		clearInterval(interval);
		$("#headerimg1").css({
			"background-image" : "url(" + bgFileName + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		$("#headerimg2").css({
			"background-image" : "url(" + bgFileName + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		$("#headerimg1").fadeTo("fast",1);
		$("#headerimg2").fadeTo("fast",1);
	};
	var loadinsidepage = function(insideFileName){
		//var popID = "insidepageWnd";
		//var popWidth = 969;
		$('#loadingAnimateDiv').css({"display" : "block"});
		$('#loadingAnimateDiv').fadeTo("fast",1);
		$('#insidepageWnd').html("&nbsp;");
		$('#insidepageWnd').hide();
		//$('#insidepageWnd').html('<img style="margin:100px 0 0 400px;" src="images/loading.gif" width="75" height="75"  />');
		$('#insidepageWnd').css({"display":"block"});
		$('#insidepageWnd').load(insideFileName); //('mazda2inside.html');
		$('#insidepageWnd').hide().fadeIn('slow');
	};
	var displaySelectedInsidePage = function(homepageBanner, insidePage, insideSubPage){

		dispMainBGImage(homepageBanner);
		
		$('#loadingAnimateDiv').css({"display" : "block"});
		$('#loadingAnimateDiv').fadeTo("fast",1);
		$('#insidepageWnd').html("&nbsp;");
		$('#insidepageWnd').hide();
		//$('#insidepageWnd').html('<img style="margin:100px 0 0 400px;" src="images/loading.gif" width="75" height="75"  />');
		$('#insidepageWnd').css({"display":"block"});
		$('#insidepageWnd').load(insidePage, function() {
			homepageBGSettings();
			$("#ContentController").load(insideSubPage,function(){ $(this).fadeIn(300);});
		}); //('mazda2inside.html');
		$('#insidepageWnd').hide().fadeIn('slow');
	};
	var homepageBGSettings = function(){
		$("#ContentController").html('<img style="margin:100px 0 0 400px;" src="images/loading.gif" width="75" height="75"  />');
		$("#ContentController").css({"display":"block"});
		$("#headerimg1").fadeTo("slow",0.2);
		$("#headerimg2").fadeTo("slow",0.2);
	};
	/***************** Promotions ***********************/
	
	var promoInterval = setInterval(function() {
		$("#promotionWnd").html('<a href="http://www.facebook.com/mazdauae?sk=app_234499546626696"><img style="margin:30px 0 0 156px;" src="images/promotions/promotionImg-Jan.jpg" width="688" height="340" /></a>');
		$("#promotionWnd").prepend('<div style="border:0px solid red; position:absolute; width:879px; height:31px; left:inherit; top:40px;z-index:102;"><div style="border:0px solid; width:774px; float:left">&nbsp;</div><div style="float:left"><a href="#" class="closePromo"><img src="images/closeBtn.png" width="105" height="15" border="0"  /></a></div></div>');
		$("#promotionWnd").fadeIn(300);	
		//$("#headerimg1").fadeTo("slow",0.4);
		//$("#headerimg2").fadeTo("slow",0.4);
		$('a.closePromo').live('click', function() { //When clicking on the close or fade layer...
			$('a.closePromo').remove();
			$('#promotionWnd').fadeOut(function() {
				//fade them both out
				//$("#headerimg1").fadeTo("slow",1);
				//$("#headerimg2").fadeTo("slow",1);			
				clearInterval(promoInterval);
				$('#promotionWnd').remove();
			});
			return false;
		});
	}, 20000);
	
	/* End **************** Promotions ***********************/
});
