function initPageJs(){
	accessibleInputs();
}


$(document).ready(function(){
  initPageJs();
});

/* accessibleInputs.js|global */
//Accessible Inputs (requires jQuery)

// moves labels value to inputs if class 'mdValueToInput' is present & then adds focus/blur to inputs
function accessibleInputs(){
	$("label.mdValueToInput[for]").each(function(i){
		// fill input fields with labeltext - html tags
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
		var newVal = this.innerHTML.replace(regexp,"");
		//if el is type=input or textarea
		if($("#"+this.htmlFor).is("input") || $("#"+this.htmlFor).is("textarea")){
			if(($("#"+this.htmlFor).val() == "") || ($("#"+this.htmlFor).val() == newVal)){
				$("#"+this.htmlFor).attr("value",newVal);
			}
			// create onclick/blur functionality
			$("#"+this.htmlFor).focus(function(){if(this.value == newVal) this.value = "";});
			$("#"+this.htmlFor).blur(function(){if(this.value == "") this.value = newVal;});
		// if el is select	
		} else if($("#"+this.htmlFor).is("select")){
			var orgOptions = $("#"+this.htmlFor).html();
			var newOptions = '<option value="">'+newVal+'</option>'+orgOptions;
			// IE special Kung Fu
			if($.browser.msie){
				var go=0;
				$("#"+this.htmlFor).find("option").each(function(i){
					if($(this).get(0).defaultSelected){
						go=1;
					}
				})
				if(go==0){
					newOptions = newOptions.replace(/selected>/g,">");
				}
			}
			$("#"+this.htmlFor).html(newOptions);
		}
		// hide label
		$(this).hide();
	})
	cleanForms();
}

// makes sure that label values are not submitted to forms
function cleanForms(){
	$("form:has(label.mdValueToInput)").submit(function(){
		$("label.mdValueToInput[for]").each(function(){
			// check if value is same as label
			var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
			var newVal = this.innerHTML.replace(regexp,"");
			if($("#"+this.htmlFor).attr("value") == newVal){
				$("#"+this.htmlFor).attr("value","");
			}
		})
	})
}

