jQuery(function(){
    jQuery('a[href*=#]').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        && location.hostname == this.hostname) {
            var target = jQuery(this.hash);
            target = target.length && target
            || jQuery('[name=' + this.hash.slice(1) +']');
            if (target.length) {
                var targetOffset = target.offset().top;
                jQuery('html,body')
                .animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
});


  jQuery(function(){
	var hl = jQuery('#hidden_lang');
	var inside = false;

	jQuery('#lang_selection').hover(function(){ 
		inside=true; 
	}, function(){ 
		inside=false; 
	});

	jQuery('body').click(function(){ 
		if(!inside) hl.slideUp(100);
	});

	hl.slideUp(0);
	jQuery('#lang_selection a:first').click(function(){
		if(hl.is(':visible')){
			hl.slideUp(100);
		} else {
			hl.slideDown(100);
		}
		return false;
	});
	jQuery('#lang_selection').click(function(){
		if(hl.is(':visible')){
			hl.slideUp(100);
		} else {
			hl.slideDown(100);
		}
	});
});


/************************************************************************************************************/
jQuery(function() {
    jQuery('input[type=text]').addClass('onBlur');
    jQuery('textarea').addClass('onBlur');
	jQuery('input[type=text]').focus(function() {
		jQuery(this).removeClass('onBlur');
        if (this.value == this.defaultValue){
        	this.value = '';
    	}
        if(this.value != this.defaultValue){
	    	this.select();
        }
    });
    jQuery('input[type=text]').blur(function() {
        if (this.value == ''){
			jQuery(this).addClass('onBlur');
        	this.value = (this.defaultValue ? this.defaultValue : '');
    	}
    });
	jQuery('textarea').focus(function() {
		jQuery(this).removeClass('onBlur');
        if (this.value == this.defaultValue){
        	this.value = '';
    	}
        if(this.value != this.defaultValue){
	    	this.select();
        }
    });
    jQuery('textarea').blur(function() {
        if (this.value == ''){
			jQuery(this).addClass('onBlur');
        	this.value = (this.defaultValue ? this.defaultValue : '');
    	}
    });
});


/************************************************************************************************************/
/*jQuery(function(){
	jQuery('nav ul').superfish({ 
		autoArrows: false 
	});
});*/
jQuery(window).one('load',function(){
	jQuery('nav > ul > li').each(function(){ 
		var t = jQuery(this);
		var tW2 = t.width()*0.5;
		t.has('ul').prepend('<div class="submenu_arrow" style="left: '+(tW2-5)+'px" />');
	});
});
jQuery(function(){
	function megaHoverOver(){
		var navOff = jQuery('nav').offset();
		jQuery(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
		(function(jQuery) {
			//Function to calculate total width of all ul's
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				jQuery(this).find("ul").each(function() { //for each ul...
					rowWidth += jQuery(this).width()+40; //Add each ul's width together
				});
			};
		})(jQuery); 
	
		if ( jQuery(this).find(".row").length > 0 ) { //If row exists...
	
			var biggestRow = 0;	
	
			jQuery(this).find(".row").each(function() {	//for each row...
				jQuery(this).calcSubWidth(); //Call function to calculate width of all ul's
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
	
			jQuery(this).find(".sub").css({'width' :biggestRow}); //Set width
			jQuery(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
	
		} else { //If row does not exist...
	
			jQuery(this).calcSubWidth();  //Call function to calculate width of all ul's
			jQuery(this).find(".sub").css({'width' : rowWidth}); //Set Width
	
		}

		var subOff = jQuery(this).offset();
		var subWidth = jQuery(this).find(".sub").width()+61;
		
		if((subOff.left+rowWidth)>(navOff.left+1000)){
			jQuery(this).find(".sub").css({'margin-left':'-'+((subOff.left+subWidth)-(navOff.left+1000))+'px'}); 
		}
		
	}
	//On Hover Out
	function megaHoverOut(){
	  jQuery(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
		  jQuery(this).hide();  //after fading, hide it
	  });
	}
	
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	jQuery("nav ul li .sub").css({'opacity':'0'});
	jQuery("nav ul li").hoverIntent(config);
});



/************************************************************************************************************/
jQuery(window).one('load',function() {
	jQuery('.main_cycle').each(function(){
		var t = jQuery(this);
		var p = t.next('.nav').find('.prev');
		var n = t.next('.nav').find('.next');
		t.cycle({
			fx: 'fade',
			timeout: 7000,
			pause: true,
			before: onBefore,
			next: n,
			prev: p,
			after: onAfter
		});
		jQuery(this).prevAll('#main_caption').hover(function(){
			t.cycle('pause');
		},function(){
			t.cycle('resume');
		});
	});

	jQuery('.cycle_stop').each(function(){
		var t = jQuery(this);
		var p = t.prev('.nav').find('.prev');
		var n = t.prev('.nav').find('.next');
		t.cycle({
			fx: 'scrollHorz',
			timeout: 5000,
			next: n,
			prev: p,
			speed: 500,
			pause:   1 
		});
	});
});
function onAfter(curr, next, opts) {
    var index = opts.currSlide;
	var h = 341; //jQuery('.cycle').height();
	jQuery('#main_caption').addClass('started');
	jQuery('#main_caption div').eq(index).show();
	var hC = jQuery('#main_caption div').eq(index).height();
	if( (h-(hC+40))*0.5 > 50){
		mT = 50;	
	} else {
		mT = (h-(hC+40))*0.5
	}
	jQuery('#main_caption').css({'margin-top':mT}).animate({'margin-left':'0'},{
        duration: 500, // how fast we are animating
        easing: 'easeOutQuart' // the type of easing
    });
	jQuery('#arrow_balloon').css({'margin-top':mT+30}).animate({'width':'10px'},300);
}
function onBefore(curr, next, opts) {
    var index = opts.currSlide;
	jQuery('#main_caption.started').animate({'margin-left':'253px'},250,function(){
		jQuery('#main_caption div').eq(index).hide();
	});
	jQuery('#arrow_balloon').animate({'width':'0'},150);
}


/************************************************************************************************************/
jQuery(function(){
	jQuery("a.map[rel='iframe']").colorbox({iframe:true, innerWidth:800, innerHeight:600, rel:'nofollow', onOpen: function(){
        $('iframe').css('visibility','hidden');
    },
	onClosed: function(){
    $('iframe').css('visibility','visible');
    }
});
	jQuery("a.colorbox").colorbox({innerWidth:900, innerHeight:600, rel:'nofollow'});
	jQuery("a.colorbox_inline").each(function(){
		var h = jQuery(this).attr('href');
		jQuery(this).colorbox({width:"30%", height:"30%", inline:true, href:h});
	});
});


/************************************************************************************************************/
jQuery(function(){
	jQuery('#approfondimenti_button > div:even').not('.clear_dashed').each(function(){
		jQuery(this).css({'background':'#e1e1e1'});
	});
});


/************************************************************************************************************/
jQuery(function(){
	jQuery('aside > div > ul:last').css({'border-bottom':0});
});


/************************************************************************************************************/
jQuery(window).one('load',function(){
	jQuery('.jqzoom').each(function(){
		var t = jQuery(this);
		var h = jQuery('img',t).height();
		var w = jQuery('img',t).width();
		t.css({'width':w,'height':h});
		var off = ((365-w)*0.5)-1;
		var off2 = ((364-h)*0.5)-2;
		t.jqzoom({
			zoomType: 'standard',
			lens:true,
			preloadImages: false,
			xOffset: off,
			yOffset: 'auto',
			zoomWidth: 379,
			zoomHeight: 363
		});
		jQuery('.zoomWindow',t).css({'top':'-'+off2+'px'});
	});
});


/************************************************************************************************************/
jQuery(function(){
	jQuery('.prodotto_quadro').each(function(){
		var t = jQuery(this);
		var hT = t.height();
		var wT = t.width();
		var e = t.find('.excerpt');
		e.height(hT-80).width(wT-40).animate({'opacity':0},0);
		t.hover(function(){
			e.stop(true, false).animate({opacity:1},200);
		},function(){
			e.stop(true, false).animate({'opacity':0},200);
		});
	});
});


/************************************************************************************************************/
jQuery(function(){
	jQuery('.prodotto_quadro .excerpt').each(function(){
		var t = jQuery(this);
		var a = t.nextAll('a').attr('href');
		t.click(function(){
			window.location.href=a;
		});
	});
});


/************************************************************************************************************/
jQuery(function(){
	jQuery('#list_categorie > ul > li').has('ul').each(function(){
		var t = jQuery(this);
		var s;
		function seThide(){
			jQuery('ul, .left_arrow',t).stop(true,false).animate({'margin-left':0,'opacity':0},100,function(){
				jQuery('ul, .left_arrow',t).hide();
			})
		}
		function seTout(){
			s = setTimeout(seThide,500);
		}
		jQuery('ul',this).before('<div class="left_arrow"></div>');
		jQuery(this).hoverIntent({
			over: function(){
				jQuery('#list_categorie li ul, #list_categorie li .left_arrow').hide();
				clearTimeout(s);
				jQuery('ul, .left_arrow',this).stop(true,false)
					.show()
					.animate({'margin-left':5,'opacity':1},200);
				var offT = jQuery('ul',this).offset();
				var hT = jQuery('ul',this).height() + 50 + offT.top;
				var hL = jQuery('#list_categorie').height() + 50;
				var offF = jQuery('#list_categorie').offset();
				if(hT>(offF.top+hL)){
					jQuery('ul',this).css({'margin-top':'-'+(hT-(offF.top+hL))+'px'});
				}
			},
			out: seTout
		});
		jQuery('ul li a',this).hover(function(){
			jQuery('.img',this).stop(true,false).animate({'opacity':0.1},200);
		},function(){
			jQuery('.img',this).stop(true,false).animate({'opacity':1},200);
		});
		jQuery('#list_categorie > ul > li:first a').css({'border-top':'0'});
		jQuery('#list_categorie > ul > li:last a').css({'border-bottom':'0'});
		jQuery('#list_categorie > ul > li > a:odd').css({'background-color':'#eee','color':'#e07f0d'});
	});
});

/************************************************************************************************************/
jQuery(function(){
	jQuery('#table_riassunto_carrello tr:odd').find('td').css({'background-color':'#eee'});
	jQuery('#table_riassunto_carrello tr:odd').find('a').css({'color':'#e07f0d'});
});

/*
------------- password ---------------------------------------------------------------------------------------*/
jQuery(document).ready(function($) {
	$(".password").passStrength({
		userid:	"#UserUsername"
	});
});

/*
------------- messages ---------------------------------------------------------------------------------------*/

jQuery(document).ready(function($) {
	jQuery("#message").live('click', function(){
         jQuery(this).hide('slow');
      });
});

/*
------------- user form ---------------------------------------------------------------------------------------*/

jQuery(document).ready(function($) {
	jQuery("#UserRegionId").change(function(e) {
         alert($(this).val());
		 
		 $.get("http://www.igffornitalia.com/districts/lists",{'val': $('#UserRegionId :selected').val()},function(data){ $('#UserDistrictId').find('option').remove().end().append(data); });
      });
});


/*
------------- user form ---------------------------------------------------------------------------------------*/

jQuery(document).ready(function($) {
	jQuery("#UserRegionId").change(function(e) {
         alert($(this).val());
		 
		 $.get("http://www.igffornitalia.com/districts/lists",{'val': $('#UserRegionId :selected').val()},function(data){ $('#UserDistrictId').find('option').remove().end().append(data); });
      });
});
