// Favola Basic js

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

function slider() {
	$('#slider').cycle({
		fx: 'fade',
		timeout: 8000
	});
}

function labelToInput(){
	$("label.labelToInput[for]").each(function(i){
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
		var newVal = this.innerHTML.replace(regexp,"");
		if($("#"+this.htmlFor).is("input")){
			if(($("#"+this.htmlFor).val() == "") || ($("#"+this.htmlFor).val() == newVal)){
				$("#"+this.htmlFor).attr("value",newVal);
				$("#"+this.htmlFor).focus(function(){if($(this).val() == newVal) $(this).val("");});
				$("#"+this.htmlFor).blur(function(){if($(this).val() == "") $(this).val(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)
		} else if($("#"+this.htmlFor).is("textarea")){
			if(($("#"+this.htmlFor).text() == "") || ($("#"+this.htmlFor).text() == newVal)){
				$("#"+this.htmlFor).text(newVal);
				$("#"+this.htmlFor).focus(function(){if($(this).val() == newVal) $(this).val("");});
				$("#"+this.htmlFor).blur(function(){if($(this).val() == "") $(this).val(newVal);});
			}
		}
		$(this).hide();
	})
	cleanForms();
}

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

function gallery(){
	$('.gallery a').has('img').attr('rel', 'gallery').fancybox({
		type: 'image',
		padding: 4,
		titlePosition: 'inside',
		overlayColor: '#241e10',
		overlayOpacity: '0.75'
	});
}
