var slideTimer = null;
var slideInterval = 1000;
var selfFlag = "";

$(function()
{
	checkUserAgent = function()
	{
		var isAndroid = false;
		var isIpod = false;
		var isIphone = false;
		var isIpad = false;
		var isIE = false;
		var isBB = false;
		var isWindowsMobile = false;
		var isPCBrowser = false;
		var canHaveFlash = false;
		var timerInterval;
		
		if (navigator.userAgent.match(/Android/i))
		{
			//console.log("Android");
			if (navigator.userAgent.match(/2.2/i))
			{
				canHaveFlash = true;
			}
			else
			{
				canHaveFlash = false;
			}
			isAndroid = true;
		}
		
		else if (navigator.userAgent.match(/iPod/i))
		{
			//console.log("Ipod");
			isIpod = true;
			canHaveFlash = false;
		}
		
		else if (navigator.userAgent.match(/iPhone/i))
		{
			//console.log("Iphone");
			isIphone = true;
			canHaveFlash = false;
		}
		
		else if (navigator.userAgent.match(/iPad/i))
		{
			//console.log("Ipad");
			isIpad = true;
			canHaveFlash = false;
		}
		
		else if(navigator.userAgent.match(/BlackBerry/i))
		{
			isBB = true;
			canHaveFlash = false;
		}
		
		else if (navigator.userAgent.match(/Windows Phone OS/i))
		{
			//console.log("Windows Phone");
			isWindowsMobile = true;
			canHaveFlash = false;
		}
		
		else
		{
			isPCBrowser = true;
			canHaveFlash = true;
		}
  		
  		
  		if (navigator.appName == 'Microsoft Internet Explorer')
  		{
			isIE = true;
      	}
      	
		if (canHaveFlash == true)
		{
			startFlash();
		}
		
		$("#downloadFlash").hide();
		
		if (canHaveFlash == true)
		{
			$("#downloadFlash").show();
		}
		else
		{
			$("#downloadFlash").hide();
		}
		
		var file_name = document.location.href;
		var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
		var new_name = file_name.substring(file_name.lastIndexOf("/")+1, end);
		
		var $videoDiv = $(".video");
		//
		if (isBB == true)
		{
			if(new_name == "gameVideo.html")
			{
				window.location = "bb.html";
			}
		}
		else if (isIE == true)
		{
			if (new_name == "journey.html") {
				$videoDiv.html('<object type="video/x-ms-wmv" data="video/journey/exxonJorney_480.wmv" width="480" height="303" ><param name="src" value="video/journey/exxonJorney_480.wmv" /><param name="autostart" value="true" /><param name="controller" value="true" /><param name="qtsrcdontusebrowser" value="true" /><param name="enablejavascript" value="true" /></object>');
			} else {
				$videoDiv.html('<object type="video/x-ms-wmv" data="video/gameVideo/Exxon_intro_03.wmv" width="480" height="303" ><param name="src" value="video/gameVideo/Exxon_intro_03.wmv" /><param name="autostart" value="true" /><param name="controller" value="true" /><param name="qtsrcdontusebrowser" value="true" /><param name="enablejavascript" value="true" /></object>');
			}
		}
		else
		{
			VideoJS.setupAllWhenReady();
		}
		//
	}
	
	startFlash = function()
	{
		var flashvars = {};
		var params = {
			wmode: "opaque",
			swfversion: "9.0.45.0"
		};
		
		var attributes = {};
		
		// To remove flash, comment out line below.
		swfobject.embedSWF("BJ011_exxonMicroSite_01.swf", "panel-flash", "900", "507", "9.0.45.0", "seo/js/expressInstall.swf", flashvars, params, attributes);
	}
	
	$("a.ro").mouseover(function(){
	
		var imgArr = $(this).children("img").attr("src").split(".");
		var imgPart1 = imgArr[0];
		var imgPart2 = imgArr[1];
		
		if(imgPart1.lastIndexOf("1") == -1){
			$(this).children("img").attr("src",imgPart1+"1."+imgPart2);
		}
	
	}).mouseout(function(){
		
		var divID = $(this).parent().attr("id");
		if(selfFlag != divID || divID == ""){
			var imgOut = $(this).children("img").attr("src").replace(/1/,"");
			$(this).children("img").attr("src",imgOut);
		}
	});
		
    $("div.nav > a.ro").click(function(){

		var clickID = $(this).parent().attr("id");
		var clickID2 = "";
		
		$("div.nav").each(function(){

			clickID2 = $(this).attr("id");
			if(clickID!=clickID2){
				var imgOut = $(this).children("a").children("img").attr("src").replace(/1/,"");
		  		$(this).children("a").children("img").attr("src",imgOut);
			}
		});

		selfFlag = clickID;		
    });

    $('div#nav0>a').click(function(){

	$("div.nav").each(function(){
		var imgOut = $(this).children("a").children("img").attr("src").replace(/1/,"");
  		$(this).children("a").children("img").attr("src",imgOut);
	});

	selfFlag = "";

    });

   //-------------------------------------------------------------------------------------------
   // SETUP ONCLICKS
   //------------------------------------------------------------------------------------------

   /*
     THIS PART IS COMMENTED OUT - CONTENT NOT READABLE 
     BY SCREEN READERS WITH THIS METHOD
     -----------------------------------------------------
     $("a[rel^='panel']").live("click", function(){

	var file = $(this).attr("href");

	$('#panelmain').fadeOut('slow', function() {

	   if(slideTimer) clearTimeout(slideTimer);

	   loadContent("panelmain", file, "panelmain", function(){ $('#panelmain').fadeIn('slow'); } );

	   if(file.indexOf("fuel-injector")!=-1 || file.indexOf("molecular-view")!=-1)
	   {
		slideTimer = setTimeout('togglePanel()', slideInterval);
	   }

	   if(file.indexOf("intake-valves.html")!=-1 || file.indexOf("combustion-chamber")!=-1 || file.indexOf("exhaust-valves.html")!=-1 ||file.indexOf("frame12")!=-1 )
	   {
		slideTimer = setTimeout('togglePanel2()', slideInterval);
	   }

	});
	return false;

     });

     -----------------------------------------------------
    */


	$("div#expand>a").live("click", function(){
		togglePanelNoTimer();
		$('#expandclose').css('visibility','visible');
		$('#linkfueltank').css('visibility','visible');
		$('#linkengine').css('visibility','visible');
		$('#linklocate').css('visibility','visible');
	});

	$("div#expandclose>a").live("click", function(){
		togglePanelNoTimer();
		$('#expandclose').css('visibility','hidden');
		$('#linkfueltank').css('visibility','hidden');
		$('#linkengine').css('visibility','hidden');
		$('#linklocate').css('visibility','hidden');
	});
     
	checkUserAgent();
	
	
	//-------------------------------------------------------------------------------------------
	// IE6 SUPPORT, fix PNG images
	//------------------------------------------------------------------------------------------ 
	if(scripts_isIE6)
	{
		$(window).load( function() { 
		
			$(document).pngFix();
		
		});
	}

});

function trackSite(s1, s2)
{
	//console.log(s1 + " " + s2);
	dcsMultiTrack('DCS.dcsuri', s1, 'WT.ti', s2);
}

function restartPanel()
{
    slideTimer = setTimeout('togglePanel()', slideInterval);
}

function togglePanelNoTimer()
{
    if($('#panelimg2').css('display') == 'block')
	$('#panelimg2').hide();
    else
	$('#panelimg2').show();
}

function togglePanel()
{
    if($('#panelimg2').css('display') == 'block')
	$('#panelimg2').hide();
    else
	$('#panelimg2').show();
    restartPanel();
}

function restartPanel2()
{
    slideTimer = setTimeout('togglePanel2()', slideInterval);
}

function togglePanel2()
{
    if($('#panelimg2').css('display') == 'block'){
	$('#panelimg2').hide();
	$('#panelimg3').show();
    } else if($('#panelimg3').css('display') == 'block') {
	$('#panelimg3').hide();
    } else {
	$('#panelimg2').show();
    }
    restartPanel2();
}


//-------------------------------------------------------------------------------------------
// LOAD HTML CONTENT FROM EXTERNAL FILE
//-------------------------------------------------------------------------------------------

function loadContent(divID,fileName,fileDivID,callback)
{
  if(fileDivID!="" && callback=="")
     $("#"+divID).load(fileName+" #"+fileDivID);
  else if(callback!="" && fileDivID!="")
     $("#"+divID).load(fileName+" #"+fileDivID, callback);
  else
     $("#"+divID).load(fileName);
}

function removeContent(fileDivID)
{
  $("."+fileDivID).remove();
}

