/* <![CDATA[ */

/**
 * @author martin.hollmann
 */

// global vars
var bodyMargin="";
var bodyPadding="";
var posVideoSmallLeft="";
var posVideoSmallTop="";
var aSWFs = [];

include("/res/videoplayer_init.js");     // bitte anpassen

function include(jsFile) {
	var newElem = document.createElement("script");
			newElem.type = "text/javascript";
			newElem.language="JavaScript";
			newElem.src = jsFile;
			var htmlHead = document.getElementsByTagName("head")[0];
			htmlHead.appendChild(newElem);	
}

function innerBrowserSize() {
	var x,y;	
	if (self.innerHeight) {  // all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {  // Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) {  // other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	var innerXY = new Array(x,y);
	return innerXY;
}

function getVideoPlayerSizeProp(videoPlayerId) {
	var prop;
	prop = parseInt(document.getElementById(videoPlayerId).width) / parseInt(document.getElementById(videoPlayerId).height);
	return prop;
}

function getVideoFullSize (x, y, prop) {
	// calculates and returns fullsize video dimensions with correct proportion
	if ( (parseInt(x) / prop) <= parseInt(y) ) 
		return new Array(x , parseInt(parseInt(x) / prop));
	else if ( (parseInt(y) / prop) < parseInt(x) ) 
		return new Array(parseInt(parseInt(y) * prop), y);
}

function timedelayedSizing(mode, widthElementId, heightElementId, stepWidth, stepHeight, ms, desiredWidth, desiredHeight){
	// mode=1 means larger, mode=2 means smaller
	// get actual width and height
	var widthElement = document.getElementById(widthElementId);
	var heightElement = document.getElementById(heightElementId);
	if (widthElement.style.width != "") {
		var nextWidthLevel = parseInt(widthElement.style.width.split("px")[0]);
		var widthOriginal = parseInt(nextWidthLevel);
	}
	else {
		var nextWidthLevel = parseInt(widthElement.width);
		var widthOriginal = parseInt(nextWidthLevel);
	}
	if (widthElement.style.height != "") {
		var nextHeightLevel = parseInt(heightElement.style.height.split("px")[0]);
		var heightOriginal = parseInt(nextHeightLevel);
	}
	else {
		var nextHeightLevel = parseInt(heightElement.height);
		var heightOriginal = parseInt(nextHeightLevel);
	}
	
	// set new width and height
	if (mode==1 && (nextWidthLevel + stepWidth) <= desiredWidth) {
		nextWidthLevel += parseInt(stepWidth);
	}
	else if (mode==2 && (nextWidthLevel - stepWidth) >= desiredWidth) {
		nextWidthLevel -= parseInt(stepWidth);
	} 
	if (mode==1 && (parseInt(nextHeightLevel) + parseInt(stepHeight)) <= parseInt(desiredHeight)) { 
		nextHeightLevel += parseInt(stepHeight);
	}
	else if (mode==2 && (parseInt(nextHeightLevel) - parseInt(stepHeight)) >= parseInt(desiredHeight)) { 
		nextHeightLevel -= parseInt(stepHeight);
	}
	
	// apply width and height to widthElement and heightElement
	widthElement.width = nextWidthLevel; 
	heightElement.height = nextHeightLevel;
	widthElement.style.width = nextWidthLevel+'px'; 
	heightElement.style.height = nextHeightLevel+'px';
	
	// if step is to big in the last pass, reduce it 
	if (mode == 1 && (widthOriginal - desiredWidth) <= stepWidth) {
		stepWidth = (widthOriginal - desiredWidth);
	}
	else if (mode==2 && (widthOriginal - desiredWidth) <= stepWidth) {
		stepWidth = (widthOriginal - desiredWidth);
	}
	if (mode==1 && (heightOriginal - desiredHeight) <= stepHeight) {
		stepHeight = (heightOriginal - desiredHeight);
	}
	else if (mode==2 && (heightOriginal - desiredHeight) <= stepHeight) {
		stepHeight = (heightOriginal - desiredHeight);
	}
	
	// repeat this function and set timeout
	if (widthElement.style.width.split("px")[0] != desiredWidth || heightElement.style.height.split("px")[0] != desiredHeight) 
		setTimeout("timedelayedSizing('" + mode + "', '" + widthElementId + "', '" + heightElementId + "', '" + stepWidth + "', '" + stepHeight + "','" + ms + "','" + desiredWidth + "', '" + desiredHeight + "');", ms);
	else {
		document.getElementById('videoBackground').style.visibility='hidden';
		return true;
	}
}

function videoSmall(videoId, desiredWidth, desiredHeight) {
	videoId = parseInt(videoId);
		
	// smoother scaling
	var smootherScaling=0;
	if (smootherScaling) timedelayedSizing(2, 'videoplayer_'+videoId, 'videoplayer_'+videoId, 20, 20, 3, desiredWidth, desiredHeight);

	// only DOM manipulations
	if (!smootherScaling && typeof document.getElementById('videoBackground') != "undefined" && document.getElementById('videoBackground')) {
		document.getElementById('videoBackground').style.visibility='hidden';
		document.getElementById('videoplayer_'+videoId).width=desiredWidth;    //'264px'; 
		document.getElementById('videoplayer_'+videoId).height=desiredHeight;  //'215px';
		document.getElementById('videoplayer_'+videoId).style.width=desiredWidth+'px';    //'264px'; 
		document.getElementById('videoplayer_'+videoId).style.height=desiredHeight+'px';  //'215px';
	}

	// urspruenglichen Margin und Padding setzen
	document.body.style.margin=bodyMargin;
	document.body.style.padding=bodyPadding;
	
	// Background-Color zuruecksetzen, width zuruecksetzen, Margin setzen, urspruengliche left- und top-Position setzen
	document.getElementById('videoContainer_'+videoId).style.backgroundColor='';
	document.getElementById('videoContainer_'+videoId).style.width ='';
	document.getElementById('videoContainer_'+videoId).style.margin='0';
	document.getElementById('videoContainer_'+videoId).style.left=posVideoSmallLeft;
	document.getElementById('videoContainer_'+videoId).style.top=posVideoSmallTop;
	
	// SrollElem()-Timer abbrechen
	if (typeof activeTimer != "undefined") window.clearTimeout(activeTimer);
	
	// scrolls the page to desired view
	window.scrollTo('', (getY(document.getElementById('videoplayer_'+videoId))-10));
	
	// Dom Manipulationen fuer de konkrete Seite vornehmen 
	individualDomManipulationsVideoSmall();
	
	// alle Objects/Embeds wieder einblenden
	for(var y=0;y<aSWFs.length;y++) {
			if (y!=videoId) document.getElementById('videoContainer_'+y).style.display='';
	}
}

function videoLarge(videoId, desiredWidth, desiredHeight, mode, level) {
	videoId = parseInt(videoId);
	// get size of inner Browser area
	var innerXYArray = innerBrowserSize();
	
	// get dimensions for fullsize video for case that desiredWidth and desiredHeight are not set
	var widthAndHeight = getVideoFullSize(innerXYArray[0], innerXYArray[1], getVideoPlayerSizeProp('videoplayer_'+videoId));
	
	// ggf. Modifikation der Playerhoehe fuer die Controls des Players 
	if ( controlHeightPlayer ) widthAndHeight[1] = parseInt(widthAndHeight[1])-parseInt(controlHeightPlayer); 
	else widthAndHeight[1] = widthAndHeight[1];
	
	// create a video background div
	createVideoBackgroundDiv('videoBackground', innerXYArray[0], innerXYArray[1], htmlBackgroundColor);
	
	// only DOM manipulation
	if (typeof document.body.cellspacing == "undefined" && !document.body.cellspacing) 
		bodyMargin = document.body.style.margin;
	else 
		bodyMargin = document.body.cellspacing;
	
	if (typeof document.body.cellpadding == "undefined" && !document.body.cellpadding) 
		bodyPadding = document.body.style.padding;
	else 
		bodyPadding = document.body.cellpadding;

	// Body Margin und Padding loeschen	
	document.body.style.margin = '0px';
	document.body.style.padding = '0px';

	// Breite und Hoehe des Object-/Embed-Elements setzen	
	if (!desiredWidth) {
		document.getElementById('videoplayer_' + videoId).width = widthAndHeight[0];
		document.getElementById('videoplayer_' + videoId).style.width = widthAndHeight[0] + "px";
	}
	else { 
		document.getElementById('videoplayer_' + videoId).width = desiredWidth;
		document.getElementById('videoplayer_' + videoId).style.width = desiredWidth + "px";
	}
	if (!desiredHeight) {
		document.getElementById('videoplayer_' + videoId).height = widthAndHeight[1];
		document.getElementById('videoplayer_' + videoId).style.height = widthAndHeight[1] + "px";
	}
	else {
		document.getElementById('videoplayer_' + videoId).height = desiredHeight;
		document.getElementById('videoplayer_' + videoId).style.height = desiredHeight + "px";
	}
	
	// videoContainer-Breite setzen
	document.getElementById('videoContainer_' + videoId).width = innerXYArray[0];
	document.getElementById('videoContainer_' + videoId).style.width = innerXYArray[0] + "px";
	
	// videoContainer-Inhalt zentrieren und Hintergrundfarbe setzen
	document.getElementById('videoContainer_' + videoId).style.textAlign='center';
	document.getElementById('videoContainer_' + videoId).style.backgroundColor = htmlBackgroundColor;
	
	// alle Objects/Embeds ausblenden bis auf aktuelles Object/Embed	
	for(var y=0;y<aSWFs.length;y++) {
			if (y!=videoId) document.getElementById('videoContainer_'+y).style.display='none';
	}

	// in den Vordergrund setzen	
	document.getElementById('videoplayer_'+videoId).style.zIndex='2002';
	document.getElementById('videoContainer_'+videoId).style.zIndex='2001'; 

	// urspruengliche left- und top-Position speichern	
	posVideoSmallLeft = document.getElementById('videoContainer_'+videoId).style.left;
	posVideoSmallTop = document.getElementById('videoContainer_'+videoId).style.top;

	// neue left- und -top Position errechnen	
	if (desiredWidth && desiredWidth<widthAndHeight[0]) var leftOffsetAddition = (parseInt(widthAndHeight[0]) - parseInt(desiredWidth)) / 2;
	else var leftOffsetAddition = 0;
	if (desiredHeight && desiredHeight<widthAndHeight[1]) var topOffsetAddition = (parseInt(widthAndHeight[1]) - parseInt(desiredHeight)) / 2;
	else var topOffsetAddition = 0;

	// neue left- und top-Position setzen	
	if (widthAndHeight[0] != innerXYArray[0]) {
		var leftOffset = (parseInt(innerXYArray[0]) - parseInt(widthAndHeight[0])) / 2;
		document.getElementById('videoContainer_'+videoId).style.left=(leftOffset + leftOffsetAddition) +"px";
		document.getElementById('videoContainer_'+videoId).style.top=(pageOffset().top + topOffsetAddition) + 'px';
		
		// scrolls the page to desired view
		window.scrollTo(getX(document.getElementById('videoContainer_'+videoId)), getY(document.getElementById('videoplayer_'+videoId)));
		// damit das Div mitscrollt
		if(videoScroll==1) scrollElem('videoContainer_' + videoId, (pageOffset().top + topOffsetAddition));
	}
	else {
		var topOffset = (parseInt(innerXYArray[1]) - parseInt(widthAndHeight[1])) / 2;
		document.getElementById('videoContainer_'+videoId).style.top=topOffset+"px";
		
		// scrolls the page to desired view
		window.scrollTo(getX(document.getElementById('videoContainer_'+videoId)), getY(document.getElementById('videoplayer_'+videoId))-topOffset);	
		// damit das Div mitscrollt
		if(videoScroll==1) scrollElem('videoContainer_' + videoId, topOffset);
	}
	
	// Dom Manipulationen fuer de konkrete Seite vornehmen 
	individualDomManipulationsVideoLarge();
	
	// Objekt erstellen zur Uebergabe an das SWF
	var widthHeightObj = {"w" : document.getElementById('videoplayer_' + videoId).style.width.split("px")[0], "h" : document.getElementById('videoplayer_' + videoId).style.height.split("px")[0]};
	return widthHeightObj;
}

function createVideoBackgroundDiv(divName, divWidth, divHeight, htmlBackgroundColor) {
	// if special video background div exists delete it
	if (typeof document.getElementById(divName) != "undefined" && document.getElementById(divName)) {
		var elemToDel = document.getElementById(divName);
		elemToDel.parentNode.removeChild(elemToDel);
	}
	
	// create new video background div
	if (typeof document.getElementById(divName) == "undefined" || !document.getElementById(divName)) {
		var newDiv = document.createElement("div");
		newDiv.id = divName;
		newDiv.style.position = 'fixed';
		newDiv.style.width = divWidth+"px";
		newDiv.style.height = (divHeight+pageOffset().top)+"px";
		newDiv.style.left = pageOffset().left + 'px';
		newDiv.style.top = '0px';
		newDiv.style.zIndex = backgroundZIndex;
		newDiv.style.visibility = 'visible';
		newDiv.style.backgroundColor = htmlBackgroundColor;
		document.body.appendChild(newDiv);
	}
	else {
		// only DOM manipulations
		document.getElementById(divName).style.visibility = 'visible';
		document.getElementById(divName).style.width = divWidth;
		document.getElementById(divName).style.height = divHeight;
	}
}

function individualDomManipulationsVideoLarge() {
	// only for bosch-do-it.de
	// bestimmte Divs ueber Visibility-Attribut ausblenden
	// bestimmte Divs mit Klassennamen aber ohne ID ausblenden	
	arrDivs = getElemWithClassname("div", "notice");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "hidden";
	}
	
	arrDivs = getElemWithClassname("div", "hrthintop");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "hidden";
	}
	
	arrDivs = getElemWithClassname("div", "hrthin");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "hidden";
	}
	
	arrDivs = getElemWithClassname("div", "cright");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "hidden";
	}
	
	arrDivs = getElemWithClassname("div", "printlink");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "hidden";
	}
	
	arrDivs = getElemWithClassname("div", "boschdivider");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "hidden";
	}
	
	// bestimmte Divs mit vorhandener ID ausblenden
	document.getElementById('marginalspalte').style.visibility="hidden";
	document.getElementById('kommentare').style.visibility="hidden";
	document.getElementById('idStageHouse').style.visibility="hidden";
	document.getElementById('idStageHouse2').style.visibility="hidden";
	document.getElementById('boschheader').style.visibility="hidden";
	document.getElementById('boschnavi').style.visibility="hidden";
	if (document.getElementById('idStageList')) document.getElementById('idStageList').style.visibility="hidden";
	if (document.getElementById('idProjectlist')) document.getElementById('idProjectlist').style.visibility="hidden";
	
	// Hintergrund grau setzen
	for (i = 0; i < getNumberPlayersOnSite(); i++) {
		document.getElementById('videoContainer_'+i).parentNode.parentNode.parentNode.parentNode.style.backgroundColor = htmlBackgroundColor;
		document.getElementById('videoContainer_'+i).parentNode.parentNode.parentNode.parentNode.style.color = htmlBackgroundColor;
	}
	document.body.style.backgroundColor = htmlBackgroundColor;
	$("div").css('backgroundColor', htmlBackgroundColor);	
}

function individualDomManipulationsVideoSmall() {
	// only for bosch-do-it.de
	// Visibility Divs der ausgeblendeten Divs wieder zuruecksetzen
	// bestimmte Divs mit Klassennamen aber ohne ID wieder einblenden
	arrDivs = getElemWithClassname("div", "notice");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "";
	}
	
	arrDivs = getElemWithClassname("div", "hrthintop");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "";
	}
	
	arrDivs = getElemWithClassname("div", "hrthin");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "";
	}
	
	arrDivs = getElemWithClassname("div", "cright");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "";
	}
	
	arrDivs = getElemWithClassname("div", "printlink");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "";
	}
	
	arrDivs = getElemWithClassname("div", "boschdivider");
	for (i = 0; i < arrDivs.length; i++) {
		arrDivs[i].style.visibility = "";
	}
	
	// bestimmte Divs mit vorhandener ID wieder einblenden
	document.getElementById('marginalspalte').style.visibility="";
	document.getElementById('kommentare').style.visibility="";
	document.getElementById('idStageHouse').style.visibility="";
	document.getElementById('idStageHouse2').style.visibility="";
	document.getElementById('boschheader').style.visibility="";
	document.getElementById('boschnavi').style.visibility="";
	if (document.getElementById('idStageList')) document.getElementById('idStageList').style.visibility="";
	if (document.getElementById('idProjectlist')) document.getElementById('idProjectlist').style.visibility="";	
	
	// Hintergrund wieder wie urspruenglich setzen
	for (i = 0; i < getNumberPlayersOnSite(); i++) {
		document.getElementById('videoContainer_'+i).parentNode.parentNode.parentNode.parentNode.style.backgroundColor = '';
		document.getElementById('videoContainer_'+i).parentNode.parentNode.parentNode.parentNode.style.color = '';
	}
	document.body.style.backgroundColor = '';
	$("div").css('backgroundColor', '');
}

function getNumberPlayersOnSite() {
	for (var lauf=1; lauf<=30; lauf++) {
		if ( document.getElementById('videoContainer_'+lauf) ) numberPlayers=lauf;
	}
	return numberPlayers;
}

function getElemWithClassname(elem, classname) {
	var arrDivs = [];	
	var arr = document.getElementsByTagName(elem);
	for (i = 0; i < arr.length; i++) {
		if (arr[i].className == classname) arrDivs[arrDivs.length] = arr[i];
	}
	return arrDivs;
}

function getX(el) {
	x = el.offsetLeft;
	if (!el.offsetParent) return x;
	else return (x+getX(el.offsetParent));
}

function getY (el) {
	y = el.offsetTop;
	if (!el.offsetParent) return y;
	else return (y+getY(el.offsetParent));
}

function pageOffset(win) {
	// get actual position of viewport
    if (!win) win = window;
    var pos = {left:0,top:0};

    if (typeof win.pageXOffset != 'undefined') {  // Mozilla/Netscape
         pos.left = win.pageXOffset;
         pos.top = win.pageYOffset;
    }
    else {
         var obj = (win.document.compatMode && win.document.compatMode == "CSS1Compat") ?
         win.document.documentElement : win.document.body || null;

         pos.left = obj.scrollLeft;
         pos.top = obj.scrollTop;
    }
    return pos;
}

function setCookie (name, value, expiresInXDays) {
	if (!expiresInXDays) expiresInXDays=7;
	var expiration = new Date();
	var inXDays = expiration.getTime() + (parseInt(expiresInXDays) * 24 * 60 * 60 * 1000);
	expiration.setTime(inXDays);
	
	if (name && value) document.cookie = name + "=" + value + "; expires=" + expiration.toGMTString();
}

function getCookie (name) {
	var cookieSplitArray= new Array();
	if (document.cookie) {
		cookieSplitArray = document.cookie.split("=");
		var a = new Array();
		for (var i = 0; i < cookieSplitArray.length; i++) {
			if (cookieSplitArray[i] == name) 
				return cookieSplitArray[++i];
			else 
				return false;
		}
	}
	else 
		return false;
}

function stopVideos(id) {
//log(id);
			id = parseInt(id);
			for(var i=0;i<aSWFs.length;i++)
			{
				if(aSWFs[i] != 'videoplayer_' +id)
				{
					//log("stopping " + aSWFs[i]);
					
					var fm = document.getElementById(aSWFs[i]);
					fm.stopVideo();
				}
			}
}
		
function setVolume(id, volume) {
//log(id);
			id = parseInt(id);
			for(var i=0;i<aSWFs.length;i++)
			{
				if(aSWFs[i] != 'videoplayer_' +id)
				{
					//log("volume: " + volume);
					var fm = document.getElementById(aSWFs[i]);
					fm.setVolume(volume);
				}
			}

			setCookie ('volume', volume, 365);
}

function scrollElem(elemId, topPosition) {
	var b = topPosition;

	if (window.pageYOffset) {
		b = topPosition + window.pageYOffset;
	}
	else {
		b = 1 + document.body.scrollTop;
	}
	
	if (typeof document.getElementById(elemId) != "undefined" && document.getElementById(elemId)) {
		document.getElementById(elemId).style.top = b + "px";
	}
	activeTimer=window.setTimeout("scrollElem('"+elemId+"',"+topPosition+")",3);
}

function osCheck() {
	agt=navigator.userAgent.toLowerCase();
	sys=(navigator.platform)?navigator.platform.toLowerCase():agt;
	agt_os=((sys.indexOf('mac')>=0)?"Macintosh":(sys.indexOf('unix')>=0 ||
	 	sys.indexOf('linux')>=0 || sys.indexOf('x11')>=0 || 
		sys.indexOf('x 11')>=0)?"Linux/Unix":(sys.indexOf('os/2')>=0)?"OS/2":(sys.indexOf('win')>=0)?"Windows":"");
	return agt_os;
}
		
function log(text) {
			//window.console.log(text);
}
		
function addSWF(videoId, target, videofile, videoplayerSwf, content_url, preview_pic, subtitles, flvPseudoStreamScriptUrl, stream_start, stream_stop) {
		videoId = parseInt(videoId);
		
		var so = new SWFObject(videoplayerSwf, 'videoplayer_' + videoId, videoWidthSmall, videoHeightSmall, "6", "#ffffff");
		// params
			so.addParam("quality", "high");
			if (osCheck()=="Macintosh") so.addParam("wmode", "window");
			else so.addParam("wmode", "opaque");
			so.addParam("salign", "tl");
			so.addParam("allowScriptAccess", "always");
			so.addParam("swliveconnect", "true");
		
		// variables
			// style
			so.addVariable("sizing", sizing);
			so.addVariable("sizingEffect", sizingEffect);
			so.addVariable("playerStyle", playerStyle);
			so.addVariable("mode", mode);
			so.addVariable("bytespersecond", bytespersecond);
			// control dimensions
			so.addVariable("normal_w", videoWidthSmall);
			so.addVariable("normal_h", videoHeightSmall);
			so.addVariable("zoomed_w", videoWidthZoomed);
			so.addVariable("zoomed_h", videoHeightZoomed);
			so.addVariable("control_h", controlHeightPlayer);
			so.addVariable("button_w", button_w);
			so.addVariable("time_w", time_w);
			so.addVariable("myID", videoId);
			// especially for video
			so.addVariable("volume", getCookie("volume") ? getCookie("volume") : "75" );
			so.addVariable("videofile", videofile);
			so.addVariable("videoextension", videoextension);
			so.addVariable("content_url", content_url);
			so.addVariable("preview_pic", preview_pic);
			so.addVariable("videoMode", videoMode);
			so.addVariable("subtitles", subtitles);
			// if videomode is 2 or 3, set these vars
			so.addVariable("flvPseudoStreamScriptUrl", flvPseudoStreamScriptUrl);
			so.addVariable("stream_start", stream_start);
			so.addVariable("stream_stop", stream_stop);
		
		// put it in the dom
			so.write(target);
			aSWFs.push('videoplayer_' + videoId);	 // Nils  // ie-alternative: aSWFs[aSWFs.length] = 'videoplayer_' + videoId;    
		
		// do some object/embed manipulations
			document.getElementById('videoplayer_' + videoId).style.display="inline";
			document.getElementById('videoplayer_' + videoId).style.width=videoWidthSmall+'px';
			document.getElementById('videoplayer_' + videoId).style.height=videoHeightSmall+'px';
			document.getElementById('videoplayer_' + videoId).style.zIndex='2002';
}

/* ]]> */
