// JavaScript Document

$(document).ready(function(){
	//alert('Ready!');
	
	//PRELOAD DELLE IMMAGINI INCLUSE NEI CSS E NELLA PAGINA
	/*cssImagesToPreload = [url+'site/template/images/logo.png',url+'site/template/images/frame.png',url+'site/template/images/flowers.png',url+'site/template/images/accordion.png'];
	pageImagesToPreload = $('img');
	
	$.preload(cssImagesToPreload,{
		onFinish: function(){ 
			$.preload(pageImagesToPreload,{
				onFinish: function(){ 
					//INIZIALIZZAZIONE DELLE ANIMAZIONI
					startAnimations(); 
				}				 
			});
		}				 
	});*/
	
	//TARGET _BLANK
	$("a[class='_blank']").each(function(){ this.target="_blank"; });
	
	//GESTIONE DEI BOX D'ERRORE
	$("div.error").click(
		function () {
			$("div.error").hide("slow");
    	}
	);
	
	if ($("div.error").html()) {
		setTimeout('$("div.error").hide("slow");',5000);
	}
	
	//DISABILITO IL TASTO DESTRO PER LE IMMAGINI DELLA PAGINA
	/*$("img").bind("contextmenu",function(e){
		return false;
	});*/
	
	//FANCYBOX
	$("a.fancyimage").fancybox({
		overlayShow: true,
		overlayOpacity: 0.7,
		overlayColor: '#000'
	});
	$("a.fancycontent").fancybox({
		'autoDimensions':	false,
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	500,
		'speedOut'		:	500,
		'width'			:	500,
		'height'		:	500
	});
	$("a.fancyplayer").fancybox();
	$("a.fancyyoutube").fancybox();
	
	
	
	

	//EFFETTI DELLA GALLERY
	/*$("a.image_frame").hoverIntent(
	  function () {
		$(this).animate({
    		opacity: 1
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
		$(this).find(".caption").slideDown('fast');
		
		$(this).find("img").animate({
    		width: '125px',
			height: '125px',
			top: '0px',
			left: '0px'
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
		$(this).find(".greyscale").fadeOut();
	  }, 
	  function () {
		$(this).animate({
    		opacity: 0.7
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
		$(this).find(".caption").slideUp('fast');
		
		$(this).find("img").animate({
    		width: '180px',
			height: '180px',
			top: '-28px',
			left: '-28px'
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
		$(this).find(".greyscale").fadeIn();
	  }
	);*/
	
	//EFFETTI DELLA GALLERY
	$("input").hoverIntent(
	  function () {
		/*$(this).animate({
    		opacity: 1
  		}, 'fast', function() {
    		// Animation complete.
  		});*/
		
		$(this).parent().find(".suggestion").animate({
    		opacity: 0.9,
			top: -7
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
	  }, 
	  function () {
		/*$(this).animate({
    		opacity: 1
  		}, 'fast', function() {
    		// Animation complete.
  		});*/
		
		$(this).parent().find(".suggestion").animate({
    		opacity: 0,
			top: 20
  		}, 'fast', function() {
    		// Animation complete.
  		});
		
	  }
	);
	
	//VALIDAZIONE DELLE FORM
	$("form").each(function(){
			$(this).validate({
				submitHandler: function(form) {
				//form.submit();		//submit in postback
				formSubmit(form);		//submit in ajax
				return false;
			}
		});
	});
	
	//RESET DEI CAMPI DELLA FORM
	$('#reset').click(
		function () {
			$(this).parent("form").clearForm();
    	}
	);
	
	//AUTOCOMPLETE
	$("#eancode").autocomplete('findproducts.php', {
		max: 34,
		width: 340,
		multiple: false,
		matchContains: true,
		formatItem: formatItem,
		formatResult: formatResult
	});
	$("#eancode").change(function() {
		if(checkIfStockRequired($(this).val())) {
			$("#stock").addClass("required");
			$("#stock").parent().show();
		} else {
			$("#stock").removeClass("required");
			$("#stock").parent().hide();
		}
	});
	$("#eancode").keyup(function() {
		if(checkIfStockRequired($(this).val())) {
			$("#stock").addClass("required");
			$("#stock").parent().show();
		} else {
			$("#stock").removeClass("required");
			$("#stock").parent().hide();
		}
	});
		
	$("#customerprovince").autocomplete('findprovince.php', {
		max: 34,
		width: 340,
		multiple: false,
		matchContains: true,
		formatItem: formatItem,
		formatResult: formatResult
	});
	
	$("#province").autocomplete('findprovince.php', {
		max: 34,
		width: 340,
		multiple: false,
		matchContains: true,
		formatItem: formatItem,
		formatResult: formatResult
	});
	
	//CARICAMENTO DEL DATEPICKER
	var calendar_button = '<a id="calendar_button" href="#" title="scegli una data"><img src="images/calendar.gif" alt="scegli una data" /></a>';
	$('input#datepicker').parent().append(calendar_button);
	$('input#datepicker').parent().find('#calendar_button').click(function(event){
		event.preventDefault;
		$('input#datepicker').DatePickerShow();
		return false;																		  
	});
	
	$('input#datepicker').DatePicker({
		format:'Y-m-d',
		date: $('input#datepicker').val(),
		//date: ['1970-01-01',$('input#datepicker').val()],
		current: $('input#datepicker').val(),
		//date: date,
		//current: date,
		starts: 1,
		mode: 'single',
		view: 'days',
		calendars: 1,
		position: 'r',
		locale: {
					days: ["Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato", "Domenica"],
					daysShort: ["Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab", "Dom"],
					daysMin: ["Do", "Lu", "Ma", "Me", "Gi", "Ve", "Sa", "Do"],
					months: ["Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"],
					monthsShort: ["Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic"],
					weekMin: 'set'
				},
		onBeforeShow: function(){
			$('input#datepicker').DatePickerSetDate($('input#datepicker').val(), true);
		},
		onRender: function(date) {
			return {
				//disabled: (date.valueOf() > now.valueOf()),
				//className: date.valueOf() == now2.valueOf() ? 'datepickerSpecial' : false
			}
		},
		onChange: function(formated, dates){
			$('input#datepicker').val(formated);
			$('input#datepicker').DatePickerHide();
		}
	});
	
	if($("#registration_form").length > 0) {
		changeFormLabels($("input[name$='type']:checked").val());
		$("input[name$='type']").change(function(){
			changeFormLabels($(this).val());										 
		});
	}
	
	if($("span#type").length > 0) {
		val = $("span#type").text();
		if (val == "Azienda") {
			val = "C";	
		} else val = "P";
		changeFormLabels(val);
	}
	
	if($("span#stock").text() != '') {
		$("span#stock").parent().show();
	} else {
		$("span#stock").parent().hide();
	}
	
	if($("input#stock").val() != '') {
		$("input#stock").parent().show();
	} else {
		$("input#stock").parent().hide();
	}

	
});

function findValueCallback(event, data, formatted) {
	$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
}
	
function formatItem(row) {
	//return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	return row[1];
}

function formatResult(row) {
	return row[0].replace(/(<.+?>)/gi, '');
}

function changeFormLabels(val) {
	switch (val) {
		case "P":
			name_label = "Nome";
			representative_label = "Cognome";
			taxcode_label = "Codice Fiscale";
			ticketnumber_label = "n. Scontrino"
			$("#taxcode").removeClass("required");
			$("#taxcode").parents("div:first").hide();
		break;
		
		case "C":
			name_label = "Ragione sociale";
			representative_label = "Referente";
			taxcode_label = "Cod.Fis./P.IVA";
			ticketnumber_label = "n. Fattura";
			$("#taxcode").addClass("required");
			$("#taxcode").parents("div:first").show();
		break;
	}
	
	$("#name_label").html(name_label);
	$("#representative_label").html(representative_label);
	$("#taxcode_label").html(taxcode_label);
	$("#ticketnumber_label").html(ticketnumber_label);
}

function checkIfStockRequired(eancode){
	//var stockNotRequired4thisEancodes = [ "8033593085231" ];
	
	if(jQuery.inArray(eancode, stockNotRequired4thisEancodes) >= 0) {
		return false;
	} else return true;
}

function ltIE7() {
	var semaphore;
	semaphore = !$.browser.msie || ($.browser.msie && ($.browser.version > 6));
	
	return semaphore;
}

//FUNZIONI DI ANIMAZIONE DEGLI OGGETTI DELLA PAGINA
function startAnimations() {
	$("#loader").fadeOut(500);
	$("#outer_outer_canvas").fadeIn(500);
	animation1();
}

function animation1() {
	$("#logo").fadeIn(300);
	$("#haccordion").fadeIn(1500,function(){animation2()});
}

function animation2() {
	$("#haccordion").animate({"left": "-=275px"}, 2000, function(){finalizeAnimations()});
	
}

function finalizeAnimations() {
	//INIZIALIZZAZIONE DEL PRIMO PANNELLO
	openPanel($(".hpanel:first").attr("id"));
}

