/* Custom JavaScript */

// Helpers *************************

var leoVars = {
	isIE : ((!window.opera) && (navigator.userAgent.indexOf('MSIE') != -1)),
	isIECSSCompat : ((document.compatMode && document.compatMode.indexOf('CSS1') >= 0) ? true : false),
	allImages : null,
	theImage : null,
	i : 0,
	j : 0,
	movie : null,
	shadow : null,
	closeMovie : null,
	theDivFlash : null,
	popup : {
		intervalID : null,
		elem : null,
		step : 10,
		delay : 4,
		topdimension : 0,
		tp : 0,
		leftdimension : 0,
		lt : 0,
		rightdimension : 0,
		rt : 0,
		bottomdimension : 0,
		bt : 0
	}
};

function leoAddEvent(elemRef, evtType, listener, useCapture) {
	var elem = leoGetEl(elemRef);
	useCapture = (useCapture) ? useCapture : false;
	if (elem.addEventListener) { // W3C
		elem.addEventListener(evtType, listener, useCapture);
		return true;
	} else if (elem.attachEvent) { // IE; function addon to make IE understand 'this'
		var r = elem.attachEvent('on' + evtType, function(){listener.call(elem)});
		return r;
	} else {
		// for IE/Mac, NN4, and older
		elem['on' + evtType] = listener;
	}
}

function leoFindPosX(elemRef) {
	var elem = leoGetEl(elemRef);
	var curLeft = 0;
	if (elem.offsetParent) {
		do {
			curLeft += elem.offsetLeft;
		} while (elem = elem.offsetParent);
	}
	else if (elem.x) {
		curLeft += elem.x;
	}
	return curLeft;
}

function leoFindPosY(elemRef) {
	var elem = leoGetEl(elemRef);
	var curTop = 0;
	if (elem.offsetParent) {
		do {
			curTop += elem.offsetTop;
		} while (elem = elem.offsetParent);
	}
	else if (elem.y) {
		curTop += elem.y;
	}
	return curTop;
}

function leoGetEl(elemRef) {
	if (typeof elemRef == 'string') {
		if (document.getElementById)
			return document.getElementById(elemRef);
		else if (document.all)
			return document.all(elemRef);
	}
	else
		return elemRef;
}

function leoRemoveLeftMenu() {
	if (leoGetEl('main') && leoGetEl('local')) {
		var main = leoGetEl('main');
		main.removeChild(leoGetEl('local'));
	}
}

function leoWinHeight() {
	var wH = 0;
	if (window.innerHeight) { // Mozilla
		wH = window.innerHeight;
	} else if (leoVars.isIECSSCompat) { // IE7
		wH = document.body.parentElement.clientHeight;
	} else if (document.documentElement.clientHeight) {
		wH = document.documentElement.clientHeight;
	} else if (document.body.clientHeight) {
		wH = document.body.clientHeight;
	} else if (screen.availHeight) {
		wH = screen.availHeight;
	}
	return wH;
}

function leoWinWidth() {
	var wW = 0;
	if (window.innerWidth) { // Mozilla
		wW = window.innerWidth;
	} else if (leoVars.isIECSSCompat) { // IE7
		wW = document.body.parentElement.clientWidth;
	} else if (document.documentElement.clientWidth) {
		wW = document.documentElement.clientWidth;
	} else if (document.body.clientWidth) {
		wW = document.body.clientWidth;
	} else if (screen.availWidth) {
		wW = screen.availWidth;
	}
	return wW;
}


// Movie Stuff ***************************

function leoShowMovie(theW, theH, theFLV) {
	leoCloseMovie();

	var winW = leoWinWidth();
	var winH = leoWinHeight();
	var theMargin = 3;
//	var theY = (theH > winH) ? 0 : parseInt((winH - theH)/3, 10) + document.documentElement.scrollTop;
	var theY = 126 + document.documentElement.scrollTop;
	var theX = ((winW/2) - ((theW + theMargin)/2));

	leoVars.movie = document.createElement('div');
	leoVars.movie.id = 'divMovie';
	leoVars.movie.style.position = 'absolute';
	leoVars.movie.style.left = theX + 'px';
	leoVars.movie.style.top = theY + 'px';
	leoVars.movie.style.width = theW + theMargin + 'px';
	leoVars.movie.style.height = theH + theMargin + 'px';
	leoVars.movie.style.paddingTop = theMargin + 'px';
	leoVars.movie.style.paddingLeft = theMargin + 'px';
	leoVars.movie.style.backgroundColor = '#4b4b4b';
	leoVars.movie.style.zIndex = 100;
	leoVars.theDivFlash = document.createElement('div');
	leoVars.theDivFlash.id = 'divFlash';
	leoVars.movie.appendChild(leoVars.theDivFlash);
	document.body.appendChild(leoVars.movie);

	leoVars.shadow = document.createElement('img');
	leoVars.shadow.id = 'divShadow';
	leoVars.shadow.style.position = 'absolute';
	leoVars.shadow.src = '/Files/Billeder/LEO_corporate_images/graphics/Shadow.png';
	leoVars.shadow.style.left = theX - (1000 - theW) + 35 + 'px';
	leoVars.shadow.style.top = theY - (1000 - theH) + 38 + 'px';
	leoVars.shadow.style.overflow = 'hidden';
	leoVars.shadow.style.clip = 'rect(' + (1000 - theH - 38) + 'px, 1000px, 10000px, ' + (1000 - theW - 33) + 'px)';
	document.body.appendChild(leoVars.shadow);

	leoVars.closeMovie = document.createElement('img');
	leoVars.closeMovie.id = 'closeMovie';
	leoVars.closeMovie.style.position = 'absolute';
	leoVars.closeMovie.src = '/Files/Billeder/LEO_corporate_images/graphics/CloseMovie.png';
	leoVars.closeMovie.style.left = theX + theW - 12 + 'px';
	leoVars.closeMovie.style.top = theY - 33 + 'px';
	leoVars.closeMovie.width = '50';
	leoVars.closeMovie.height = '50';
	leoVars.closeMovie.alt = 'Close video';
	leoVars.closeMovie.title = 'Close video';
	leoVars.closeMovie.style.cursor = 'pointer';
	leoVars.closeMovie.style.zIndex = 50;
	document.body.appendChild(leoVars.closeMovie);
	leoAddEvent('closeMovie', 'mouseover', leoCloseOver, false);
	leoAddEvent('closeMovie', 'mouseout', leoCloseOver, false);
	leoAddEvent('closeMovie', 'click', leoCloseMovie, false);

	var theFlashContent = "";
	theFlashContent += "<div id='flashcontent'>Get the free <a href='http://www.macromedia.com/go/getflashplayer'>Adobe Flash Player</a> to see this video <br />"
	theFlashContent += "<br />"
	theFlashContent += "<a href='http://www.macromedia.com/go/getflashplayer'><img border='0' alt='Get Adobe Flash Player' width='112' height='33' src='http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif' /></a></div>"
	theFlashContent += "</div>"
	leoGetEl("divFlash").innerHTML = theFlashContent;

	var width = theW;
	var height = theH;
	var fl = {};
	fl.flv = '/Files/Filer/Flash/FLV/' + theFLV;
	fl.width = width;
	fl.height = height;
	fl.autoplay = '1';
	fl.autoload = '1';
	fl.showiconplay = '1';
	fl.margin = '0';
	fl.showstop = '1';
	fl.showvolume = '1';
	fl.showtime = '1';
	fl.showfullscreen = '1';
	fl.buffermessage = '';
	fl.buffercolor = 'a9a9a9';
	fl.buffershowbg = '0';
	var params = {};
	params.allowFullScreen = 'true';
	params.wmode = 'opaque';
	swfobject.embedSWF('/files/system/leo/js/player_flv_maxi.swf?reload=' + new Date().getTime(), 'flashcontent', width, height, '9.0.0','expressInstall.swf', fl, params);
}

function leoCloseOver(e) {
	var e = e || window.event;
	var t = e.target || e.srcElement;
	if (e.type == 'mouseover') {
		t.src = '/Files/Billeder/LEO_corporate_images/graphics/CloseMovieOver.png';
		t.width = '60';
		t.height = '60';
		t.style.left = (leoFindPosX(t) - 5) + 'px';
		t.style.top = (leoFindPosY(t) - 5) + 'px';
		t.style.zIndex = '101';
	}
	else if (e.type == 'mouseout') {
		t.src = '/Files/Billeder/LEO_corporate_images/graphics/CloseMovie.png';
		t.width = '50';
		t.height = '50';
		t.style.left = (leoFindPosX(t) + 5) + 'px';
		t.style.top = (leoFindPosY(t) + 5) + 'px';
		t.style.zIndex = '50';
	}
}

function leoCloseMovie() {
	if (document.getElementById('closeMovie'))
		document.body.removeChild(document.getElementById('closeMovie'));
	if (document.getElementById('divMovie'))
		document.body.removeChild(document.getElementById('divMovie'));
	if (document.getElementById('divShadow'))
		document.body.removeChild(document.getElementById('divShadow'));
}


// Text Helpers ***************************

// test if searchString is contained in mainString
function leoIsInMainstring(mainStr, searchStr) {
	var re = new RegExp(searchStr, 'g');
	return mainStr.match(re) ? true : false;
}

// extract front part of string prior to searchString
function leoGetFront(mainStr, searchStr) {
	var foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset == -1) return null;
	return mainStr.substring(0,foundOffset);
}

// extract back end of string after searchString
function leoGetEnd(mainStr, searchStr) {
	var foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset == -1) return null;
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length);
}

// extract back end of string after searchChar searching backwards
function leoGetEndBack(mainStr, searchChar) {
	for (var i = mainStr.length; i > 0; i--)
			if (mainStr.charAt(i) == searchChar)
				return mainStr.substring(i + 1);
}

// get image name before image type
function leoGetImgName(theImg) {
	var theL = theImg.length;
	for (var i = theL; i > 0 ; i--) {
		if (theImg.substring(i, i + 1) == ".") {
			return theImg.substring(0, i);
		}
	}
}

// get image type
function leoGetImgType(theImg) {
	var theL = theImg.length;
	for (var i = theL; i > 0 ; i--) {
		if (theImg.substring(i, i + 1) == ".") {
			return theImg.substring(i, theL);
		}
	}
}


// Add Listeners *************************

function leoAddListeners() {
	if (!document.getElementsByTagName) return;

	var allImages = document.getElementsByTagName('img');
	var theS = '';
	for (var i = 0; i < allImages.length; i++) {
		var theImage = allImages[i];
		// Roll Image
		if (theImage.className.toLowerCase() == 'leorollimage') {
			leoAddEvent(theImage, 'mouseover', leoRollImage, false);
			leoAddEvent(theImage, 'mouseout', leoRollImage, false);
			theS = theImage.src;
			theImage.overSrc = leoGetImgName(theS) + 'Roll' + leoGetImgType(theS);
			theImage.outSrc = theS;
		// Pop Image
		} else if (theImage.className.toLowerCase() == 'leopopimage') {
			leoAddEvent(theImage, 'mousedown', leoPopImage, false);
			theS = theImage.src;
			theImage.popImage = leoGetImgName(theS) + 'Pop' + leoGetImgType(theS);
		// Hyper Image
		} else if (theImage.className.toLowerCase() == 'leohyperimage') {
			leoAddEvent(theImage, 'mouseover', leoHyperImage, false);
			leoAddEvent(theImage, 'mouseout', leoHyperImage, false);
			theS = theImage.src;
			theImage.hyperImage = leoGetImgName(theS) + 'Hyper' + leoGetImgType(theS);
		// Zoom Image
		} else if (theImage.className.toLowerCase() == 'leozoomimage') {
			leoAddEvent(theImage, 'click', leoZoomImage, false);
			theS = theImage.src;
			theImage.zoomImage = leoGetImgName(theS) + 'Zoom' + leoGetImgType(theS);
		// 4 Random Images
		} else if (theImage.className.toLowerCase() == 'leorandomimage') {
			theS = theImage.src;
			var theFront = theS.substring(0, (theS.length - 5));
			var theType = leoGetImgType(theS);
			var theNewNum = Math.floor(Math.random() * 4) + 1; // giver resultat fra 1 til 4
			theImage.src = theFront + theNewNum + theType;
		}
	}
}

function leoPopImage(ev) {
	var e = window.event ? window.event : ev;
	var t = e.target ? e.target : e.srcElement;
	if (document.getElementById('divPopup'))
		document.body.removeChild(document.getElementById('divPopup'));
	var popImage = new Image();
	popImage.onload = function() {
		var winW = leoWinWidth();
		var winH = leoWinHeight();
		var imgWidth = popImage.width;
		var imgHeight = popImage.height;
		var theY = (imgHeight > winH) ? 0 : parseInt((winH - imgHeight)/3, 10);
		leoVars.popup.elem = document.createElement('div');
		leoVars.popup.elem.style.position = 'absolute';
		leoVars.popup.elem.style.left = (((winW - 24) / 2) - (imgWidth/2)) + 'px';
		leoVars.popup.elem.style.top = theY + document.documentElement.scrollTop + 'px';
		leoVars.popup.elem.style.width = imgWidth + 3 + 'px';
		leoVars.popup.elem.style.height = imgHeight + 3 + 'px';
		leoVars.popup.elem.id = 'divPopup';
		leoVars.popup.elem.title = 'Click to close';
		leoVars.popup.elem.style.paddingTop = '1px';
		leoVars.popup.elem.style.paddingLeft = '1px';
		leoVars.popup.elem.style.backgroundColor = '#000';
		leoVars.popup.elem.style.zIndex = 100;
		leoVars.popup.elem.style.cursor = 'pointer';
		leoVars.popup.elem.onclick = function() {
			if (document.getElementById('divPopup'))
				document.body.removeChild(document.getElementById('divPopup'));
			return false;
		};
		var theCloseImage = document.createElement('img');
		theCloseImage.src = '/Files/Billeder/LEO_corporate_images/graphics/Close.png';
		theCloseImage.style.width = '44px';
		theCloseImage.style.height = '22px';
		theCloseImage.title = 'Click to close';
		theCloseImage.style.position = 'absolute';
		theCloseImage.style.right = '2px';
		theCloseImage.style.top = '2px';
		theCloseImage.style.zIndex = 110;
		leoVars.popup.elem.appendChild(theCloseImage);
		var theImage = document.createElement('img');
		theImage.src = t.popImage;
		theImage.width = imgWidth;
		theImage.height = imgHeight;
		theImage.style.border = '1px solid #fff';
		leoVars.popup.elem.appendChild(theImage);
		document.body.appendChild(leoVars.popup.elem);
	};
	popImage.src = t.popImage;
}

function leoRollImage(ev) {
	var e = window.event ? window.event : ev;
	var t = e.target ? e.target : e.srcElement;
	if (e.type == 'mouseover')
		t.src = t.overSrc;
	else if (e.type == 'mouseout')
		t.src = t.outSrc;
}

function leoHyperImage(ev) {
	var body = document.getElementsByTagName('body')[0];
	var theHyperDiv = null;
	theHyperDiv = leoGetEl('hyperDiv');
	if (theHyperDiv)
		body.removeChild(theHyperDiv);
	var e = window.event ? window.event : ev;
	var t = e.target ? e.target : e.srcElement;

	if (e.type == 'mouseover') {
		var popImage = new Image();
		popImage.onload = function() {
			var theImage = document.createElement('img');
			theImage.src = t.hyperImage; // Dynamically set, so do NOT use t.getAttribute('hyperImage');
			theDiv = document.createElement('div');
			theDiv.id = 'hyperDiv';
			theDiv.style.position = 'absolute';
			theDiv.style.left = leoFindPosX(t) + t.width + 6 + 'px';
			theDiv.style.top = leoFindPosY(t) + t.height - popImage.height - 1 + 'px';
			theDiv.style.borderWidth = '1px';
			theDiv.style.borderStyle = 'solid';
			theDiv.style.borderColor = '#808080';
			theDiv.appendChild(theImage);
			body.appendChild(theDiv);
		}
		popImage.src = t.hyperImage; // Dynamically set, so do NOT use t.getAttribute('hyperImage');
	} else if (e.type == 'mouseout') {
		theHyperDiv = leoGetEl('hyperDiv');
		if (theHyperDiv)
			body.removeChild(theHyperDiv);
	}
}

function leoZoomImage(ev) {
	var e = window.event ? window.event : ev;
	var t = e.target ? e.target : e.srcElement;
	if (document.getElementById('divPopup'))
		document.body.removeChild(document.getElementById('divPopup'));
	var popImage = new Image();
	popImage.onload = function() {
		var winW = leoWinWidth();
		var winH = leoWinHeight();
		var imgWidth = popImage.width;
		var imgHeight = popImage.height;
		leoVars.popup.rightdimension = imgWidth + 4;
		leoVars.popup.bottomdimension = imgHeight + 4;
		leoVars.popup.tp = leoVars.popup.bt = (leoVars.popup.bottomdimension / 2);
		leoVars.popup.rt = leoVars.popup.lt = (leoVars.popup.rightdimension / 2);
		var theY = (imgHeight > winH) ? 0 : parseInt((winH - imgHeight)/3);
		leoVars.popup.elem = document.createElement('div');
		leoVars.popup.elem.style.position = 'absolute';
		leoVars.popup.elem.style.left = (((winW - 24) / 2) - (imgWidth/2)) + 'px';
		leoVars.popup.elem.style.top = theY + document.documentElement.scrollTop + 'px';
		leoVars.popup.elem.style.width = imgWidth + 12 + 'px';
		leoVars.popup.elem.style.height = imgHeight + 5 + 'px';
		leoVars.popup.elem.id = 'divPopup';
		leoVars.popup.elem.title = 'Click to remove popup';
		leoVars.popup.elem.style.paddingTop = '1px';
		leoVars.popup.elem.style.paddingLeft = '1px';
		leoVars.popup.elem.style.backgroundColor = '#000';
		leoVars.popup.elem.style.zIndex = 100;
		leoVars.popup.elem.style.cursor = 'pointer';
		leoVars.popup.elem.onclick = function() {
			if (leoVars.popup.intervalID)
				clearInterval(leoVars.popup.intervalID);
			leoVars.popup.intervalID = setInterval(leoBoxIn, leoVars.popup.delay);
			return false;
		};
		var theImage = document.createElement('img');
		theImage.src = t.zoomImage;
		theImage.width = imgWidth;
		theImage.height = imgHeight;
		theImage.style.border = '1px solid #fff';
		leoVars.popup.elem.appendChild(theImage);
		leoSetClip(leoVars.popup.tp, leoVars.popup.rt, leoVars.popup.bt, leoVars.popup.lt); // Forbered clip. Skjul leoVars.popup.elem
		leoVars.popup.intervalID = setInterval(leoBoxOut, leoVars.popup.delay);
		document.body.appendChild(leoVars.popup.elem);
	};
	popImage.src = t.zoomImage;
}

function leoSetClip(tp, rt, bt, lt) {
	leoVars.popup.elem.style.clip = 'rect(' + tp + 'px,' + rt + 'px,' + bt + 'px,' + lt + 'px)';
}

function leoBoxIn() {
	if ((leoVars.popup.bt - leoVars.popup.tp) >= (leoVars.popup.rt - leoVars.popup.lt)) {
		leoVars.popup.tp += leoVars.popup.step;
		leoVars.popup.bt -= leoVars.popup.step;
	}
	if ((leoVars.popup.rt - leoVars.popup.lt) >= (leoVars.popup.bt - leoVars.popup.tp)) {
		leoVars.popup.lt += leoVars.popup.step;
		leoVars.popup.rt -= leoVars.popup.step;
	}
	if ((leoVars.popup.rt <= (leoVars.popup.lt - 6)) || (leoVars.popup.tp >= (leoVars.popup.bt + 6))) {
		if (leoVars.popup.intervalID)
			clearInterval(leoVars.popup.intervalID);
		leoVars.popup.tp = leoVars.popup.bt = (leoVars.popup.bottomdimension / 2);
		leoVars.popup.rt = leoVars.popup.lt = (leoVars.popup.rightdimension / 2);
		leoSetClip(leoVars.popup.tp, leoVars.popup.rt, leoVars.popup.bt, leoVars.popup.lt);
		if (typeof document != 'undefined') {
			if (document.getElementById('divPopup'))
				document.body.removeChild(document.getElementById('divPopup'));
		}
	} else
		leoSetClip(leoVars.popup.tp, leoVars.popup.rt, leoVars.popup.bt, leoVars.popup.lt);
}

function leoBoxOut() {
	if ((leoVars.popup.tp + leoVars.popup.step) > leoVars.popup.topdimension)
		leoVars.popup.tp -= leoVars.popup.step;
	else
		leoVars.popup.tp = leoVars.popup.topdimension;
	if ((leoVars.popup.rt + leoVars.popup.step) < leoVars.popup.rightdimension)
		leoVars.popup.rt += leoVars.popup.step;
	else
		leoVars.popup.rt = leoVars.popup.rightdimension;
	if ((leoVars.popup.bt + leoVars.popup.step) < leoVars.popup.bottomdimension)
		leoVars.popup.bt += leoVars.popup.step;
	else
		leoVars.popup.bt = leoVars.popup.bottomdimension;
	if ((leoVars.popup.lt + leoVars.popup.step) > leoVars.popup.leftdimension)
		leoVars.popup.lt -= leoVars.popup.step;
		else
			leoVars.popup.lt = leoVars.popup.leftdimension;
	if ((leoVars.popup.rt >= leoVars.popup.rightdimension) && (leoVars.popup.lt <= leoVars.popup.leftdimension) && (leoVars.popup.tp <= leoVars.popup.topdimension) && (leoVars.popup.bt >= leoVars.popup.bottomdimension)) {
		if (leoVars.popup.intervalID)
			clearInterval(leoVars.popup.intervalID);
		leoSetClip(leoVars.popup.topdimension, leoVars.popup.rightdimension, leoVars.popup.bottomdimension, leoVars.popup.leftdimension);
	} else
		leoSetClip(leoVars.popup.tp, leoVars.popup.rt, leoVars.popup.bt, leoVars.popup.lt);
}

leoAddEvent(window, 'load', leoAddListeners, false);

