// protoHover
// a simple hover implementation for prototype.js
// Sasha Sklar and David Still

(function() {
	// copied from jquery
	var withinElement = function(evt, el) {
		// Check if mouse(over|out) are still within the same parent element
		var parent = evt.relatedTarget;

		// Traverse up the tree
		while (parent && parent != el) {
			try {
				parent = parent.parentNode;
			} catch (error) {
				parent = el;
			}
		}
		// Return true if we actually just moused on to a sub-element
		return parent == el;
	};

	// Extend event with mouseEnter and mouseLeave
	Object.extend(Event, {
		mouseEnter: function(element, f, options) {
			element = $(element);

			// curry the delay into f
			var fc = (options && options.enterDelay)?(function(){window.setTimeout(f, options.enterDelay);}):(f);

			if (Prototype.Browser.IE) {
				element.observe('mouseenter', fc);
			} else {
				element.hovered = false;

				element.observe('mouseover', function(evt) {
					// conditions to fire the mouseover
					// mouseover is simple, the only change to default behavior is we don't want hover fireing multiple times on one element
					if (!element.hovered) {
						// set hovered to true
						element.hovered = true;

						// fire the mouseover function
						fc(evt);
					}
				});
			}
		},
		mouseLeave: function(element, f, options) {
			element = $(element);

			// curry the delay into f
			var fc = (options && options.leaveDelay)?(function(){window.setTimeout(f, options.leaveDelay);}):(f);

			if (Prototype.Browser.IE) {
				element.observe('mouseleave', fc);
			} else {
				element.observe('mouseout', function(evt) {
					// get the element that fired the event
					// use the old syntax to maintain compatibility w/ prototype 1.5x
					var target = Event.element(evt);

					// conditions to fire the mouseout
					// if we leave the element we're observing
					if (!withinElement(evt, element)) {
						// fire the mouseover function
						fc(evt);

						// set hovered to false
						element.hovered = false;
					}
				});
			}
		}
	});


	// add method to Prototype extended element
	Element.addMethods({
		'hover': function(element, mouseEnterFunc, mouseLeaveFunc, options) {
			options = Object.extend({}, options) || {};
			Event.mouseEnter(element, mouseEnterFunc, options);
			Event.mouseLeave(element, mouseLeaveFunc, options);
		}
	});
})();

/*
Script om div tabbladen te laten zien
*/
function ToggleDiv(elementId)
{
    var el = document.getElementById(elementId);
    if (el.style.display == 'none')
    { el.style.display = 'block'; }
    else
    { el.style.display = 'none'; }
}
/*
 * Clear Default Text: functions for clearing and replacing default text in
 * <input> elements.
 *
 * by Ross Shannon, http://www.yourhtmlsource.com/
 */


Event.observe(window, 'load', init);


function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
            /* Add event handlers */          
	    Event.observe(theInput,'focus', clearDefaultText);
		Event.observe(theInput,'blur', replaceDefaultText);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}
/*
EINDE CLEAR TEXT SCRIPT
*/


winkelwagen.addProductEx = function(pid)
{
	var pars = "winkelwagen=addProduct&paantal=1&pid="+pid;
	winkelwagen.submitFunction(pars,null,function(resp,res) { document.location.href='/index.php?p=winkelwagen'; } );
}

winkelwagen.addProduct = function(pid,frm)
{
	
	var knop = $(k);
	if (knop == null)
	{

		return;	
	}
	if (winkelwagen.disabledbuttons[knop.id] != undefined)
	{
		return;
	}
	winkelwagen.setLoading(knop.id);

	
	
	var pars = Form.serialize(frm)+"&winkelwagen=addProduct";
	winkelwagen.submitFunction(pars,null,function(resp,res) { winkelwagen.stopLoading(knop.id); } );

}
winkelwagen.wwdiv = "wwcdiv";

winkelwagen.setWinkelwagenContents = function(arr)
{
	$(winkelwagen.wwdiv).innerHTML = "";
	var output = "";
//	var txt = trans['wwcontents'];
				var txt = "<p>"+trans['voorpagina_winkelwagen_inhoud']+": #x# "+trans['voorpagina_winkelwagen_artikelen']+"</p>";
				txt += "<p>"+trans['voorpagina_winkelwagen_totaal']+": &euro; #y#</p>";
	
var tprijs = 0.00;
var tproducten = 0;
  for (var key in arr) 
  {
  tproducten++;
  	
  	var pr = arr[key].totaal.replace(",",".");
  	
  	tprijs += parseFloat(pr);
   	// arr[key].naam
   	// arr[key].id
   	// arr[key].aantal
   	// output += "<a href='/"+currTaal+"/p/"+arr[key].id+"/product.html'>"+winkelwagen.verkort(arr[key].naam,22)+"</a><br/>";
  } 
  var tprijsx = Math.round(tprijs*100)/100;
  tprijsx += "";
  var tpx = tprijsx.replace(".",",");
output = txt.replace("#x#", tproducten);
output = output.replace("#y#", tpx);


  
  $(winkelwagen.wwdiv).innerHTML = output; 		
}

winkelwagen.stopLoading = function(knop)
{
	
	$(knop).innerHTML=getTrans('toegevoegd');
	
	
	winkelwagen.updateWwdiv();
	
	
	
	setTimeout("$('"+knop+"_span').innerHTML=winkelwagen.disabledbuttons['"+knop+"'];winkelwagen.disabledbuttons['"+knop+"'] = null;",1500);
} 
winkelwagen.setLoading = function(knop)
{
	
	winkelwagen.disabledbuttons[knop] = $(knop+"_span").innerHTML;
	$(knop).value= getTrans('bezigmettoevoegen');
}

var global = { };

account.saveNewformBedrijf = function(frm)
{
	var pars = Form.serialize(frm);
	account.submitFunction(pars+"&account=nieuweKlant",null,account.saveNewReplyBedrijf);		
}
account.saveNewReplyBedrijf = function(resp,rep)
{
	if (resp.responseText.indexOf('<script') == 0)
	{
		resp.responseText.evalScripts();
		return;
	}
	account.clearErrors();

  $('aanmelding').hide();
  $('aanmeldingok').show();
  	
}    
AIM = {
	rdiv : "",
	frame : function(c) {

		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);

		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}

		return n;
	},

	form : function(f, name) {
		f.setAttribute('target', name);
	},

	submit : function(f, c, resdiv) {
		AIM.rdiv = resdiv;
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}

		if (typeof(i.onComplete) == 'function') {
			// var resp = new dummyResponse(d.body.innerHTML);
			var resp = new Object()
			
			resp.responseText = d.body.innerHTML;
			i.onComplete(resp,AIM.rdiv);
		}
	}

}

global.displayAttribuutText = function(obj,txtobj)
{
	if (obj.className == "text")
	{
		$(txtobj).style.display='';
		$(txtobj).value = $(txtobj).defaultValue;
		$(txtobj).focus();
		$(txtobj).select();
	} else {
		$(txtobj).style.display='none';
		$(txtobj).value = '';
	}
}
account.loginReply = function(resp,re)
{
	if (resp.responseText=="FOUT")
	{
		alert("Ongeldige login");
		document.location.href="index.php?p=account&page=login";
	} else if (resp.responseText.substring(0,5) == "REDIR") {
		document.location.href = resp.responseText.substring(5).unescapeHTML();
	} else {
		alert(resp.responseText);
	}
}

Event.observe( window, 'load', function() {


var visibleList = { };
$$('div.besteldiv').each(function(obj) {
		var elid = $(obj).id;
		elid = elid.substring(elid.indexOf('_')+1);
		$('idiv_'+elid).hover(
    function() {
    	
    },
    function() {
    	if (visibleList[elid] != undefined && visibleList[elid] != null)
    	{
    		visibleList[elid] = null;
      	Effect.Fade('bestel_'+elid,{duration:0.2});
      }
    },
    {
       enterDelay: 0,
       leaveDelay: 500
    }
  );
	 Event.observe($('idivk_'+elid),'click', 
		function() {
	    	if (visibleList[elid] == undefined || visibleList[elid] != null) 
	    	{
	    		visibleList[elid] = 'bestel_'+elid;
	      	Effect.Appear('bestel_'+elid,{duration:0.2});
	      } else {
	    		visibleList[elid] = null;
	      	Effect.Fade('bestel_'+elid,{duration:0.2});      	
	      }		
		});
	}); 

} );







var ctab = null;
var tknop = null;
function openTab(tid,scr,knop)
{
	if (ctab != null)
	{
		$(tknop).removeClassName('active');
		Effect.Puff($(ctab),{duration:0.4});
		
	}
	if (ctab == tid)
	{
		ctab = null;
		tknop == nul;
		return;
	}
	if ($(tid).style.display=='none')
	{
		Effect.BlindDown(tid,{duration:0.4} ); 
		$(knop).addClassName('active');
		if (scr)
			Effect.ScrollTo('notificatieBox',{duration:0.4});	
	}
	ctab = tid;
	tknop = knop;
}


function verstuurNotificatie()
{
/*	var pars = Form.serialize($('gf'));
	var myAjax = new Ajax.Request(
		'/data.php', 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: verwerkBericht
		})	*/
		$('knop').disabled=true;
}	
function verwerkNotificatie(resp)
{
	if (resp.responseText=="OK")
	{
		$('pndiv').hide();
		$('pnmelding').show();
	} else {
		alert(resp.responseText);
	}
}	
function removeCompareResponse(resp,id)
{
	if (resp.responseText!="OK")
		return;	
	// comp_pid compx_pid
	$('compx_'+id).hide();
	if ($('vergelijkbox').getElementsByTagName('LI').length == 2)
	{
		$('compx_'+id).remove(); 
		$('comp_'+id).remove();
		$('comp1_'+id).remove();
		$('comp2_'+id).remove();
		Effect.Shrink($('vergelijkbox'),{ duration:0.3  });
	} else {
		Effect.Fade($('comp_'+id),{ duration:0.3, afterFinish: function() {  $('compx_'+id).remove(); $('comp_'+id).remove(); $('comp1_'+id).remove(); $('comp2_'+id).remove();   }  });
	}	
}
function removeCompare(id)
{
	
		var pars = "s=deletecompare&pid="+id;
		var myAjax = new Ajax.Request(
			'/data.php', 
			{
				method: 'post', 
				parameters: pars, 
				onComplete: function(resp) { removeCompareResponse(resp,id); }
			}
		);	
	

	
}
function addCompareResponse(resp,id,name)
{
	if (resp.responseText!="OK")
		return;
		
	var a = document.createElement("LI");
	a.id = 'comp_'+id;
	a.innerHTML = name;
	var b = document.createElement("LI");
	b.id = 'compx_'+id;
	b.className = "last";
	b.innerHTML = "<a href='#' onclick='removeCompare("+id+"); return false;'>verwijder</a>";
	a.style.display='none';
	b.style.display='none';
	var ulx = $('compareul');
	ulx.appendChild(a);
	ulx.appendChild(b);
	if ($('vergelijkbox').style.display=='none')
	{
		a.style.display='';
		b.style.display='';		
		Effect.BlindDown($('vergelijkbox'),{ duration:0.6 });
	}	else {	
		Effect.Appear( $(a.id), { afterFinish: function() { $(b.id).show(); } } );
	}
}

function addCompare(id,name)
{
		var pars = "s=addcompare&pid="+id;
		var myAjax = new Ajax.Request(
			'/data.php', 
			{
				method: 'post', 
				parameters: pars, 
				onComplete: function(resp) { addCompareResponse(resp,id,name); }
			}
		);
}

function SetCookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/



document.cookie = name + "=" +escape( value ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
