/****** console object trick ********/
if (typeof console == "undefined" || typeof console.log == "undefined"){
	var console = { 
		log: function() {} ,
		warn: function() {} ,
		error: function() {} ,
		dir: function() {} 
	};
}

// Menu prodotti

$('ul#menu-prodotti li a.prodotti_liv1').click(function(){
/*
	var arr = ($(this).attr("id")).split('_');
	var id = arr[1];
	$('ul#sottomenu_'+id).show();
*/
	$(this).next().toggle();
});


// Gestione Forms
$(document).ready(function(){
	$('.mustbeFilled').parent().append("<span class='stars'>*</span>");
});

// Validazione Forms

//var form = function (){return new form.core.constructor();};
form = {	//form.prototype
	obj: null,
	validated: true,
	alert: false,	
	/*
	constructor: function(){
		form.check_unfilled_field();
		form.check_wrong_date_field();
	},
	*/
	std: {
			validate: function(obj){
				form.validated = true;
				form.obj = $(obj);
				form.check_unfilled_field();
				form.check_wrong_date_field();
				form.check_acepted_forms();
				
				if( form.validated==true ){
					form.obj.submit();
				}
				else{
					return false;
				}
									
			}
	},
	
	ajax: {
			validate: function(obj){
				form.validated = true;
				form.obj = $(obj);
				form.check_unfilled_field();
				form.check_wrong_date_field();
				form.check_acepted_forms();
				
				if( form.validated )
					ajax_sumbit(obj);
				else
					return false;
			}
	},
	
	check_unfilled_field: function(){
		$(this.obj).find('input.mustbeFilled, select.mustbeFilled').each(function(){			
			if (($(this).val() == "" || $(this).is('.default')) && !$(this).is('.no-check') ){
				$(this).focus()
					.addClass("wrong");
				form.validated = false;
				form.error_append("Completare tutti i campi");
			}
		})
		return form.validated;
	},
	
	check_wrong_date_field: function(){
		reg_data = new RegExp("\\d{2}\/\\d{2}\/\\d{4}");
		$(this.obj).find('input.mustbeDate').each(function(){			
			if ( !reg_data.test( $(this).val() ) ){
				$(this).focus()
					.addClass("wrong");
				form.validated = false;
				form.error_append("Correggere i campi data (una data deve essere gg/mm/aaaa )");
			}
		})
		return form.validated;
	},
	
	check_acepted_forms: function(){
		
		if( typeof $('#check_ok').val() != "undefined" ){
			if( !$('#check_ok').attr("checked") ){
				form.validated = false;
				form.error_append("Non è stato acconsentito il trattamento dei dati personali");
			}
		}
		return form.validated;
		
	},
	
	error_append: function(errorString){
		if(!form.alert) alert("Errore: "+errorString);
		form.alert = true;
		//console.warn("Errore form:"+errorString);
	}
	
};

function switch_lang(obj){
	
	var val=obj.val();
	$('#switch_to_lang').val(val);
	$('#form_switch_to_lang').submit();
	
}

////

function carousel_initCallback(carousel) {
	$('.jcarousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval($(this).text()));
		return false;
	});
	/*
	$('.jcarousel-scroll select').bind('change', function() {
		carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
		return false;
	});
	*/
	$('#carousel-next').bind('click', function() {
		carousel.next();
		return false;
	});
	
	$('#carousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};

function json_encode(data){
	sep = "";
	// Variabile di appoggio per la creazione della nuova variabile JSON
	var NewJSON = "{";
	for (var i in data){
		val = data[i];
		if ( typeof i == "string" ) i = "\""+i+"\"";
		if ( typeof val == "string" ) val = "\""+val+"\"";
		if ( typeof val == "object" ){
			json_encode(val);
		}
		else{
			NewJSON += sep+i+":"+val;
		}		
		sep = ",";
	}
	NewJSON += '}';
	// Conversione stringa un variabile json
	//	var NewJSON = eval("(" +NewJSON+ ")");
	return NewJSON;
}

function ajax_submit(obj){
	
	vars = obj.serialize();
	obj.html("Loading...");
	
	$.post( obj.attr("action"), vars, function(data){
		if ( data ){
			//check_ajax_state( data );
			// OK
			if ( data == '1'){
				obj.html("<b>Dati salvati</b>");
			}
			else obj.text( data );
		}
		else{
			obj.text("Aggiornamento FALLITO.");
		}
	})
	
	// Impedisci il submit del form
	return false;
}



