$(document).ready(function() {

	$('#d_be').hide();
	$('#d_fr').hide();
	$('#d_usa').hide();
	$('#d_cn').hide();
	$('#d_jp').hide();

	$('#belgium').bind("click", function(){
		$('#mapContainer').hide();
		$('#d_be').show();
	});
	
	$('#france').bind("click", function(){
		$('#mapContainer').hide();
		$('#d_fr').show();
	});
	
	$('#usa').bind("click", function(){
		$('#mapContainer').hide();
		$('#d_usa').show();
	});
	
	$('#china').bind("click", function(){
		$('#mapContainer').hide();
		$('#d_cn').show();
	});
	
	$('#japan').bind("click", function(){
		$("#mapContainer").hide();
		$('#d_jp').show();
	});
	
	$('.close').bind("click", function(){
		$('#d_be').hide();
		$('#d_fr').hide();
		$('#d_usa').hide();
		$('#d_cn').hide();
		$('#d_jp').hide();
		$('#mapContainer').show();
	});
	
	$("#taalkeuzeForm #language").bind("change", function(){
		if ( $("#language").val() != "") {
			var language =  $("#language").val();
			
			//Devel
			//var url_be = $("#site_be").val();
			//var url_cn = $("#site_cn").val();
			
			//Live
			var url_be_nl = "http://www.cappelle.be/nl/31";
			var url_be_en = "http://www.cappelle.be/en/31";
			var url_be_fr = "http://www.cappelle.be/fr/31";
			var url_cn = "http://www.cappelle.cn/cn/31";
			
			//var url_be_nl = "http://cappelle-be.devel1.duo.be/nl/31";
			//var url_be_en = "http://cappelle-be.devel1.duo.be/en/31";
			//var url_be_fr = "http://cappelle-be.devel1.duo.be/fr/31";
			//var url_cn = "http://cappelle-cn.devel1.duo.be/cn/31";
			
			if(language == "en")
			{
				document.location = url_be_en;
			}
			if(language == "fr")
			{
				document.location = url_be_fr;
			}
			if(language == "nl")
			{
				document.location = url_be_nl;
			}
			if(language == "cn")
			{
				document.location = url_cn;
			}
			
		}
	});
	
	$.validator.addMethod("notdefault", function(value, element) { if(value==element.defaultValue) { return false; } else { return true; } }, "");
	$.validator.messages.required = "";
	$.validator.messages.email = "";
	$("#contactForm").validate();
	$("#loginform").validate();
	$("#forgotpasswordform").validate();
	$("#searchForm").validate();
	
	$('#searchValue').formatInput();
	$('#s_appArea').bind("change", function(){
		populateCategory();
	});	
	
	$('#advSearchLink').bind("click", function(){
		$('#advSearchForm').show();
		return false;
	});
	$('#close').bind("click", function(){
		$('#advSearchForm').hide();
		return false;
	});
	
	$("#navigation>ul>li>a").click(function(){
		if(!$(this).parent().hasClass("active")&&$(this).siblings("ul").length>0){
			if($(this).hasClass("open")){
				$(this).siblings().hide();
				$(this).removeClass("open");
				$(this).addClass("closed");
			}else{
				$(this).siblings().show();
				$(this).removeClass("closed");
				$(this).addClass("open");
			}
			return false;
		}
	});
	
});

$.fn.formatInput = function() {
 return this.focus(function() {
  if( this.value == this.defaultValue ) {
   this.value = "";
  }
 }).blur(function() {
  if( !this.value.length ) {
   this.value = this.defaultValue;
  }
 });
};

function populateCategory() {

	var theForm = this.document.advSearchForm;
	var areas = document.getElementById("s_appArea");
	var categories = document.getElementById("s_suitable");

	var selectedArea =  areas.options[areas.selectedIndex].value;

	if (selectedArea != "0") {  //area selected
		var selectedArray = eval("Array" + selectedArea);
	
		while (selectedArray.length < categories.options.length) {
			categories.options[(categories.options.length - 1)] = null;
		}
		for (var i=0; i < selectedArray.length; i++) {
			eval("categories.options[i]=" + "new Option" + selectedArray[i]);
		}
	}else{
		//empty the category dropdown
		while (categories.options.length > 1) {
			categories.options[(categories.options.length - 1)] = null;
		}
		categories.options[0]= new Option("", "0", true, true);
	}
}


