﻿addLoadEvent(checkForPopupInTimers);
addLoadEvent(checkForPopupInTemplate);

function popupIn(obj, objWidth, objHeight)
{
//	Identify object
	globalObjForPopUpIn = obj;
	var link = obj.getAttribute('href');
	
	
	var ifmWidth		= obj.getAttribute('width');
	var ifrmHeight		= obj.getAttribute('height');
	
	if(ifmWidth==null)		ifmWidth	=objWidth;
	if(ifrmHeight==null)	ifrmHeight	=objHeight;
	
	ifmWidth = parseInt(ifmWidth) + 0;
	ifrmHeight = parseInt(ifrmHeight) + 0;

//	Define Type of popup
	var typeOfPopup;
	if(!link)link="";
	if(link.indexOf('#')!=-1)typeOfPopup='div';
	else typeOfPopup ='iframe';
	
//	Create iframe for popupIn
	var popupInObject;
	var popupInContainer		= document.createElement('div');
	var popupInCover			= document.createElement('div');
	
	if(typeOfPopup=="iframe")	popupInObject = document.createElement('iframe');
	if(typeOfPopup=="div")
	{
//		Find element
		var curObjId = link.split('#')[1];
		popupInObject = document.getElementById(curObjId);
		if(!popupInObject)
			{popupInObject = document.createElement('iframe');typeOfPopup ='iframe'}
		else
			{popupInObject.style.display="block"}
	}
//	Define elements	
	popupInContainer.className	= "popupInContainer";
	popupInObject.className		= "popupInIframe";
	popupInCover.className		= "popupInCover";
	
	popupInCover.title			= "Klik her for at komme tilbage";
	
	popupInObject.src			= link;
	popupInObject.scrolling		= "no";
	popupInObject.frameBorder	= "no";
	popupInObject.style.width	= parseInt(ifmWidth) + "px";
	popupInObject.style.height	= parseInt(ifrmHeight) + "px";
	popupInObject.style.left	= "-" + parseInt(ifmWidth) / 2 + "px";
	
	popupInCover.style.height	= document.body.offsetHeight + "px";
	popupInCover.style.width	= document.body.offsetWidth + "px";
	
//	Check if element got any extra styling
	var borderSize				= obj.getAttribute('borderSize');
	var borderColor				= obj.getAttribute('borderColor');
	var bgColor					= obj.getAttribute('bgColor');
	var bgOpacity				= obj.getAttribute('bgOpacity');
	var boxPosition				= obj.getAttribute('boxPosition');
	var coverText				= obj.getAttribute('coverText');
	var closeText				= obj.getAttribute('closeText');
	var closeColor				= obj.getAttribute('closeColor');
	var scrollBar				= obj.getAttribute('scroll');
	var hide					= obj.getAttribute('hide');
	var coverClick				= obj.getAttribute('coverClick');
	
	if(borderSize==null)borderSize = 0;

//	Set extra styling
	if(borderSize!=null)		popupInObject.style.border			= borderSize + "px solid white"; popupInObject.style.left= "-" + parseInt(ifmWidth + (borderSize * 2)) / 2 + "px";
	if(borderColor!=null)		popupInObject.style.borderColor		= borderColor;
	if(bgColor!=null)			popupInCover.style.background		= bgColor;
	if(bgOpacity!=null)			popupInCover.setAttribute('style',	popupInCover.getAttribute('style') 
																	+ "filter:alpha(opacity=" + bgOpacity + ");-moz-opacity:." + bgOpacity + ";opacity:." + bgOpacity + ";");
	if(boxPosition!=null)		{/*Box position will be set under [Define location]*/}
	if(coverText!=null)			popupInCover.title = coverText;
	if(scrollBar!=null)			popupInObject.style.overflow = scrollBar;
	if(hide!=null)				popupInHideTags(hide, true)
	if(coverClick==null)		coverClick="true";
	if(coverClick=="false")		popupInCover.style.cursor="default";

//	Define location
	var locWinY = top.document.documentElement.scrollTop;
	var dimWinY;
	var dimDocY = top.document.body.offsetHeight;
	if		(top.window.innerHeight){dimWinY = top.window.innerHeight}
	else	{dimWinY = top.document.documentElement.clientHeight}
	var locIframeY; 
	
//	Add border to iframe height if any
	if(borderSize!=null)
		ifrmHeight += (borderSize * 2);
	
//	Define location point
	if(boxPosition=='middle'|boxPosition==null)
		locIframeY = (dimWinY / 2) + locWinY - (ifrmHeight / 2);
	
	if(boxPosition=='top')
		locIframeY = locWinY;
	
	if(boxPosition=='bottom')
		locIframeY = (dimWinY) + locWinY - (ifrmHeight);
	
//	Set location to current object
	popupInObject.style.top = locIframeY.toString() + 'px';

//	Add "close button/link"
	var closeCaption		= 'Klik her for at lukke';
	var closeLink			= document.createElement('a');
	
	if(closeText != null) closeCaption = closeText;
	if(closeColor != null) closeLink.style.color = closeColor;
	
	closeLink.innerHTML		= closeCaption;
	closeLink.className		= "popupInCloseLink";
	
	if(boxPosition != 'bottom') closeLink.style.top = ifrmHeight + locIframeY + 'px';
	else closeLink.style.top	= locIframeY - 22 + 'px';
	
	closeLink.style.width		= (ifmWidth) + parseInt(borderSize) + 'px';
	closeLink.style.marginLeft	= -(ifmWidth / 2) + "px";

//	Inserting elements in document
	document.body.appendChild(popupInCover);
	document.body.appendChild(popupInContainer);
	popupInContainer.appendChild(popupInObject);
	popupInContainer.appendChild(closeLink);

//	removes the top banner
	var topBanner = document.getElementById('headerSpace');
	topBanner.style.visibility = "hidden";
	
//	remove scrollbar on body
	document.getElementsByTagName('html')[0].style.overflow="hidden";
	top.document.documentElement.scrollTop = locWinY;
	popupInCover.style.width = document.body.offsetWidth + "px";
	
//	Set exit function on CoverDiv
	if(typeOfPopup=="iframe")
	{
		if(coverClick=="true"){popupInCover.onclick = function(){topBanner.style.visibility = "visible";document.getElementsByTagName('html')[0].style.overflow="auto";if(hide!=null)popupInHideTags(hide, false);popupInCover.parentNode.removeChild(popupInCover);popupInContainer.parentNode.removeChild(popupInContainer);}}
		closeLink.onclick = function(){topBanner.style.visibility = "visible";document.getElementsByTagName('html')[0].style.overflow="auto";if(hide!=null)popupInHideTags(hide, false);popupInCover.parentNode.removeChild(popupInCover);popupInContainer.parentNode.removeChild(popupInContainer);}
		
	}
	else if(typeOfPopup=="div")
	{
		if(coverClick=="true"){topBanner.style.visibility = "visible";popupInCover.onclick = function(){document.getElementsByTagName('html')[0].style.overflow="auto";if(hide!=null)popupInHideTags(hide, false);popupInCover.parentNode.removeChild(popupInCover);document.body.appendChild(popupInObject);popupInContainer.parentNode.removeChild(popupInContainer);popupInObject.style.display="none"}}
		closeLink.onclick = function(){topBanner.style.visibility = "visible";document.getElementsByTagName('html')[0].style.overflow="auto";if(hide!=null)popupInHideTags(hide, false);popupInCover.parentNode.removeChild(popupInCover);document.body.appendChild(popupInObject);popupInContainer.parentNode.removeChild(popupInContainer);popupInObject.style.display="none"}
		
	}
}

function popupInHideTags(className,hide)
{
	var allTags = document.getElementsByTagName('*');
	var tagsToChange = new Array();
	for(var i=0;i<allTags.length;i++)
	{
		var curTag = allTags[i];
		if(curTag.className.indexOf(className)!=-1)
			tagsToChange[tagsToChange.length] = curTag;
	}
	for(var i=0;i<tagsToChange.length;i++)
	{
		var curTag = tagsToChange[i];
		if(hide)
			curTag.style.visibility = "hidden";
		else
			curTag.style.visibility = "visible";
	}
}


function checkForPopupInTemplate()
{
//	Identify template tag
	var templateId = 'popupInTemplate';
	var template = document.getElementById(templateId);
	if(!template)return false;
	
//	Check all tags for popups
	var allTags = document.getElementsByTagName('*');
	var tagsToUse = new Array();
	
	for(var i=0;i<allTags.length;i++)
	{
		var curTag = allTags[i];
		var onClickCmd = curTag.getAttribute('onclick');
		if(onClickCmd)
		{
			var cmdTxt = onClickCmd + "...";
			if(cmdTxt.indexOf('popupIn')!=-1)
				tagsToUse[tagsToUse.length] = curTag;
		}
	}
	
//	Get values
	var borderSize		= template.getAttribute('borderSize');
	var borderColor		= template.getAttribute('borderColor');
	var bgColor			= template.getAttribute('bgColor');
	var bgOpacity		= template.getAttribute('bgOpacity');
	var boxPosition		= template.getAttribute('boxPosition');
	var coverText		= template.getAttribute('coverText');
	var closeText		= template.getAttribute('closeText');
	var closeColor		= template.getAttribute('closeColor');
	var scrollBar		= template.getAttribute('scroll');
	var hide			= template.getAttribute('hide');
	
//	Adding value to every popup tag
	for(var i=0;i<tagsToUse.length;i++)
	{
		var curTag = tagsToUse[i];
		if(borderSize!=null		& curTag.getAttribute('borderSize')==null)		curTag.setAttribute('borderSize',borderSize);
		if(borderColor!=null	& curTag.getAttribute('borderColor')==null)		curTag.setAttribute('borderColor',borderColor);
		if(bgColor!=null		& curTag.getAttribute('bgColor')==null)			curTag.setAttribute('bgColor',bgColor);
		if(bgOpacity!=null		& curTag.getAttribute('bgOpacity')==null)		curTag.setAttribute('bgOpacity',bgOpacity);
		if(boxPosition!=null	& curTag.getAttribute('boxPosition')==null)		curTag.setAttribute('boxPosition',boxPosition);
		if(coverText!=null		& curTag.getAttribute('coverText')==null)		curTag.setAttribute('coverText',coverText);
		if(closeText!=null		& curTag.getAttribute('closeText')==null)		curTag.setAttribute('closeText',closeText);
		if(closeColor!=null		& curTag.getAttribute('closeColor')==null)		curTag.setAttribute('closeColor',closeColor);
		if(scrollBar!=null		& curTag.getAttribute('scroll')==null)			curTag.setAttribute('scroll',scrollBar);
		if(hide!=null			& curTag.getAttribute('hide')==null)			curTag.setAttribute('hide',hide);
	}
}



function checkForPopupInTimers()
{


//	Identify timer
	var timerId = 'timerPopupIn';
	var timer = document.getElementById(timerId);
	
	if(!timer)return false;
	var minutes = timer.getAttribute('min');
	var seconds = timer.getAttribute('sec');
	var popupQName = "popupin";
	var timerQuery = timer.getAttribute(popupQName);
	var pageQuery = document.location + "";
	var total = 0;
	
	var gameHeight = 300;
	var gameWidth = 500;
	
	var queryValue;
	var qStart;
	var qEnd;

	
	
	puplicPopUpInTimerObj = timer;
	
	if(timerQuery)
	{
		queryValue = getQuerystring('popupin', '');

		if(queryValue == "")return false;
		puplicPopUpInTimerObj.setAttribute("href",queryValue);

		
	}

//	Set timers for 0 if nothing is set
	if(minutes==null)minutes=0;
	if(seconds==null)seconds=0;
	
	total = (minutes * 60) * 1000;
	total += (seconds * 1000);
	

	if(puplicPopUpInTimerObj.href == "") return false;
	setTimeout('popupIn(puplicPopUpInTimerObj)',total);
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if(typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
} 











	
















