/*
 * 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.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;
	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) { 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';  } );	 // 
}
fep.previd = null;
fep.setNewZoom = function(id,obj)
{
	var zoom = document.getElementById('zoom');

	MagicZoom.update(zoom, '/img/product/'+id+'/abc.jpg', '/img/product285/'+id+'/abc.jpg', 'selectors-effect: dissolve');
	
	var xid = $F(obj);
	var idx = obj.options[0].value;
	
	if (fep.previd==null )
	{
		
		var x = $('thumbs_'+idx);
		if (x);
			x.style.display='none';
	}
	if (fep.previd != null)
	{
		if ($('thumbs_'+fep.previd) != null)
			$('thumbs_'+fep.previd).style.display='none';
	}
	
	
	if ($('thumbs_'+xid) != null)
	{
		$('thumbs_'+xid).style.display='block';
	}
	fep.previd = xid;
	
}
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);
                }
        }

}
function addFilter(type,id,text)
{
	return "<div class='filteroptie'>"+text+" <a href='#' onclick=\"$('"+type+"_"+id+"').click(); return false;\">X</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,'maat');
	container.innerHTML += createlistfor(obsj,'merk');
	container.innerHTML += createlistfor(obsj,'kleur');
	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;
}

