/***********************************************
* Iniatize functions on document ready
***********************************************/
$(document).ready(function() {	
 resizeImages();
 catalogStyle = "";
 $('#maincats').load("d1.aspx?p258(ct67&s24&fParentCategoryID~1=Q!)[st(ct67*CategoryID%20asc*)]");
 $('#topbrands').load("d1.aspx?p409(ct72)");
 
 $('#searchForm').ajaxForm({
	url: 'd1.aspx',
	type: 'POST',
    target: '#kinetixContent',
	beforeSubmit: validateSearch,
	success: function () {
 		$('#kinetixContent').fadeIn('fast',function(){$('#loading').fadeOut();});
		paging();
		getStyle();
		resizeImages();
	}			   
 });


 $('form a#advanced').click(function () {
      $('span#advanced').slideToggle('slow');
	  $('form a#advanced').toggleClass('active');
 });

 $('select[name=type]').change( function(){
	$('input[@name=dcm_search]').setValue(this.value);
	$('select[name=order]').html("");
	$('select[name=order]').toggle();
	$('select[name=order]').html("<option>Title</option>")
 });
 
  });

function validateSearch(data){
	var searchQuery = data[1].value;
	if(searchQuery.length <= 2 || searchQuery == "Zoeken..."){
		$('input[value="'+searchQuery+'"]').css('color','#F00');
		$('input[value="'+searchQuery+'"]').focus();
		return false;
	}else{
		$('input[value="'+searchQuery+'"]').css('color','#000');
		$('#loading').fadeIn();
	}
}
/***********************************************
* Open images in new window
***********************************************/
function openImages(w,x,y,z) 
{
var url= w;
var hoog='';
if(!x){hoog='550';}else{hoog=x;}
var breed='';
if(!y){breed='800';}else{breed=y;}
var naam= z;
if(z){naam='newWindow';}else{naam=z;}

var n = open(url,naam,'height='+hoog+',innerHeight='+hoog+',width='+breed+',innerWidth='+breed+',screenX=10,left=10,screenY=10,top=10,resizable=1,scrollbars=0');
}

/***********************************************
* Resize Images
***********************************************/
function resizeImages(){
	/*alert("pick me, pick me!");*/
	var img = $('.productphoto'); // load images into array
	var mxI = 120; //set maximum width and height
	for(var i=0;i<img.length;i++){
		if(img[i].width>mxI || img[i].height>mxI){
			var fW = img[i].width / mxI;
			var fH = img[i].height / mxI;
			if(fW>fH){
				img[i].width = img[i].width/fW;
			}else{
				img[i].width = img[i].width/fH;
			}
		}
		if(img[i].width==1 && img[i].height==1){
			img[i].width = 90;	
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", resizeImages);
if (window.addEventListener) window.addEventListener("load", resizeImages, false);

/***********************************************
* Image Swap
***********************************************/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/***********************************************
* Son of Suckerfish dropdown navigation for IE
***********************************************/
sfHover = function() {
	var sfEls = document.getElementById("kinetixToolbar").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);

/***********************************************
* Drop Down/ Overlapping layer
***********************************************/

var dropdowncontent={
 delaybeforehide: 80000, //set delay in milliseconds before content box disappears onMouseout (1000=1 sec)
	disableanchorlink: true, //when user clicks on anchor link, should it be disabled?

	getposOffset:function(what, offsettype){
		return (what.offsetParent)? what[offsettype]+this.getposOffset(what.offsetParent, offsettype) : what[offsettype]
	},

	isContained:function(m, e){
		var e=window.event || e
		var c=e.relatedTarget || ((e.type=="mouseover")? e.fromElement : e.toElement)
		while (c && c!=m)try {c=c.parentNode} catch(e){c=m}
		if (c==m)
			return true
		else
			return false
	},

	show:function(anchorobj, subobj, e){
		if (!this.isContained(anchorobj, e)){
			var horizontaloffset=(subobj.dropposition[0]=="left")? -(subobj.offsetWidth-anchorobj.offsetWidth) : 0 //calculate user added horizontal offset
			var verticaloffset=(subobj.dropposition[1]=="top")? -subobj.offsetHeight : anchorobj.offsetHeight //calculate user added vertical offset
			subobj.style.left=this.getposOffset(anchorobj, "offsetLeft") + horizontaloffset + "px"
			subobj.style.top=this.getposOffset(anchorobj, "offsetTop")+verticaloffset+"px"
			subobj.style.clip=(subobj.dropposition[1]=="top")? "rect(auto auto auto 0)" : "rect(0 auto 0 0)" //hide drop down box initially via clipping
			subobj.style.visibility="visible"
			subobj.startTime=new Date().getTime()
			subobj.contentheight=parseInt(subobj.offsetHeight)
			if (typeof window["hidetimer_"+subobj.id]!="undefined") //clear timer that hides drop down box?
				clearTimeout(window["hidetimer_"+subobj.id])
			this.slideengine(subobj, (subobj.dropposition[1]=="top")? "up" : "down")
		}
	},

	curveincrement:function(percent){
		return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
	},

	slideengine:function(obj, direction){
		var elapsed=new Date().getTime()-obj.startTime //get time animation has run
		if (elapsed<obj.glidetime){ //if time run is less than specified length
			var distancepercent=(direction=="down")? this.curveincrement(elapsed/obj.glidetime) : 1-this.curveincrement(elapsed/obj.glidetime)
			var currentclip=(distancepercent*obj.contentheight)+"px"
			obj.style.clip=(direction=="down")? "rect(0 auto "+currentclip+" 0)" : "rect("+currentclip+" auto auto 0)"
			window["glidetimer_"+obj.id]=setTimeout(function(){dropdowncontent.slideengine(obj, direction)}, 10)
		}
		else{ //if animation finished
			obj.style.clip="rect(0 auto auto 0)"
		}
	},

	hide:function(activeobj, subobj, e){
		if (!dropdowncontent.isContained(activeobj, e)){
			window["hidetimer_"+subobj.id]=setTimeout(function(){
				subobj.style.visibility="hidden"
				subobj.style.left=subobj.style.top=0
				clearTimeout(window["glidetimer_"+subobj.id])
			}, dropdowncontent.delaybeforehide)
		}
	},
	hidediv:function(subobjid){
		document.getElementById(subobjid).style.visibility="hidden"
	},


 init:function(anchorid, pos, glidetime){
		var anchorobj=document.getElementById(anchorid)
		var subobj=document.getElementById(anchorobj.getAttribute("rel"))
		subobj.dropposition=pos.split("-")
		subobj.glidetime=glidetime || 1000
		subobj.style.left=subobj.style.top=0
		anchorobj.onmouseover=function(e){dropdowncontent.show(this, subobj, e)}
		anchorobj.onmouseout=function(e){dropdowncontent.hide(subobj, subobj, e)}
		if (this.disableanchorlink) anchorobj.onclick=function(){return false}
		subobj.onmouseout=function(e){dropdowncontent.hide(this, subobj, e)}
	}
}
/***********************************************
* Attach paging behavior after AJAX
***********************************************/

function paging() { 
 $('.paging').click( function(){
	toLoad = $(this).attr('href');
	toLoad = toLoad.replace(/ /g,"%20");
    $('#loading').fadeIn('normal');
   	$('#kinetixContent').load(toLoad,'',function(){$('#kinetixContent').fadeIn('fast',function(){$('#loading').fadeOut();});
	paging();getStyle();resizeImages();});
	return false;  
 });
}
/***********************************************
* Set Catelog Style
***********************************************/
function setStyle(style){
 $('#catalog').removeClass();
 $('#catalog').addClass(style);
 catalogStyle = style;
}

/***********************************************
* Check if Cookie is already set
***********************************************/
function getStyle(){
	if (catalogStyle != "") {
		setStyle(catalogStyle)
	}else{
		setStyle('detail');
	}
}
