// DOM ready
$(document).ready(function(){
	labelToInput();
	slider();
});

function slider(){
	$('#slidersub').after('<ul></ul>').cycle({
		fx: 'scrollHorz',
		timeout: 8000,
		pager:  '#slider ul',
		pagerAnchorBuilder: function(idx) { 
			return '<li></li>'; 
		}
	});
}

function labelToInput(){
	$("label.labelToInput[for]").each(function(i){
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
		var newVal = this.innerHTML.replace(regexp,"");
		if($("#"+this.htmlFor).is("input") || $("#"+this.htmlFor).is("textarea")){
			if(	($("#"+this.htmlFor).val() == "") || ($("#"+this.htmlFor).val() == newVal)	){
				$("#"+this.htmlFor).attr("value",newVal);
				$("#"+this.htmlFor).focus(function(){if(this.value == newVal) this.value = "";});
				$("#"+this.htmlFor).blur(function(){if(this.value == "") this.value = newVal;});
			}
		} else if($("#"+this.htmlFor).is("select")){
			var orgOptions = $("#"+this.htmlFor).html();
			$("#"+this.htmlFor).html("")
			strSelected = ' selected="selected"';
			if(orgOptions.indexOf("selected=") > -1) strSelected = "";
			newOptions = "<option"+strSelected+">"+newVal+"</option>"+orgOptions;
			$("#"+this.htmlFor).html(newOptions)
		}
		$(this).hide();
	})
	cleanForms();
};

function cleanForms(){
	$("form:has(label.mdValueToInput)").submit(function(){
		$("label.mdValueToInput[for]").each(function(){
			var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
			var newVal = this.innerHTML.replace(regexp,"");
			if($("#"+this.htmlFor).attr("value") == newVal){
				$("#"+this.htmlFor).attr("value","");
			}
		})
	})
};
