var zindexcount = 20;
var localizedLabels = {
    GeenInfo: 'Geen info'
};

function addevent(obj, ev, func) {
	if (obj.addEventListener) {
		obj.addEventListener(ev, func, false);
	}
	else if (obj.attachEvent) {
		obj.attachEvent('on' + ev, func);
	} else {
		alert('fout');
	}
}

// ------------------------------------------------------------------------------------------------------------------

var specialtooltips = new Array();
specialtooltips["specialtooltip"] = -1;

var currentspecialtooltip = "";
var timeoutspecialtooltip ;

// o = target object van de specialtooltip
// n = ? (een soort van id, geef altijd null mee, anders blijven uw specialtooltips lang staan en gaan andere niet open zolang deze er is)
// div = "specialtooltip" (er zijn blijkbaar ook andere)
// 4rde parameter: specialtooltip inhoud
// 5de parameter : breedte specialtooltip
// 6de parameter : offset x specialtooltip (tov object o) (standaard = 0), maar hij doet altijd +16 dus trek er 16 van af als ge één mee geeft
// 7de parameter : offset y specialtooltip (tov object o) (standaard = 0), maar hij doet altijd -3 dus tel er +3 bij op als ge één mee geeft.
// 8ste parameter: als deze patameter is opgegeven zal de specialtooltip geen timeout hebben
// 9de parameter : als deze patameter is opgegeven zal de specialtooltip altijd langs links verschijnen

function showspecialtooltip(o, n, div) {
	var a = arguments;
	htmldiv = null;
	htmlwidth = 0;
	extrawidth = 0;
	yOffset = 0;
	timeoutEnabled = true;
	showLeft = false;
	if (a.length >= 4) if (a[3] != null) htmldiv = a[3];
	if (a.length >= 5) if (a[4] != null) htmlwidth = a[4];
	if (a.length >= 6) if (a[5] != null) extrawidth = a[5];
	if (a.length >= 7) if (a[6] != null) yOffset = a[6];
	if (a.length >= 8) if (a[7] != null) timeoutEnabled = false;
	if (a.length >= 9) if (a[8] != null) showLeft = true;

	if (currentspecialtooltip == div && specialtooltips[div] == n && n != null) return;

	hidespecialtooltip(currentspecialtooltip == div);

	a = getpos(o);
	a[1]+= yOffset;

	var c = "";
	if (htmldiv != "" && (div == "specialtooltip" || div == "specialtooltip_popup")) {
		c = localizedLabels.GeenInfo;
		if (typeof htmldiv == "object") {
			try { c = htmldiv.innerHTML; } catch(e) { }
		} else if (typeof htmldiv == "string") c = htmldiv;
		else return;
		c = str_replace("<OMG ", "<IMG ", c);
		c = str_replace("<omg ", "<IMG ", c);
		if (c == localizedLabels.GeenInfo) htmlwidth = 0;
		_$u(div+'content').innerHTML = c;

		if (htmlwidth > 0) {
			_$u(div+'content').style.width = htmlwidth + "px";
			_$u(div+'content').style.padding = "4px 5px 4px 5px";
		} else {
			_$u(div+'content').style.width = "";
			_$u(div+'content').style.padding = "0px";
		}
	}

	w = parseInt(_$u(div+'div').offsetWidth) + 5;
//	if (a[0] + w > document.body.clientWidth) {
// bug fix here. w contains the full width of the tip, a[0] is the x coordinate of the target object.
// specialtooltips that are too big for the screen will be cut into multiple lines by the browser.
	if ((a[0] + w > document.body.clientWidth && w < a[0]-100) || showLeft) {
		_$u(div+'div').style.left = (parseInt(a[0]) - w - 5) + "px";
		_$u('trileft'+div).style.visibility = "hidden";
		_$u('triright'+div).style.visibility = "visible";
	} else {
		_$u(div+'div').style.left = (parseInt(a[0]) + 25 + extrawidth) + "px";
		_$u('triright'+div).style.visibility = "hidden";
		_$u('trileft'+div).style.visibility = "visible";
	}

	_$u(div+'div').style.top = (parseInt(a[1]) - 3) + "px";
	if (!ie) fadein(div+'div', 0.15); else _$u(div+'div').style.visibility = "visible";
	specialtooltips[div] = n;
	currentspecialtooltip = div;

	var ttt = 10000;
	if (c.length > 50) ttt = 15000;
	if (timeoutEnabled) timeoutspecialtooltip = window.setTimeout("hidespecialtooltip(false)", ttt);
	else skiphidespecialtooltip = true;
}
function hidespecialtooltip(quick) {
	if (timeoutspecialtooltip) window.clearTimeout(timeoutspecialtooltip);
	if (currentspecialtooltip == "") return;
	if (ie || quick) {
		_$u(currentspecialtooltip+'div').style.visibility = "hidden";
		_$u('triright'+currentspecialtooltip).style.visibility = "hidden";
		_$u('trileft'+currentspecialtooltip).style.visibility = "hidden";
	} else {
		fadeout(currentspecialtooltip+'div', 0.2);
	}
	specialtooltips[currentspecialtooltip] = -1;
	currentspecialtooltip = "";
}

var skiphidespecialtooltip = false;
addevent(document, "mouseup", function() {
	if (skiphidespecialtooltip) {
		skiphidespecialtooltip = false;
		return;
	}
//	hideautocomplete();
	window.setTimeout("hidespecialtooltip(false)", 50);
});


// 	position functions

function getRealLeft(el){
	if (el == null) return false;
	xPos = _$u(el).offsetLeft;
	tempEl = _$u(el).offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}

function getRealTop(el){
	if (el == null) return false;
	yPos = _$u(el).offsetTop;
	tempEl = _$u(el).offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

function getpos(isID){
	trueX = getRealLeft(isID);
	trueY = getRealTop(isID);
	return Array(trueX,trueY);
}


function str_replace(needle, rep, haystack) {
	if (haystack == "" || needle == "" || haystack == null) return "";

	cursor = 0;
	loc = haystack.indexOf(needle, 0)
	while (loc != -1) {
		endOfNeedleIndex = loc + needle.length;
		haystack = haystack.substr(0, loc) + rep + haystack.substr(endOfNeedleIndex, haystack.length - endOfNeedleIndex);
		cursor = loc + rep.length;
		loc = haystack.indexOf(needle, cursor)
	}
		
	return haystack;
}

// ------------------------------------------------------------------------------------------------------------------

function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, prec = decimals;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
    var dec = (typeof dec_point == "undefined") ? '.' : dec_point;

    var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = Math.abs(n).toFixed(prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');

        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    return s;
}

// ------------------------------------------------------------------------------------------------------------------

function sanitizePriceField(priceField) {
	number = priceField.value;

	if (number == "")
		return "";

	number = new String(number);
	number = number.replace(/[^0123456789,]/g, ""); // remove all but digits & comma (we don't need dots)
	number = number.replace(",", ".");
	number = number_format(number, 2, ",", ".");
	if (number == "0.00")
		priceField.value = "";

	priceField.value = number;
}

// ------------------------------------------------------------------------------------------------------------------

function fadeIn(whatdiv, speed) {
	$(whatdiv).style.visibility = "hidden";
	$(whatdiv).style.zIndex = zindexcount;

	zindexcount++;
	if (document.all && navigator.userAgent.indexOf("MSIE 5") == -1) {
		$(whatdiv).style.filter = "blendTrans(duration=" + speed + ")";
		if ($(whatdiv).filters.blendTrans.status != 2) {
			$(whatdiv).filters.blendTrans.apply();
			$(whatdiv).style.visibility = "visible";
			$(whatdiv).filters.blendTrans.play();
		}
	} else {
		$(whatdiv).style.opacity = 0;
		$(whatdiv).style.mozOpacity = 0;
		$(whatdiv).style.visibility = "visible";
		for (i = 0.1; i <= 1; i += 0.025) {
			window.setTimeout("$('" + whatdiv + "').style.opacity=" + i, i * 1000 * speed);
			window.setTimeout("$('" + whatdiv + "').style.mozOpacity=" + i, i * 1000 * speed);
		}
	}
}

// ------------------------------------------------------------------------------------------------------------------

function fadeOut(whatdiv,speed) {
	$(whatdiv).style.visibility = "visible";
	$(whatdiv).style.zIndex = zindexcount;

	zindexcount++;
	if (document.all && navigator.userAgent.indexOf("MSIE 5") == -1) {
		$(whatdiv).style.filter = "blendTrans(duration=" + speed + ")";
		if ($(whatdiv).filters.blendTrans.status != 2) {
			$(whatdiv).filters.blendTrans.apply();
			$(whatdiv).style.visibility = "hidden";
			$(whatdiv).filters.blendTrans.play();
		}
	} else {
		$(whatdiv).style.opacity = 0;
		$(whatdiv).style.mozOpacity = 0;
		$(whatdiv).style.visibility = "visible";
		for (i = 0.1; i <= 1; i += 0.025) {
			window.setTimeout("$('" + whatdiv + "').style.opacity=" + (0.9 - i), i * 1000 * speed);
			window.setTimeout("$('" + whatdiv + "').style.mozOpacity=" + (0.9 - i), i * 1000 * speed);
		}
	}
}

// ------------------------------------------------------------------------------------------------------------------

// returns true if using FF and there is NO vertical scrollbar
function isFirefoxScrollBarInvisible() {
	if (navigator.userAgent.indexOf("MSIE") > 0)
		return false;

	// scrollHeight only appears to work in FF, but that is where we are having issues
	if (document.documentElement.scrollHeight <= window.innerHeight) // too big for screen
		return true;


	document.getElementById("website").style.paddingLeft = '17px';
}

//addevent(window, "load", function () { isFirefoxScrollBarInvisible(); });

// ------------------------------------------------------------------------------------------------------------------

var u_preloads = new Array();
addevent(window,"load",function () {
	im = document.getElementsByTagName("img");
	for (i=0; i<im.length; i++) {
		if (im[i].getAttribute("noalt") != null) im[i].setAttribute("alt","");
		if (im[i].getAttribute("swap") != null) {
			soff = im[i].getAttribute('src'); son = soff.replace("_off","_on");
			u_preloads[i] = new Image(); u_preloads[i].src = son;
			//alert('1.'+im[i].onmouseover);
			addevent(im[i], 'mouseover', new Function("im["+i+"].src='"+son+"'"));
			addevent(im[i], 'mouseout', new Function("im["+i+"].src='"+soff+"'"));
			//alert('2.'+im[i].onmouseover);
		}
	}
});

// ------------------------------------------------------------------------------------------------------------------

// 	whoa...! returns object or array of objects (given by parameters)

function $() {
	es = new Array();
	for (i=0; i<arguments.length; i++) {
		e = arguments[i];
		if (typeof e == "string") e = document.getElementById(e);
		if (arguments.length == 1) return e;
		es.push(e);
	}
	return es;
}

function _$u() {
	es = new Array();
	for (i=0; i<arguments.length; i++) {
		e = arguments[i];
		if (typeof e == "string") e = document.getElementById(e);
		if (arguments.length == 1) return e;
		es.push(e);
	}
	return es;
}

// ------------------------------------------------------------------------------------------------------------------

// DomReady - 'cause onload sucks
if (typeof Event == 'undefined') Event = new Object();

/*
 * Registers function +fn+ will be executed when the dom
 * tree is loaded without waiting for images.
 *
 * Example:
 *
 *  Event.domReady.add(function() {
 *    ...
 *  });
 *
 */
Event.domReady = {
  add: function(fn) {

    //-----------------------------------------------------------
    // Already loaded?
    //-----------------------------------------------------------
    if (Event.domReady.loaded) return fn();

    //-----------------------------------------------------------
    // Observers
    //-----------------------------------------------------------
    var observers = Event.domReady.observers;
    if (!observers) observers = Event.domReady.observers = [];
    // Array#push is not supported by Mac IE 5
    observers[observers.length] = fn;

    //-----------------------------------------------------------
    // domReady function
    //-----------------------------------------------------------
    if (Event.domReady.callback) return;
    Event.domReady.callback = function() {
      if (Event.domReady.loaded) return;

      Event.domReady.loaded = true;
      if (Event.domReady.timer) {
        clearInterval(Event.domReady.timer);
        Event.domReady.timer = null;
      }

      var observers = Event.domReady.observers;
      for (var i = 0, length = observers.length; i < length; i++) {
        var fn = observers[i];
        observers[i] = null;
        fn(); // make 'this' as window
      }
      Event.domReady.callback = Event.domReady.observers = null;
    };

    //-----------------------------------------------------------
    // Emulates 'onDOMContentLoaded'
    //-----------------------------------------------------------
    var ie = !!(window.attachEvent && !window.opera);
    var webkit = navigator.userAgent.indexOf('AppleWebKit/') > -1;

    if (document.readyState && webkit) {

      // Apple WebKit (Safari, OmniWeb, ...)
      Event.domReady.timer = setInterval(function() {
        var state = document.readyState;
        if (state == 'loaded' || state == 'complete') {
          Event.domReady.callback();
        }
      }, 50);

    } else if (document.readyState && ie) {

      // Windows IE
      var src = (window.location.protocol == 'https:') ? '://0' : 'javascript:void(0)';
      document.write(
        '<script type="text/javascript" defer="defer" src="' + src + '" ' +
        'onreadystatechange="if (this.readyState == \'complete\') Event.domReady.callback();"' +
        '><\/script>');

    } else {

      if (window.addEventListener) {
        // for Mozilla browsers, Opera 9
        document.addEventListener("DOMContentLoaded", Event.domReady.callback, false);
        // Fail safe
        window.addEventListener("load", Event.domReady.callback, false);
      } else if (window.attachEvent) {
        window.attachEvent('onload', Event.domReady.callback);
      } else {
        // Legacy browsers (e.g. Mac IE 5)
        var fn = window.onload;
        window.onload = function() {
          Event.domReady.callback();
          if (fn) fn();
        }
      }

    }

  }
}
