$(document).ready(function() {

	$(".kleur").change( function () { 
		var optie = $(this).val();
		var product_referentie = $('#product_referentie').val();
		var kleur_colorsuffix = $('#kleur_colorsuffix').val();
		
		var dataString = "optie=" + optie + ",product_referentie=" + product_referentie + ",kleur_colorsuffix=" +kleur_colorsuffix;
		$.ajax({ 
			type: "GET",
			url: "/inc/getimage.dhtml",
			data: dataString,
			async: false,
			success: function(html) {
				var afbeelding = html;
				$('#prodimg').attr("src",html);
			}
		});
		
		var title = $('#title').val();
		var language = $('#language').val();
		
		$.ajax({ 
			type: "GET",
			url: "/inc/getbigimage.dhtml",
			data: dataString,
			async: false,
			success: function(html) {
				var afbeelding = html;
				html = html + ",title=" + title + ",language=" + language;
				$('#prodimgzoom').attr("href",html);
				$('#knopGroter').attr("href",html);
			}
		});
		
	});
	
});




function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(homepage);
addLoadEvent(links);
addLoadEvent(productform);
addLoadEvent(searchform);
addLoadEvent(advancedsearchform);
addLoadEvent(zoomwindow);
addLoadEvent(groep);

function zoomwindow() {
	var theZoomwindow = document.getElementById("zoom");
	if(theZoomwindow) {
		var linkClose = document.getElementById("closeLink");
		if(linkClose) {
			linkClose.onclick=function() {
				window.close();
				return false;
			}
		}
		
		var theImg = document.getElementById("zoomImg");
		if(theImg) {
			var theWidth = theImg.width + 100;
			var theHeight = theImg.height + 160;
			window.resizeTo(theWidth,theHeight);
		}
	}
	
}
function homepage() {
	var theHomePage = document.getElementById("home");
	if(theHomePage) {
		var height = getBrowserHeight();
		if(height < 700){
			var secondNewProduct = document.getElementById("new_products_2nd");
			if(secondNewProduct) {
				secondNewProduct.style.display = "none";
			}
			var secondSalesSpecial = document.getElementById("sales_specials_2nd");
			if(secondSalesSpecial) {
				secondSalesSpecial.style.display = "none";
			}
		}
	}
}

function getBrowserHeight() {
	if (window.innerHeight) {
		return window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight != 0) {
		return document.documentElement.clientHeight;
	} else if (document.body) {
		return document.body.clientHeight;
	}
	return 0;
}

function searchform() {
	var frmSearch = document.getElementById("searchForm");
	if(frmSearch) {
		var q = document.getElementById("q");
		if(q) {
			q.onclick = function() {
				this.select();
			}
		}
	}
	var btnSearch = document.getElementById("searchButton");
	if(btnSearch) {
		btnSearch.onclick = function() {
			frmSearch.submit();
			return false;
		}
	}
}

function advancedsearchform() {
	var frmSearch = document.getElementById("advancedSearchForm");
	var btnSearch = document.getElementById("advancedSearchButton");
	if(btnSearch) {
		btnSearch.onclick = function() {
			frmSearch.submit();
			return false;
		}
	}
}
	
function links() {
	var links = document.getElementsByTagName("a");
	for(i=0;i<links.length;i++) {
		if(links[i].className.indexOf("zoom") >= 0) {
			links[i].onclick=function() {
				popup(this.href, 720, 500, this.title, "yes", "yes");
				return false;
			}
		}
		if(links[i].className.indexOf("deleteFromCart") >= 0) {
			links[i].onclick=function() {
				return confirm('Are you sure you want to remove this product?');
			}
		}
	}
	var setCountryLink = document.getElementById("setCountry");
	if(setCountryLink) {
		setCountryLink.onclick = function () {
			var theForm = document.getElementById("countryForm");
			if(theForm) {
				theForm.submit();
				return false;
			}
		}
	}
	var securityCodeLink = document.getElementById("securityCode");
	if(securityCodeLink) {
		securityCodeLink.onclick=function() {
			popup(this.href, 530, 580, "Popup", "yes", "yes");
			return false;
		}
	}
	var theProductInfo = document.getElementById("productInfo");
	if(theProductInfo) {
		var links = theProductInfo.getElementsByTagName("a");
		for(i=0;i<links.length;i++) {
			links[i].onclick=function() {
				if(this.href.indexOf("popup=Y") >= 0) {
					popup(this.href, 530, 400, "Popup", "yes", "yes");
					return false;
				}
			}
		}	
	}
	var linkHowToReachUs = document.getElementById("howToReachUsLink");
	if(linkHowToReachUs) {
		linkHowToReachUs.onclick=function() {
			popup(this.href, 700, 550, "Popup", "yes", "yes");
			return false;
		}
	}
	var linkPrint = document.getElementById("printlink");
	if(linkPrint) {
		linkPrint.onclick=function() {
			print();
			return false;
		}
	}
	
	var btnSort = document.getElementById("sortButton");
	if(btnSort) {
		btnSort.onclick=function() {
			theForm = document.getElementById("sortform");
			if(theForm) {
				theForm.submit();
			}
			return false;
		}
	}
	
	var btnCheckOut = document.getElementById("buttonCheckOut");
	if(btnCheckOut) {
		btnCheckOut.onclick=function() {
			theForm = document.getElementById("basketCheckoutForm");
			if(theForm) {
				theForm.submit();
			}
			return false;
		}
	}
	
	var btnRecalculate = document.getElementById("buttonRecalculate");
	if(btnRecalculate) {
		btnRecalculate.onclick=function() {
			theForm = document.getElementById("formCart");
			if(theForm) {
				theForm.submit();
			}
			return false;
		}
	}
}

function productform() {
	var formProduct = document.getElementById("productform");
	if(formProduct) {
		var inputs = formProduct.getElementsByTagName("input");
		for(i=0;i<inputs.length;i++) {
			if(inputs[i].type=="radio") {
				inputs[i].onclick = function() {
					var optie_id = this.id;
					var spanPrice = document.getElementById(optie_id+"_price");
					var spanDescription = document.getElementById(optie_id+"_description");
					var productPrice = document.getElementById("productPrice");
					var productDescription = document.getElementById("productDescription");
					if(spanPrice && productPrice) {
						productPrice.innerHTML = spanPrice.innerHTML;
					}
					if(spanDescription && productDescription) {
						productDescription.innerHTML = spanDescription.innerHTML;
					}
				}
			}
		}
	}
	var buttonAddToCart = document.getElementById("addToCart");
	if(buttonAddToCart) {
		buttonAddToCart.onclick = function () {
			var formProduct = document.getElementById("productform");
			if(formProduct) {
				var productPrice = document.getElementById("productPrice");
				if(productPrice) {
					if(productPrice.innerHTML != "on demand") {
						formProduct.submit();
					} else {
						alert("This product is only available on demand.\nYou cannot add it to your shopping cart.\nPlease use the contact form to request more information.");
					}
				}
				return false;
			}
		}
	}	
}

function groep() {
	
	var divCategories = document.getElementById("select");
	var imgGroupPic = document.getElementById("groupPic");
	if(divCategories && imgGroupPic) {
		var originalSrc = imgGroupPic.src;
		var links = divCategories.getElementsByTagName("a");
		for(i=0;i<links.length;i++) {	
			links[i].onmouseover=function() {
				id = this.id.replace("g","");

				gXmlHttp = GetXmlHttpObject();
				if (gXmlHttp.readyState == 4 || gXmlHttp.readyState == 0) {
					gXmlHttp.open("GET", "/inc/categorie_foto.dhtml?ajax=Y,item="+id, true);
					gXmlHttp.send(null);
					gXmlHttp.onreadystatechange = function() {
						if (gXmlHttp.readyState == 4) {
							imgGroupPic.src=gXmlHttp.responseText;
						}
					}
				}		
			}
		}
	}	
}

function popup(url, width, height, name, resizable, scrollbars) {

	pWidth = width;
	pHeight = height;
	
	pTop = Math.ceil((screen.height-pHeight*1.5)/2);
	pLeft = Math.ceil((screen.width-pWidth)/2);
	
	if(resizable != "") {
		pResizable = resizable;
	} else {
		pResizable = "no";
	}
	
	if(scrollbars != "") {
		pScrollbars = scrollbars;
	} else {
		pScrollbars = "no";
	}
	
	if(name != "") {
		pName = name;
	} else {
		pName = "Popup";
	}
	
	winopts = "toolbar=0,location=0,directories=0,status=0,menubar=0,copyhistory=0,scrollbars="+pScrollbars+",resizable="+pResizable+",width="+pWidth+",height="+pHeight+",top="+pTop+",left="+pLeft;
	smallwindow=window.open(url, pName, winopts);
	smallwindow.focus();
}


function changeAdvSearchUnit(href) {
	var theForm = document.advancedSearchForm;
	theForm.dont_convert.value = "Y";
	theForm.action = href;
	theForm.submit();
}



var gXmlHttp = null;

function GetXmlHttpObject() {
	if (gXmlHttp == null) {
		var localXmlHttp = null;
		try {
			// Firefox, Opera 8.0+, Safari
			localXmlHttp=new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer
			try {
				localXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				localXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		
		return localXmlHttp;
	} else {
		return gXmlHttp;
	}
}
