/* CORE PART */
var isIE = document.all;

function focusOn(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).focus();
	}
}

function eleId(id) {
	if (document.getElementById(id)) {
		return document.getElementById(id);
	}
	return false;
}

function redirectTo(url, openInNewWindow) {
	if (openInNewWindow == true) {
		window.open(url);
	} else {
		document.location.href = url;
	}
}


/* HOVER PART */
var imgShow = false;
var hoverHeight = 2;
var hoverWidth = 2;

var hoverDiv = false;
var hoverFrame = false;
var hoverImage = false;

var posx = 1;
var posy = 1;

function mpMousePosition (e) {
	try {
		if (isIE) {
			scrollTop = parseInt(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
			
			if (event.clientX) {
				posx = event.clientX;
				posy = event.clientY + scrollTop;
			}
		} else {
			posx = e.pageX;
			posy = e.pageY;
		}
		posx = (posx < 0 ? 0 : posx);
		posy = (posy < 0 ? 0 : posy);
	} catch (catchedError) {
	}
	
	return true;
}

function dpHover(ele, url) {
	if (document.images) {
		if (!hoverDiv) {
			hoverDiv = document.createElement('div');
			hoverDiv.style.position        = 'absolute';
			hoverDiv.style.border          = 'solid black 1px';
			hoverDiv.style.backgroundColor = 'white';
			hoverDiv.style.padding         = '5px';
			hoverDiv.style.display         = 'none';
			hoverDiv.style.zIndex		   = 1001;
			
			if (document.all) {
				hoverFrame = document.createElement('iframe');
				hoverFrame.style.position = 'absolute';
				hoverFrame.style.display  = 'none';
				hoverFrame.frameBorder = '1';
				hoverFrame.style.filter = 'alpha(opacity = 0)';
				hoverFrame.style.zIndex = 1000;
				
				document.body.appendChild(hoverFrame);
			}
			
			document.body.appendChild(hoverDiv);
		}
		
		pointingX = posx + 10;
		pointingY = posy;
		
		if (!imgShow || hoverHeight <= 2) {
			var img = new Image();
			img.src = url;

			hoverHeight = img.height;
			hoverWidth = img.width;
			
			newPositions = dpHoverCalcPosition(pointingX, (pointingY - 20 - hoverHeight));
			pointingX = newPositions[0];
			pointingY = newPositions[1];
		} else {
			pointingY -= 20;
			pointingY -= hoverHeight;
		}
	   
		hoverDiv.style.left = pointingX + 'px';
		hoverDiv.style.top = pointingY + 'px';
		
		if (hoverFrame) {
			hoverFrame.style.left = hoverDiv.style.left;
			hoverFrame.style.top = hoverDiv.style.top;
		}

		if (!imgShow) {
			scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
			imgShow = true;
			hoverDiv.innerHTML = '<img id=\"hoverImage\" src="' + url + '" onload=\"dpHoverFix();\" onerror=\"dpHoverStop(\'' + ele.id + '\');\" />';
			hoverDiv.style.display = '';
			
			if (hoverFrame) {
				hoverFrame.style.display = '';
				hoverFrame.style.width = (hoverWidth + 12); // border is 2px + padding van 10px totaal
				hoverFrame.style.height = (hoverHeight + 12); // border is 2px + padding van 10px totaal
			}
		} else {
			dpHoverFix();
		}
	}
}

function dpHoverStop(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).onmousemove = 'return false';
		document.getElementById(id).onmouseout = 'return false';
		dpHoverClose();
	}
}
 
function dpHoverClose() {
	if (hoverDiv) {
		hoverDiv.style.display = 'none';
		
		if (hoverFrame) {
			hoverFrame.style.display = 'none';
		}
		imgShow = false;
		
		hoverImage = false;
	}
}

function dpHoverCalcPosition(x, y) {
	browserSize = calculateBrowserSize();

	newLeft = parseInt(x);
	newTop = parseInt(y);
	
	// Breedte check
	if (hoverWidth + 28 + parseInt(x) > browserSize[0]) {
		newLeft = (browserSize[0] - hoverWidth - 28);
	}
	
	// Hoogte check
	if (parseInt(y) - parseInt(document.documentElement.scrollTop) < 0) {
		newTop = (parseInt(y) + hoverHeight + 30);
	}
	
	return new Array(newLeft, newTop);
}

function dpHoverFix() {
	if (hoverDiv) {
		newPositions = dpHoverCalcPosition(hoverDiv.style.left, hoverDiv.style.top);
		
		hoverDiv.style.left = newPositions[0] + 'px';
		hoverDiv.style.top = newPositions[1] + 'px';
		
		if (hoverFrame) {
			hoverFrame.style.left = newPositions[0] + 'px';
			hoverFrame.style.top = newPositions[1] + 'px';
		}
	}
}



/* SEARCHRESULTS PART */
var requestDisplay = false;

window.onscroll = checkDoubleResult;
window.onresize = checkDoubleResult;

function checkDoubleResult() {
	if (requestDisplay) {
		if (requestDisplay.style.display == '') {
			requestDisplay.style.top = calculateTop() + 'px';
		}
	}
}

function calculateBrowserSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	return new Array(myWidth, myHeight);
}

function calculateTop() {
	browserSize = calculateBrowserSize();
	
	calcTop = (parseInt(browserSize[1]) / 2);
	calcTop += parseInt(document.documentElement.scrollTop);
			
	return calcTop;
}

function updateProgressBar(start, increment, totalresult) {
	end = (start + increment);
	for (i = start; i <= end; i += 10) {
		if (eleId('progress_' + i)) {
			eleId('progress_' + i).style.backgroundColor = '#ff8000';
		}
	}
	
	if (eleId('total')) {
		eleId('total').innerHTML = 'Aantal occasions gevonden: ' + totalresult;
	}
}

function createRequestDisplay() {
	if (!requestDisplay) {
		requestDisplay = document.createElement('div');
		requestDisplay.id = 'requestdisplay';
		requestDisplay.style.zIndex = 1002;
		
		document.body.appendChild(requestDisplay);
	}
	requestDisplay.style.top = calculateTop() + 'px';
}

function hdRequestDisplay() {
	if (requestDisplay) {
		requestDisplay.style.display = 'none';
	}
}


/* Images
------------------------------------------------------------------*/
function shrinkImage(ele, usewidth, useheight, checksize) {
	if (usewidth == 0) {
		usewidth = 64;
	}
	if (useheight == 0) {
		useheight = 64;
	}
	
	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 noImage(ele, backgroundColor) {
	ele.src = '/images/noimage_' + backgroundColor + '.gif';
	ele.style.width = '60px';
	ele.style.height = '60px';
	ele.onmousemove = 'return false';
	ele.onmouseout = 'return false';
}

function noImageHover(id, backgroundColor) {
	if (eleId(id)) {
		ele = eleId(id);
		ele.src = '/images/noimage_' + backgroundColor + '.gif';
	}
}

function errorImage(trId, photoId, backgroundColor) {
	if (eleId(trId) && eleId(photoId)) {
		noImage(eleId(photoId), backgroundColor);

		tr = eleId(trId);
		if (tr.addEventListener) {
			tr.addEventListener("mouseover", function(event){noImageHover(photoId, 'e5f2f8');},false);
			tr.addEventListener("mouseout", function(event){noImageHover(photoId, backgroundColor);},false);	
		} else if (tr.attachEvent) {
			tr.attachEvent("onmouseover", function(event){noImageHover(photoId, 'e5f2f8');});
			tr.attachEvent("onmouseout", function(event){noImageHover(photoId, backgroundColor);});
		}
	}
}


document.onmousemove = mpMousePosition;