// ELIMINA I 'FILTER' SULLE FUNZIONI DI FAEDIN E FADEOUT
(function($){
		  ;
	$.fn.customFadeIn = function(speed, callback) {		
		$(this).fadeIn(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeTo = function(speed,to,callback) {
		return this.animate({opacity: to}, speed, function() {
			if (to == 1 && jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback();
		});
	};
})(jQuery);

$(document).ready(function()
{
	settaRel();
	$.historyInit(pageload);
	
	/* caso del primo caricamento */
	if(location.hash == '')
		$.historyLoad('home');	
		
});	
var currHash = "";
var currSubSection = "";
//FUNZIONE DI CALLBACK DELLA HISTORY (CARICA LE SEZIONI)
function pageload(hash)
{	
	//alert("hash : " + hash)
	if(currHash == hash)
	return;
	
	currHash = hash;
	
	var z_index = 27;
	if(hash == 'home'){
		z_index = 27;
	$('.container_centrale').addClass('home');
	}else
	{
		$('.container_centrale').removeClass('home');	
	}
		
	if(hash)
	{
		//alert(hash)
		if(hash != 'contatti')
			loadPage('container_centrale',hash+'.html',z_index)
		else
			loadPage('container_centrale',hash+'.cfm',z_index)
	}
	
}

function settaRel()
{
	$("a[rel='history']").click(function()
	{			
		if($(this).hasClass('menuSelect'))
			return;
		
		$('.menuSelect').removeClass('menuSelect');
		
		$('.btn_menu').css('background-position', 'top left');
		$('.btn_menu').mouseout(function()
		{				
			$(this).css('background-position', 'top left');			
		})
		
		$('.btn_menu').mouseover(function()
		{				
			$(this).css('cursor', 'pointer');
		})		
		
		$(this).find('.btn_menu').css('background-position', 'bottom left');
		$(this).find('.btn_menu').css('cursor', 'default');
		
		$(this).find('.btn_menu').mouseout(function()
		{				
			$(this).css('background-position', 'bottom left');
		})
		
		$(this).find('.btn_menu').mouseover(function()
		{				
			$(this).css('cursor', 'default');
		})
		
		$(this).addClass('menuSelect');
		
		var hash = this.href;													
		hash = hash.replace(/^.*#/, '');												
		$.historyLoad(hash);	
		return false;										  
	});	 		  	
									  
}


function settaBottoni()
{
	$('.btnCont').mouseover(function(){		
										 $(this).find('.btn_left, .btn_center,  .btn_right').css('backgroundPosition','bottom center');	
										 $(this).css('cursor','pointer');
									  });
	$('.btnCont').mouseout(function(){		
										 $(this).find('.btn_left, .btn_center,  .btn_right').css('backgroundPosition','top center');	
										 $(this).css('cursor','default')	;	
									  });		
	$('.btnCont').click(function(){
										$(this).find('.btn_center').find('a').trigger('click');
		
		})								  
									  		
}

function loadPage(parentId,filename,objectClicked)
{
	//alert("filename:" + filename + "objectClicked: " +objectClicked)
	if($(objectClicked).parent().hasClass('selected'))
	{
		return;
	}
	
	
	if(isNaN(objectClicked))
	{
		//alert($(objectClicked).html())
		currSubSection = $.trim($(objectClicked).html()).replace(/\s/g,'-');
		trackAnalytics(currHash+"/"+currSubSection);
		document.title="Sammontana " + currHash + " - " + currSubSection;
	}
	else
	{
		currSubSection = "";
		trackAnalytics(currHash);
		document.title="Sammontana " + currHash;
	}
	
	
	settaRel();
	
	var parentIdIndex = 27;
	
	$(objectClicked).blur();
	
	$('.selected').removeClass('selected');
	
	$(objectClicked).parent().addClass('selected');
		
	$.ajax({
		type: "GET",
		url: 'pagine/'+filename,				
		dataType: "html",
		success: function(html)
		{
			$('#'+parentId).css('zIndex',parentIdIndex)
			$('#'+parentId).html(''+html);			
		}
	});
	
	
}

function loadProduct(famiglia,gusto,titolo,testo)
{
	$('#titolo_gusto').html(''+titolo);
	$('#testo_gusto').html(''+testo+'.');
	$('#immagine_famiglia').attr('src','immagini/products/'+famiglia+'/'+gusto+'.jpg');
	$('#immagine_logo').attr('src','immagini/products/'+famiglia+'/'+gusto+'_logo'+'.jpg');
	trackEventAnalytics(famiglia,titolo.replace(/\s/g,'-'),'CLICK-VARIAZIONE-PRODOTTO');
}

function loadReserved()
{
	$.ajax({
			type: "GET",
			url: 'pagine/reserved.html',				
			dataType: "html",
			success: function(html)
			{			
				$.overlayAlert({content: html});			
			}
		});

}

function loadDownload()
{
	$.ajax({
			type: "GET",
			url: 'pagine/download.html',				
			dataType: "html",
			success: function(html)
			{			
				$.overlayAlert({content: html});			
			}
		});

}

function loadIncorretLogin()
{
	$.ajax({
			type: "GET",
			url: 'pagine/error_login.html',				
			dataType: "html",
			success: function(html)
			{			
				$.overlayAlert({content: html});			
			}
		});

}

function loadError_contatti()
{		
	$.ajax({
			type: "GET",
			url: 'pagine/error_contatti.html',				
			dataType: "html",
			success: function(html)
			{			
				$.overlayAlert({content: html});			
			}
		});
}

function feedback_ok_contatti()
{	
	$.ajax({
			type: "GET",
			url: 'pagine/messaggio_email_inviata.html',				
			dataType: "html",
			success: function(html)
			{			
				$.overlayAlert({content: html});
				trackEventAnalytics('contatti','form-contattaci','INVIO-FORM-CONTATTI');
			}
		});
}

var currentImage = 1;
var massimoNumeroImmagini = 1;
var currentFolfer = '';


function loadImage(immagine,folder,maxNumeroImmagini)
{

	currentImage = immagine;
	massimoNumeroImmagini = maxNumeroImmagini;
	currentFolfer = folder;
	
	$.ajax({
			type: "GET",
			url: 'pagine/gallery.html',				
			dataType: "html",
			success: function(html)
			{			
				var prepath = 'immagini/gallery_grande/'+folder+'/';				
				var $nuovo = $(html).clone();
				$nuovo.find('.gallery_container').css('visibility','visible');
				$nuovo.find('#centro_immagine').find('#imageWrapper').find('#immagine').attr('src',''+prepath+currentImage+'.jpg');				
				$.overlayAlert({content: $nuovo});
				$(html)
				.find('#immagine')
				.load(function(){	
							var $div_centrale = $('#centro_immagine');
							$div_centrale.width($('#immagine').width());
							$('.top > .centro').add('.bottom > .centro').css('width',$div_centrale.width());
							$('.center > .sinistra').add('.center > .destra').css('height',$div_centrale.height());

							$('.gallery_container').width($('#immagine').width() + $('.top > .sinistra').width() + $('.top > .destra').width() );
							$(window).trigger('resize');
							
							$('.gallery_container').css('visibility','visible');
					
					}).error(function(){
							
					}).attr('src',prepath+currentImage+'.jpg');
										
			}
		});	
}





/*function loadImage(immagine,folder,maxNumeroImmagini)
{

	currentImage = immagine-1;
	massimoNumeroImmagini = maxNumeroImmagini;
	currentFolfer = folder;
	
	$.ajax({
			type: "GET",
			url: 'pagine/gallery.html',				
			cache:false,
			dataType: "html",
			success: function(oggetto)
			{			
				//$.overlayAlert({content: oggetto});
				var prepath = 'immagini/gallery_grande/'+folder+'/';			
				
				$(window).trigger('resize');
				
				var $nuovo = $(oggetto).clone();
				$nuovo.find('.gallery_container').css('visibility','visible');
				$nuovo.find('#centro_immagine').find('#imageWrapper').find('#immagine').attr('src',''+prepath+currentImage+'.jpg');
				alert($nuovo.html());				
				
				$.overlayAlert({content: $nuovo});
				
				//$(oggetto).find('#centro_immagine').find('#imageWrapper').find('#immagine').attr('src',''+prepath+currentImage+'.jpg');
				
				/*var $img = $('<img />');
				$img.attr('id', 'immagine');
				$img.load(function()
				{
					// alert($img.attr('src'));
					$img.appendTo($(html).find('#wrapperImmagine'));
					// alert($img.parent().html());
					var $div_centrale = $('#centro_immagine');
					$div_centrale.width($('#immagine').width());
					$('.top > .centro').add('.bottom > .centro').css('width',$div_centrale.width());
					$('.center > .sinistra').add('.center > .destra').css('height',$div_centrale.height());
					$('.gallery_container').width($('#immagine').width() + $('.top > .sinistra').width() + $('.top > .destra').width() );
					$(window).trigger('resize');
					$('.gallery_container').css('visibility','visible');
					
				}).attr({src: prepath + '' + currentImage+'.jpg', id: 'immagine'});*/
				
				/*
				$(html)
				.find('#immagine')
				.load(function(){	
							var $div_centrale = $('#centro_immagine');
							$div_centrale.width($('#immagine').width());
							$('.top > .centro').add('.bottom > .centro').css('width',$div_centrale.width());
							$('.center > .sinistra').add('.center > .destra').css('height',$div_centrale.height());

							$('.gallery_container').width($('#immagine').width() + $('.top > .sinistra').width() + $('.top > .destra').width() );
							$(window).trigger('resize');
							
							$('.gallery_container').css('visibility','visible');
							
					
					}).error(function(){
						
					}).attr('src',prepath+currentImage+'.jpg');
					
					*/	
										
		/*	}
		});	
}*/

function nextImage()
{
	currentImage++;
	if(currentImage > massimoNumeroImmagini)
	{		
		currentImage--;
		return;
	}
	var prepath = 'immagini/gallery_grande/'+currentFolfer+'/';	
	
	$('#immagine').load(function(){																		 
									var $div_centrale = $('#centro_immagine');
									$div_centrale.width($('#immagine').width());
									$('.top > .centro').add('.bottom > .centro').css('width',$div_centrale.width());
									$('.center > .sinistra').add('.center > .destra').css('height',$div_centrale.height());		
									$('.gallery_container').width($('#immagine').width() + $('.top > .sinistra').width() + $('.top > .destra').width() );
									$(window).trigger('resize');									
									$('.gallery_container').css('visibility','visible');
								 }).attr('src',prepath+currentImage+'.jpg');
								
	
}

function prevImage()
{
	currentImage--;
	if(currentImage < 0)
	{
		currentImage++;	
		return;
	}
	
	var prepath = 'immagini/gallery_grande/'+currentFolfer+'/';	
	$('#immagine').load(function(){																		 
									var $div_centrale = $('#centro_immagine');
									$div_centrale.width($('#immagine').width());
									$('.top > .centro').add('.bottom > .centro').css('width',$div_centrale.width());
									$('.center > .sinistra').add('.center > .destra').css('height',$div_centrale.height());
		
									$('.gallery_container').width($('#immagine').width() + $('.top > .sinistra').width() + $('.top > .destra').width() );
									$(window).trigger('resize');
									
									$('.gallery_container').css('visibility','visible');
								 }).attr('src',prepath+currentImage+'.jpg');;
}

(function($){

	$.fn.customSelect = function(pParams)
	{
		
		var params = $.extend({
			durationOpen: 300,
			defaultOption: 'Seleziona posizione',
			maxVoice: 4
		}, pParams);
		
		var isOpen = false;
		var $select = $(this);
		var $wrapper = $('<div><span class="customSelectText">' + params.defaultOption + '</span><span class="customSelectButton"></span></div>');
		var $options = $('<ul></ul>');
		var heightLink = 22;
		var heightOptions;
		
		$select.css({width: 0, height:0, visibility: 'hidden', position:'absolute', zIndex: 99});
		
		var $voices = $select.find('option');
		
		
		$voices.each(function()
		{
			var $link =	$('<a href="#' + $(this).val()+ '">' + $(this).html() + '</a>');
			var $this = $(this);
			$link.click(function()
			{	
				$(this).blur();
				$wrapper.find('.customSelectText').html($(this).html());
				hideOptions();
				$select.val($this.val());
				return false;
			});
			$link.appendTo($options).wrap('<li />');
		});
		heightOptions = $voices.length * heightLink;
		if(heightOptions > params.maxVoice * heightLink)
		{
			heightOptions = params.maxVoice * heightLink;
			$options.find('a').css({width: 222});
		}
		$options.css({height : heightOptions});
		$all = $('<div class="customSelect" style="z-index:100"></div>');
		$wrapper.appendTo($all);
		$options.appendTo($all);
		$select.after($all);
		
		$wrapper.click(function()
		{
			if(isOpen)
				hideOptions();
			else
			{
				$options.animate({height: heightOptions}, params.durationOpen, function()
				{
					isOpen = true;
					$(document).bind('click',hideOptions);
				});
			}
		});
		
		function hideOptions()
		{
			$options.css({display: 'none', height: 0});
			isOpen = false;
			$(document).unbind('click', hideOptions);
		}
	}
})(jQuery);



function resetCampi()
{
	$('input','#form_contatti').val('').removeAttr('selected');
	
}


