﻿// mcm.at standard scripts

var bolDOM = (document.getElementById ? true : false); 
var bolIE4 = ((document.all && !bolDOM) ? true : false);
var bolIE5 = ((document.all && bolDOM) ? true : false);
var bolNS4 = (document.layers ? true : false);
var bolOpera = (navigator.userAgent.toLowerCase().indexOf("opera") > -1) ? true : false;
var bolSafari = (navigator.userAgent.toLowerCase().indexOf("safari") > -1) ? true : false;
var bolIE = ((document.all && !bolOpera) ? true : false);

function addLoadListener(fn) {
	if (typeof window.addEventListener != "undefined") {
		window.addEventListener("load", fn, false);
	} else if (typeof document.addEventListener != "undefined") {
		/* for OPERA */
		document.addEventListener("load", fn, false);
	} else if (typeof window.attachEvent != "undefined") {
		/* for MSIE */
		window.attachEvent("onload", fn);
	} else {
		var fnOld = window.onload;
		if (typeof window.onload != "function") {
			window.onload = fn;
		} else {
			window.onload = function() {
				fnOld();
				fn();
			};
		}
	}
}

function attachEventListener(target, eventType, functionRef, capture) {
	if (typeof target.addEventListener != "undefined") {
		target.addEventListener(eventType, functionRef, capture);
	} else if (typeof target.attachEvent != "undefined") {
		target.attachEvent("on"+eventType, functionRef);
	} else {
		eventType = "on"+eventType;
		if (typeof target[eventType] == "function") {
			var oldListener = target[eventType];
			target[eventType] = function() {
				oldListener();
				return functionRef();
			};
		} else {
			target[eventType] = functionRef;
		}
	}
}

function getScrollingPosition() {
	var position = [0, 0];
	if (typeof window.pageYOffset != 'undefined') {
		position = [window.pageXOffset, window.pageYOffset];
	} else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0) {
		position = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
	} else if (typeof document.body.scrollTop != 'undefined') {
		position = [document.body.scrollLeft, document.body.scrollTop];
	}
	return position;
}

function getCursorPosition(eventObj) {
	if (typeof eventObj == "undefined") {
		eventObj = window.event;
	}
	var scrollingPosition = getScrollingPosition();
	var cursorPosition = [0, 0];
	if (typeof eventObj.pageX != "undefined" && typeof eventObj.x != "undefined") {
		cursorPosition[0] = eventObj.pageX;
		cursorPosition[1] = eventObj.pageY;
	} else {
		cursorPosition[0] = eventObj.clientX + scrollingPosition[0];
		cursorPosition[1] = eventObj.clientY + scrollingPosition[1];
	}
	return cursorPosition;
}

function getViewportSize() {
	var size = [0, 0];
	if (typeof window.innerWidth != 'undefined') {
		size = [window.innerWidth, window.innerHeight];
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
	} else {
		size = [document.getElementsByTagName('body')[0].clientWidth, document.getElementsByTagName('body')[0].clientHeight];
	}
	return size;
}

function getEventTarget(event) {
	/* returns the element from which the event originated */
	var targetElement = null;
	if (typeof event.target != "undefined") {
		targetElement = event.target;
	} else {
		targetElement = event.srcElement;
	}
	while (targetElement.nodeType == 3 && targetElement.parentNode != null) {
		targetElement = targetElement.parentNode;
	}
	return targetElement;
}

/*
To get all a elements in the document with a info-links class.
    getElementsByClassName(document, "a", "info-links");
To get all div elements within the element named container, with a col class.
    getElementsByClassName(document.getElementById("container"), "div", "col"); 
To get all elements within in the document with a click-me class.
    getElementsByClassName(document, "*", "click-me"); 
*/

function getElementsByClassName(oElm, strTagName, strClassName){
	/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
	*/
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function toggleDisplay(strElm, strType) {
	switch (strType) {
		case 'display':
			document.getElementById(strElm).style.display = (document.getElementById(strElm).style.display == 'none') ? 'block' : 'none';
			break;
		case 'visibility':
			document.getElementById(strElm).style.visibility = (document.getElementById(strElm).style.visibility == 'hidden') ? 'visible' : 'hidden';
			break;
		default:
			break;
	}		
}

function initWincodeInput() {
	var elContainer = document.getElementById("wincode_input");
	var iptField;
	if(elContainer) {	
		for (var i=0; i < elContainer.childNodes.length; i++) {
			if (elContainer.childNodes[i].nodeName.toLowerCase() == "input") {
				iptField = elContainer.childNodes[i];
			}
		}
		if (!iptField) { return; }
		iptField.value = "";
		iptField.onfocus = function() {
			iptField.style.backgroundImage = "none";	
		};
	}
}

function openInBlank(url) {
    window.open(url,"window","");
}

function checkPlugin(strPluginName) {
	// check for IE
	if (!navigator.plugins || navigator.plugins.length == 0) return null;
	// check other browsers
	for (var i=0; i<navigator.plugins.length; i++) {
		if (navigator.plugins[i].name.toLowerCase().indexOf(strPluginName.toLowerCase()) > -1) {
			return true;
		}
	}
	return false;
}

function openCentered(strUrl, strName, strParams) {
	// opens popup window centered on screen
	var arrParams = strParams.split(",");
	var strCenteredParams = "";
	var intLeft, intTop, intWidth, intHeight;
	
	for (var i=0; i<arrParams.length; i++) {
		if (arrParams[i].indexOf("width") > -1) {
			intWidth = arrParams[i].substr(6);
			intLeft = parseInt((screen.availWidth-intWidth)/2);
		} else if (arrParams[i].indexOf("height") > -1) {
			intHeight = arrParams[i].substr(7);
			intTop = parseInt((screen.availHeight-intHeight)/2);
		} else if (arrParams[i].indexOf("left") > -1) {
		} else if (arrParams[i].indexOf("top") > -1) {			
		} else {
			strCenteredParams += arrParams[i] + ",";
		}
	}
	strCenteredParams += "width="+intWidth+",height="+intHeight+",left="+intLeft+",top="+intTop;
	var popup = window.open(strUrl, strName, strCenteredParams);
	popup.focus();
}

function openInOpener(strUrl) {
	// opens url in opener window and closes the popup
	window.opener.location.href = strUrl;
	window.opener.focus();
	window.close();
}

function getUrlFromFlash(url, target) {	
	var objFrame;
	if (url.indexOf("http://") == -1) {
		var urlFull = "http://" + window.location.hostname + url;
	} else {
		urlFull = url;
	}
	
	if (!url) return;
	if (!target || target == "") target = "_self";
	
	if (url.indexOf("popup:") > -1) {
		// open popup from flash and track target url
		var strCommand = url.substr(6);
		var arrParameters = strCommand.split(";");
		openCentered(arrParameters[0], arrParameters[1], arrParameters[2]);
		trackPage(arrParameters[0]);
		return;
	}
	
	var elBody = document.getElementsByTagName("body")[0];
	var newForm = document.createElement("form");
	newForm.id = "formFlashLink";
	newForm.method = "get";
	newForm.action = urlFull;
	newForm.target = target;
	// create hidden fields for query-string parameters
	var intQueryPos = url.indexOf("?");
	if (intQueryPos > -1) {
		var arrQuery = url.substr(intQueryPos+1).split("&");
		for (var i in arrQuery) {
			var newHiddenField = document.createElement("input");
			newHiddenField.name = arrQuery[i].split("=")[0];
			newHiddenField.value = arrQuery[i].split("=")[1];
			newForm.appendChild(newHiddenField);
		}
	}
	elBody.appendChild(newForm);
	
	var f = document.getElementById("formFlashLink");
	f.submit();
}

/* TRACKING
------------------------------------------------ */
function trackPage(path) {
	var strPath = (!path) ? window.location.pathname : path;
	// check if page is opened from newsletter
	if (/\/newsletter\/[0-9]{8}\//.test(strPath)) {
		// track the newsletter url for newsletter stats
		var strNlPath = strPath.match(/\/newsletter\/[0-9]{8}\//, "") + strPath.replace(/\/newsletter\/[0-9]{8}\//, "").replace(/\//g, "_");
		pageTracker._trackPageview(strNlPath);
		// track the real url for overall site stats
		var strContentPath = strPath.replace(/\/newsletter\/[0-9]{8}/, "");
		pageTracker._trackPageview(strContentPath);
	} else {
		pageTracker._trackPageview();	
	}
}

function trackExternalLink(url) {
	// Google Analytics
	var urchinUrl = url.replace(/http:\/\//, "");
	pageTracker._trackPageview("/outgoing/" + urchinUrl); // urchinUrl.replace(/\./g, "_"));	
	//
	/*
	obj = document.getElementById("trackImg");
	if(obj) {
		obj.src = "/tracking/index.aspx?e="+encodeURIComponent(url)+"&em="+encodeURIComponent("http://"+window.location.hostname+window.location.pathname)+"&rnd=" + Math.random();
	}
	*/
}
/* --------------------------------------------- */

// lucene search
function initSearch() {
	var transfer = this;
    var iptSearch = document.getElementById('searchtext');	
	attachEventListener(iptSearch, "keydown", function(e) {
		if (e.keyCode == 13) {
			startSearch();
		}
	});
}

function startSearch()
{
    var strQuery = document.getElementById('searchtext').value;
    // alert('QueryString ' + strQuery);
    if (strQuery.length <= 2) {
        alert('Bitte geben Sie mindesten 3 Zeichen als Suchbegriff ein!\n');
    } else {
        window.location = 'suche.aspx?seite=1&searchtext=' + strQuery;
    }
}

//addLoadListener(initSearch);

