var browserChecked = false;
var browserIsIE6 = false;
var visibleSelects = new Array();
var modalContainerTop = 279;
// fixed on page, means it keeps on screen while scolling
var popupIsFixed = true;

function checkBrowser()
{
	if (!browserChecked)
	{
		if (navigator.appVersion.indexOf("MSIE") > -1)
			try
			{
				var ieVersion = parseFloat(navigator.appVersion.split("MSIE")[1]);
				if ((ieVersion >= 5.5) && (ieVersion < 7) && (document.body.filters)) 
					browserIsIE6 = true;
			}
			catch (ex) { }
		browserChecked = true;
	}
}

function togglePopup(id)
{
	checkBrowser()
	
	// due to IE6 problems, this must be rendered in a timeout function
	if (browserIsIE6)
		window.setTimeout("togglePopupIndeed('" + id + "')", 100);
	else
		togglePopupIndeed(id)
}

function initOverlay(id)
{
	if (!window.onresize)
		window.onresize = function() { positionModalbox(id); }
		
	var overlay = document.getElementById(id);
	
	var popup = document.getElementById(id + "Container");

	checkBrowser();
	fixOverlay(overlay);
	positionModalbox(id);
	if (popup)
	{
		if (browserIsIE6)
			hideSelects();
		positionContainerbox(id + "Container");
	}
	
	// check popupheight
	if (popup.offsetHeight > 600)
	{
		// Overlay scrolls with the page
				
		popup.style.position = "absolute";
		popupIsFixed = false;
		// repositioning
		positionContainerbox(id + "Container");
	}
	else
	{
		// Overlay keeps on screen while scrolling
				
		if (!browserIsIE6)
		{
			popup.style.position = "fixed";
		}
		popupIsFixed = true;
		// repositioning
		positionContainerbox(id + "Container");
	}
}

function togglePopupIndeed(id)
{
	if (!window.onresize)
		window.onresize = function() { positionModalbox(id); }

	var overlay = document.getElementById(id);

	// ie6 PNG transparency fix
	fixOverlay(overlay);

	if (overlay)
	{
		if (overlay.style.display == "none" || overlay.style.display == "")
		{
			positionModalbox(id);
			overlay.style.display = "block";
		}
		else
		{
			overlay.style.display = "none";
		}
	}

	popup = document.getElementById(id + "Container");
	if (popup)
	{
		var selects;
		if (popup.style.display == "none" || popup.style.display == "")
		{
			if (browserIsIE6)
				hideSelects();

			positionContainerbox(id + "Container");

			popup.style.display = "block";

			// check popupheight
			if (popup.offsetHeight > 600)
			{
				// Overlay scrolls with the page
				
				popup.style.position = "absolute";
				popupIsFixed = false;
				// repositioning
				positionContainerbox(id + "Container");
			}
			else
			{
				// Overlay keeps on screen while scrolling
				
				if (!browserIsIE6)
				{
					popup.style.position = "fixed";
				}
				popupIsFixed = true;
				// repositioning
				positionContainerbox(id + "Container");
			}
			
		}
		else
		{
			popup.style.display = "none";

			if (browserIsIE6)
				showSelects();
		}
	}
}

function togglePopupContent(id, content)
{
	checkBrowser();

	// due to IE6 problems, this must be rendered in a timeout function
	if (browserIsIE6)
		window.setTimeout("togglePopupContentIndeed('" + id + "','" + content + "')", 200);
	else
		togglePopupContentIndeed(id, content);
}

function togglePopupContentIndeed(id, content)
{
	popupInner = document.getElementById(id);
	if (popupInner)
		popupInner.innerHTML = content;
}

function hideSelects()
{
	var selects = document.getElementsByTagName("select");
	for (var i = 0; i <= selects.length-1; i++)
	{
		var parent = selects.parent
		var hide = true;
		while (parent != null)
		{
			if (parent.className == "modalContainer")
			{
				hide = false;
				parent = null;
			}
			else
				parent = parent.parent;
		}
		if (hide)
		{
			selects[i].style.visibility = 'hidden';
			visibleSelects.push(selects[i]);
		}
	}
}

function showSelects()
{
	var select;
	for (var i=0; i<= visibleSelects.length; i++)
	{
		visibleSelects[i].style.visibility = 'visible';
	}
	visibleSelects = new Array();
}

function positionModalbox(id)
{
	// Get viewport and page dimensions (pageWidth, pageHeight, windowWidth, windowHeight) 
	var dimensions = getPageSize();
	var modalOverlayHeight = dimensions[1];
	var modalOverlayWidth = dimensions[0];

	document.getElementById(id).style.height = (modalOverlayHeight + 50) + "px";
	document.getElementById(id).style.width = (modalOverlayWidth) + "px";
}

function positionContainerbox(id)
{
	if (modalContainerTop < 280)
	{
		if ((document.getElementById(id)) && (document.getElementById(id).currentStyle))
			modalContainerTop = document.getElementById(id).currentStyle.top.replace(/px/i, "");
		else
			modalContainerTop = 279;
	}

	if ((browserIsIE6) || (!popupIsFixed))
	{
		// Overlay scrolls with the page. Top must be set initially to the current scroll position
		document.getElementById(id).style.top = ((modalContainerTop * 1) + getPageScroll()[1]) + "px";
	}
	else
	{
		// Overlay keeps on screen. Due to style "position:fixed" no scoll position of the page is needed.
		document.getElementById(id).style.top = modalContainerTop + "px";
	}
}

function fixOverlay(root)
{
	if (root && browserIsIE6)
	{
		root.style.position = "absolute";
		//root.style.top = getPageScroll()[1] + "px";
		
		if (root.currentStyle.backgroundImage.match(/\.png/i) !== null) 
			root.style.backgroundImage = 'url(/Images/pixel.gif)';
	
		var container = document.getElementById(root.id + "Container");
		if (container)
		{
			container.style.position = "absolute";
			popupIsFixed = false;
			for (var i = container.all.length - 1, obj = null; (obj = container.all[i]); i--)
				if (obj.currentStyle.backgroundImage.match(/\.png/i) !== null) 
					fixPNGStyle(obj);
					//obj.style.backgroundImage = obj.currentStyle.backgroundImage.replace(/\.png/i, ".gif");
		}
	}
}

function fixPNGStyle(tag)
{
	var mode = 'scale';
	var bg	= tag.currentStyle.backgroundImage;
	var src = bg.substring(5,bg.length-2);
	if (tag.currentStyle.backgroundRepeat == 'no-repeat') 
		mode = 'crop';
	tag.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
	//tag.style.background = "none";
	tag.style.backgroundImage = 'url(/Images/pixel.gif)';
}

function PopupReposition(elementId)
{
	// Get current scroll position (x, y)
	var scrollPositions = getPageScroll();
	
	// Calculate values
	var videoTop = (getVisibleHeight() / 2) - (getElementHeight(elementId) / 2);
	var videoLeft = (getVisibleWidth() / 2) - (getElementWidth(elementId) / 2);

	if (videoTop < 0)
		videoTop = 0;

	// Set values
	document.getElementById(elementId).style.top = videoTop + "px";
	document.getElementById(elementId).style.left = videoLeft + "px";
}

function getElementHeight(elementId) 
{
	var strHeight = document.getElementById(elementId).style.height;
	var height = strHeight.split("px");
	
	if (height[0] == "")
		height[0] = 0;

	if (height[0] <= 0)
		return 491;
	return height[0];
}

function getElementWidth(elementId) 
{
	var strWidth = document.getElementById(elementId).style.width;
	var width = strWidth.split("px");
	
	if (width[0] == "")
		width[0] = 0;
		
	if (width[0] <= 0)
		return 794;
	return width[0];
}

// Get width of visible browser area
function getVisibleWidth()
{
	var clientWidth;

	if (window.innerWidth)
	{
		clientWidth = (window.__safari ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
	}
	else
	{
		if (document.documentElement.clientWidth == 0)
		{
			clientWidth = document.body.clientWidth;
		}
		else
		{
			clientWidth = document.documentElement.clientWidth;
		}
	}
	return clientWidth;
}


// Get height of visible browser area
function getVisibleHeight()
{
	var clientHeight;

	if (window.innerHeight)
	{
		clientHeight = (window.__safari ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
	}
	else
	{
		if (document.documentElement.clientHeight == 0)
		{
			clientHeight = document.body.clientHeight;
		}
		else
		{
			clientHeight = document.documentElement.clientHeight;
		}
	}
	return clientHeight;	
}

// Returns array with x,y page scroll values.
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// Returns array with page width, height and window width, height
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
