var TOOLS;

$(document).ready(function ( ) {
	var $searchForms	=	$(".searchForm");
	// Tools
	TOOLS	=	{
		"width100pc":		function ( ) {
			var $this		=	$(this),
			    $parent		=	$this.parent(),
			    width		=	parseInt($parent.width(), 10),
			    isSelect		=	(this.nodeName.toLowerCase() === "select"),
			    isIE		=	!!$(".ie6, .ie7, .ie8")[0];
			
			
			if (!isSelect) {
				width		-=	parseInt($this.css("paddingLeft"), 10);
				width		-=	parseInt($this.css("paddingRight"), 10);
				width		-=	parseInt($this.css("borderLeftWidth"), 10);
				width		-=	parseInt($this.css("borderRightWidth"), 10);
			}
			
			width		-=	isNaN(parseInt($this.css("marginLeft"), 10)) ? 0 : parseInt($this.css("marginLeft"), 10);
			width		-=	isNaN(parseInt($this.css("marginRight"), 10)) ? 0 : parseInt($this.css("marginRight"), 10);
			
			
			return width;
		}
	};
	
	// Truncate
	$(".truncate").each(function ( ) {
		var $this	=	$(this);
		$this.attr({"title": _.trim($this.text())});
	}).truncate();
	
	// Tooltip
	$(".tooltip").tooltip();
	
	// inputs
	$("input.text, textarea").each(function ( ) {
		input.resize.apply(this);
		input.placeHolder.apply(this);
		input.required.apply(this);
	});
	
	$("select").each(function ( ) {
		input.resize.apply(this);
		input.required.apply(this);
	});
	
	$("a.submit").click(function ( ) {
		$(this).closest("form").submit();
		return false;
	});
	
	//
	// Forms
	//
	$("form").submit(function ( ) {
		var $this	=	$(this),
		    valid	=	true,
		    width	=	$this.width(),
		    height	=	$this.height(),
		    ajax	=	$this.data("ajax"),
		    action	=	$this.attr("action"),
		    method	=	$this.attr("method").toUpperCase();
		
		$this.find("input, select, textarea").each(function ( ) {
			var $this	=	$(this),
			    value	=	$this.val(),
			    placeHolder	=	$this.data("placeholder"),
			    required	=	$this.data("required");
			
			if (value === placeHolder) {
				$this.val("");
				value	=	"";
			}
			
			if (!value) {
				if (required) {
					$this.addClass("invalid");
					valid	=	false;
				}
			}
		});
		
		if (!valid) {
			$("input, select, textarea").each(function ( ) {
				var $this	=	$(this),
				    value	=	$this.val(),
				    placeHolder	=	$this.data("placeholder");
				
				if (!value) {
					$this.val(placeHolder);
				}
			});
		}
		
		if (ajax && valid) {
			$.ajax({
				"type":		method,
				"url":		action,
				"data":		$this.serialize(),
				"success":	function (data) {
					$this.css({
						"minWidth":	width,
						"minHeight":	height
					}).html(data);
				}
			});
			return false;
		} else {
			return valid;
		}
	});
	
	//
	// Search forms
	//
	if ($searchForms[0]) {
		$searchForms.each(function ( ) {
			var change,
			    $this		=	$(this),
			    $koophuur		=	$this.find(".koophuur"),
			    $koop		=	$this.find(".koop"),
			    $huur		=	$this.find(".huur"),
			    $formElement	=	$this.find(".formElement");
			
			change	=	function ( ) {
				var koophuur	=	$koophuur.val();
				
				if (koophuur === "koop") {
					$koop.show();
					$huur.hide();
				} else {
					$koop.hide();
					$huur.show();
				}
				
				$koop.find("input, select, textarea").each(function ( ) {
					var $this	=	$(this);
					
					if (koophuur === "koop") {
						input.resize.apply(this);
						$this.attr({"name": $this.data("name")});
					} else {
						$this.attr({"name": ""});
					}
				});
				
				$huur.find("input, select, textarea").each(function ( ) {
					var $this	=	$(this);
					
					if (koophuur === "huur") {
						input.resize.apply(this);
						$this.attr({"name": $this.data("name")});
					} else {
						$this.attr({"name": ""});
					}
				});
			};
			
			$formElement.each(function ( ) {
				var $this	=	$(this),
				    $value	=	$this.find(".value"),
				    $selects	=	$this.find("select"),
				    $inputs	=	$this.find("input"),
				    value;
				
				if ($value[0]) {
					value	=	$value.text();
					
					if ($selects[0] && value) {
						$selects.each(function ( ) {
							$(this).find("option").each(function ( ) {
								var $this	=	$(this);
								
								if ($this.attr("value") == value) {
									$this.attr({"selected": "selected"});
								}
							});
						});
					}
					if ($inputs[0] && value) {
						$inputs.each(function ( ) {
							$(this).val(value);
						});
					}
				}
			});
			
			change();
			
			$koophuur.bind("change", change);
		});
	}
});

