var activeSlide = -1;
var mouseIsActive = false;
var slideEffect = null;
var slideTime = 1500;
function showSlide(i)
{
	clearTimeout(slideTimerObj);
	hideSlide();
	
	if ($$('#knop'+i+' a').length == 0)
		i = 1;
	$$('#knop'+i+' a')[0].addClassName('selected');
	var l = ((i-1)*277*-1);
	
	if (slideEffect!=null)
		slideEffect.cancel();
	
	slideEffect = new Effect.Move($('slidebox'), { x:l, y: 0, mode: 'absolute', duration:0.7, afterFinish: function() { slideEffect=null;} });
	
	// $('slidebox').setStyle({left: l+'px'});
	
	activeSlide = i;
	if (!mouseIsActive)
		slideTimer();
}
function showNextSlide()
{
	showSlide(activeSlide+1);
}

var slideTimerObj = null;
function slideTimer()
{
	clearTimeout(slideTimerObj);
	slideTimerObj = setTimeout("showNextSlide()",slideTime);
}
function exitSlide()
{
	mouseIsActive = false;
	slideTimer();
}
function hideSlide()
{
	if (activeSlide==-1)
		return;
		
	$$('#knop'+activeSlide+' a')[0].removeClassName('selected');
	activeSlide = -1;
}

function doMenuArrow(i)
{
	
	if ($('ul-'+i).style.display!='none')
	{
		
		Element.addClassName($('catlink'+i).parentNode,'selected'); 
	}
	else 
	{
	
		Element.removeClassName($('catlink'+i).parentNode,'selected'); 
	} 
}

function moveProductSlide(obj,w)
{
	
	var o = $(obj);
	if (o.slideEffect!=null)
		o.slideEffect.cancel();
		
	var wi = $(obj).getWidth();
	var offset = parseInt($(obj).style.left);
	if (isNaN(offset))
		offset = 0;
	
	
	wx = Math.abs(w)*2;
	
	if (Math.abs(offset) + (w<0?wx:-wx) >= wi)
	{
		$(o.onext).setStyle({display:'none'});
	} else {
		$(o.onext).setStyle({display:'block'});
	}
	
	if (Math.abs(offset) + (w<0?wx:-wx) < 0)
	{
		$(o.oprev).setStyle({display:'none'});
	} else {
		$(o.oprev).setStyle({display:'block'});
	}	
	
	o.slideEffect = new Effect.Move($(obj), { x:w, y: 0, mode: 'relative', duration:0.4, afterFinish: function() { o.slideEffect=null;} });
}
function initProductSlide(obj,offset)
{
	var pr = $$('#'+obj+' a.prev')[0];
	var ne = $$('#'+obj+' a.next')[0];
	var cont = $$('#'+obj+' ul')[0];
	cont.setStyle({position: 'relative'});
	cont.slideEffect = null;
	cont.onext = ne;
	cont.oprev = pr;
	$(cont.oprev).setStyle({display:'none'});
	var wi = offset;
	pr.observe("click", function() {
		moveProductSlide(cont,wi);
	});
	ne.observe("click", function() {
		moveProductSlide(cont,wi*-1);
	});	
}

winkelwagen.addProduct = function(k,frm)
{

        var arr =$(frm).getInputs('text');
   arr.each(function(item) {
      if (item.defaultValue == item.value)
        item.value = '';
   });


        var pars = Form.serialize(frm)+"&winkelwagen=addProduct";
        winkelwagen.submitFunction(pars,null,function(resp,res) {  document.location.href='/cart'; } );

}


var fep = {};

fep.currentMenu = null;

fep.catShowMenu = function(o)
{
	
	if (fep.currentMenu!=null && fep.currentMenu != o)
	{
		Effect.Fade(fep.currentMenu, { duration: 0.2 });
		Event.stopObserving(document, 'mousedown', fep.documentClick);		
	}
	fep.currentMenu = o;
	
	if (fep.currentMenu == null)
		return;
		
	Effect.Appear(fep.currentMenu, {duration: 0.3 });

	Event.observe(document, 'mousedown', fep.documentClick);		
} 

fep.documentClick = function(event)
{
	var element = $(Event.element(event));
	var cm = $(fep.currentMenu).parentNode;
	if (element.descendantOf(cm))
		return;
	fep.catShowMenu(null);
}

fep.catOptionChanged = function(resetPage)
{
	fep.catShowMenu(null);
	fep.loadCatPage(0);
}
fep.hash = location.hash;
fep.skipscroll = true;
var focusprod = 0;
fep.loadCatPage = function(page)
{
	createfilterlist();
	$('pagenr').value = page;
	
	document.location.hash = parseInt(page)+1;
	fep.hash = document.location.hash;
	var pars = Form.serialize('catoptions');
	$('contentDiv').innerHTML="<img src='/images/loading.gif' style='margin-left:390px; margin-top:100px;' />";
	if (focusprod!=0)
		fep.skipscroll = true;
	new Ajax.Updater('contentDiv', '/data.php', {
	  parameters:  pars,
	  onComplete: function(res) { if (focusprod!=0) { var el = document.getElementsByName('p'+focusprod)[0]; Element.scrollTo(el);  focusprod = 0; }  }
	});	
	
			if (!fep.skipscroll)
			{
				Effect.ScrollTo('wrapper', {duration:0.5} );
				
			}	 else {
				fep.skipscroll =false;
			}	
	
}

fep.updateAantal = function(a,b)
{
	var obs = document.getElementsByName(b);
	var x = 0;
	for (i=0;i<obs.length;i++)
		if (obs[i].checked)
			x++;
	if (x == 0)
	{
		$(a).innerHTML="";
	} else {
		$(a).innerHTML="("+x+")";
	}
}
fep.addOneItem = function(id,aantal)
{
	var pars = "pid="+id+"&paantal="+aantal+"&s=addExtraProduct";
	winkelwagen.submitFunction(pars,null,function(resp,res) { document.location.href='/index.php?p=winkelwagen';  } );
}
fep.busy =false;
fep.resetOptions = function(n,o)
{
	if (fep.busy)
		return;
	fep.busy = true;
	var obs = document.getElementsByName(n);
	for (i=0;i<obs.length;i++)
	{
		if (obs[i].hasClassName('active') && obs[i].value == o.value )
		{
			o.removeClassName('active');
			o.checked = false;
			continue;
		}
		if (obs[i].value != o.value)
		{
			obs[i].checked = false;
			obs[i].removeClassName('active');
		}
	}
	$(o).addClassName('active');
	fep.busy =false;
}

fep.addItem = function()
{
	 
 	var pars = Form.serialize('pform')+"&s=addProduct";
	winkelwagen.submitFunction(pars,null,function(resp,res) { document.location.href='/index.php?p=winkelwagen';  } );	 // 
}


function addFilter(type,id,text)
{
	return "<li>"+text+" <a href='#' onclick=\"$('"+type+"_"+id+"').click(); return false;\">X</a></li>";
}
function createfilterlist()
{
	var container = $('filters');
	if (container == null)
		return;
	container.innerHTML = '';
	var obsj = Form.serialize('catoptions',true);
	var i = 0;
	container.innerHTML += createlistfor(obsj,'certificaat');
	container.innerHTML += createlistfor(obsj,'merk');
	
	container.innerHTML += createlistfor(obsj,'prijs');
	if (container.innerHTML == '')
		$('filterkeuze').style.display='none';
	else
		$('filterkeuze').style.display='block';
}

function createlistfor(obsj,type)
{
        var i = 0;
	var cont = "";
        if (obsj[type+'[]'])
        {
                if (typeof(obsj[type+'[]']) == "string")
                        obsj[type+'[]'] = [obsj[type+'[]']];
                for (i=0;i<obsj[type+'[]'].length;i++)
                {
                        var merkid = obsj[type+'[]'][i];
                        var merk = $(type+'_'+merkid).next('label').innerHTML;
                        cont += addFilter(type,merkid,merk);
                }
        }
	return cont;
}

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.addProductWw = function(pid)
{
	var pars = "winkelwagen=addProduct&paantal=1&pid="+pid+"&src=winkelwagen";
	winkelwagen.submitFunction(pars,null,function(resp,res) { document.location.href='/index.php?p=winkelwagen'; } );
}


function updatePrijs(opt)
{
	var prijs = Element.readAttribute(opt,'prijs');
	var aanbieding = Element.readAttribute(opt,'aanbieding');
	var aprijs = Element.readAttribute(opt,'aprijs');
	var target = $('prijsspan');
	if (aanbieding == 'y')
	{
		target.innerHTML = "<span>&euro; "+prijs+"</span>&euro; "+aprijs;
	} else {
		target.innerHTML = "&euro; "+prijs;
	}
}

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);
		}
	}

}
