// JavaScript Document

//Functions by Peter Zonneveld <peter@lytjepole.nl>


function fadeIn(objectId) {
	object = document.getElementById(objectId);
	object.style.visibility = "visible";
	goFadeIn(objectId);
}

function fadeOut(objectId) {
	object = document.getElementById(objectId);
	goFadeOut(objectId);
}

var x = 0;
var t;

function goFadeIn(objectId) {
x = (x + 10);
object = document.getElementById(objectId);
t = setTimeout("goFadeIn('"+objectId+"')",5);
setOpacity(objectId,x);
	if(x >= 100)
		{
			stopCount();
			x = 0;
			
		}
	return true;
}

function goFadeOut(objectId) {
x = (x + 10);
object = document.getElementById(objectId);
t = setTimeout("goFadeOut('"+objectId+"')",5);
setOpacity(objectId,(100 - x) );
	if(x >= 100)
		{
			stopCount();
			x = 0;
			object.style.visibility = "hidden";
			
		}
	return true;
}


function stopCount() {
	clearTimeout(t);
}

function setOpacity(objectId,value) {
object = document.getElementById(objectId);
//document.getElementById('serverTime').innerHTML = objectId;
	object.style.opacity=value/100;
	object.style.filter = "alpha(opacity:"+(value)+")";
	object.style.MozOpacity = value/100;
	object.style.KHTMLOpacity = value/100;
}

//end fade functions

// date functions

function formatDate(timestamp) {
	var weekdays = new Array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag');
	var months = new Array('januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december');
	var date = new Date(timestamp);
	return weekdays[date.getDay()]+' '+date.getDate()+' '+months[date.getMonth()]+' '+date.getFullYear();
}

// end date functions

// choice functions

function setView(object) {
	//alert(object.value);
	if(object.value == 0)
		{
			window.location="http://www.lytjepole.nl/index.php?show=0";
		}
		else
		{
			window.location="http://www.lytjepole.nl/index.php?show=1";
		}
}

// end choice funtions

function collapseObject(objectId)	{
	var objectId;
	
	object = document.getElementById(objectId);
	gosetHeight(objectId);
	//alert(object.offsetHeight);
	//object.style.height = '50px';
	//alert(object.offsetHeight);
}

function gosetHeight(objectId) {
var  x = document.getElementById(objectId).offsetHeight - 50;
//alert(object.offsetHeight);
	if(x <= 25)
	{
		//stopCount();
		document.getElementById(objectId).style.height = '0px';
		//alert(document.getElementById(objectId).offsetHeight);		
		return false;
	}
	else
	{
	document.getElementById(objectId).style.height = x+'px';
	var t = setTimeout("gosetHeight('"+objectId+"')",3);
	}
}
var  timer;
function hideItemDetails(object) {
	timer = window.setTimeout("gohideItemDetails('"+object+"')",500);
	
}

function gohideItemDetails(object) {
	document.getElementById(object).style.visibility = 'hidden';
	document.getElementById(object).style.height = '0px';
}

function showItemDetails(object) {
	var timershow = window.setTimeout("goshowItemDetails('"+object+"')",250);
}

function goshowItemDetails(object) {
	document.getElementById(object).style.visibility = 'visible';
	document.getElementById(object).style.height = 'auto';
}

function clearTimeOut() {
	window.clearTimeout(timer);
}


//for laoding external links
function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
}

function showOverlay(url) {

var body = document.getElementsByTagName("body")[0];

//create overlay
divHandle = document.createElement("div");
divHandle.setAttribute('id','overlay');
divHandle.setAttribute('name','overlay');

body.appendChild(divHandle);

var pagewidth = document.body.scrollWidth;
document.getElementById('overlay').style.width = pagewidth+'px';
document.getElementById('overlay').style.height = document.body.scrollHeight+'px';
setOpacity('overlay',65);

//create container
divHandle = document.createElement("div");
divHandle.setAttribute('id','overlay-container');
divHandle.setAttribute('name','overlay-container');
body.appendChild(divHandle);
var overlaycontainer = document.getElementById('overlay-container');
var offsettop = document.documentElement.scrollTop+25;
var offsetleft = Math.floor( (document.body.scrollWidth-600)/2 );

overlaycontainer.style.postition = 'absolute';
overlaycontainer.style.top = offsettop+'px';
overlaycontainer.style.left = offsetleft+'px';
overlaycontainer.innerHTML = 'gegevens ophalen...';

// get contents
url = document.getElementById('url').value;

overlaycontainer.innerHTML = getContents(url);
}

function closeOverlay() {
	//alert('sluiten');	
	//fadeOut('google_frame');
	//setOpacity('Div',0);
	object = document.getElementById('overlay');
	document.body.removeChild(object);
	object = document.getElementById('overlay-container');
	document.body.removeChild(object);
}

function getContents(url) {
        var httpRequest;
		var url;

	//url = '/item/php/google.inc.php?item_id='+item_id;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/html');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }


		httpRequest.onreadystatechange = function() 
			{
				if(httpRequest.readyState <= 3)
					{
						document.getElementById('overlay-container').innerHTML = "laden...";	
					}
				if(httpRequest.readyState == 4)
					{
						if(httpRequest.status == 200)
							{
								//document.getElementById('google_frame').innerHTML = httpRequest.responseText;
							}
							else
							{
								alert('There was a problem with the request.');	
							}
					}
			}
	httpRequest.open('GET', url, false);
    httpRequest.send(null);
	return httpRequest.responseText;
}

window.onload = externalLinks;