// JavaScript Document

function init() {
	ClearInputEvents('searchquery');
	ClearInputEvents('newsletter-name');
	ClearInputEvents('newsletter-email');
	ClearInputEvents('dealerlogin-username');
	
	ClearInputEvents('dealerlogin-password-fake',true);
	//SwitchTypes('dealerlogin-password');
	
	ClearInputEvents('emailadres');
	ClearInputEvents('wachtwoord');
	
	ClearInputEvents('forgot_password');
	
	if(window.alphaPNG) alphaPNG();
	
	var promotions_div = document.getElementById('promotions');
	if (promotions_div) {
		detect_flash();
		load_flash('promotions', '/swf/carroussel_v0.6.swf', 439, 195, 9, 'promotions_swf');
	}
	
	init_tooltips();
}



function init_tooltips() {
	tooltips  =$$('a.tooltip');
	for(i=0; i< tooltips.length; i++){
		tooltips[i].onmouseover = function() { showtooltip(this); }
		tooltips[i].onclick = function() { this.blur(); return false; }
	}
}


tt_text_obj = null;
tt_bg_obj = null;
tt_timeoutId = null; 

function showtooltip(obj) {
	closeall();
	tt_text_obj = $('tooltip_'+obj.name);
	tt_bg_obj = $('tooltip_bg');
	pageOffset = findPageOffset();
	objPos = findPos(obj);
	textoffset = [12,18];
	
	/* create a nice sized box */
	tt_text_obj.style.zIndex = 0;
	tt_text_obj.style.display = "block";
	tt_text_obj.style.width = "420px";
	
	w = parseInt($('tooltip_'+obj.name).offsetWidth)
	h = parseInt($('tooltip_'+obj.name).offsetHeight)
	w = Math.ceil(Math.sqrt((h*w)*(4/3)));
	
	tt_text_obj.style.width =  w +"px";
	tt_bg_obj.style.width = (w + 24) + "px";
	tt_bg_obj.style.height = (tt_text_obj.offsetHeight + 36) + "px";
	$('tooltip_text').style.height =  parseInt(tt_bg_obj.style.height) - 6 + "px"
	
	/* put it on a nice place */
		
	if(((objPos[1]-parseInt(tt_bg_obj.style.height)+15) - pageOffset[1]) > 0) {
		$('toplabel').style.display = "none";
		$('bottomlabel').style.display = "block";
		tt_bg_obj.style.left = (objPos[0] - 0) + "px";
		tt_bg_obj.style.top = (objPos[1]-parseInt(tt_bg_obj.style.height)-11) + "px";
	} else {
		$('bottomlabel').style.display = "none";
		$('toplabel').style.display = "block";
		tt_bg_obj.style.left = (objPos[0] - 0) + "px";
		tt_bg_obj.style.top = (objPos[1] + parseInt(obj.offsetHeight)-0) + "px";
	}
	
	tt_text_obj.style.left = parseInt(tt_bg_obj.style.left) + textoffset[0] + "px"
	tt_text_obj.style.top = parseInt(tt_bg_obj.style.top) + textoffset[1] + "px"
			
	tt_bg_obj.style.display = "block";
	tt_text_obj.style.zIndex = 10001;
	tt_bg_obj.style.zIndex = 10000;
	
	tt_bg_obj.onmouseout = function () {if(tt_timeoutId != null) window.clearTimeout(tt_timeoutId); timeoutId = window.setTimeout('hidetooltip()', 1000); }
	tt_bg_obj.onmouseover = function() { window.clearTimeout(tt_timeoutId); }
	tt_text_obj.onmouseover = function() { window.clearTimeout(tt_timeoutId); }
}


function hidetooltip() {
	if(tt_text_obj != null) {
		tt_text_obj.style.display = 'none';
		tt_bg_obj.style.display = 'none';
		tt_text_obj = null ;
	}
	tt_timeoutId = null;
}

function closeall() {
	hidetooltip();
}

/* findPageOffset 
----------------------------------------------------------------*/
function findPageOffset() {
	if(window.pageYOffset) {
		pageOffset =  [parseInt(window.pageXOffset), parseInt(window.pageYOffset)];
	} else {
		pageOffset =  [parseInt(document.documentElement.scrollLeft),  parseInt(document.documentElement.scrollTop)];
	}
	return pageOffset;
}

/* findPos(obj) written by ppk, www.quirksmode.org
----------------------------------------------------------------*/
function findPos(obj) {
	var x = '';
	for (a in obj) x = x + a + ' ';
	x = '';
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		x = curleft;
		while (obj = obj.offsetParent) {
			x = x + ' ' + obj.id + ':' + obj.offsetLeft;
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


function ClearInputEvents(id,changeType) {
	obj = document.getElementById(id);
	
	if (obj) {
		val = obj.value;
		obj.startvalue = val;
		obj.value = val;
		
		if(changeType){
			obj.changetype = 1
			myid = obj.id;
			realId = myid.substr(0,(myid.length-5));
			obj.style.display = "block";
			document.getElementById(realId).style.display = 'none';
		}
		
		obj.onfocus = function() { 
			if(this.value == this.startvalue) { this.value = '';}
			if(this.changetype == 1){
				this.style.display = "none";
				myid = this.id;
				realId = myid.substr(0,(myid.length-5));
				document.getElementById(realId).style.display = 'block';
				document.getElementById(realId).focus();
				
			}
		}
		obj.onblur =  function() { 
			if(this.value == '') { this.value = this.startvalue; }
		}	
	}
}

function laad_measure_in_subselect(measure_what_id, measure_in_id, key) {
	var target_id = 'measure_in_subselect_'+key;
	new Ajax.Updater(document.getElementById(target_id),
							'/ajax/laad_measure_in_subselect.ajax.php',{
							method:'post',
							postBody:'measure_what_id='+measure_what_id+'&measure_in_id='+measure_in_id+'&key='+key,
							onComplete:function() {},
							asynchronous:true,
							evalScripts:true
						});
}

function laad_measure_in_select(measure_what_id, key) {
	var target_id = 'measure_in_select_'+key;
	new Ajax.Updater(document.getElementById(target_id),
							'/ajax/laad_measure_in_select.ajax.php',{
							method:'post',
							postBody:'measure_what_id='+measure_what_id+'&key='+key,
							onComplete:function() {},
							asynchronous:true,
							evalScripts:true
						});
}

/*
function post_send_to_a_friend_form() {
	var target_id = 'measure_in_subselect_'+key;
	new Ajax.Updater(document.getElementById(target_id),
							'/ajax/laad_measure_in_subselect.ajax.php',{
							method:'post',
							postBody:'measure_in_id='+measure_in_id+'&key='+key,
							onComplete:function() {},
							asynchronous:true,
							evalScripts:true
						});
}
*/

function toon_product_toegevoegd_melding(label) {
	var product_toegevoegd = document.getElementById('product_toegevoegd');
	var product_label_span = document.getElementById('product_toegevoegd_label');
	
	if (product_label_span) {
		product_label_span.innerHTML = label;
	}
	
 	if (product_toegevoegd) {
 		product_toegevoegd.style.display = 'block';
 		setTimeout("verberg_toegevoegd_melding()",3000);
 	}
	
	new Effect.Morph('product_toegevoegd',{
  	style:'background:#0076BB; color:#FFFFFF;',duration:0.8});

	setTimeout("verberg_toegevoegd_melding()",3000);
}



function verberg_toegevoegd_melding() {
	new Effect.Morph('product_toegevoegd',{
  	style:'background:#FFFFFF; color:#FFFFFF;',duration:0.7});
  	
  	Effect.toggle('product_toegevoegd', 'blind');
}

function confirm_product_deletion(product_id) {
	
	var confirmed = false; 

	confirmed = window.confirm('Are you sure you want to remove this product from your shoppingcart?');
	
	if (confirmed) {
		location.href='?action=del_product&product_id=' + product_id;
	}
}

/*
function post_staf_form() {
	alert(document.getElementById('staf_div'));
	alert(document.getElementById('staf'));
	new Ajax.Updater(document.getElementById('staf_div'), '/ajax/send_to_a_friend.php', { method:'post', postBody:Form.serialize($("staf"), onComplete:function(){ alert('joehoe'); }, asynchronous:true, evalScripts:true });
}
*/



/* SHORT GENERIC ONLOAD BY BROTHERCAKE
----------------------------------------------------------------*/
if(typeof window.addEventListener != 'undefined') {
	window.addEventListener('load', init, false);
} else if(typeof document.addEventListener != 'undefined') {
	document.addEventListener('load', init, false);
} else if(typeof window.attachEvent != 'undefined') {
	window.attachEvent('onload', init);
}

function img_rollover(id, new_src) {
	var img_to_change = document.getElementById(id);
	
	if (img_to_change) {
		img_to_change.src = new_src;
	}
}
