
window.addEvent('domready', function() {
	initDom();
});

function initDom() {
	pngfix();
	var x = new Chain();
	var x1 = function(){ setTextbox(); };
	var x2 = function(){ observeWindow(); };
	x.chain(x1); x.chain(x2);
	x.callChain(); x.callChain();
	set1stHomeVideo();
	set1stFlash();
}

function setTextbox() {
	var iLastRow = 0;
	for (var i = 1; i < 50; i++) {
		var iHighest = 0;
		$$('.textboxRow' + i).each(function (obj) {
			if (obj.getStyle('height').toInt() > iHighest) {
				iHighest = obj.getStyle('height').toInt();
			}
			iLastRow = i;
		});
		if (iHighest > 0) {
			$$('.textboxRow' + i).each(function (obj) {
				if (!window.ie6) {
					obj.setStyle('height', iHighest);
				}
				else {
					obj.setStyle('height', iHighest+1); /* because of the box-model from ie6 */
				}
			});
		}
	}
	$$('.textboxRow' + iLastRow).each(function (obj) {
		obj.setStyle('border-bottom', '0');
	});

	if ($('textboxHomeNews') && $$('.textboxCol').length) {
		var iHighest = 0;
		$$('.textboxCol').each(function (obj) {
			if (obj.getStyle('height').toInt() > iHighest) {
				iHighest = obj.getStyle('height').toInt();
			}
		});
		var iHomeNews = $('textboxHomeNews').getStyle('height').toInt();
		if (iHomeNews > iHighest) {
			var iNewTextboxHeight = iHomeNews - iHighest;
			$$('.textboxRow1').each(function (obj) {
				if (!window.ie6) {
					obj.setStyle('height', obj.getStyle('height').toInt()+iNewTextboxHeight);
				}
				else {
					obj.setStyle('height', obj.getStyle('height').toInt()+iNewTextboxHeight+1); /* because of the box-model from ie6 */
				}
			});
		}
	}
}

function observeWindow() {
	window.addEvent('resize', setBG);

	if(window.ie6){
		window.addEvent('scroll', function() {
			var iNewFooterPos = window.getScrollTop() + window.getHeight() - 51;
			$('footerBox').setStyle('top', iNewFooterPos);
		});
	}

	setBG();
}

function setBG() {
	var iScrWinX = window.getScrollWidth();
	var iWinY = window.getHeight();
	var iScrWinY = window.getScrollHeight();

	if (!window.ie6) {
		$('fixContentBox').setStyle('width', iScrWinX);
		$('fixContentBox').setStyle('height', iScrWinY);
	}
	else {
		if (iScrWinX == 990) {
			$('headerBox').setStyle('width', iScrWinX);
			$('fixContentBox').setStyle('width', iScrWinX);
			$('footerBox').setStyle('width', iScrWinX);
		}
		if (iScrWinY > iWinY) {
			$('fixContentBox').setStyle('height', iScrWinY-25);
		}
	}
}

function set1stHomeVideo() {
	var arVids = $$('.videos');
	if (arVids.length > 0) {
		var arTmp = arVids[rand(0, arVids.length)].id.split('vid');
		playVideo(arTmp[1], false, true);
	}
}

function set1stFlash() {
	var arVids = $$('.flashs');
	if (arVids.length > 0) {
		var arTmp = arVids[rand(0, arVids.length)].id.split('vidLeft');
		playLeftSideVideo(arTmp[1]);
	}
}

function rand(min, max) {
	return Math.floor(Math.random() * (max - min)) + min;
}

var curVid = 0;
function playVideo(aId, bNoAutostart, bNoVolume) {

	if (curVid > 0) {
		$('player' + curVid).setHTML('');
		$('playerBox' + curVid).setStyle('display', 'none');
	}

	$('playerBox' + aId).setStyle('display', 'block');

	var sAutostart = true;
	if (bNoAutostart) {
		sAutostart = false;
	}

	var iVolume = 80;
	if (bNoVolume) {
		iVolume = 0;
	}

	var sFlv = $('vid' + aId).getProperty('value');
	var so = new SWFObject('fileadmin/templates/lrp/swf/mediaplayer.swf', 'player' + aId, '360', '240', '7');
	so.addParam("allowfullscreen", "true");
	so.addVariable('width', '360');
	so.addVariable('height', '240');
	so.addVariable('repeat', 'always');

	so.addVariable('file', '/uploads/media/' + sFlv);
	so.addParam('wmode', 'transparent');
	so.addVariable('autostart', sAutostart);
	so.addVariable('volume', iVolume);
	so.write('player' + aId);

	curVid = aId;
}

function playLeftSideVideo(aId) {
	if(!aId)
		return

	$('playerLeft' + aId).setHTML('');
	$('playerBoxLeft' + aId).setStyle('display', 'inline');

	var sAutostart = false;
	var iVolume = 0;

	var sFlv = $('vidLeft' + aId).getProperty('value');

	//alert('/uploads/media/' + sFlv)

	var so = new SWFObject('/uploads/media/' + sFlv, 'playerLeft' + aId, '360', '240', '7');
	so.addParam("allowfullscreen", "true");
	so.addVariable('width', '360');
	so.addVariable('height', '240');
	so.addParam('wmode', 'opaque');
	so.addVariable('autostart', sAutostart);
	so.addVariable('volume', iVolume);
	so.write('playerLeft' + aId);
}



var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

function pngfix() {
	if (window.ie6) {
		if ((version >= 5.5) && (document.body.filters))
		{
		   for(var i=0; i<document.images.length; i++)
		   {
		      var img = document.images[i]
		      var imgName = img.src.toUpperCase()
		      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		      {
		         var imgID = (img.id) ? "id='" + img.id + "' " : ""
		         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		         var imgStyle = "display:inline-block;" + img.style.cssText
		         if (img.align == "left") imgStyle = "float:left;" + imgStyle
		         if (img.align == "right") imgStyle = "float:right;" + imgStyle
		         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
		         var strNewHTML = "<span " + imgID + imgClass + imgTitle
		         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
		         img.outerHTML = strNewHTML
		         i = i-1
		      }
		   }
		}
	}
}


function openPic(url,winName,winParams) {       //
	var theWindow = window.open("/"+url,winName,winParams);
	if (theWindow)  {theWindow.focus();}
}
