$(document).ready(function(){
	
	////
	// Lang
	////
	var langHandlers	=	{
		nl:	function ( ) { return false; },
		en:	function ( ) { return false; }
	};
	
	if (lang === "nl") {
		langHandlers.en	=	function ( ) {
			if (/\/nl\//.test(window.location.href)) {
				window.location		=	window.location.href.replace("/nl/", "/en/");
				return false;
			} else {
				return true;
			}
		}
	} else {
		langHandlers.nl	=	function ( ) {
			if (/\/en\//.test(window.location.href)) {
				window.location		=	window.location.href.replace("/en/", "/nl/");
				return false;
			} else {
				return true;
			}
		}
	}
	
	$(".langNL").click(langHandlers.nl);
	$(".langEN").click(langHandlers.en);
	
	////
	// Menu
	////
	var menu	=	$("ul.menu");
	
	menu.find("li:has(ul)").hover(
		function(){
			$("ul", this).show();
		},
		function(){
			$("ul", this).hide();
		}
	);
	
	////
	// Search box widgets
	////
	var searchBox	=	$(".search");
	
	if(searchBox[0]){
		
		var searchBox_change	=	function(){
			
			var type	=	searchBox.find("select[name=type]").val();
			var salesRental	=	searchBox.find("input[name=koophuur]:checked").val();
			
			searchBox.find("select[name=plaats]").attr({"name": ""}).hide();
			searchBox.find(".plaatsen_"+type+"_"+salesRental).attr({"name": "plaats"}).show();
			
			searchBox.find("select[name=prijs_min]").attr({"name": ""}).hide();
			searchBox.find(".prijs_min_"+salesRental).attr({"name": "prijs_min"}).show();
			
			searchBox.find("select[name=prijs_max]").attr({"name": ""}).hide();
			searchBox.find(".prijs_max_"+salesRental).attr({"name": "prijs_max"}).show();
			
			searchBox.attr({"action": searchBox.find(".url_"+type).text()});
			
		}
		
		searchBox.find("select[name=type], input[name=koophuur]").change(function(){ searchBox_change() });
		
		searchBox_change();
		
	}
	
	////
	// New object widgets
	////
	
	// Slide
	var newObjects	=	$(".new_objects.slide");
	
	if(newObjects[0]){
		
		var newObjects_slider	=	function(el){
		
			var self		=	this;
			self.el			=	el;
			self.elements		=	0;
			self.elementWidth	=	self.el.find(".new_object:first").width();
			self.pos		=	0;
			
			self.el.find(".new_object").each(function(){
			
				self.elements++;
			
			});
			
			self.maxPos		=	-((self.elements * self.elementWidth)-self.elementWidth);
			
			self.slide		=	function(){
				self.pos	-=	self.elementWidth;

				if(self.pos < self.maxPos){
				
					self.pos	=	0;
				
				};
				
				self.el.find(".slider").animate({left: self.pos+"px"}, 200);
				
				setTimeout(function(){ self.slide() }, 10000);
				
			};
			
			setTimeout(function(){ self.slide() }, 10000);
		
		}
		
		newObjects.each(function(){
			new newObjects_slider($(this));
		});
		
	}
	
	// Fade
	var newObjects	=	$(".new_objects.fade");
	
	if(newObjects[0]){
		
		var newObjects_fader	=	function(el){
		
			var self		=	this;
			self.el			=	el;
			self.elements		=	0;
			self.elementWidth	=	self.el.find(".new_object:first").width();
			self.elementCurrent	=	0;
			
			self.el.find(".new_object").each(function(){
			
				self.elements++;
			
			});
			
			self.fade		=	function(){
				
				var elementNext	=	(self.elementCurrent+1);
				
				if((self.elementCurrent+1) >= self.elements){
				
					elementNext	=	0;
				
				};
				
				self.el.find(".new_object:eq("+self.elementCurrent+")").fadeOut(
					200,
					function(){
						self.el.find(".new_object:eq("+elementNext+")").fadeIn(200);
					}
				);
				
				if((self.elementCurrent+1) >= self.elements){
				
					self.elementCurrent	=	0;
				
				}else{
					self.elementCurrent++;
				}
				
				setTimeout(function(){ self.fade() }, 10000);
				
			};
			
			setTimeout(function(){ self.fade() }, 10000);
		
		}
		
		newObjects.each(function(){
			new newObjects_fader($(this));
		});
		
	}
	
	////
	// Galleries
	////
	var gallery	=	$(".gallery");
	
	if(gallery[0]){
		
		var Gallery	=	{
			active:		0,
			first:		0,
			last:		-1,
			top:		0,
			bottom:		2,
			speed:		80,
			thumb:		{
				"selector":	".thumb",
				"height":	0
			},
			photo:		{ "selector":	".photo img" },
			slider:		{ "selector":	".thumbs .slider" },
			previous:	{ "selector":	".up, .navigatie .vorige" },
			next:		{ "selector":	".down, .navigatie .volgende" },
			counter:	{ "selector":	".onderschrift span" },
			init:			function(){
				// Add active class to first image
				$(Gallery.thumb.selector+":first, "+Gallery.thumb.selector+":first img").addClass("active");
				$(Gallery.thumb.selector).each(function(){
					Gallery.last++;
					$(this).attr({"rel": Gallery.last});
				});
				Gallery.thumb.height	=	$(Gallery.thumb.selector).outerHeight(true);
				Gallery.update_counter();
				Gallery.activate_deactivate();
			},
			up:			function(){
				if(Gallery.top > Gallery.first && Gallery.active == Gallery.top + 1){
					$(Gallery.slider.selector).animate({"top": "+="+Gallery.thumb.height});
					Gallery.top--;
					Gallery.bottom--;
					Gallery.active--;
				}else if(Gallery.active > Gallery.first){
					Gallery.active--;
				}
				Gallery.change_photo();
			},
			down:			function(){
				if((Gallery.bottom < Gallery.last) && Gallery.active == Gallery.bottom - 1){
					$(Gallery.slider.selector).animate({"top": "-="+Gallery.thumb.height});
					Gallery.top++;
					Gallery.bottom++;
					Gallery.active++;
				}else if(Gallery.active < Gallery.last){
					Gallery.active++;
				}
				Gallery.change_photo();
			},
			click:			function(current){
				var active	=	parseInt($(current).attr("rel"));
				Gallery.active	=	active;
				if(active == Gallery.top){
					Gallery.active++;
					Gallery.up();
				}else if(active == Gallery.bottom){
					Gallery.active--;
					Gallery.down();
				}else{
					Gallery.active	=	active;
					Gallery.change_photo();
				}
			},
			change_photo:		function(){
				// Add/remove active class
				$(Gallery.thumb.selector+".active, "+Gallery.thumb.selector+".active img").removeClass("active");
				$(Gallery.thumb.selector+"[rel="+Gallery.active+"], "+Gallery.thumb.selector+"[rel="+Gallery.active+"] img").addClass("active");
				$(Gallery.photo.selector).attr({src: $(Gallery.thumb.selector+"[rel="+Gallery.active+"] img").attr("src")});
				Gallery.update_counter();
				Gallery.activate_deactivate();
			},
			update_counter:		function(){
				if(Gallery.last == -1){
					$(Gallery.counter.selector).text("geen fotos");
				}else{
					$(Gallery.counter.selector).text("foto "+(Gallery.active+1)+" van "+(Gallery.last+1));
				}
			},
			activate_deactivate:	function(){
				if(Gallery.active <= Gallery.first){
					$(Gallery.previous.selector).addClass("prev_disabled");
				}else{
					$(Gallery.previous.selector).removeClass("prev_disabled");
				}
				if(Gallery.active >= Gallery.last){
					$(Gallery.next.selector).addClass("next_disabled");
				}else{
					$(Gallery.next.selector).removeClass("next_disabled");
				}
			},
			tb:			function(){
				tb_show("", $(Gallery.photo.selector).attr("src"), "");
			}
		}
		
		Gallery.init();
	
		$(".up, .controls .vorige").click(function(){
			
			Gallery.up();
			return(false);
		
		});
		
		$(".down, .controls .volgende").click(function(){
			
			Gallery.down();
			return(false);
		
		});
	
		$(".controls .vergroting").click(function(){
			
			Gallery.tb();
			return(false);
		
		});
	
		$(".thumb").click(function(){
			
			Gallery.click(this);
			return(false);
		
		});
		
	}
	
	////
	// Tabs
	////
	var tabs	=	$(".tabcontainer");
	if(tabs[0]){
		tabs.each(function(){
		
			var self	=	this;
			
			$(".tab:first > span", self).addClass("active");
			
			$(".tab_content:not(:first)", self).hide();
			
			$(".tab", self).click(function(){
				
				var activeTab	=	$(".tab_content:eq("+$(".tab", self).index(this)+")");
				
				$("span.active", self).removeClass("active");
				$(this).addClass("active");
				$(".tab_content:visible").hide();
				activeTab.show();
				if($("iframe", activeTab)[0]){
					$("iframe", activeTab).attr({src: $("iframe", activeTab).attr("title")});
				}
				
				return(false);
				
			});
			
		});
	}
	
	////
	// Reageer, stuurdoor en contact
	////
	$(".reageer form, .stuurdoor form, .contact form").validate();
	$(".reageer form, .stuurdoor form, .contact form:not(.route)").submit(function(){
		var form	=	$(this),
		    data	=	{};
		    
		if($(this).validate().validate()){
			
			// Keep height the same
			form.parent().height(form.parent().height());
			
			$("input[type=text], input[type=radio]:checked, input[type=checkox]:checked, select, textarea", this).each(function(){
			
				data[$(this).attr("name")]	=	$(this).val();
			
			});
			
			$.post(
				form.attr("action"),
				data,
				function(html){
					form.parent().html(html);
				}
			)
			
		}
		return(false);
	});
	
	////
	// Bieden
	////
	$("#bieden form").validate({
		error_output:		{
			text:			"alert",
			email:			"alert",
			select:			"alert",
			other:			"alert"
		}
	});
	
	////
	// Route
	////
	$.fn.route	=	function(options){
		var self	=	$(this);
		self.validate();
		
		self.submit(function(){
			
			if(self.validate().validate()){
				
				var link	=	"http://maps.google.nl/maps?f=d&source=s_d&saddr=";
				
				link	+=	$("input[name=adres]", self).val();
				link	+=	", ";
				link	+=	$("input[name=postcode]", self).val();
				link	+=	"&daddr=";
				link	+=	options.adres;
				link	+=	", ";
				link	+=	options.postcode;
				link	+=	" ";
				link	+=	options.plaats;
				
				window.open(link);
				
			}
			
			return(false);
		
		});
		
	};
	
	//
	// Diensten menu
	//
	

	if ($(".diensten").hasClass("active")) {
	
		$("#submenu").show();
	
	} else {
		
		var over	=	false,
		overTime	=	0,
		checkOver	=	function() {
	
			if (over) {
				setTimeout(checkOver, 200);
				return false;
			}else if (overTime >= (new Date().getTime() - 500)) {
				setTimeout(checkOver, 200);
				return false;
			}
			over	=	false;
			$(".diensten").removeClass("active");
			$("#submenu").hide();
			
		};

		$(".diensten").hover(
			function() {
				$(this).addClass("active");
				$("#submenu").show();
				over	=	true;
				overTime=	new Date().getTime();
				setTimeout(checkOver, 200);
			},
			function() {
				over	=	false;
			}
		);
		
		$("#submenu").hover(
			function() {	
				over	=	true;
				overTime=	new Date().getTime();
			},
			function() {
				over	=	false;
			}
		);
		
		$("#menu li:not(.diensten)").mouseover(function() {
			$(".diensten").removeClass("active");
			$("#submenu").hide();
			over	=	false;
		});
		
	}
	
});

