/* ALGEMEEN */
var docAll = document.all;

function eleId(id) {
	if (document.getElementById(id)) {
		return document.getElementById(id);
	}
	return false;
}

function focusOn(id) {
	if (eleId(id)) {
		eleId(id).focus();
	}
}

function redirectTo(url, openInNewWindow) {
	if (openInNewWindow == true) {
		window.open(url);
	} else {
		document.location.href = url;
	}
}

function calcTop() {
	fs = getFrameSize();
		
	getTop = (parseInt(fs.fH) / 2);
	getTop += parseInt(fs.sT);
			
	return getTop;
}


/* AJAX */
function AjaxLoadURL(url, callFunction) {
	AjaxCallFunction = callFunction;
	
    if (window.XMLHttpRequest) {
		// branch for native XMLHttpRequest object
        AjaxRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
		// branch for IE/Windows ActiveX version
        AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }

	if (AjaxRequest) {
		if (AjaxCallFunction) {
			AjaxRequest.onreadystatechange = AjaxProcess;
		}
        AjaxRequest.open("GET", url, true);
        AjaxRequest.send("");
		
		return true;
	}
	return false;
}

function AjaxProcess() {
    if (AjaxRequest.readyState == 4) {
        if (AjaxRequest.status == 200) {
			loadfunction = new Function(AjaxCallFunction);
			loadfunction();
        }
    }
}

function AjaxResponse() {
	//var result = new Function('return ' + AjaxRequest.responseText);
	//alert(result);
}



/* RESULTAAT IN LIJST KLIK */
function viewItem(nr) {
	for (i = 0; i < resultArray.length; i++) {
		if (resultArray[i].nr == nr) {
			displayResult(nr, resultArray[i].title, resultArray[i].sites);
			
			return false;
			break;
		}
	}
}

function displayResult(nr, title, sites) {
	if (sites.length == 1) {
		// Occasion is op 1 site gevonden
		hdAskDisplay();
		visitedLink(nr, sites[0].srsNr);
		redirectTo(sites[0].url, true);
	} else {
		// Occasion is op meerdere sites gevonden
		dpAskDisplay();
		if (askDisplay) {
			sitesHTML = '';

			for (i = 0; i < sites.length; i++) {
				sitesHTML += '<span>-</span> ';
				sitesHTML += '<a href="' + sites[i].url + '" target="_blank" ';
				sitesHTML += 'onclick="hdAskDisplay(); visitedLink(\'' + nr + '\', \'' + sites[i].srsNr + '\');" title="Klik hier om naar de site van ' + sites[i].name + ' te gaan">' + sites[i].name + '</a><br />';
			}

			// Titel
			html = '<div class="title">';
			html += '<a href="#" style="float: right; color: white; padding-right: 5px; line-height: 26px;" onclick="hdAskDisplay(); return false;" title="Venster sluiten">X</a>';
			html += title + '</div>';
			
			html += '<div><div style="margin: 10px;">Deze advertentie is op meerdere websites gevonden.<br />';
			html += 'Klik op de website waar u de advertentie wilt bekijken:';
			html += '<div style="height: 88px; overflow: auto; text-align: left; margin-top: 10px; margin-left: 5px; display: block;">' + sitesHTML;
			html += '</div></div>';
			html += '<div style="height: 24px; margin: 0 auto; text-align: center;"><a href=\"#\" onclick="hdAskDisplay(); return false;">Venster sluiten</a></div>';
			
			askDisplay.innerHTML = html;
			askDisplay.style.display = '';
		}
	}
}

var askDisplay = false;
function dpAskDisplay() {
	hideMap();
	if (!askDisplay) {
		askDisplay = document.createElement('div');
		askDisplay.id = 'askdisplay';
		askDisplay.style.zIndex = 1002;
		
		document.body.appendChild(askDisplay);
	}
	askDisplay.style.top = calcTop() + 'px';
}

function hdAskDisplay() {
	if (askDisplay) {
		askDisplay.style.display = 'none';
	}
}

function visitedLink(srNr, srsNr, isSaved) {
	AjaxLoadURL('/action.php?type=visit&srnr=' + srNr + '&srsnr=' + srsNr + '&issaved=' + isSaved, false);
	
	if (eleId('a-' + srNr)) {
		eleId('a-' + srNr).className = 'visited';
	}
}



/* ADVERTENTIE OPSLAAN */
function saveFavorite(id, nr, actionNr) {
	if (AjaxLoadURL('/action.php?type=savefavorite&id=' + id + '&nr=' + nr + '&actionnr=' + actionNr, false)) {
		var eleDiv = eleId('save-' + nr);
		eleDiv.innerHTML = 'opgeslagen';
	}
}

function deleteFavorite(id, nr) {
	if (AjaxLoadURL('/action.php?type=deletefavorite&id=' + id + '&nr=' + nr, false)) {
		if (eleId('favorite-' + nr)) {
			liEle = eleId('favorite-' + nr);
			ulParent = liEle.parentNode;
			ulParent.removeChild(liEle);
			
			liElements = ulParent.getElementsByTagName('li');
			if (liElements.length == 0) {
				eleId('favorites').style.display = 'none';
			}
		}
	}
}

function deleteSearched(id, nr) {
	if (AjaxLoadURL('/action.php?type=deletesearch&id=' + id + '&nr=' + nr, false)) {
		if (eleId('searched-' + nr)) {
			liEle = eleId('searched-' + nr);
			ulParent = liEle.parentNode;
			ulParent.removeChild(liEle);
			
			liElements = ulParent.getElementsByTagName('li');
			if (liElements.length == 0) {
				eleId('searched').style.display = 'none';
			}
		}
	}
}

/* Pagina frame informatie
------------------------------------------------------------------*/
function getFrameSize(dW, dH) {
	doc = document;
	
	// Scrollbar
	var x,y;
	if (self.pageYOffset) {
		// all except Explorer
		x = self.pageXOffset;
		y = self.pageYOffset;
	} else if (doc.documentElement && doc.documentElement.scrollTop) {
		// Explorer 6 Strict
		x = doc.documentElement.scrollLeft;
		y = doc.documentElement.scrollTop;
	} else if (doc.body) {
		// all other Explorers
		x = doc.body.scrollLeft;
		y = doc.body.scrollTop;
	}
	
	// Page frame size
	if (self.innerWidth) {
		// all except Explorer
		fWidth = self.innerWidth;
		fHeight = self.innerHeight;
	} else if (doc.documentElement && doc.documentElement.clientWidth) {
		// Explorer 6 Strict
		fWidth = doc.documentElement.clientWidth;
		fHeight = doc.documentElement.clientHeight;
	} else if (document.body) {
		// all other Explorers
		fWidth = doc.body.clientWidth;
		fHeight = doc.body.clientHeight;
	}
	
	if (fWidth && fHeight) {
		return {fW: fWidth, sW: fWidth + x, fH: fHeight, sH: fHeight + y, sL: x, sT: y};
	}
	return {fW: dW, sW: dW + x, fH: dH, sH: dH + y, sL: x, sT: y};
}


/* Picture view
------------------------------------------------------------------*/
function PicView() {
	this.pointerX = false;
	this.pointerY = false;
	this.allow = (document.images ? true : false);
	
	this.div = false;
	this.img = false;
	this.src = false;
	
	this.ele = false;
	this.hide = false;
	
	this.frame = false;
	this.funcError = false;
}
PicView.prototype.show = function(e, src, hide, funcError) {
	this.src = src;
	this.hide = hide;
	if (funcError) {
		this.funcError = new Function(funcError);
	}
	
	if (this.allow) {
		if (!e) var e = window.event;
		this.pointerX = e.clientX;
		this.pointerY = e.clientY;
		
		this.ele = (e.srcElement ? e.srcElement : e.target);
		if (!this.ele.onmouseout) {
			this.ele.onmouseout = function() { pV.div.style.display = 'none'; };
		}
		
		if (!this.div) {
			// Afbeelding frame
			this.div = document.createElement('div');
			with (this.div.style) {
				position = 'absolute';
				backgroundColor = 'white';
				border = 'solid 1px #d5d5d5';
				padding = '5px';
			}
						
			document.body.appendChild(this.div);
		}
		
		// Browser frame grootte
		this.frame = getFrameSize(2000, 2000);
	
		// Afbeelding frame tonen
		with (this.div.style) {
			top = (this.pointerY + this.frame.sT) + 'px';
			left = (this.pointerX + 15 + this.frame.sL) + 'px';
			display = '';
		}
		
		this.div.innerHTML = 'Bezig met laden...';
		this.img = document.createElement('img');
		this.img.onload = function() { pV.loaded();	}
		this.img.onerror = function() { pV.error(); }
		this.img.src = this.src;
	}
}
PicView.prototype.loaded = function() {
	// Maximale afbeelding grote
	imgShrink(this.img, 194, 145);
	
	// Overschrijding browser frame verticaal
	picHeight = (this.img.height + 12);
	picBottom = (picHeight + (this.pointerY + this.frame.sT));
	if (picBottom > this.frame.sH) {
		newTop = (this.pointerY + this.frame.sT);
		newTop -= (picBottom - this.frame.sH);
		this.div.style.top = (newTop - 24) + 'px';
	}
	
	// Overschrijding browser frame horizontaal
	if (this.pointerX > 194) {
		picWidth = (this.img.width + 32);
		picRight = (picWidth + (this.pointerX + this.frame.sL));
		if (picRight > this.frame.sW) {
			newLeft = (this.pointerX + this.frame.sL);
			newLeft -= picWidth;
			this.div.style.left = (newLeft - 15) + 'px';
		}
	}

	while (this.div.hasChildNodes()) {
		this.div.removeChild(this.div.firstChild);
	}
	this.div.appendChild(this.img);
}
PicView.prototype.error = function() {
	if (this.hide) {
		this.ele.style.display = 'none';
	}
	
	if (this.funcError) {
		this.funcError();
	} else {
		this.div.innerHTML = 'De afbeelding is niet gevonden...';
	}
}

pV = new PicView();



/* AFBEELDING VERKLEINEN */
function imgShrink(ele, usewidth, useheight, checksize) {
	if (usewidth == 0) {
		usewidth = 80;
	}
	if (useheight == 0) {
		useheight = 80;
	}
	
	if (ele.width > usewidth) {
		ele.height = ((usewidth / ele.width) * ele.height); 
		ele.width = usewidth; 
	}
	
	if (ele.height > useheight) {
		ele.width = ((useheight / ele.height) * ele.width); 
		ele.height = useheight; 
	}
	
	if (checksize) {
		var checkImage = new Image();
		checkImage.src = ele.src;
			
		if (checkImage.width < ele.width) {
			ele.width = checkImage.width;
		}
		if (checkImage.height < ele.height) {
			ele.height = checkImage.height;
		}
	}
}
function imgError(nr) {
	if (eleId('img-' + nr)) {
		eleId('img-' + nr).style.display = 'none';
	}
}



/* LANDKAART */
var map = false;

function checkMap() {
	if (map && map.style.display == '') {
		map.style.top = (calcTop() - 217) + 'px';
	}
}

function showMap(lat, lng, title) {
	hdAskDisplay();
	if (!map) {
		map = document.createElement('div');
		with (map.style) {
			height = '460px';
			width = '510px';
			margin = '0 auto';
			border = 'solid 1px #b5b5b5';
			padding = '0';
			backgroundColor = '#daeacf';
			position = 'absolute';
			left = '50%';
			marginLeft = '-256px'; // 500 /2 = 250 + 5 padding + 1 border
		}
		
		html = '<div id="mapTitle" style="overflow: hidden; white-space: nowrap; padding-left: 10px; line-height: 26px; width: 500px; text-align: left; color: white; font-weight: bold; background: url(../images/greenbar.gif) repeat-x;"></div>';
		html += '<div id="GMDiv" style="width: 500px; height: 400px; margin: 5px;"></div>';
		html += '<a href="#" style="margin: 0 auto; line-height: 20px;" title="Kaart venster sluiten" onclick="hideMap(); return false;">';
		html += '<b>Venster sluiten</b></a>';
		
		map.innerHTML = html;
		document.body.appendChild(map);
		
		GMLoad({});
	}
	
	eleId('mapTitle').innerHTML = '<a href="#" style="float: right; color: white; padding-right: 5px;" onclick="hideMap(); return false;" title="Kaart venster sluiten">X</a>' + title;
	
	map.style.display = '';
	checkMap();	

	GMEle.clearOverlays();
	GMAddMarker(lat, lng);
	GMMoveTo(lat, lng, 12);
}

function hideMap() {
	if (map) {
		map.style.display = 'none';
	}
}

function fixJohnDeere() {
	document.write('<object width="120" height="400">');
	document.write('<param name="movie" value="<?=IMAGES?>banner/John_Deere_120x400.swf">');
	document.write('<embed src="/images/banner/John_Deere_120x400.swf" width="120" height="400"></embed>');
	document.write('</object>');
}

window.onscroll = checkMap;
window.onresize = checkMap;