// JavaScript Document

////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Global variables
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

var i, j, n, alpha, s, t, a, b;
var obj;



////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	SUPERGLOBAL FUNCTIONS
//////////
//////////	The following functions can be used on any site.
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Window size
//////////
//////////	The following function returns an object that contains
//////////	the width and height of the window.
//////////
//////////	Last modified: 2010/08/20
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getWindowSize() {
	var obj = new Object();
	
	if (window.innerWidth || window.innerHeight) {
		obj.width = window.innerWidth;
		obj.height = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		obj.width = document.documentElement.clientWidth;
		obj.height = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		obj.x = document.body.clientWidth;
		obj.y = document.body.clientHeight;
	} else if (document.body && (document.body.offsetWidth || document.body.offsetHeight)) {
		obj.width = document.body.offsetWidth;
		obj.height = document.body.offsetHeight;
	} else {
		obj.width = 0;
		obj.height = 0;
	}
	
	return obj;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Window scroll
//////////
//////////	The following function returns an object that contains
//////////	the x and y scrolling of the window.
//////////
//////////	Last modified: 2010/08/20
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getWindowScroll() {
	var obj = new Object();
	
	if (typeof(window.scrollX) == "number" && typeof(window.scrollY) == "number") {
		obj.x = window.scrollX;
		obj.y = window.scrollY;
	} else if (typeof(window.pageXOffset) == "number" && typeof(window.pageYOffset) == "number") {
		obj.x = window.pageXOffset;
		obj.y = window.pageYOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		obj.x = document.body.scrollLeft;
		obj.y = document.body.scrollTop;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		obj.x = document.documentElement.scrollLeft;
		obj.y = document.documentElement.scrollTop;
	} else {
		obj.x = 0;
		obj.y = 0;
	}
	
	return obj;
}

function getWindowScrollLimits() {
	var obj = new Object();
	
	if (window.scrollMaxX || window.scrollMaxY) {
		obj.x = window.scrollMaxX;
		obj.y = window.scrollMaxY;
	} else if (document.documentElement && (document.documentElement.scrollWidth || document.documentElement.scrollHeight)) {
		obj.x = document.documentElement.scrollWidth;
		obj.y = document.documentElement.scrollHeight;
	} else if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
		obj.x = document.body.scrollWidth;
		obj.y = document.body.scrollHeight;
	} else {
		obj.x = 0;
		obj.y = 0;
	}
	
	return obj;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Cookies
//////////
//////////	The following functions handle the creation, retrieval and removal of JS cookies
//////////
//////////	Last modified: 2010/07/07
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function setCookie(name, value, expires) {
	if (navigator.appName.indexOf("Microsoft") > -1) {
		document.cookie = name + "=" + value + "; expires=Wed, 1 Jan 2070 00:00:00 GMT; path=/";
	} else {
		document.cookie = name + "=" + value + "; expires=" + expires + "; path=/";
	}
}

function getCookie(name) {
	var value = "";
	
	var cookies = document.cookie.split("; ");
	
	for (var i = 0; i < cookies.length; i++) {
		var cookieData, cookieName, cookieValue;
		
		cookieData = cookies[i].split("=");
		cookieName = cookieData[0];
		cookieValue = cookieData[1];
		if (cookieName == name) {
			value = cookieValue;
		}
	}
	
	return value;
}

function deleteCookie(name) {
	document.cookie = name + "=; expires=Thu, 1 Jan 1970 00:00:00 GMT; path=/";
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Styles
//////////
//////////	The following function allows cross-platform compatible interpretation of
//////////	CSS styles.
//////////
//////////	Last modified: 2010/10/27
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getStyle (element, style) {
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return false;
	}
	if (element === null) {
		return false;
	}
	
	var value = null;
	
	if (style == "height") {
		if (document.body.offsetHeight) {
			value = element.offsetHeight;
		}
	} else if (style == "width") {
		if (document.body.offsetWidth) {
			value = element.offsetWidth;
		}
	}
	
	if (value == null) {
		if (element.style[style]) {
			value = element.style[style];
		} else if (element.currentStyle) {
			value = element.currentStyle[style];
		} else if (window.getComputedStyle) {
			value = document.defaultView.getComputedStyle(element, null).getPropertyValue(style);
		}
	}
	
	return value;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Alpha channel
//////////
//////////	The following functions allow cross-platform compatible opacity filtering of
//////////	HTML elements
//////////
//////////	Last modified: 2011/02/04
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

function getAlpha(element) {
	var alpha = new Number(1);
	
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return null;
	}
	if (element === null) {
		return null;
	}
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		if (element.style.filters == null) {
			alpha = Number(element.style.filter.slice(element.style.filter.indexOf("alpha(opacity=") + String("alpha(opacity=").length, element.style.filter.indexOf(")", String("alpha(opacity=").length))) / 100;
		} else {
			alpha = parseFloat(element.style.filters.alpha.opacity);
		}
	} else {
		alpha = parseFloat(element.style.opacity);
	}
	
	if (isNaN(alpha)) {
		alpha = 1.00;
	} else {
		alpha = Math.round(alpha * 100) / 100
	}
	
	return alpha;
}

function setAlpha(element, alpha) {
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return false;
	}
	if (element === null) {
		return false;
	}
	if (typeof(alpha) != "number") {
		alpha = Number(alpha);
		if (isNaN(alpha)) {
			return false;
		}
	}
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		if (element.style.filters == null) {
			element.style.filter = "alpha(opacity=" + Math.round(alpha * 100) + ")";
		} else {
			element.style.filters.alpha.opacity = alpha;
		}
	} else {
		element.style.opacity = alpha;
	}
	
	return true;
}

function clearAlpha(element) {
	if (typeof(element) == "string") {
		element = document.getElementById(element);
	} else if (typeof(element) != "object") {
		return false;
	}
	if (element === null) {
		return false;
	}
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		if (element.style.filters == null) {
			if (element.style.filter && element.style.removeAttribute) {
				element.style.removeAttribute("filter");
			}
		} else {
			if (element.style.filters.alpha && element.style.filters.removeAttribute) {
				element.style.filters.removeAttribute("alpha");
			}
		}
	} else {
		if (element.style.opacity && element.style.removeProperty) {
			element.style.removeProperty("opacity");
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//////////	Launch popup
//////////
//////////	The following code allows you to launch a popup that fades out
//////////	the rest of the background
//////////
//////////	Last modified: 2010/08/18
//////////
////////////////////////////////////////////////////////////////////////////////////////////////////

// This is crap and needs to be rebuilt

var blackoutDiv = null;
var popupContentHolder = null;
var popupContent = null;
var blackoutFadeInterval = null;

function launch_popup(type, src, width, height) {
	blackoutDiv = document.createElement("div");
	blackoutDiv.style.position = "fixed";
	blackoutDiv.style.zIndex = "998";
	blackoutDiv.style.top = "0px";
	blackoutDiv.style.left = "0px";
	blackoutDiv.style.width = "100%";
	blackoutDiv.style.height = "100%";
	blackoutDiv.style.backgroundColor = "#000000";
	
	popupContentHolder = document.createElement("div");
	popupContentHolder.style.position = "absolute";
	popupContentHolder.style.zIndex = "999";
	popupContentHolder.style.width = width + "px";
	popupContentHolder.style.height = height + "px";
	popupContentHolder.style.backgroundColor = "#FFFFFF";
	
	relocatePopup();
	
	setAlpha(blackoutDiv, 0.05);
	setAlpha(popupContentHolder, 0.05);
	
	if (type == "image") {
		popupContent = document.createElement("img");
		popupContent.src = src;
		popupContent.width = width;
		popupContent.height = height;
	} else if (type == "html") {
		popupContent = document.createElement("div");
		popupContent.innerHTML = src; // BETTER METHOD NEEDED. should remove the other div from the page and add it to the popup element instead of copying its contents.
	}
	
	popupContentHolder.appendChild(popupContent);
	document.body.appendChild(popupContentHolder);
	document.body.appendChild(blackoutDiv);
	
	blackoutFadeInterval = setInterval(fadeInPopup, 20);
	
	window.onresize = relocatePopup;
	window.onscroll = relocatePopup;
}

function relocatePopup() {
	if (popupContentHolder != null) {
		var windowSizeObj = getWindowSize();
		var windowScrollObj = getWindowScroll();
		var windowScrollLimitsObj = getWindowScrollLimits();
		
		var width, height, top, left;
		
		width = parseFloat(getStyle(popupContent, "width"));
		height = parseFloat(getStyle(popupContent, "height"));
		
		if (height > windowSizeObj.height) {
			top = windowScrollObj.y - (windowScrollObj.y / (windowScrollLimitsObj.y - windowSizeObj.height)) * (height - windowSizeObj.height);
		} else {
			top = windowSizeObj.height / 2;
			top -= parseFloat(popupContentHolder.style.height) / 2;
			top += windowScrollObj.y;
		}
		
		if (width > windowSizeObj.width) {
			left = windowScrollObj.x - (windowScrollObj.x / (windowScrollLimitsObj.x - windowSizeObj.width)) * (width - windowSizeObj.width);
		} else {
			left = windowSizeObj.width / 2;
			left -= parseFloat(popupContentHolder.style.width) / 2;
			left += windowScrollObj.x;
		}
		
		popupContentHolder.style.top = top + "px";
		popupContentHolder.style.left = left + "px";
	}
}

function fadeInPopup() {
	setAlpha(blackoutDiv, getAlpha(blackoutDiv) + 0.05);
	setAlpha(popupContentHolder, getAlpha(blackoutDiv) * 1.4285715);
	if (getAlpha(blackoutDiv) >= 0.70) {
		setAlpha(blackoutDiv, 0.70);
		clearAlpha(popupContentHolder);
		blackoutDiv.onclick = closePopup;
		popupContentHolder.onclick = closePopup;
		clearInterval(blackoutFadeInterval);
		blackoutFadeInterval = null;
	}
}

function closePopup() {
	blackoutDiv.onclick = null;
	popupContentHolder.onclick = null;
	blackoutFadeInterval = setInterval(fadeOutPopup, 20);
}

function fadeOutPopup() {
	setAlpha(blackoutDiv, getAlpha(blackoutDiv) - 0.05);
	setAlpha(popupContentHolder, getAlpha(blackoutDiv) * 1.4285715);
	if (getAlpha(blackoutDiv) <= 0.00) {
		clearAlpha(blackoutDiv);
		clearAlpha(popupContentHolder);
		clearPopup();
		clearInterval(blackoutFadeInterval);
		blackoutFadeInterval = null;
	}
}

function clearPopup() {
	document.body.removeChild(popupContentHolder);
	document.body.removeChild(blackoutDiv);
	popupContentHolder = null;
	blackoutDiv = null;
	window.onresize = null;
	window.onscroll = null;
}
