// ### Function to show a popup to load the video within.
function showPopup(s)
{
	// ### Check if a file is supplied.
	if (s != '')
	{
		// ### Popup width and height.
		var iPopWidth = parseInt(400);
		var iPopHeight = parseInt(600);
		
		// ### Open the popup.
		window.open("popup.php?img=" + s,"WindowBeeldWeb","width="+ iPopWidth +",height="+ iPopHeight +",scrollbars=no,toolbar=no,resizable=yes," + align(iPopWidth, iPopHeight)).focus();
	}
}

// ### Function to align the popup to the middle of screen.
function align(iPopWidth, iPopHeight)
{
	// ### The screen width and height.
	var breedte = screen.width;
	var hoogte 	= screen.height;
	
	// ### Calculate the center of the screen.
	var prop_left 	= (screen.width/2) - (iPopWidth/2);
	var prop_top	= (screen.height/2) - (iPopHeight/2);
	
	// ### Return the align values.
	return "left=" + prop_left + ",top=10";
}

/// #region Debugger functions

// Show the debug window
function showDebug()
{
  window.top.debugWindow =
  window.open("",
	  "Debug",
	  "left=0,top=0,width=300,height=700,scrollbars=yes,status=yes,resizable=yes");
  window.top.debugWindow.opener = self;
  // open the document for writing
  window.top.debugWindow.document.open();
  window.top.debugWindow.document.write("<html><head><title>Debug Window</title></head><body><pre>\n");
}

// If the debug window exists, then write to it
function debug(text)
{
	if (window.top.debugWindow && !window.top.debugWindow.closed)
	{
		window.top.debugWindow.document.write(text+"\n");
	}
}

// If the debug window exists, then close it
function hideDebug()
{
	if (window.top.debugWindow && ! window.top.debugWindow.closed)
	{
		window.top.debugWindow.close();
		window.top.debugWindow = null;
	}
}

/// #endregion

/// #region Global functions to support all browsers

function getElementById(id)
{
	if (document.getElementById)
	{
		return document.getElementById(id);
	}
	if (document.all)
	{
		return document.all[id];
	}
	if (!oDoc)
	{
		oDoc = document;
	}
	if( document.layers )
	{
		if( oDoc.layers[id] )
		{
			return oDoc.layers[id];
		}
		else
		{
			//repeatedly run through all child layers
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ )
			{
				//on success, return that layer, else return nothing
				y = getElementById(id,oDoc.layers[x].document);
			}
			return y;
		}
	}
	return false;
}

function getElementLeft(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function getElementTop(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getElementHeight(elem)
{
	return elem.offsetHeight;
}

function getElementWidth(elem)
{
	return elem.offsetWidth;
}

function getScrollLeft()
{
	if( typeof( window.pageYOffset ) == 'number' )
	{
		//Netscape compliant
		return window.pageXOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
	{
		//DOM compliant
		return document.body.scrollLeft;
	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
	{
		//IE6 standards compliant mode
		return document.documentElement.scrollLeft;
	}
	return 0;
}

function getScrollTop()
{
	if( typeof( window.pageYOffset ) == 'number' )
	{
		//Netscape compliant
		return window.pageYOffset;
	}
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
	{
		//DOM compliant
		return document.body.scrollTop;
	}
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
	{
		//IE6 standards compliant mode
		return document.documentElement.scrollTop;
	}
	return 0;
}

function getWindowInnerWidth() {
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		return window.innerWidth;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		//IE 6+ in 'standards compliant mode'
		return document.documentElement.clientWidth;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//IE 4 compatible
		return document.body.clientWidth;
	}
	return 0;
}

function getWindowInnerHeight()
{
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		return window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		//IE 6+ in 'standards compliant mode'
		return document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//IE 4 compatible
		return document.body.clientHeight;
	}
	return 0;
}

//The mouse location property pair returns the mouse coordinates relative to the document.
function getMouseLeft(e)
{
	// W3C
	if(e.pageX || e.pageY)
	{
		return e.pageX;
	}
	// IE
	else if (e.clientX || e.clientY)
	{
		return e.clientX + getScrollLeft();
	}
	return 0;
}

function getMouseTop(e)
{
	// W3C
	if(e.pageX || e.pageY)
	{
		return e.pageY;
	}
	// IE
	else if (e.clientX || e.clientY)
	{
		return e.clientY + getScrollTop();
	}
	return 0;
}

function fitWindowToElement(elem)
{
	var iWidth = getElementWidth(elem) - getWindowInnerWidth();
	var iHeight = getElementHeight(elem) - getWindowInnerHeight();
	
	window.resizeBy(iWidth, iHeight);
}

function attachEventListener(element, eventname, attachmethod)
{
	if (typeof(attachmethod) == 'string')
		eval('attachmethod = ' + attachmethod);
	if (element.attachEvent)
	{
		var result;
		result = element.attachEvent('on' + eventname, attachmethod);
		return result;
	}
	else if (element.addEventListener)
	{
		element.addEventListener(eventname, attachmethod, false);
		return true;
	}
	else
	{
		eval("element.on" + eventname + " = attachmethod;");
		return true;
	}
}

function detachEventListener(element, eventname, attachmethod)
{
	if (element.removeEventListener)
	{
		eval("element.removeEventListener('" + eventname + "', " + attachmethod + ", false);");
		return true;
	}
	else if (element.detachEvent)
	{
		var result;
		eval("result = element.detachEvent('on" + eventname + "', " + attachmethod + ");");
		return result;
	}
}

function getEvent(e)
{
	if (!e)
		var e = window.event;
	return e;
}

function getEventSender(e)
{
	if (e.target)
		return e.target;
	else if (e.srcElement)
		return e.srcElement
	return null;
}

/// #endregion

/// #region Date methods
function cloneDate(date)
{
	var cloned = new Date();
	cloned.setDate(date.getDate());
	cloned.setMonth(date.getMonth());
	cloned.setYear(date.getYear());
	return cloned;
}

function getWeek(date){
	//lets calc weeknumber the cruel and hard way :D
	//Find JulianDay 
	year = date.getYear();
	month = date.getMonth() + 1; //use 1-12
	day = date.getDate();
	var a = Math.floor((14-(month))/12);
	var y = year+4800-a;
	var m = (month)+(12*a)-3;
	var jd = day + Math.floor(((153*m)+2)/5) + 
		(365*y) + Math.floor(y/4) - Math.floor(y/100) + 
		Math.floor(y/400) - 32045;
	var d4 = (jd+31741-(jd%7))%146097%36524%1461;
	var L = Math.floor(d4/1460);
	var d1 = ((d4-L)%365)+L;
	NumberOfWeek = Math.floor(d1/7) + 1;
	return NumberOfWeek;        
}

function greaterthanDate(datea, dateb)
{
	if (datea.getFullYear() > dateb.getFullYear())
		return true;
	if (datea.getMonth() > dateb.getMonth())
		return true;
	if (datea.getDate() > dateb.getDate())
		return true;
	if (datea.getHours() > dateb.getHours())
		return true;
	if (datea.getMinutes() > dateb.getMinutes())
		return true;
	if (datea.getSeconds() > dateb.getSeconds())
		return true;
	if (datea.getMilliseconds() > dateb.getMilliseconds())
		return true;
	return false;
}
/// #endregion