/*
 * 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
*/ 

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);
		}
	}

}

winkelwagen.addProduct = function(k,frm)
{
	var arr =$(frm).getInputs('text');
	arr.each(function(item) {
      if (item.defaultValue == item.value && item.name != 'paantal' )
      	item.value = '';
	});
	 
	
	var pars = Form.serialize(frm)+"&winkelwagen=addProduct";
	winkelwagen.submitFunction(pars,null,function(resp,res) {  document.location.href='/winkelwagen'; } );
}


function start_slideshow(slidebox, delay) {
	
	var slides = $$("."+slidebox)[0].select('li');
	slides.each(function(s,i){
		$(s).setStyle({
			position:'absolute', 
			opacity:(i === 0 ? 1 : 0),
			visibility:'visible',
			display:(i === 0 ? 'block' : 'none')
		});
	});     
	setTimeout(switch_slides(slides,0,delay), delay);
}
						
function switch_slides(slides,current, delay) {
	return (function() {
		var nextFrame = (current == slides.length-1)?0:current+1;
		new Effect.Parallel([
			new Effect.Fade(slides[current] ,{sync:true}),
            new Effect.Appear(slides[nextFrame],{sync:true})
			],
			{
				duration: 2, 
				queue : 'Crossfade', 
				afterFinish:function() 
				{
					setTimeout(switch_slides(slides,nextFrame,delay), delay);
				}
			});	
	})
}

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;
	if (document.location.hash == '' && fep.hash=='' && parseInt(page) == 0)
	{
		
	} else {
		if ( isNaN(parseInt(page)) )
		{
			fep.hash = '';
		} else {
			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) {  res.responseText.evalScripts(); if (focusprod!=0) { var el = document.getElementById('prod_'+focusprod); Element.scrollTo(el);  focusprod = 0; }  }
	});	
	
	if (!fep.skipscroll)
	{
		Effect.ScrollTo('contentDiv', {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).up('li').removeClassName('selected');
		$(a).innerHTML="";
	} else {
		$(a).up('li').addClassName('selected');
		$(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;
}

function addFilter(type,id,text)
{
	return "<li><a href='#' onclick=\"$('opt["+type+"]_"+id+"').click(); return false;\">"+text+"</a></div>";
}
function createfilterlist()
{
	var container = $('filters');
	if (container == null)
		return;
	container.innerHTML = '';
	var obsj = Form.serialize('catoptions',true);
	var i = 0;
	
	container.innerHTML += createlistfor(obsj,'5');
	container.innerHTML += createlistfor(obsj,'3');
	container.innerHTML += createlistfor(obsj,'2');
	container.innerHTML += createlistfor(obsj,'1');
	container.innerHTML += createlistfor(obsj,'cat');
	container.innerHTML += createlistfor(obsj,'prijs');
	if (container.innerHTML == '')
	{
		$('filterkeuze').style.display='none';
	}
	else
	{
		container.innerHTML = '<ul>'+container.innerHTML+'</ul>';	
		$('filterkeuze').style.display='block';
	}
}

function createlistfor(obsj,type)
{
        var i = 0;
		var cont = "";
		
        if (obsj['opt['+type+'][]'])
        {
			
                if (typeof(obsj['opt['+type+'][]']) == "string")
                        obsj['opt['+type+'][]'] = [obsj['opt['+type+'][]']];
				
                for (i=0;i<obsj['opt['+type+'][]'].length;i++)
                {
                        var merkid = obsj['opt['+type+'][]'][i];
                        var merk = $('opt['+type+']_'+merkid).next('label').innerHTML;
                        cont += addFilter(type,merkid,merk);
                }
        }
		
	return cont;
}


