var flashContentHeight = 0;

function flashEvent(inSwf, inEvent, inParams)
{
	var divContainer = "flash_setvariables_" + inSwf;
	if(!$(divContainer)) {
		var divHolder = Builder.node("div", {
			id: divContainer,
			className: "access"
		});
		if($("worldwide-flash-content")) {
			$("worldwide-flash-content").appendChild(divHolder);
		}
	}

	var divHtml = "<embed src='" + RESOURCES_PATH + "swf/worldwide/gateway.swf' FlashVars='lc=" + inSwf + "&ev=" + escape(inEvent) + "," + escape(inParams) + "' width='0' height='0' type='application/x-shockwave-flash'></embed>";
	$(divContainer).innerHTML = "";
	$(divContainer).innerHTML = divHtml;
}

function changeFlashHeight(newFlashHeight, fromFlash)
{
	if(Info.browser.isIEpre7){ return; } //aborts IE6 resizing, cause we use fixed height now
	if(fromFlash==null) {
		var fromFlash = true;
	}
	//var innerHeight = Position.getWindowSize().height;
	//var innerHeight = document.viewport.getHeight();
	var innerHeight;
	if(Info.browser.isIEpre7){
		innerHeight = document.documentElement.clientHeight;
		innerHeight = parseInt(innerHeight);
		}
	else {
		innerHeight = document.viewport.getHeight();
	}
	//alert("test:" + typeof(innerHeight));

	var visibleHeight = innerHeight - elementsHeight;
	if ($("worldwide-flash-content")) {
		if(fromFlash) {
			if(newFlashHeight >= visibleHeight) {
				//resize to given height if it is larger than viewport height
				$("worldwide-flash-content").setStyle({"height": parseInt(newFlashHeight) + "px"});
			} else {
				$("worldwide-flash-content").setStyle({"height": parseInt(visibleHeight) + "px"});
			}
			flashContentHeight = newFlashHeight;
		} else {
			if(newFlashHeight >= flashContentHeight) {
				$("worldwide-flash-content").setStyle({"height": parseInt(newFlashHeight) + "px"});
			} else {
				$("worldwide-flash-content").setStyle({"height": parseInt(flashContentHeight) + "px"});
			}
		}
	}
	/* NOTE: There will be a JS Error but the call is processed as expected.
	 * This is because the Method we call using External Interface in Flash is
	 * not registered in Javascript and the script crashes.
	 */
	try {
		if($("siemens_worldwide")) flashEvent('worldwide-flash-content', 'onScaleEnd');
	} catch(error) {}
}


var WorldwideFlash = Class.create();
var elementsHeight = 0;

WorldwideFlash.prototype = {
	initialize: function() {
		var clientFlashVersion = deconcept.SWFObjectUtil.getPlayerVersion()["major"];
		var fVersion = 0;
		if(clientFlashVersion >= 8) {
			fVersion = 8;
		} else if(clientFlashVersion >= 7) {
			fVersion = 7;
		}

		if(fVersion != 0) {
			this.flashVersion = fVersion;
			Event.observe(window, "resize", this.resizeFlash);
			this.initWorldwideFlash();
			this.initLayout();
		}
	},

	initLayout: function() {
		//var innerHeight = Position.getWindowSize().height;
		var innerHeight = document.viewport.getHeight();

		//set the flash height depending to the viewport height
		if ($("header-zone") && $("toolbar-zone") && $("footer-position-placeholder")) {
			elementsHeight = ($("headervisual-zone").getDimensions().height + $("toolbar-zone").getDimensions().height + $("footer-position-placeholder").getDimensions().height);
			if(Info.browser.isIEpre7){ 
				$("worldwide-flash-content").setStyle({"height": "900px"});
			} else {
				changeFlashHeight(parseInt(innerHeight) - parseInt(elementsHeight), false);
			}
		}

		//hide html-based breadcrumb
		if($("breadcrumb-zone")) {
			$("breadcrumb-zone").hide();
		}

		//adjust the breadcrumb height
		/*if($("content-zone")) {
			$("content-zone").setStyle({"padding-top":  "41px"});
		}*/
	},
	
	resizeFlash: function() {
		if(Info.browser.isIEpre7){ return; }
		if(Info.browser.isIEpre7){
			var innerHeight = document.documentElement.clientHeight; 
		}	else {
			var innerHeight = document.viewport.getHeight();
		}
		//set the flash height depending to the viewport height
		changeFlashHeight(innerHeight - elementsHeight, false);
	},

	initWorldwideFlash: function() {
		var so = new SWFObject(ROOT_PATH + "pool/headerflash/worldwide/worldwide_v" + this.flashVersion +".swf", "siemens_worldwide", "100%", "100%", this.flashVersion , "#ffffff");
		so.addVariable("xmlPath", ROOT_PATH + "pool/headerflash/worldwide/xml");
		//so.addVariable("xmlPath", RESOURCES_PATH + "swf/worldwide/xml");
		so.addVariable("language", PAGE_LANG);
		so.addVariable("movieid", "worldwide-flash-content");

		//so.addVariable("regionXmlPath", "../../../apps/worldwide/nwa/server.php?lang=de");  
		so.addVariable("regionXmlPath", ROOT_PATH + "apps/worldwide/nwa/server.php?lang="+PAGE_LANG);
		// pfad zum php-skript, das das regions-xml ersetzt 
 
		//so.addVariable("pdfPath", RESOURCES_PATH + "worldwide/pdf");
		so.addVariable("pdfPath", ROOT_PATH + "pool/regions");
		// pfad zum pdf-ordner (pfade im xml m�ssen nicht angepasst werden, 
		// der dateiname des pdfs wird aus dem xml geparst und an den pdfPath angeh�ngt)

		so.addParam("wmode", "transparent");		
		so.write("worldwide-flash-content");
	}
}

document.observe('dom:loaded', function() {
	new WorldwideFlash();
	
	$('content-zone').style.backgroundImage="url()";
	$('content-zone').style.height="100%";
});