/*jshint asi: false, bitwise: true, boss: false, curly: true, debug: false, devel: false, eqeqeq: true, evil: false, forin: true, immed: true, laxbreak: false, newcap: true, noarg: true, noempty: true, nonew: true, nomen: true, onevar: true, plusplus: true, regexp: false, undef: true, sub: false, strict: true, white: false*/
/*global jQuery, console, baseUrl, window */
(function ($) {	
	"use strict";
	var requiredInput, paceholderInputFocus, paceholderInputBlur, initInput;
	
	requiredInput		=	function ( ) {
		var $this	=	$(this);
		
		$this.removeClass("invalid");
		if ($this.val() && $this.val() !== $this.data("paceholder")) {
			$this.addClass("valid");
		} else {	
			$this.removeClass("valid");
		}
	};
	
	paceholderInputFocus	=	function ( ) {
		var $this	=	$(this),
		    value	=	$this.val() || $this.text();
		
		if (value === $this.data("paceholder")) {
			$this.val("").text("");
		}
		
		$this.removeClass("paceholder");
	};
	
	paceholderInputBlur	=	function ( ) {
		var $this	=	$(this),
		    value	=	$this.val() || $this.text();
		
		if (!value || value === $this.data("paceholder")) {
			$this.val($this.data("paceholder")).text($this.data("paceholder"));
			$this.addClass("paceholder");
		}
	};
	
	initInput		=
	window.initInput	=	function ( ) {
		var $this	=	$(this),
		    $parent	=	$this.parent(),
		    width	=	parseInt($parent.width(), 10);
			
		
		if ($this.hasClass("resize")) {
			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);
			
			if (this.nodeName.toLowerCase() === "select" && !$(".ie6, .ie7, .ie8")[0]) {
				width	+=	10;
			}
			
			$this.width(width);
		}
		
		if ($this.data("required")) {
			$this
				.addClass("required")
				.bind("keyup change blur focus", requiredInput);
			
			requiredInput.apply(this);
		}
		
		if ($this.data("paceholder")) {
			$this
				.bind("focus click", paceholderInputFocus)
				.bind("blur", paceholderInputBlur);
			paceholderInputBlur.apply(this);
		}
	};
	
	$("input, textarea, select").each(initInput);
}(jQuery));

