﻿// 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 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
	top.opener.location.href = strUrl;
	top.opener.focus();
	window.close();
}

//
function applyHoverClass(elHover) {
	// workaround for missing :hover pseudoclass support in MSIE 6.0 and lower
	if (bolIE && elHover) {
		elHover.onmouseover=function() { this.className+=" hover"; }
		elHover.onmouseout=function() { this.className=this.className.replace(" hover", ""); }
	}
}

// ONLOAD
function initSite() {
	var offsetX = Math.round((1280 - getViewportSize()[0]) / 2);
	var offsetY = getScrollingPosition()[1];
	if (offsetX > 0) {
		window.scrollTo(offsetX, offsetY);
	}
	// apply hover workaround if needed
	arrHoverElements = getElementsByClassName(document.getElementById("primary"), "*", "expand-on-hover");
	for (var i=0; i<arrHoverElements.length; i++) {
		applyHoverClass(arrHoverElements[i]);
	}
}

addLoadListener(initSite);

/*
$(document).ready(function() {
	$("a.prize_1").click(function() {
		$(this).dialog({ buttons: { "X": function() { $(this).dialog("close"); } } });
	});
});
*/

function openPrizeDetails(n) {
	$("#prize_"+n+"_details").dialog({ width:345, height:550, modal:true });
}