//INIZIO FUNZIONI CARRELLO

function submitCarrelloByMail()
{
		
	var form = $j('#invio_carrello');


	//setto parametro antispam
	var d = new Date()
	param = d.getTime();		
	$('input[name="secure"]',form).val(param);


//inizio validazione
	var error_found = 0;

	if(validateFormElement($j('#frm_nome'),'left')) error_found++;
	if(validateFormElement($j('#frm_cognome'),'left')) error_found++;
	if(validateFormEmail($j('#frm_email'),'left')) error_found++;
	if((validateCheckBoxElement($j('#frm_privacy'),'left'))) error_found++;
	if(validateHiddenElement($j('#frm_tipo_richiesta'),'left')) error_found++;
//fine validazione	

	if(error_found==0)
	{
			$j('#form_carrello_all_fields').slideUp('slow');

			var options = { 
			
				beforeSubmit: function()
				{
					//$j('#form_carrello_loading').slideDown('slow');					
				},
				resetForm: true,
				error: function()
				{
					toConsole('Errore di invio');
					return false;
				},
			    success: function() 
			    { 			    			        

					//$j('#form_email_loading').slideUp('slow');
					$j('#form_carrello_thanks').slideDown('slow');		
				  return false; 			        
			        
			    } 
			}; 					 
			$('#invio_carrello').ajaxForm(options); 
		    $('#invio_carrello').ajaxSubmit(options); 
	    
	//document.forms['invio_carrello'].submit();		
	
	}	
	return false;
}


function carrelloDeleteRow(id_elem)
{
	var form = $j('#delete_from_carrello');
		
	$j('#todelete').attr('value', id_elem);

	var options = { 
	
		beforeSubmit: function()
		{

		},
		resetForm: true,
		error: function()
		{
			toConsole('Errore di invio');
			return false;
		},
	    success: function() 
	    { 			    			        

		  return false; 			        
	        
	    } 
	}; 					 

//	$('#delete_from_carrello').ajaxForm(options); 
//    $('#delete_from_carrello').ajaxSubmit(options); 	
	document.forms['delete_from_carrello'].submit();    
	return false;
	
}

function submitCarrello()
{
	var form = $j('#add_to_carrello');


//inizio validazione
	var error_found = 0;		
	if(validateFormElement($j('#frm_quantita'),'top')) error_found++;
	if(validateFormElement($j('#frm_formato'),'top')) error_found++;
//fine validazione	

	if(error_found==0)
	{
			$j('#form_carrello_all_fields').slideUp('slow');

			var options = { 
			
				beforeSubmit: function()
				{
					//$j('#form_email_loading').slideDown('slow');					
				},
				resetForm: true,
				error: function()
				{
					toConsole('Errore di invio');
					return false;
				},
			    success: function() 
			    { 			    			        

//					$j('#form_email_loading').slideUp('slow');
					$j('#form_carrello_all_thanks').slideDown('slow');		
				  return false; 			        
			        
			    } 
			}; 					 

			$('#add_to_carrello').ajaxForm(options); 
		    $('#add_to_carrello').ajaxSubmit(options); 
	    
//	document.forms['guestbooksign'].submit();		
	
	}	
	return false;
}


//FINE FUNZIONI CARRELLO

//	inizio funzioni di debug
debug_msg = false;
function toDebug(string)
{
	if (debug_msg)
	{
		if((typeof window.console=="undefined")) {alert(string);}
		else {
			console.error();
			console.group(string);
			console.trace();
			console.groupEnd();
		}
	}
}
function toConsole(string)
{
	if((typeof window.console=="undefined")) {alert(string);}
	else console.log(string);
}
//	fine funzioni di debug
//--------------------------------------------------------------------
function mainHeight()
{
try
{
	var main = $('#main');
	var head = $('#head');
	var container = $('#container');
	var footer = $('#footer');
	var container_padding = 30;
	var total_height;
	total_height = head.height() + container.height()+footer.height();
	container.css({'top':head.height()+'px'});
	if(total_height < 700)
	{
		total_height = 700;
		container.css({'height':'532px'});
		$('#contenuto').css({'height':'376px'});
	}
	footer.css({'top':total_height+'px'});
	main.height(total_height);
	overMenu();
	if($j('#menu-orizzontale') && !$j('#menu-orizzontale ul li.active'))
	{
		$j('#menu-orizzontale ul li')[0].className = 'active';
	}

}
catch(e) {toDebug(e);}
}
//--------------------------------------------------------------------
//	inizio funzioni di scroll
var moving = false;
var timer;
var top = 0;
function scrollGallery()
{
try
{
	var freccia_su = $('div.freccia-su');
	var freccia_giu = $('div.freccia-giu');
	//linkImages();
	freccia_su.hover(function(){scrollUp();},function(){moving = false;clearTimeout(timer);});
	freccia_giu.hover(function(){scrollDown()},function(){moving = false;clearTimeout(timer);});
}
catch(e) {toDebug(e);}
}

function scrollUp()
{
	var gallery = $('#miniature');
	if(gallery.offset().top >= 298) return false;
	moving = true;
	top +=5;
	gallery.animate({ 
		top:top
      }, 10 );
	if(moving) timer = setTimeout(scrollUp,10);
	else return false;
}

function scrollDown()
{
	var gallery = $('#miniature');
	if(gallery.offset().top < -(gallery.height()-710)){return false;}
	moving = true;
	top -=5;
	gallery.animate({ 
		top:top
      }, 10 );
	if(moving) timer = setTimeout(scrollDown,10);
	else return false;	
}
function linkImages()
{
try
{
	var link_array = $('#miniature a');
	link_array.click(function()
	{ 
		$('#big-image').attr('src',this.href);
		return false;
	 });
}
catch(e) {toDebug(e);}
}
//--------------------------------------------------------------------
//	fine funzioni di scroll
//--------------------------------------------------------------------
function overMenu() {
	var link_array = $('#menu a');
	link_array.hover(function()
	{
		$(this).animate(
		{
			paddingTop: 107,
			height: 63
		}, 500);
		$(this).css({ 'background':'url(images/background/menu-on.gif) repeat-x bottom #df9c0c','font-weight':'bold'});
	},
	function()
	{
		if(!$(this).hasClass('active'))
		{
			$(this).animate(
			{
				paddingTop: 77,
				height: 93
			}, 500);
			$(this).css({ 'background':'','font-weight':'normal'});
		}
	});
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
function getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
			return arrayPageSize;
		};
		/**
		 / THIRD FUNCTION
		 * getPageScroll() by quirksmode.com
		 *
		 * @return Array Return an array with x,y page scroll values.
		 */
		function getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll) 
			return arrayPageScroll;
		};
//--------------------------------------------------------------------
function showNewsletter()
{
try
{
	var overlay = $('#my_overlay');
	var box = $('#box-newsletter');
	var overlay_height  = getPageSize();
	var page_scroll  = getPageScroll();
	overlay.css({'height': overlay_height[1], 'opacity':'0'});
	box.css({'top':page_scroll[1]});
	overlay.one("click", function(){
		hideNewsletter();
	});
	overlay.show();
	overlay.fadeTo('slow',0.5,function(){box.slideToggle('slow');});
}
catch(e) {toDebug(e);}
}
function hideNewsletter()
{
try
{
	var overlay = $('#my_overlay');
	var box = $('#box-newsletter');
	box.slideToggle('fast',function(){overlay.fadeOut('fast');});
}
catch(e) {toDebug(e);}
}




/*BOF FORM CONTATTI*/

function HasNoValue(string)
{
	if(string==null || string == undefined || string == '')
	return true;
	else return false;
}


function validateFormElement(elem,movement)
{
	if(HasNoValue(elem.attr('value')))
	{
		errorOnFormElement(elem,movement);
		return true;
	}
	else 
	{
		elem.css({'background':'#ffffff'});
		return false;
	}
}

function validateHiddenElement(element,movement)
{

	if(validateFormElement(element))
	{
		errorOnFormElement('fieldset_dettagli_richiesta',movement);
		errorOnFormElement('fieldset_dettagli_preventivo',movement);
		return true;
	}	
	else return false;
}

function validateCheckBoxElement(elem,movement)
{
	if(HasNoValue(elem.attr('checked')))
	{
		errorOnFormElement(elem.parent(),movement);			
		return true;
	}
	else
	{
		elem.css({'background':'#ffffff'});
		return false;
	}
}

function validateFormEmail(elem,movement)
{
	var emailRegexp = RegExp('^[A-Za-z0-9._-]+[@]([A-Za-z0-9-]+[.])+([A-za-z]{2,4})$', 'i');
		
	if (emailRegexp.test(elem.attr('value')))
	{
		elem.css({'background':'#ffffff'});
		return false;
	}
	else
	{
		errorOnFormElement(elem,movement);
		return true;
	}
}

function errorOnFormElement(elem,movement)
{
	elem.css({'position':'relative'});
	elem.css({'background':'#ffee5b'});
	if(movement=='top')
		elem.animate({top: '-10'},120).animate({top: '+10'},120).animate({top: '-10'},120).animate({top: '0'},120);
	else
		elem.animate({left: '-10'},120).animate({left: '+10'},120).animate({left: '-10'},120).animate({left: '0'},120);
	
}

function showOnClick(element)
{
	if(element == 'frm_dettagli_richiesta')
	{		
		$j('#'+element).slideToggle('slow');
		if($j('#frm_dettagli_preventivo:visible')) $j('#frm_dettagli_preventivo').slideToggle('slow');
		document.forms['guestbooksign'].frm_tipo_richiesta.value = 'richiesta informazioni';

	}
	else
	{

		$j('#'+element).slideToggle('slow');
		if($j('#frm_dettagli_richiesta:visible')) $j('#frm_dettagli_richiesta').slideToggle('slow');
		document.forms['guestbooksign'].frm_tipo_richiesta.value = 'richiesta preventivo';

	}

}

function submitGuestBook()
{
	var d = new Date()
	param = d.getTime();
		
	var form = $j('#guestbooksign');

	//setto parametro antispam
	var d = new Date()
	param = d.getTime();		
	$('input[name="secure"]',form).val(param);

//inizio validazione
	var error_found = 0;
	
	if(validateFormElement($j('#frm_nome'),'left')) error_found++;
	if(validateFormElement($j('#frm_cognome'),'left')) error_found++;
	if(validateFormEmail($j('#frm_email'),'left')) error_found++;
	if((validateCheckBoxElement($j('#frm_privacy'),'left'))) error_found++;
	if(validateHiddenElement($j('#frm_tipo_richiesta'),'left')) error_found++;
//fine validazione	

	if(error_found==0)
	{
			$j('#form_email_all_fields').slideUp('slow');

			var options = { 
			
				beforeSubmit: function()
				{
					//$j('#form_email_loading').slideDown('slow');					
				},
				resetForm: true,
				error: function()
				{
					toConsole('Errore di invio');
					return false;
				},
			    success: function() 
			    { 			    			        

					//$j('#form_email_loading').slideUp('slow');
					$j('#form_email_thanks').slideDown('slow');		
				  return false; 			        
			        
			    } 
			}; 					 
			$('#guestbooksign').ajaxForm(options); 
		    $('#guestbooksign').ajaxSubmit(options); 
	    
//	document.forms['guestbooksign'].submit();		
	
	}	
	return false;
}

/*EOF FORM CONTATTI*/
/*BOF GUESTBOOK*/
function submitTherealguestbook()
{
	var d = new Date()
	param = d.getTime();
	var form = $j('#guestbooksign');
	
	document.forms['guestbooksign'].secure.value = param;

//inizio validazione
	var error_found = 0;			
	if(validateFormElement($j('#frm_nome')),'left') error_found++;	
	if(validateFormEmail($j('#frm_email')),'left') error_found++;
//fine validazione	
//Effect.BlindUp('form_guestbook_all_fields');

	if(error_found==0)
	{
		$j('#form_guestbook_all_fields').slideUp('slow');

		var options = { 
		
			beforeSubmit: function()
			{
				$j('#form_guestbook_loading').slideDown('slow');					
			},
			resetForm: true,
			error: function()
			{
				toConsole('Errore di invio');
				return false;
			},
		    success: function() 
		    { 			    			        

				$j('#form_guestbook_loading').slideUp('slow');
				$j('#form_guestbook_thanks').slideDown('slow');		
			  return false; 			        
			
		    } 
		}; 					 
		$('#guestbooksign').ajaxForm(options); 
	    $('#guestbooksign').ajaxSubmit(options); 
//	document.forms['guestbooksign'].submit();		
	
	}	
	return false;

}

function submitConfirm()
{
	var d = new Date()
	param = d.getTime();

	
//inizio validazione
	var error_found = 0;			
	if(validateFormElement($j('#frm_nome'),'left')) error_found++;
	if(validateFormEmail($j('#frm_email'),'left')) error_found++;
//fine validazione	


	if(error_found==0)
	{
		$j('#form_confirm_all_fields').slideUp('slow');

		var options = 
			{ 		
				beforeSubmit: function()
				{
					$j('#form_confirm_loading').slideDown('slow');					
				},
				resetForm: true,
				error: function()
				{
					toConsole('Errore di invio');
					return false;
				},
			    success: function() 
			    { 			    			        
	
					$j('#form_confirm_loading').slideUp('slow');
					$j('#form_confirm_thanks').slideDown('slow');		
				  return false; 			        
				
			    } 
			}; 					 
		$('#confirm_form').ajaxForm(options); 
	    $('#confirm_form').ajaxSubmit(options); 


	}	
	return false;
	
}

/*EOF GUESTBOOK*/


/* inizio funzioni gallery jquery */

function loadGallery()
{
	if(	$j('ul#miniature'))
	{
		//	$('.miniature').addClass('gallery_demo'); // adds new class name to maintain degradability
		$('ul#miniature').galleria(
		{
			history   : false, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main-image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) 
			{ // let's add some image effects for demonstration purposes	
				// fade in the image & caption
				image.css('display','none').fadeIn(1000);
				caption.css('display','none').fadeIn(1000);
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.5);
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				// add a title for the clickable image
				image.attr('title','Prossima immagine >>');
			},
			onThumb : function(thumb) 
			{ // thumbnail effects goes here	
				// fetch the thumbnail container
				var _li = thumb.parents('li');			
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.5';			
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);			
				// hover effects
				thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.5); } // don't fade out if the parent is active
				)
			}
		});
	}
}

function showDetail()
{
	var hover_element = $j('#mostra-dettagli span');
	var box = $j('#dettaglio');
	var close_button = $j('#close-button');
	hover_element.click(function() 
	{ 
	  box.slideDown('slow');
	});
	close_button.click(function()
	{
		 box.slideUp('normal');
	});

}
/* fine funzioni gallery jquery */
/****************************************/

function $j(argument)
{
	var result = jQuery(argument);
	if (result.length == 0) return null;
	else return result;
}


$(document).ready(function()
{
	
	//disabilitazione tasto destro
	$(document).bind("contextmenu",function(e){
		return false;
	});
	
	
	mainHeight();
	scrollGallery();
	if($j('#miniature-container'))
	{
		if($j('#mostra-dettagli')) showDetail();
		CSBfleXcroll('miniature-container');
	}
	/* init gallery script */
	loadGallery();
	/* init gallery script */

	$('#loader').hide();
	

});
