// HANDLE LIVESTREM WINDOWS
function handleLiveStreamingWindow() {
	$(document).ready(function() {
		//LIVE STREAMING -- FATTORE  MAMMA
		$("#boxlive .slidebtn#slideOpen").click(function() {
			$("#boxlive #sx #video").html('Loading Live Streaming...');
			$("#boxlive").animate( {"width": "910px"}, 500 , function() {
				// Animation complete
				$("#boxlive .slidebtn#slideOpen").hide(); 
				//$("#boxlive #sx #video").html('<iframe src="http://cdn.livestream.com/embed/fattoremamma?layout=2&autoPlay=false" width="560" height="592" style="border:0;outline:0" frameborder=0 scrolling=no></iframe>');
				$("#boxlive #video").html('<iframe src="http://player.vimeo.com/video/25172545?byline=0&amp;portrait=0" width="560" height="315" frameborder="0"></iframe>');
				//$("#boxlive #sx #chat").html('<iframe width="480" height="242" src="http://cdn.livestream.com/embed/fattoremamma?layout=6&showTimestamp=true" style="border:0;outline:0" frameborder="0" scrolling="no" bgcolor="#ffffff"></iframe>');
			});			
		});

		$("#boxlive .slidebtn#slideClose").click(function() { 
			$("#boxlive .slidebtn#slideOpen").show();
			$("#boxlive").animate( {"width": "51px"}, 500 , function() {
				// Animation complete
				$("#boxlive #video").html('');
				//$("#boxlive #sx #chat").html('');
			});			
		});	
	});
}

function initMenu(){
	$('#menudropdown').hide();

	$('#menutop li').mouseenter(function(){
		if (noFirst){
			noFirst=false;
			idTimer = window.setTimeout("menuTopMouseEnter('"+$(this).attr('fam')+"');",300);
			return;
		}
		menuTopMouseEnter($(this).attr('fam'));
	});
	$('#menutop li').mouseleave(function(){
		window.clearTimeout(idTimer);
		$('#menutop li').removeClass('hover');
	});

	$("#menutop li").mouseleave(outMenuCategoria);

	$("#menudropdown").mouseenter(showMenuCategoria);
	$("#menudropdown").mouseleave(outMenuCategoria);

	$('#dd_close').click(function(){
		window.clearTimeout(idTimer);
		hideMenuCategoria();		
	});
}

var noFirst=false;
var idTimer;
function showMenuCategoria(posli,wli){
	window.clearTimeout(idTimer);	
	$("#menudropdown").show();
	
	//SET THE MENU POSITION
	var menuPos = 0;
	var liLeft = posli.left+5;
	var liRight = posli.left+(wli-3);
	if ((liLeft+666) >= 960) {
		if ((liRight-666) >= 0) { menuPos = liRight-650;}
		else { menuPos = (liLeft+(wli/2))-335;}
	}
	else { menuPos = liLeft-10; }
	
	$("#menudropdown").css({ left:menuPos }); 
}
function outMenuCategoria(){
	idTimer = window.setTimeout("hideMenuCategoria()",300);
}
function hideMenuCategoria(){
	$('#menutop li').removeClass('hover2');
	$("#menudropdown").hide();
}

function menuTopMouseEnter(fam){
	var li = $('#menutop li[fam='+fam+']');
	$('#menutop li').removeClass('hover');
	$(li).addClass('hover');
	hideMenuCategoria();
	
	var fam=$(li).attr('fam');
	$('#dd_body .fam').hide();
	$('#dd_body #'+fam).show();

	$(li).addClass('hover2');
	var posli = $(li).position();
	var wli = $(li).width();
	showMenuCategoria(posli,wli);

};

function initHome(){
	// SCROLL EVENT
	$(window).scroll(function() {
		// Handle Box Position
		var yOffSett = $(window).scrollTop();
		var posFooter = $("div#footer").offset();
		var winHeight = $(window).height();
		var posBox = (winHeight - 130) + yOffSett;
		if ((posBox+130) >= posFooter.top) { posBox = posFooter.top-130 }
		else if (posBox < 480 ) { posBox = 480};
		var strBox = posBox+"px";
		$("div#box_cont").css({ top:strBox });
	}); 
	
	// WINDOW RESIZE EVENT
	$(window).resize(function() {
		// HANDLE height flashome
		var fhWidth = $("div#flashone").width();
		//var fhHeight = fhWidth / 1.5421686;
		//var roundH = Math.round(fhHeight);
		//if (roundH < 664 ) { roundH = 664};
		//alert(roundH);
		var fhHeight = (fhWidth / 16) * 9;
		$("div#flashone").height(fhHeight);
		
		// Handle Box Position
		var posFooter = $("div#footer").offset();
		var winHeight = $(window).height();
		var posBox = winHeight - 130;
		if ((posBox+130) >= posFooter.top) { posBox = posFooter.top-130 }
		else if (posBox < 480 ) { posBox = 480};
		var strBox = posBox+"px";
		$("div#box_cont").css({ top:strBox });
	}); 
	
	// DOCUMENT LOADED EVENT
	$(document).ready(function() {
		// HANDLE height flashome
		var fhWidth = $("div#flashone").width();
		//var fhHeight = fhWidth / 1.5421686;
		var fhHeight = (fhWidth / 16) * 9;
		//var roundH = Math.round(fhHeight);
		//if (roundH < 664 ) { roundH = 664};
		$("div#flashone").height(fhHeight);
	
		// HANDLE BOX HOME PAGE
		// Handle Box Position
		var posFooter = $("div#footer").offset();
		var winHeight = $(window).height();
		var posBox = winHeight - 130;
		if ((posBox+130) >= posFooter.top) { posBox = posFooter.top-130 }
		else if (posBox < 480 ) { posBox = 480};
		var strBox = posBox+"px";
		$("div#box_cont").css({ top:strBox });
	
		// Box Scrollable
		// Initialize scrollable 
		$("div.scrollable").scrollable({
			size: 4, 
			keyboard : false,
			clickable: false
		});
		// Loop Circular
		$("div.scrollable").circular();
		// Navi
		$("div.scrollable").navigator();
		// Auto Scroll
		$("div.scrollable").autoscroll({
			autoplay: true,
			interval: 15000,
			steps: 4
		});
			
	}); 
}

// HANDLE FEATURES OVERVIEW PRODUCT
function handleFeatureOverview() {
	$(document).ready(function() {
		$("#featcont .items li").click(function() { 
			
				if ($(this).hasClass("feat_active")) return;
				$(".feat_active").removeClass("feat_active");
				$(this).addClass("feat_active");
				
				// calclulate large image\'s URL based on the thumbnail URL 
				var url = $(this).find("img").attr("src").replace("_ICON_", "_PHOTO_");
				var label = $(this).find("img").attr("alt"); 
				
				// get handle to element that wraps the image and make it semitransparent 
				var wrap = $("#imgcont").fadeTo("medium", 0, function(){
				// the large image 
				var img = new Image(); 
				 
					// call this function after it\'s loaded 
					img.onload = function() { 
				 
						// make wrapper fully visible 
						wrap.fadeTo("fast", 1); 
				 
						// change the image & alt
						wrap.find("img").attr("src", url); 
						wrap.find("img").attr("alt", label); 
				 
					}; 
					 
					// begin loading the image from flickr 
					img.src = url;
					
				}); 
				//var wrap = $("#imgcont").hide(); 

		// when page loads simulate a "click" on the first image 
		}).filter(":first").click();
		
		$("#featcont .items li").mouseover(function() {$(this).addClass("feat_over");}).mouseout(function(){$(this).removeClass("feat_over");});
	});
}


// HANDLE FEATURES USA OVERVIEW PRODUCT
function handleFeatureUsaOverview() {
	$(document).ready(function() {
		
		$("#imgintro img[rel]").overlay({
			effect: 'apple',
			top:  	'1%'
		});
		
		var label_img = $('#imgintro').find("img").attr("alt"); 
		$('#img_zoom .details h3').html(label_img);
	
		$("#featuresprod").find("#feat_text").css("display","none");
		
		$("#featcont .scrollable").scrollable({
			size: 4, 
			clickable: true,
			keyboard : false
		});
	
		$("#featcont .items li").click(function() { 
			
				if ($(this).hasClass("feat_active")) return;
				$(".feat_active").removeClass("feat_active");
				$(this).addClass("feat_active");
				
				// calclulate large image\'s URL based on the thumbnail URL 
				var url = $(this).find("img").attr("src").replace("_ICON_", "_PHOTO_");
				var label = $(this).find("img").attr("alt"); 
				
				// Text Caption
				$("#featuresprod").find("#feat_text").html('Loading....');
				$("#featuresprod").find("#feat_text").animate( { width:"0%"},200, function() {
					// Animation complete
					$("#featuresprod").find("#feat_text").animate( { width:"92%"}, 500 , function() {
							// Animation complete
							$("#featuresprod").find("#feat_text").html(label);
					});			
				});	

				// get handle to element that wraps the image and make it semitransparent 
				var wrap = $("#imgcont img").fadeTo("medium", 0, function(){
				// the large image 
				var img = new Image(); 
					// call this function after it\'s loaded 
					img.onload = function() { 
				 
						// make wrapper fully visible 
						wrap.fadeTo("fast", 1); 
				 
						// change the image & alt
						wrap.attr("src", url); 
						wrap.attr("alt", label); 
				 
					}; 
					 
					// begin loading the image from flickr 
					img.src = url;
	
				}); 
				//var wrap = $("#imgcont").hide(); 

		// when page loads simulate a "click" on the first image 
		}).filter(":first").click();
		
		$("#featcont .items li").mouseover(function() {$(this).addClass("feat_over");}).mouseout(function(){$(this).removeClass("feat_over");});
	});
}

// HANDLE FEATURES OVERVIEW MODULAR PRODUCT
function handleFeatureModularOverview2(idTab) {
	$(document).ready(function() {
		$("#contimgintro img[rel]").overlay({
			effect: 'apple',
			top:  	'1%'
		});
	
	
		$("#tab_" + idTab).find("#feat_text").css("display","none");
		
		$("#featcont_" + idTab + " .scrollable").scrollable({
			size: 4, 
			clickable: true,
			keyboard : false
		});
				
		$("#featcont_" + idTab + " .items li").click(function() { 
			
				if ($(this).hasClass("feat_active")) return;
				$("#featcont_" + idTab + " .items li.feat_active").removeClass("feat_active");
				$(this).addClass("feat_active");
				
				// calclulate large image\'s URL based on the thumbnail URL 
				var url = $(this).find("img").attr("src").replace("_ICON_", "_PHOTO_");
				var label = $(this).find("img").attr("alt"); 
			
				// Text Caption
				$("#tab_" + idTab).find("#feat_text").html('Loading....');
				$("#tab_" + idTab).find("#feat_text").animate( { width:"0%"},200, function() {
					// Animation complete
					$("#tab_" + idTab).find("#feat_text").animate( { width:"92%"}, 500 , function() {
							// Animation complete
							$("#tab_" + idTab).find("#feat_text").html(label);
					});			
				});	
				
				// get handle to element that wraps the image and make it semitransparent 
				var wrap = $("#imgcont_" + idTab + " img").fadeTo("medium", 0, function(){
				// the large image 
				var img = new Image(); 
				 
					// call this function after it\'s loaded 
					img.onload = function() { 
				 
						// make wrapper fully visible 
						wrap.fadeTo("fast", 1); 
				 
						// change the image & alt
						wrap.attr("src", url); 
						wrap.attr("alt", label); 
				 
					}; 
					 
					// begin loading the image from flickr 
					img.src = url;
					
				}); 
				//var wrap = $("#imgcont").hide(); 

		// when page loads simulate a "click" on the first image 
		}).filter(":first").click();
		
		$("#featcont_" + idTab + " .items li").mouseover(function() {$(this).addClass("feat_over");}).mouseout(function(){$(this).removeClass("feat_over");});
	});
}

// HANDLE FEATURES OVERVIEW MODULAR PRODUCT
function handleFeatureModularOverview(idTab) {
	$(document).ready(function() {
		$("#featcont_" + idTab + " .items li").click(function() { 
			
				if ($(this).hasClass("feat_active")) return;
				$(".feat_active").removeClass("feat_active");
				$(this).addClass("feat_active");
				
				// calclulate large image\'s URL based on the thumbnail URL 
				var url = $(this).find("img").attr("src").replace("_ICON_", "_PHOTO_");
				var label = $(this).find("img").attr("alt"); 
				
				// get handle to element that wraps the image and make it semitransparent 
				var wrap = $("#imgcont_" + idTab).fadeTo("medium", 0, function(){
				// the large image 
				var img = new Image(); 
				 
					// call this function after it\'s loaded 
					img.onload = function() { 
				 
						// make wrapper fully visible 
						wrap.fadeTo("fast", 1); 
				 
						// change the image & alt
						wrap.find("img").attr("src", url); 
						wrap.find("img").attr("alt", label); 
				 
					}; 
					 
					// begin loading the image from flickr 
					img.src = url;
					
				}); 
				//var wrap = $("#imgcont").hide(); 

		// when page loads simulate a "click" on the first image 
		}).filter(":first").click();
		
		$("#featcont_" + idTab + " .items li").mouseover(function() {$(this).addClass("feat_over");}).mouseout(function(){$(this).removeClass("feat_over");});
	});
}



// HANDLE FABRICS GALLERY PRODUCT
function handleFabricsGallery() {
	$(document).ready(function() {
		$("#imgfabric img[rel]").overlay({
			effect: 'apple',
			top:  	'1%'
		});
		
		$("#fabricsgallery .items li").click(function() { 
			
				if ($(this).hasClass("fabric_active")) return;
				$(".fabric_active").removeClass("fabric_active");
				$(this).addClass("fabric_active");
				
				// calclulate large image\'s URL based on the thumbnail URL 
				var url = $(this).find("img").attr("src").replace("_ICON_", "_PHOTO_"); 
				var urlLarge = $(this).find("img").attr("src").replace("_ICON_", "_LARGE_"); 
				var label = $(this).find("img").attr("alt"); 
				
				//Change Large image
				$('#fabricsgallery #img_zoom_fab').find("img").attr("src", urlLarge);
				$('#fabricsgallery #img_zoom_fab .details h3').html(label);
				
				// get handle to element that wraps the image and make it semitransparent 
				var wrap = $("#imgfabric").fadeTo("medium", 0, function(){
					// the large image 
					var img = new Image(); 
				 
					// call this function after it\'s loaded 
					img.onload = function() { 
				 
						// make wrapper fully visible 
						wrap.fadeTo("fast", 1); 
				 
						// change the image & alt
						wrap.find("img").attr("src", url);
						wrap.find("img").attr("alt", label); 
				 
					}; 
				 
					// begin loading the image from flickr 
					img.src = url; 
				
				}); 
				//var wrap = $("#imgcont").hide(); 
			// when page loads simulate a "click" on the first image 
		}).filter(":first").click();
		
		$("#fabricsgallery .items li").mouseover(function() {$(this).addClass("fabric_over");}).mouseout(function(){$(this).removeClass("fabric_over");});
	});
}

// HANDLE PHOTO GALLERY PRODUCT
function handlePhotoGallery() {
	$(document).ready(function() {
		$("#photogallery .scrollable").scrollable({
			size: 3, 
			clickable: true,
			keyboard : false,
			vertical:true
		});
		
		$("#textphoto").css('display','none');
	
		$("#photogallery .items li").click(function() { 
			
				if ($(this).hasClass("photo_active")) return;
				$(".photo_active").removeClass("photo_active");
				$(this).addClass("photo_active");
				
				// calclulate large image\'s URL based on the thumbnail URL 
				var url = $(this).find("img").attr("src").replace("_ICON_", "_PHOTO_"); 
				var label = $(this).find("img").attr("alt"); 
				
				// get handle to element that wraps the image and make it semitransparent 
				var wrap = $("#imgphoto img").fadeTo("medium", 0, function(){
					// the large image 
					var img = new Image(); 
				 
					// call this function after it\'s loaded 
					img.onload = function() { 
				 
						// make wrapper fully visible 
						wrap.fadeTo("fast", 1); 
				 
						// change the image & alt
						wrap.attr("src", url);
						wrap.attr("alt", label); 							
				 
					}; 
				 
					// begin loading the image from flickr 
					img.src = url;
					
					if (label!='') {
						$(this).find("#textphoto").html(label);
						$(this).find("#textphoto").css('display','block');
					}
					else {
						$(this).find("#textphoto").css('display','none');
					}

				}); 
				//var wrap = $("#imgcont").hide(); 

			// when page loads simulate a "click" on the first image 
		}).filter(":first").click();
		
		$("#photogallery .items li").mouseover(function() {$(this).addClass("photo_over");}).mouseout(function(){$(this).removeClass("photo_over");});
	});
}

// HANDLE ACCESSORIES LIST
function handleAccessoriesList() {
	$(document).ready(function() {
		$("#acclist .items li").click(function() { 
			
				if ($(this).hasClass("acc_active")) return;
				$(".acc_active").removeClass("acc_active");
				$(this).addClass("acc_active");
				
				// calclulate large image\'s URL based on the thumbnail URL 
				var url = $(this).find("img").attr("src").replace("_ICON_", "_PHOTO_");
				var label = $(this).find("img").attr("alt"); 
				
				// get handle to element that wraps the image and make it semitransparent 
				var wrap = $("#accimg").fadeTo("medium", 0, function(){
				// the large image 
				var img = new Image(); 
				 
					// call this function after it\'s loaded 
					img.onload = function() { 
				 
						// make wrapper fully visible 
						wrap.fadeTo("fast", 1); 
				 
						// change the image & alt
						wrap.find("img").attr("src", url); 
						wrap.find("img").attr("alt", label); 
				 
					}; 
					 
					// begin loading the image from flickr 
					img.src = url;
					
				}); 
				//var wrap = $("#imgcont").hide(); 

		// when page loads simulate a "click" on the first image 
		}).filter(":first").click();
		
		$("#acclist .items li").mouseover(function() {$(this).addClass("acc_over");}).mouseout(function(){$(this).removeClass("acc_over");});
	});
}

// NEWS TICKER
function initNewsTicker(){
	var first = 0;
	var speed = 1000;
	var pause = 7000;
	
	function removeFirst(){
		first = $('#newsticker ul li:first').html();
		$('#newsticker ul li:first')
		.fadeOut(speed, function() {$(this).remove();});
		addLast(first);
	}
	function addLast(first){
		last = '<li style="display:none">'+first+'</li>';
		$('#newsticker ul').append(last)
		$('#newsticker ul li:last')
		.fadeIn(speed)
	}

	interval = setInterval(removeFirst, pause);
}

function initTagCloud(){
	$('#tagcloud a').click(function(){
		$('#search input[name="search"]').val($(this).html());
		$('#search form').submit();
	});
}

function get(url) {
	url += '&htr='+ Math.random()
	$.get(url);
}

function wtb(url) {
	url += '/?cmd=wtb';
	// Serialize form
  $('#frmWtb input, #frmWtb select').each(function (i) {
    url += '&' + $(this).attr('name') + '=' + $(this).val();
  });
	get(url);
	return false;
}
function wtb_page() {
	$('#frmWtb input[name=wtbLocatorPage]').val($(this).attr('page'));
	$('#frmWtb').submit();
	return false;
}

function cnt_check() {
	var chk_required=true;
	$('#frmContact input, #frmContact select, #frmContact textarea').each(function (i) {
	  if ($(this).val()==''){
	  	$(this).focus();
	  	chk_required=false;
	  	return false;
	  }
	});
	return chk_required;
}

function handleChristmasBox() {
  $(document).ready(function() {
    $('.christmas').animate({
      height: 'toggle'
    }, 1000, function() {
      // Animation complete.
    });
    });
  $(window).scroll(function() {
      var yOffSett = $(window).scrollTop();
	   	var winHeight = $(window).height();
		  var posBox = (winHeight - 295) + yOffSett;
		  posBox = posBox+"px";
      $(".christmas").css({ top:posBox });
  }); 
	
	// WINDOW RESIZE EVENT
	$(window).resize(function() {
	   	var yOffSett = $(window).scrollTop();
      var winHeight = $(window).height();
		  var posBox = (winHeight - 295) + yOffSett;
		  posBox = posBox+"px";
      $(".christmas").css({ top:posBox });
	}); 
}
