function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;

function preloadImages() {
	if (document.images) {
		/*vlaggen_01_over = newImage("images/vlaggen_01-over.gif");
		vlaggen_02_over = newImage("images/vlaggen_02-over.gif");
		vlaggen_03_over = newImage("images/vlaggen_03-over.gif");
		vlaggen_04_over = newImage("images/vlaggen_04-over.gif");
		
		vlaggen_01_over = newImage("images/vlaggen_01-out.gif");
		vlaggen_02_over = newImage("images/vlaggen_02-out.gif");
		vlaggen_03_over = newImage("images/vlaggen_03-out.gif");
		vlaggen_04_over = newImage("images/vlaggen_04-out.gif");*/
			
		wm = newImage("images/InWinkelmand-out.gif");
		wm_over = newImage("images/InWinkelmand-over.gif");
		af = newImage("images/Add2Favs-over.gif");
		af_over = newImage("images/Add2Favs-out.gif");
		preloadFlag = true;
		
		//alert( knopover.src );
	}
}

function openRoute()
{
	openCenteredWindow( 'Route.html', 750, 536, '', 'menubar=yes' );
}
function openLiveHelpdesk()
{
	openCenteredWindow( 'Componenten/LiveHelpdesk/Paginas/Client.aspx', 750, 420, '', 'menubar=no' );
}
function openCenteredWindow(url, width, height, name, parms) {
   var left = Math.floor( (screen.availWidth - width) / 2);
   var top = Math.floor( (screen.availHeight - height - 30) / 2);
   var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
   if (parms) { winParms += "," + parms; }
   //alert(  "width:" + width + ", h:" + height );
   var win = window.open(url, name, winParms);
   return win;
}

//var maxWinW = 775;
//var maxWinH = 536;
var screenW = screen.width;
var screenH = screen.height;
//alert( screenW );
function checkFotoWidth( img, maxw )
{
	if( maxw==undefined)
	    maxw = 200;
	var imgW = GetImageWidth(img.src);
	var imgH = GetImageHeight(img.src);
        //alert(GetImageWidth(img.src).toString()); // img.width); // objToString(img));
	if (imgW > maxw || (img.style.cursor == "hand" && imgW == maxw))
	{
	    if (imgW > screenW) winW = screenW;
		else winW = imgW;

		if (imgH > screenH) winH = screenH;
		else winH = imgH;
		
		img.width=maxw;
		img.style.cursor = "pointer";

		img.WinW = winW;
		img.WinH = winH;
		//alert("winW : " + img.WinW);
		img.onclick = function() { openCenteredWindow("Dialogs/ZoomWin.aspx?img=" + this.src, this.WinW, this.WinH, '', 'resizable=1'); }
		img.alt = "Klik voor een uitvergroting :: click to enlarge :: klicken Sie um das Bild zu vergrössern";
		//alert("adding onclick);
	}
}

function objToString( obj, toonUndefined ) {
   var ret = "";
   
   ret += "tagName : " + obj.tagName + "\n";
   ret += "id : " + obj.id + "\n";   
   
   for (var prop in obj)
   {
		if( toonUndefined==false && this[prop]==undefined )
			continue;
		ret += " " + prop + " is " + this[prop] + " - ";// ";\n";
   }
   return ret;
}

function GetImageWidth(imgSrc) 
{
    var img = new Image();
    img.src = imgSrc;
    return img.width;
} 

function GetImageHeight(imgSrc) 
{
    var img = new Image();
    img.src = imgSrc;
    return img.height;
} 

function addOnloadFunction( func, args )
{
	if( isUserFireFox()==true )
	{
		var fonl = window.onload;
		window.onload = function(){ 
			//alert("in onload");
			if(args!=undefined)
				func( args ); 
			else
				func();
			if( fonl != undefined )
				fonl();
		}
	}
	else
	{
		var fonl = document.body.onload;
		document.body.onload = function(){ 
			if(args!=undefined)
				func( args ); 
			else
				func();
			if( fonl != undefined )
				fonl();
		}
	}
}

function CancelBubble(e)
{
	if (!e) var e = window.event;
	if (e.stopPropagation) 
		e.stopPropagation();
	else
		event.cancelBubble = true;
}

function addToFavs( productID )
{
	CancelBubble();
	this.location.href = this.location.href + "&opt3=add2favs&productid=" + productID;
}
function isIE()
{
	return ( document.all?true:false );
}
function isUserFireFox()
{
	return( navigator.userAgent.indexOf("Firefox")>0 );
}


 //-------- IMAGESLIDE ---------------------
var zoomWachtID = "";
var zoomResetted = false;
 function doZoom( url )
{
	zoomWachtID = setTimeout( "startZoom('" + url + "')", 200 );
}
function startZoom(url)
{
	if( zoomWachtID != "" )
	{
		zoomResetted = false;
		var img = document.getElementById( "zoomImg" );
		//img.style.display = "none";
		img.style.visibility = "hidden";
		img.onload = positionFoto;
		img.src = url;
	}
}
function reset( )
{
	zoomResetted = true;
	clearTimeout( zoomWachtID );
	zoomWachtID = "";
	var img = document.getElementById( "zoomImg" );
	img.style.visibility = "hidden";
	if(isUserFireFox()==false)
		img.clearAttributes();
	//alert( document.getElementById( "zoomDiv" ).style.top );
	document.getElementById( "zoomDiv" ).style.top = "-2000px";
}
var origW = 0;
var origH = 0;
function positionFoto()
{
	if( zoomResetted == true )
		return;
	var maxW = imageSlide_maxW;//775;
	var maxH = imageSlide_maxH;//mouseY - 12;// 500;
	var img = document.getElementById( "zoomImg" );
	origW = img.width;
	origH = img.height;
	if(img.width<=1)
	{
		//setTimeout( "positionFoto()", 300 );
		return;
	}
	
	var fotoW = img.width;
	var fotoH = img.height;
	var screenW = screen.availWidth;
	var screenH = screen.availHeight;
	
	var ratio = parseFloat(img.width) / parseFloat(img.height);
	if( img.width > img.height )
	{
		//horizontale foto
		if( img.width > maxW && (img.width / ratio) < maxH)
		{
			img.width = maxW;
			fotoW = maxW;
			img.height = img.width / ratio;
			//alert("hor foto, height:" + img.height + " / ratio:" + ratio);
		}
		else
		{
			img.height = maxH;
			fotoH = maxH;
			img.width = img.height * ratio;
		}
	}
	else
	{
		//verticale foto
		if( img.height > maxH && (img.height * ratio)<maxW)
		{
			img.height = maxH;
			fotoH = maxH;
			img.width = img.height * ratio;
		}
		else
		{
			img.width = maxW;
			fotoW = maxW;
			img.height = img.width / ratio;
		}
	}
	
	img.style.cursor = "hand";
	img.style.border = "2px solid black";
	img.onclick = openZoom;
	img.onmouseout = reset;
	img.style.visibility = "visible";
	document.getElementById( "zoomDiv" ).style.top = "0px";
}
function getMouseY(e) {
	var tempY = 0;
	if (!e)
		e = window.event||window.Event;
	//alert(e.pageY);
	if (isIE()) { // grab the x-y pos.s if browser is IE
		tempY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempY = e.pageY;
	}  
	// catch possible negative values in NS4
	if (tempY < 0){tempY = 0}  
	return tempY;
}
function openZoom()
{
	var img = document.getElementById( "zoomImg" );
	img.galleryimg = "false";
	var url = "Dialogs/ZoomWin.aspx?img=" + img.src;
	var win = openCenteredWindow(  url, origW, origH, '' );
}
/*function openZoomFromUrl( url, w, h )
{
	var img = document.getElementById( "zoomImg" );
	var url = "Dialogs/ZoomWin.aspx?img=" + url;
	var win = openCenteredWindow(  url, w, h, '' );
}
function openZoomFromUrl( url, w, h )
{
	alert( url );
	var url = "Dialogs/ZoomWinImageSlide.aspx?img=" + url;
	var win = openCenteredWindow(  url, w, h, '' );
}*/
var imageSlideOpenWin;
function openZoomFromUrl( folder, foto, fotoNr )
{
	var maxW = 1024;
	var maxH = 768;
	//alert( folder + " : " + foto );
	var w = screen.availWidth-10;//-50;
	var h = screen.availHeight-37;//-80;
	if( w > maxW )
	{
		w = maxW;
		h = maxH;
	}
	var vensterwidth = w-30;
	var vensterheight = h-50;
	var url = "Dialogs/ZoomWinImageSlide.aspx?folder=" + folder + "&width=" + vensterwidth + "&height=" + vensterheight + "&startFoto=" + foto + "&startNr=" + fotoNr;
	

	//alert("w:" + screen.availWidth + ", h:" + h );
	//var win = window.open(url, '', "left=0, top=0, width=" + w + ",height=" + h + ", resizable=yes");
	var imageSlideOpenWin = openCenteredWindow(  url, w, h, '', 'resizable=yes' );
}
//-------- END IMAGESLIDE

//-------- FACTUREN

function openFactuur( factuurID, isPDF)
{
	if( isPDF==true )
	{
		//location.href="FileDownloader.aspx?file=" + escape( "Componenten/Facturatie/Paginas/FactuurPDF.aspx?id="+factuurID );
		location.href="Componenten/Facturatie/Paginas/FactuurPDF.aspx?id="+factuurID;
	//openCenteredWindow( "Componenten/Facturatie/Paginas/FactuurPDF.aspx?id="+factuurID, 810, 536, '', 'menubar=yes, resizable=1, scrollbars=1' );
	}
	else
	{
	openCenteredWindow( "Componenten/Facturatie/Paginas/FactuurView.aspx?id="+factuurID, 810, 536, '', 'menubar=yes, resizable=1, scrollbars=1' );
	}
}



//---- IMAGE TRAIL
/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

var w = 1;
var h = 1;

if (document.getElementById || document.all)
    document.write('<div id="trailimageid" style="position:absolute;visibility:hidden;left:0px;top:-1000px;width:1px;height:1px;border:1px solid #888888;background:#DDDDDD;"><img id="ttimg" src="img/s.gif" /></div>')

function gettrailobj() {
    if (document.getElementById) return document.getElementById("trailimageid").style
    else if (document.all) return document.all.trailimagid.style
}

function gettrailIMGobj() {
    if (document.getElementById) return document.getElementById("ttimg")
    else if (document.all) return document.all.ttimg
}

function truebody() {
    return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body
}

function hidetrail() {
    document.onmousemove = ""
    document.getElementById('ttimg').src = '/img/s.gif'
    gettrailobj().visibility = "hidden"
    gettrailobj().left = -1000
    gettrailobj().top = 0
}

/*
function showtrail(width, height, file) {
    if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
        w = width;
        h = height;

        // followmouse()

        document.getElementById('ttimg').src = file;
        document.onmousemove = followmouse;
        gettrailobj().visibility = "visible";
        gettrailobj().width = w + "px"
        gettrailobj().height = h + "px"
        //gettrailMainobj().style.width = "50";
        //gettrailMainobj().style.width = 50;

        alert(gettrailIMGobj().width);

        //gettrailIMGobj().style.width = w + "px";

        //alert(gettrailIMGobj().toString());
    }
}*/

var hoverImageMaxWOfH = 300;
function showtrail(maxWofH, file) {
    if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) {
        w = maxWofH;
        h = maxWofH;
        hoverImageMaxWOfH = maxWofH;
        //document.getElementById('ttimg').removeAttribute("width");
        //document.getElementById('ttimg').removeAttribute("height");
        document.getElementById('ttimg').removeAttribute("style");
        //document.getElementById('ttimg').height = null;
        document.getElementById('ttimg').src = file;
        document.getElementById('ttimg').onload = resizeHoverImg;
        document.onmousemove = followmouse;
        //gettrailobj().visibility = "visible";

        //alert(gettrailobj().width);
        //gettrailIMGobj().style.width = maxWofH + "px";
        /*var imgW = gettrailIMGobj().width;
        var imgH = gettrailIMGobj().height;
        if( imgW > imgH )
            gettrailIMGobj().style.width = maxWofH + "px";
        else
            gettrailIMGobj().style.height = maxWofH + "px";
            
            
        alert(gettrailIMGobj().width + " - " + gettrailIMGobj().height);*/

    }
}

function resizeHoverImg() {

    gettrailobj().visibility = "visible";
    var imgW = document.getElementById('ttimg').width;
    var imgH = document.getElementById('ttimg').height;
    var factor = imgW / imgH;
    //alert(imgW + " - " + imgH);
    //return;
    if (imgW > imgH) {
        w = hoverImageMaxWOfH;
        h = (hoverImageMaxWOfH / factor);
        gettrailIMGobj().style.width = w + "px";
        gettrailIMGobj().style.height = h + "px";
    }
    else {
        w = (hoverImageMaxWOfH * factor);
        h = hoverImageMaxWOfH ;
        gettrailIMGobj().style.height = h + "px";
        gettrailIMGobj().style.width = w + "px";
    }
    //document.getElementById('ttimg').onload = null;
    //return;
}

function followmouse(e) {

    if (navigator.userAgent.toLowerCase().indexOf('opera') == -1) {

        var xcoord = 20
        var ycoord = 20

        if (typeof e != "undefined") {
            xcoord += e.pageX
            ycoord += e.pageY
        }
        else if (typeof window.event != "undefined") {
            xcoord += truebody().scrollLeft + event.clientX
            ycoord += truebody().scrollTop + event.clientY
        }

        var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15
        var docheight = document.all ? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

        //alert(docwidth + " - " + docheight);
        if (xcoord + w + 3 > docwidth)
            xcoord = xcoord - w - (20 * 2)

        if (ycoord - truebody().scrollTop + h > truebody().clientHeight)
            ycoord = ycoord - h - 20;

        gettrailobj().left = xcoord + "px"
        gettrailobj().top = ycoord + "px"

        //gettrailobj().width = "50px";
    }

}
