/* ============================================ */
/*    Flash Akamai Session check                */
/* ============================================ */
function inChina(){
	
 	try{
		if(!document.cookie.match("needFlashCheck=false")){
			
			//not checked, perform ajax test
			createCookie("needFlashCheck", false);
			if (window.console){
				console.debug('xhr performed')
			}
			var response = $.ajax({
				url: "/dealers/am-i-in-china.do",
				async: false
			}).responseText;
			
			if (response == "1"){
				if (window.console){
					console.debug('response = 1');
				}
				createCookie("disableFlash", true);
				return true;
			}
			else{
				if (window.console){
					console.debug('response = 0');
				}
			}	
		}
		else{
			//checked, 
			if(document.cookie.match("disableFlash=true")){
				return true;
			}
			if (window.console){
				console.debug('xhr not performed')
			}
			
		}
	}catch(e){}
	return false;
}

function flashKilled(){
	if (typeof flashDisabled != 'undefined'){
		if (flashDisabled == true){
			return true;
		}
	}
	return false;
}

function showContent(contentType){
	if (contentType == 'flash'){
		setTimeout("for(var i=0;i<onloadHTMLHandlers.length;i++){eval(onloadFlashHandlers[i]);} $('#flashcontent').css({visibility:'visible'});", 200);
		$('body').addClass('rolex-flash-active');

		window.onresize = function(){
			 for(var i=0;i<onresizeFlashHandlers.length;i++){
				eval(onresizeFlashHandlers[i]);
			} 
		}
	} else if (contentType == 'html'){
		
		for(var i=0;i<onloadHTMLHandlers.length;i++){
				eval(onloadHTMLHandlers[i]);
		} 
		$('#htmlcontent,#footer').css({visibility:'visible'});
		
		window.onresize = function(){
			 for(var i=0;i<onresizeHTMLHandlers.length;i++){
				eval(onresizeHTMLHandlers[i]);
			} 
		} 
	}
}
function embedFlash(){
	var vers = $.hasFlash.playerVersion();
	var versPart = vers.split(',');
	var belowFlashMinVersion = false;
	
	try{
		if (parseInt(versPart[0]) <= 9 && parseInt(versPart[1]) == 0 && parseInt(versPart[2]) < 124)
			belowFlashMinVersion = true;
	}catch(e){}
	
	if (navigator.userAgent.indexOf('Mac')>0 && navigator.userAgent.indexOf('Intel')<0){
		//kill Flash in PPC
        showContent('html');
    } else if (flashKilled()){
		showContent('html');	
	} else if (inChina()){
		//kill Flash in China
		showContent('html');
	} else if(!document.cookie && belowFlashMinVersion){
		//kill Flash if cookies disabled and Flash version less than 9.0.124
		showContent('html');
	} else { 
		//attempt to show flash
		var url = window.location.href;
		baseTemp = url.split('/');
		base="";
		for(var i=0;i<3;i++){
			base+=baseTemp[i]+"/";
		}
		fla = url.substring(url.lastIndexOf('?')+1,url.lastIndexOf('?')+9);
		lang = baseTemp[3];
		
		var browserUserAgent = navigator.userAgent;
        var binaryBase = base;
        try{
            if (binaryURI != ""){
                binaryBase = binaryURI + "/";
            }
        }catch(e){}  
		
		
		
		var ext = ".jsp";
		var deeplink = "";
		var params = "";
		var firstDeepLink = "";
		var urlUpdate = "";
		var hash = url.substring(url.indexOf('#')+1,url.length);
		if(hash.indexOf('http')<0){
			hashIndex = hash.indexOf('|');
			if(hashIndex>0){
				params = "?"+hash.substring(hashIndex+1,hash.length);
				hash = hash.substring(0,hashIndex);
			}
			firstDeepLink = hash;
			urlUpdateTemp = hash.split('/');
			urlUpdateTempLength = urlUpdateTemp.length;
			var start = ((hash.indexOf("/oyster-perpetual/") > -1 || hash.indexOf("/faq/") > -1 || hash.match(/^\/[a-z]+\-?[a-z]*\/index/ig) != null || hash.indexOf("store-locator/") > -1 || hash.indexOf("/defining-moments/") > -1 || hash.indexOf("/spirit-of-achievement/") > -1 || hash.indexOf("/site-map") > -1 ) ? 2 : 3);
			
			for(var i=start;i<urlUpdateTempLength;i++){
				urlUpdate+="/"+urlUpdateTemp[i];
			}
			urlUpdate = urlUpdate.substring(1,urlUpdate.length);

			if (params!=''){
				if(hash.indexOf("/"+lang+"/xml/index") > -1)
					window.location.href = base+lang+'/index.jsp'+params;
				else
					window.location.href = base+lang+"/"+urlUpdate+ ((urlUpdate.indexOf("/oyster-perpetual/") > -1 || urlUpdate.indexOf("store-locator/") > -1) ? "" :  ext) +params;
			}else{
				if(hash.indexOf("/"+lang+"/xml/index") > -1)
					window.location.href = base+lang+'/index.jsp';
				else
					window.location.href = base+lang+"/"+urlUpdate+ ((urlUpdate.indexOf("/oyster-perpetual/") > -1 || urlUpdate.indexOf("store-locator/") > -1) ? "" : ext );
			}
		}else{
			params = url.substring(url.indexOf('?')+1,url.length);
			baseTempLength = baseTemp.length;
			deeplink="/"+lang+"/";
			for(var i=4;i<baseTempLength;i++){
				deeplink+=baseTemp[i]+"/";
			}
			if (deeplink.indexOf(".jsp") > 0){
				firstDeepLink = deeplink.substring(0,deeplink.lastIndexOf('.'));
			} else{
				firstDeepLink = deeplink.substring(0,deeplink.length-1);
				firstDeepLinkParts = firstDeepLink.split('/');	//for homepage short url exception
				if (firstDeepLinkParts.length > 2 && (firstDeepLinkParts[2].replace(/\?.*$/, '')) == ""){
					firstDeepLink = (firstDeepLink.replace(/\?.*$/, '')) + "index";
				}
			}
			if(/\/collection\/cellini\//i.test(firstDeepLink) == false)
				firstDeepLink = firstDeepLink.replace(/\?.*$/, '');
		}

		var time = new Date().getTime();
		var src = '/media/swf/proxy.swf?t='+time;
		try{
			if (versPart[0] == '9' && versPart[1] == '0' && parseInt(versPart[2]) < 124){
				src = '/media/swf/detect.swf?t='+time;
			}
		}catch(e){}
		
		$('#flashcontent').media({
			width: '100%',
			height: '100%',
			bgColor: "#000000",
			src: src,
			params:{movie:'/media/swf/proxy.swf', wmode:"opaque", scale:"noscale", allowscriptaccess:"always", quality:"high", fullscreen:"true", base:binaryBase},
			attrs: {id:"loader", wmode:"opaque", scale:"noscale", name:"loader", allowscriptaccess:"always", quality:"high", fullscreen:"true", base:binaryBase},
			flashvars: { firstDeeplink:firstDeepLink, params: params, lang:lang, baseTitle:baseTitle,  breadcrumbHomeText:breadcrumbHomeText, browserUserAgent:browserUserAgent, dealerAPIKey:dealerAPIKey, ipAddress:ipAddress },
			flashVersion:9
		});
		if ($('#flashcontent').attr("class") != "flash-replaced" || document.location.href.toLowerCase().indexOf("rolex.de") >= 0){
			$('#flashcontent').remove();
			showContent('html');
		} else{
			showContent('flash');
			flashEmbedded = true; //update global var
		} 
	}
}

window.onload = function(){
	embedFlash();
}