// JavaScript Document


//Menu DROPDOWN
var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function menu_open()
{	menu_canceltimer();
	menu_close();
	ddmenuitem = $(this).find('ul').eq(0).css('display', 'block');

}

function menu_close()
{	if(ddmenuitem) ddmenuitem.css('display', 'none');}

function menu_timer()
{	closetimer = window.setTimeout(menu_close, timeout);}

function menu_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}
/*
$(document).ready(function()
{	});
*/
document.onclick = menu_close;


/**
 *  jquery.popupt
 *  (c) 2008 Semooh (http://semooh.jp/)
 *
 *  Dual licensed under the MIT (MIT-LICENSE.txt)
 *  and GPL (GPL-LICENSE.txt) licenses.
 *
 **/
 
 
(function($){
	$.fn.extend({
		imghover: function(opt){
			return this.each(function() {
        opt = $.extend({
            prefix: '',
            suffix: '_o',
            src: '',
            btnOnly: true,
            fade: false,
            fadeSpeed: 500
          }, opt || {});

        var node = $(this);
				if(!node.is('img')&&!node.is(':image')){
          var sel = 'img,:image';
          if (opt.btnOnly) sel = 'a '+sel;
          node.find(sel).imghover(opt);
          return;
        }

        var orgImg = node.attr('src');

        var hoverImg;
        if(opt.src){
          hoverImg = opt.src;
        }else{
          hoverImg = orgImg;
          if(opt.prefix){
            var pos = hoverImg.lastIndexOf('/');
            if(pos>0){
              hoverImg = hoverImg.substr(0,pos-1)+opt.prefix+hoverImg.substr(pos-1);
            }else{
              hoverImg = opt.prefix+hoverImg;
            }
          }
          if(opt.suffix){
            var pos = hoverImg.lastIndexOf('.');
            if(pos>0){
              hoverImg = hoverImg.substr(0,pos)+opt.suffix+hoverImg.substr(pos);
            }else{
              hoverImg = hoverImg+opt.suffix;
            }
          }
        }

        if(opt.fade){
          var offset = node.offset();
          var hover = node.clone(true);
          hover.attr('src', hoverImg);
          hover.css({
            position: 'absolute',
            left: offset.left,
            top: offset.top,
            zIndex: 1000
          }).hide().insertAfter(node);
          node.mouseover(
            function(){
              var offset=node.offset();
              hover.css({left: offset.left, top: offset.top});
              hover.fadeIn(opt.fadeSpeed);
              node.fadeOut(opt.fadeSpeed,function(){node.show()});
            }
          );
          hover.mouseout(
            function(){
              node.fadeIn(opt.fadeSpeed);
              hover.fadeOut(opt.fadeSpeed);
            }
          );
        }else{
          node.hover(
            function(){node.attr('src', hoverImg)},
            function(){node.attr('src', orgImg)}
          );
        }
			});
		}
	});
})(jQuery);

//fine menu DROPDOWN



function update()
{
    $.post("/_addon/server.php", { 
					/*tuonome: $("#tuonome").val(),
					tuaemail: $("#tuaemail").val(),
					amiconome: $("#amiconome").val(),
					amicoemail: $("#amicoemail").val()
					*/
				}, 
				function(data){ 
					$("#messaggioi").html('');
					}
				);

	$.post("/_addon/serverp.php", { 
					/*tuonome: $("#tuonome").val(),
					tuaemail: $("#tuaemail").val(),
					amiconome: $("#amiconome").val(),
					amicoemail: $("#amicoemail").val()
					*/
				}, 
				function(data){ 
					$("#messaggioip").html('');
					}
				);

	
 
   // setTimeout('update()', 1000);
}


$(document).ready(function(){
		update();
		
		//segnala ad un amico
$("#button").click(    


			function() 
		    { 		
				
				 var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
				 
				if(
				   	($("#tuonome").val() == '')
					||
					($("#tuaemail").val() == '')
					||
					($("#amiconome").val() == '')
					||
					($("#amicoemail").val() == '')
					||
					($("#messaggio").val() == '')
									
				  ){
					
						alert('* = campi obbligatori');
						
				}
				else if(
						(!email_reg_exp.test($("#tuaemail").val()))
					||
					(!email_reg_exp.test($("#amicoemail").val()))
					){
					
						alert('Inserire indirizzi email corretti');
				}
				else{//inizio else
				
				//loading....
				$('#screen').hide(); 
	 			$("#contentLoading").show();  
				
				//invio
			    $.post("/_addon/server.php", 
				{ 
					lingua: $("#lingua").val(),
					tuonome: $("#tuonome").val(),
					tuaemail: $("#tuaemail").val(),
					amiconome: $("#amiconome").val(),
					amicoemail: $("#amicoemail").val(),
					messaggio: $("#messaggio").val()

},
				function(data){	
				//recupero dati 
				$("#messaggioi").html(data); 
				$("#tuonome").val(''); 
				$("#tuaemail").val(''); 
				$("#amiconome").val(''); 
				$("#amicoemail").val(''); 
				$("#messaggio").val(''); 

				//visualizzo
				$("#contentLoading").hide("fast");
				$('#screen').show();  


				}
				);
				
				}//fine else
				 

		    }
			
	    );
//fine 

$("#buttonp").click(    


			function() 
		    { 		
				
				 var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
				 
				if(
				   	($("#tuonomep").val() == '')
					||
					($("#tuaemailp").val() == '')
					||
					($("#amiconomep").val() == '')
					||
					($("#amicoemailp").val() == '')
					||
					($("#messaggiop").val() == '')
									
				  ){
					
						alert('* = campi obbligatori');
						
				}
				else if(
						(!email_reg_exp.test($("#tuaemailp").val()))
					||
					(!email_reg_exp.test($("#amicoemailp").val()))
					){
					
						alert('Inserire indirizzi email corretti');
				}
				else{//inizio else
				
				//loading....
				$('#screenp').hide(); 
	 			$("#contentLoadingp").show();  
				
				//invio
			    $.post("/_addon/serverp.php", 
				{ 
					lingua: $("#lingua").val(),
					tuonomep: $("#tuonomep").val(),
					linkinvia: $("#linkinvia").val(),
					nomeinvia: $("#nomeinvia").val(),
					tuaemailp: $("#tuaemailp").val(),
					amiconomep: $("#amiconomep").val(),
					amicoemailp: $("#amicoemailp").val(),
					messaggiop: $("#messaggiop").val()

},
				function(data){	
				//recupero dati 
				$("#messaggioip").html(data); 
				$("#tuonomep").val(''); 
				$("#tuaemailp").val(''); 
				$("#amiconomep").val(''); 
				$("#amicoemailp").val(''); 
				$("#messaggiop").val(''); 

				//visualizzo
				$("#contentLoadingp").hide("fast");
				$('#screenp').show();  


				}
				);
				
				}//fine else
				 

		    }
			
	    );

	$('.img_home_ruota').fadeIn("slow");
	$('.img_home_ruota').cycle({ 
    	fx:    'fade', 
    	delay: -2000,
		random: 1,
		pause:1
	});
		
		
		$('#dropdown > li').bind('mouseover', menu_open);
		$('#dropdown > li').bind('mouseout',  menu_timer);
		//attivo lightbox	
    
	  $('a.lightbox').lightBox();
		
		//attivo tabs
		$('#container-1 ul').tabs();
		
		$('img.bandiere').imghover({suffix: '_on'});
		
		$('#speciale_ruota').cycle ({
    fx:    'fade',
    delay: -5000 ,
	random: 0,
	pause:   1,
	pager: '#pagersp'
	
});		
		
		$('#cicla_offerte').show();
		  
		$('#cicla_offerte').cycle({ 
    fx: 'fade',
	random: 0,
	pause:   1
	
});		
		
		$('#routa_home_b').show("fast");
	/*	$('#routa_home_b').cycle({ 
    fx:    'fade',
    //delay: -5000,
	random: 0,
	pause:   1

	
});		*/
		
		
		$("#tabellaprodotti")
			.tablesorter({widthFixed: true, sortList: [[2,0]]})
			.tablesorterPager({container: $("#pagprodotti")});


		
			
		$('ul.gallery').galleria({
			history   : true, // 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
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					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.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Clicca per visionare la 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.3';
				
				// 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.3); } // don't fade out if the parent is active
				)
			}


		
	});


	
		
    });