/********* Embed object to be used for embedding of (fullscreen) flash content **********/
/* @param	swfPath:String		path to swf (without extension)							*
 * @param 	fsLayer:Boolean		fullstreen Layer?										*
 * @param 	divID:String		id of flash object (leave empty for fullscreen flash)	*
 * @param 	linkID:String		id for "a"-element to add link for fs-showElem function	*
 * @param 	transparency:Boolean		set true for transparent flash object			*
 * @param 	jumpTo:String		needed for flash to be able to switch content			*
 * @param 	autoPlay:Boolean	start flash instantly?									*/
 var deeplink="";
 var t8=0;
 var userPath;
 var customerId;
 var userAgent="notSafari2";
 
function Embed(deep,customerId,swfPath,fsLayer,divID,transparency,jumpTo,autoPlay,bgcolor){	
	this.flObjID=divID?divID+"_flash":"fsFlashCnt";
	this.flashvars = {};	// the variables that will be accessible from flash via "root.loaderInfo.parameters"
	this.flashObj = {};		// the params of this object could be dynamically filled... 
	this.params = {};		// the following params need not to be changed dynamically
	this.attributes = {};	// optional attributes
	/* the parameters needed for swfobject.embedSWF */
	this.flashObj.path=swfPath;			// the path of the swf to be embedded
	this.flashObj.width="100%";					// flash object's width
	this.flashObj.height="100%";				// flash object's height
	this.flashObj.version="9";					// flash object's version
	this.flashObj.id=this.flObjID;				// flashobject's ID
	/* the parameters for the flash object */
	this.params.play="true";	// default value: "true"!
	this.params.loop="false";					// sometimes this should be set to "true"
	this.params.menu="false";					// usually we do not want the user to see the menu
	this.params.quality="high";					// use high quality
	this.params.scale="showall";				// set to "noscale" if no scaling desired
	this.params.salign="t";						// flash alignment top and centered
	this.params.allowScriptAccess="always";		// needed to call ExternalInterface method?
	if (t8==1) {
		this.params.allowFullScreen=true;		// maybe fscommand possible from flash after fade in?!
	} else {
		this.params.allowFullScreen=fsLayer;		// maybe fscommand possible from flash after fade in?!
	}
	this.params.wmode=transparency?"transparent":"opaque";		// set wmode based on transparency param!	
	this.params.id=this.flObjID;				// flashobject's ID
	this.params.name=this.flObjID;				// flashobject's name
	/* the flashvars to be sent to the embedded swf */
	this.flashvars.pageLang=getNavigatorLang();
	this.flashvars.bgColor=bgcolor?bgcolor:"000000";				// set default background color
	/* don't know how important these are */
	this.attributes.id=this.flObjID;
	this.attributes.name=this.flObjID;
	this.attributes.align="middle";
	/* full screen flash content */
	this.flashvars.deep=deep;
	this.flashvars.customerId=customerId;
	if(fsLayer){ 
		this.defaultPath=this.flashObj.path;
		this.linkObjects=new Array();
		this.cntID='topFLayer';
		/* add fullscreen-specific flashvars */
		this.flashvars.fullscreen="true";
		autoPlay?this.flashvars.noclose="true":false;
		/* jumpTo will be also available in flash without transition and MUST be handled by Flash! */
		jumpTo?this.flashvars.jumpTo=jumpTo:false;	// may contain subproject/id!
		this.fsLayer=this.addFlashLayerDivs();	// add flashLayer HTML
		//this.pagetitle=document.getElementsByTagName("title")[0];
	}
	/* flash content to be placed inside div specified in "divID" parameter */
	else{
		var flContainer=elem(divID);
		var flObjDiv="<div id='"+this.flObjID+"'/>";
		this.flashvars.noclose="true";
		flContainer?flContainer.innerHTML=flObjDiv:alert("object "+divID+" not found!");
	}
}

/* Do the embedding! */
Embed.prototype.doEmbed=function(){
	try{
		swfobject.embedSWF(this.flashObj.path, this.flashObj.id, this.flashObj.width, this.flashObj.height, this.flashObj.version, false, this.flashvars,this.params,this.attributes);
		this.flObj=swfobject.getObjectById(this.flashObj.id);
		isIE?this.flObj.onblur=this.setFocus:false;
		this.flObj.tabindex=1;
		return true;
	}catch(exc){
		return false;
	}
}

/* Focus embedded Flash */
Embed.prototype.setFocus=function(){
	//isIE?alert("focus "+embed.flObj):false;
	window.embed.flObj.focus();
}

/* Add javascript link */
Embed.prototype.addJsLink=function(link, url){
	if(typeof(link)=="string"){
		var linkObj=elem(link);
	}else if(typeof(link)=="object"){
		var linkObj=link.nodeName=="A"?link:null;
	}
	if(linkObj){
		if (url) {
			linkObj.setAttribute("href","javascript:showFsLayer(null,'"+url+"');");			
		} else {
			linkObj.setAttribute("href","javascript:showFsLayer(null,null);");
		}
		this.linkObjects.push(linkObj);
		return true;
	}else{
		return false;
	}
}

Embed.prototype.setLinkTxt=function(link,langStr){
	if(langStr){
		var lang=langStr;
		this.flashvars.pageLang=langStr;
	}else{
		var lang=this.flashvars.pageLang;
	}
	if(typeof(link)=="string"){
		for(var i=0;i<this.linkObjects.length;i++){
			if(this.linkObjects[i].id==link){
				var linkObj=this.linkObjects[i];
			};
		}
	}else if(typeof(link)=="object"){
		for(var i=0;i<this.linkObjects.length;i++){
			if(this.linkObjects[i]==link){
				var linkObj=this.linkObjects[i];
			};
		}
	}else if(typeof(link)=="number"){
		try{
			var linkObj=this.linkObjects[link];
		}catch(e){
			alert("link object at index "+link+"not found!");
		}
	}else{
		alert(typeof(link)+" currently not supported!");
	}
	if(linkObj&&linkObj.defArr){
		for(var i=0;i<linkObj.defArr.length;i++){
			if(linkObj.defArr[i][0]==lang){
				linkObj.innerHTML=linkObj.defArr[i][1];
			}
		}if(linkObj.innerHTML==""){
			linkObj.innerHTML=linkObj.defArr[0][1];
		}
		return true;
	}else{
		return false;
	}
}

/* change current (fullscreen) flash content	*/
Embed.prototype.changeSwf_old=function(swfPath,wmode){
	replaceClass('bgLayer',"hideBg","showBg");
	this.flashObj.path=swfPath+".swf";
	wmode=isOpera&&wmode=="normal"?"opaque":wmode;
	this.params.wmode=wmode?wmode:"";
	var succ=this.doEmbed();
	if(!succ){
		//alert("could not embed "+this.flashObj.path);
	}else{
		window.setTimeout("embed.setFocus();",1000);	// focus embedded Flash after 1 second
	}
}

/* swap current (fullscreen) flash content	*/
Embed.prototype.changeSwf=function(swfPath,wmode){
	if((swfPath+".swf")!=this.flashObj.path){	// break possible recursion!
		replaceClass('bgLayer',"hideBg","showBg");
		if(!this.newLayer){
			var newFlID="newFlashFsCnt";
			this.oldFlID=this.flashObj.id;
			this.flObjID="fsFlashCnt2";
			this.flashObj.id=newFlID;
			this.params.id=newFlID;
			this.params.name=newFlID;
			this.attributes.id=newFlID;
			this.attributes.name=newFlID;
			this.newLayer=document.createElement("div");
			this.newLayer.setAttribute("id","newFLayer");
			this.newLayer.innerHTML=this.flashCntDivArr.join('');
			this.newLayer.getElementsByTagName("div")[0].getAttributeNode("id").value=newFlID;
			replaceClass(this.newLayer,"show","hide");
			htmlBody.insertBefore(this.newLayer,this.fsLayer);
			this.oldLayer=this.fsLayer;
			this.fsLayer=this.newLayer;
		}
		this.flashObj.path=swfPath+".swf";		
		wmode=isOpera&&wmode=="normal"?"opaque":wmode;
		this.params.wmode=wmode?wmode:"";
		var succ=this.doEmbed();
		if(!succ){
		//	alert("could not embed "+this.flashObj.path);
		}else{
			window.setTimeout("embed.setFocus();",1000);	// focus embedded Flash after 1 second
		}
	}
}

Embed.prototype.hideCnt=function(fake){
	if(this.newLayer){
		replaceClass(this.oldLayer,"show","hide");
		swfobject.removeSWF(this.oldFlID);
		var timeout=isMoz?250:1;
		window.setTimeout('replaceClass(embed.newLayer,"hide","show");',timeout);
	}
}

/* reset Embed, remove embedded swf */
Embed.prototype.reset=function(){
	swfobject.removeSWF(this.params.id);
	if(this.newLayer){
		replaceClass('bgLayer',"showBg","hideBg");
		this.flObj=null;
		this.flashObj.id=this.oldFlID;
		this.params.id=this.oldFlID;
		this.params.name=this.oldFlID;
		this.attributes.id=this.oldFlID;
		this.attributes.name=this.oldFlID;
		this.oldFlID=null;
		this.fsLayer=this.oldLayer;
		this.flashObj.path=this.defaultPath;
		htmlBody.removeChild(this.newLayer);
		this.newLayer=null;
	}
	this.flObjID="fsFlashCnt";
	this.params.wmode=window.defaultWmode;
	document.documentElement.style.overflow="";
	document.documentElement.style.height="";
	htmlBody.style.overflow="";
	htmlBody.style.height="";
	replaceClass(this.fsLayer,"show","hide");
	this.fsLayer.innerHTML=this.flashCntDivArr.join('');
}

/* Add HTML containers needed for fullscreen flash content 	*
 * see http://www.quirksmode.org/dom/innerhtml.html 		*/
Embed.prototype.addFlashLayerDivs=function(){
	if(!this.fsLayer){	// do this only once!
		this.flashCntDivArr=new Array(
				'<div id="',this.flObjID,'">',
				'	We have detected that you do not have the Flash 9 player installed.<br />',
				'	Please <a href="http://www.adobe.com/go/getflashplayer" target="_blank">Click Here</a> to',
				'	download the Flash plugin.',
				'</div>'
		);
		var bgLayerDiv='<div id="bgLayer" class="hideBg" style="background-color:#'+this.flashvars.bgColor+'"></div>';
	   	var body=document.getElementsByTagName('body')[0];
	   	var newBody="";
		if(isOpera&&!this.flashvars.noclose){
			newBody='<div id="'+this.cntID+'" class="hide you">'+this.flashCntDivArr.join('')+'</div>'+bgLayerDiv+body.innerHTML;
		}else{
	   		newBody='<div id="'+this.cntID+'" class="hide you">'+this.flashCntDivArr.join('')+'</div>'+bgLayerDiv+"<div id='tplCnt'>"+body.innerHTML+"</div>";
	   	}
	   	body.innerHTML=newBody;
		return elem(this.cntID);
	}else{
		return false;
	}
}

function initializeApp(){
	window.isOpera=window.navigator.userAgent.indexOf("Opera")!=-1;
	window.isMoz=window.navigator.userAgent.indexOf("Mozilla/5")!=-1;
	window.isIE=window.navigator.userAgent.indexOf("MSIE")!=-1;
	window.htmlBody=document.getElementsByTagName("body")[0];
}

/* Function - load custom swf file													*
 * @param swfPath:String 	relative or absolute path to swf  (NO extension!)		*
 * @param swfID:String		id of flash object (optional for fullscreen flash)		*
 * @param linkID:String		id for "a"-element to add link for fs-showElem function	*
 * @param transparency:Boolean		set true for transparent flash object			*/
function loadCustomSWF(deep,customerId,swfPath,swfFile,divID,transparency){
	var embd=new Embed(deep,customerId,swfPath+swfFile,false,divID,null,transparency,null,null);
	var succ=embd.doEmbed();
	if(!succ){
		//alert("could not embed "+swfPath);
	}
}

/* add flash layer (singleton!) 													*
 * @param defaultSWF:String			the default SWF file							*
 * @param transparency:Boolean		set true for transparent flash object			*/
function addFsLayer(swfPath,defaultSWF,transitionSWF,transparency,color){
	if(!window.fsLayer){
		initializeApp();
		listen("resize",window,resize);	// !important: add resize event listener function!
		isOpera?listen("scroll",window,trickOpera):false;	// trick opera ;)
		var wmodeBool=transparency;
		window.defaultWmode=wmodeBool?"transparent":"opaque";
		/* get the fullscreen flash path from the site's URL! */
		//window.flashPath=window.location.hash?window.location.hash.substr(1):null;
		var autoPlay=elem("flashAutoPlay")?true:false;
		if(window.location.hash){
			var hashPath=window.location.hash.substr(2);
			// start directly without fade in effect
			if(hashPath=="overview"){
				window.flashPath=swfPath+defaultSWF+".swf";
			}else{
				// use fade in effect and jump to path specified in window.location.hash
				wmodeBool=transitionSWF?true:wmodeBool;
				if(wmodeBool){
					window.flashPath=transitionSWF?swfPath+transitionSWF:swfPath+defaultSWF;
					var jumpTo=swfPath+defaultSWF+".swf#/"+hashPath;
				}else{
					window.flashPath=swfPath+hashPath;
				}
			}
		}
		else{
			window.flashPath=transitionSWF?swfPath+transitionSWF:swfPath+defaultSWF;
			wmodeBool=transitionSWF?true:wmodeBool;
			wmmodeBool=false;
			var jumpTo=transitionSWF?swfPath+defaultSWF:null;
		}
		if(window.flashPath){
			window.embed=new Embed(deeplink,customerId,window.flashPath,true,null,wmodeBool,jumpTo,autoPlay,color);
			autoPlay?showFsLayer():false;
		}
	}else{
		alert("window.embed is a singleton, use embed.changeSwf() instead!");
	}
}

/* show fullscreen flash content	*
 * @param wmode:Boolean (optional) 	*/
function showFsLayer(wmode, deep){
	this.deeplink=deep;
	swfobject.addDomLoadEvent(addFlashContent);
	embed.flashObj.path=window.flashPath+".swf";
	var succ=embed.doEmbed();
	if(!succ){
		//alert("could not embed "+embed.swfPath);
	}else if(window.embed){
		/* attach fade objects to window object in order to allow global access */
		window.myScrollY=getViewportScrollY();
		window.scroll(0,0);
		window.tplCnt=elem('tplCnt');
		tplCnt?elem('tplCnt').style.width=getViewPortWidth().toString()+"px":false;
		document.getElementsByTagName('body')[0].style.overflow="hidden";
		document.documentElement.style.overflow="hidden";
		replaceClass(embed.fsLayer,"hide","show");
		document.documentElement.style.overflow="hidden";
		document.documentElement.style.height=getViewPortHeight().toString()+"px";
		htmlBody.style.overflow="hidden";
		htmlBody.style.height=document.documentElement.style.height;
	}else{
		alert("flash embed failed permanently!");
	}
}

/* call to hide fullscreen flash layer */
function hideFsLayer(){
	replaceClass("bgLayer","showBg","hideBg");
	document.getElementsByTagName('body')[0].style.overflow="";
	document.documentElement.style.overflow="";
	var tplCnt=elem("tplCnt");
	tplCnt?tplCnt.style.width="":false;
	window.embed.reset();
	var autoPlayDiv=elem("flashAutoPlay");
	autoPlayDiv?replaceClass(autoPlayDiv,"hide","show"):false;
	window.setTimeout("scrollBack()",1);	// fight FF2 autoclose-bug!
	window.location.hash="";
}
/* scroll back to a priori scroll position */
function scrollBack(){
	if(window.myScrollY){
		window.scrollTo(0,window.myScrollY);
	}else{
		window.scrollTo(0,1);
	}
}

function trickOpera(){
	embed.flObj?window.scrollTo(0,0):false;
}

function resize(){
	if(embed.flObj){
		window.scroll(0,0);
		document.documentElement.style.height=getViewPortHeight().toString()+"px";
		htmlBody.style.height=document.documentElement.style.height;
	}
}

/**************** general viewport helper functions *****************/
/* get vertical scroll position 									*/
function getViewportScrollY() {
    var scrollY = 0;
    if( document.documentElement && document.documentElement.scrollTop ) {
        scrollY = document.documentElement.scrollTop;
    }
    else if( htmlBody && htmlBody.scrollTop ) {
        scrollY = document.body.scrollTop;
    }
    else if( window.pageYOffset ) {
        scrollY = window.pageYOffset;
    }
    else if( window.scrollY ) {
        scrollY = window.scrollY;
    }
    return scrollY;
}

function getViewPortWidth(){
	var viewportwidth;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof document.documentElement != 'undefined'
		&& typeof document.documentElement.clientWidth !=
		'undefined' && document.documentElement.clientWidth != 0){
		viewportwidth = document.documentElement.clientWidth;
	}
	// older versions of IE
	else{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	}
	return viewportwidth;
}

function getViewPortHeight(){
	var viewportheight;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientHeight !=
	     'undefined' && document.documentElement.clientHeight != 0){
	 	viewportheight = document.documentElement.clientHeight;
	}
	// older versions of IE
	else{
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	return viewportheight;
}
function getNavigatorLang(){
	var navLang;
	if(document.documentElement.lang){
		navLang=document.documentElement.lang;
	}else if(window.navigator.language){
		navLang=window.navigator.language;
	}else{
		navLang=window.navigator.systemLanguage
	}
	return navLang;
}

function handleFlashKey(keyCode){
	if(keyCode==122||keyCode==27){
		embed.flObj.blur(); // steal flash Focus
	}else{
		embed.flObj.focus();
	}
}

/* DOM Helper-functions */ 
/** get element by ID! */
function elem(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (window[id]) {
		return window[id];
	}
	return null;
}
/** set class! */
function setClass(obj,className) {
	if(typeof(obj)=="string"){
		var classElem=elem(obj);
	}else if(typeof(obj)=="object"){
		var classElem=obj;
	}
	var classAttName=isIE?"className":"class";
	classElem.setAttribute(classAttName,className);
}

/** add class! */
function addClass(obj,className) {
	if(typeof(obj)=="string"){
		var classElem=elem(obj);
	}else if(typeof(obj)=="object"){
		var classElem=obj;
	}
	var classAttName=isIE?"className":"class";
	var oldClass=classElem.getAttributeNode(classAttName)?
			classElem.getAttributeNode(classAttName).value:null;
	var newClass=oldClass?oldClass+" "+className:className;
	classElem.setAttribute(classAttName,newClass);
}
/** remove class! */
function removeClass(obj,className) {
	if(typeof(obj)=="string"){
		var classElem=elem(obj);
	}else if(typeof(obj)=="object"){
		var classElem=obj;
	}
	if(classElem){
		var classAttName=isIE?"className":"class";
		var oldClassStr=classElem.getAttributeNode("class")?
				classElem.getAttributeNode("class").value:null;
		if(oldClassStr){
			var oldClassArr=oldClassStr.indexOf(" ")>-1?oldClassStr.split(" "):null;
			if(oldClassArr){
				for(var i=0;i<oldClassArr.length;i++){
					if(oldClassArr[i]==oldClass){
						oldClassArr[i]=null;
					}
				}
				var newClassStr=oldClassArr.join(" ");
			}else{
				var newClassStr="";
			}
		}else{
			var newClassStr="";
		}
		classElem.setAttribute(classAttName,newClassStr);
	}
	else{
		alert("object "+obj+" not found!");
	}
}
/** replace class! */
function replaceClass(obj,oldClass,className) {
	if(typeof(obj)=="string"){
		var classElem=elem(obj);
	}else if(typeof(obj)=="object"){
		var classElem=obj;
	}
	if(classElem){
		var classAttName=isIE?"className":"class";
		var oldClassStr=classElem.getAttributeNode("class")?
				classElem.getAttributeNode("class").value:null;
		if(oldClassStr){
			var oldClassArr=oldClassStr.indexOf(" ")>-1?oldClassStr.split(" "):null;
			if(oldClassArr){
				for(var i=0;i<oldClassArr.length;i++){
					if(oldClassArr[i]==oldClass){
						oldClassArr[i]=className;
					}
				}
				var newClassStr=oldClassArr.join(" ");
			}else{
				var newClassStr=className;
			}
		}else{
			var newClassStr=className;
		}
		classElem.setAttribute(classAttName,newClassStr);
	}
	else{
		alert("object "+obj+" not found!");
	}
}

/* cross browser add event listener function */
function listen(evnt, elem, func) {
	if (elem.addEventListener) // W3C DOM
		elem.addEventListener(evnt,func,false);
		else if (elem.attachEvent) { // IE DOM
		var r = elem.attachEvent("on"+evnt, func);
		return r;
	}
		else window.alert('I\'m sorry Dave, I\'m afraid I can\'t do that.');
}

/* Cookie-related functions */
setCookie=function(c_name,value,expiredays,path){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	( ( path ) ? ";path=" + path : "") +
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
};

getCookie=function(c_name) {
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}return "";
};
function setLinksForFlashContent() {
	var allA=document.getElementsByTagName("a");
	for (var i=0;i<allA.length;i++) {
		if (allA[i].href.match("flashlayer")) {
			var split=allA[i].href.split("#");
			embed.addJsLink(allA[i], split[1]);
		}
	}
}

function getDeeplink() {
	var allA=document.getElementsByTagName("a");
	for (var i=0;i<allA.length;i++) {
		if (allA[i].href.match("flashlayer")) {
			if (allA[i].href.split("#")[1]) {
				deeplink=allA[i].href.split("#")[1];
			} 
		}
	}
	return deeplink;
}
function setDeeplink(link) {
	this.deeplink=link;
}
function setTemplate8() {
	this.t8=1;
}
function setPath(path,customer,userAgent) {
	if ((userAgent.match("Safari")) && (!userAgent.match("Version/3"))) {
		this.userAgent="safari2";
	}
	this.userPath=path;
	this.customerId=customer;
}
function addFlashContent() {
	 if (this.userAgent!="safari2") {
	 	 loadCustomSWF(null,null,userPath,'start','flashContainer',true);
		 loadCustomSWF(deeplink,customerId,userPath,'dark_world','flashContainer2',true);		
		 addFsLayer(userPath,'dark_world','start',true,null);
	} else {
		loadCustomSWF(deeplink,customerId,userPath,'dark_world','flashContainer2',false);
		addFsLayer(userPath,'dark_world',null,false,'000000');
	}
	
}
