// JavaScript Document

var backgrounds=new Array();
backgrounds[0]="images/bg.jpg";
backgrounds[1]="images/bg2.jpg";
backgrounds[2]="images/bg3.jpg";
backgrounds[3]="images/bg4.jpg";
backgrounds[4]="images/bg5.jpg";
backgrounds[5]="images/bg6.jpg";
var numBg=6;
var currentImage=0;

var contentSpaceHeight=80;  //E' la somma degli spazi tra top e bottom

var visible=false;

 function showAziendale(stato) {
 	switch(stato) {
		case true:
			$("#aziendale").addClass("selected");
			$(".content").fadeIn();
			$.ajax({
				type:"POST",
				url:"ajax/getPage.php",
				data:"IDPagina=119", 
				success: function(response) {				
					$("#thePage").html(response);			
			}
		});
		break;
		case false:
			$("#aziendale").removeClass("selected");
			$(".content").fadeOut(0);
			$("#thePage").html("<img src=\"images/loading.gif\">");	
		break;
	}
 }
 
 function showGaranzia(stato) {
 	switch(stato) {
		case true:
			$("#garanzia").addClass("selected");
			$(".content").fadeIn();
			$.ajax({
				type:"POST",
				url:"ajax/getPage.php",
				data:"IDPagina=136", 
				success: function(response) {				
					$("#thePage").html(response);			
			}
		});
		break;
		case false:
			$("#garanzia").removeClass("selected");
			$(".content").fadeOut(0);
			$("#thePage").html("<img src=\"images/loading.gif\">");	
		break;
	}
 }
 
 function showContatti(stato) {
 	switch(stato) {
		case true:
			$("#contatti").addClass("selected");
			$(".content").fadeIn();
			$.ajax({
				type:"POST",
				url:"ajax/getPage.php",
				data:"IDPagina=137", 
				success: function(response) {				
					$("#thePage").html(response);			
			}
		});
		break;
		case false:
			$("#contatti").removeClass("selected");
			$(".content").fadeOut(0);
			$("#thePage").html("<img src=\"images/loading.gif\">");	
		break;
	}
 }
 
  function showContattaci(stato) {
 	switch(stato) {
		case true:
			$("#thePage").html("<img src=\"images/loading.gif\">");
			$.ajax({
				type:"POST",
				url:"ajax/getPage.php",
				data:"IDPagina=contatti", 
				success: function(response) {				
					$("#thePage").html(response);			
			}
		});
		break;
		case false:
			$("#thePage").html("<img src=\"images/loading.gif\">");	
			$.ajax({
				type:"POST",
				url:"ajax/getPage.php",
				data:"IDPagina=137", 
				success: function(response) {				
					$("#thePage").html(response);			
			}
		});
		break;
	}
 }
 
  function showRegistrati(stato) {
 	switch(stato) {
		case true:
			showStriscia(false);
			$("#thePage").html("<img src=\"images/loading.gif\">");
			$(".content").fadeIn();
			$.ajax({
				type:"POST",
				url:"ajax/getPage.php",
				data:"IDPagina=registrati", 
				success: function(response) {				
					$("#thePage").html(response);			
			}
		});
		break;
		case false:
			$(".content").fadeOut(0);
			$("#thePage").html("<img src=\"images/loading.gif\">");
		break;
	}
 }
 
 function showStriscia(stato) {
 	switch(stato) {
		case true:
			if(!visible) 
			{
				$(".cat_products").fadeIn();
				$("#gallery").addClass("selected");
				//alert($.browser.msie);
				if(($.browser.msie==undefined || $.browser.msie=="false" ) || ($.browser.version == "9.0")) 
				{
					$("aside")[0].style.width="758px";
					$(".content")[0].style.left="539px";
				}
				else {
					$("#cat_products")[0].style.left="0px";
					if($.browser.version < "8.0") $("#cat_products")[0].style.top="310px";
					if($.browser.version == "8.0") $("#cat_products")[0].style.top="325px";
					$("#cat_products ul")[0].style.width="180px";
				}
				visible=true;
			}
			else
			{
				$(".cat_products").fadeOut();
				$("#gallery").removeClass("selected");
				if(($.browser.msie==false) || ($.browser.version == "9.0")) 
				{
					$("aside")[0].style.width="573px";
					$(".content")[0].style.left="374px";
				}
				visible=false;
			}
		break;
		case false:
			$("#striscia").fadeOut();
			$("#ricAv").fadeOut();
			$("#scroll").fadeOut();
			if($("#product")!=null) $("#product").fadeOut();
			if($("#detailProduct")!=null) $("#detailProduct").fadeOut();
			if(visible) 
			{
				$(".cat_products").fadeOut();
				$("#gallery").removeClass("selected");
				if(($.browser.msie==undefined || $.browser.msie=="false" ) || ($.browser.version == "9.0")) 
				{
					$("aside")[0].style.width="573px";
					$(".content")[0].style.left="374px";
				}
				visible=false;
			}
		break;
	}
 }
 
 $(document).ready(function() {
	$('select').selectmenu();
	
	$('#lang').change(function(){
		location.href="?lang="+$(this).val();
	});
	
	//Posizionamento del footer
	if($("footer").position().top < 573 ) $("footer")[0].style.top = "573px";
	
	$('#logo').click(function(){
		//Navigate to homepage
		window.location = "http://www.clicsrl.com";
	});
	
	$("#lightBox").css('width', $(document).width());
	$("#lightBox").css('height',$(document).height());
	
	setInterval(function() {
		  $(".backgroundImage")[1].src=backgrounds[currentImage];
		  $(".backgroundImage")[1].style.display="block";
		  currentImage++;
		  if(currentImage >= numBg) currentImage=0;
		  $(".backgroundImage")[0].src=backgrounds[currentImage];
		  $(".backgroundImage")[0].style.display="block";
		  $(".backgroundImage").slice(1).fadeOut("slow");
    }, 30000);
	
	
	$("#gallery").click(function(){
		showAziendale(false);
		showGaranzia(false);
		showContatti(false);
		showStriscia(true);
	});
	
	$("#aziendale").click(function(){
		//Visualizzazione 
		showGaranzia(false);
		showStriscia(false);
		showContatti(false);
		showAziendale(true);
		
	});
	
	$("#garanzia").click(function(){
		//Visualizzazione 
		showAziendale(false);
		showStriscia(false);
		showContatti(false);
		showGaranzia(true);
		
	});
	
	$("#contatti").click(function(){
		//Visualizzazione 
		showAziendale(false);
		showStriscia(false);
		showGaranzia(false);
		showContatti(true);
		
	});
	
	 $("#Ricerca").click(function() {
		showStriscia(false);
		$(".content").fadeIn();
		$("#thePage").html("<img src=\"images/loading.gif\">");
		$.ajax({
			type:"POST",
			url:"ajax/cerca.php",
			data:"ricerca="+encodeURI($("#ric").val()), 
			success: function(response) {				
				$("#thePage").html(response);			
			}
		});
	 });
	
	$(".category").click(function() {
		$("#scroll").fadeOut();
		$("#striscia").fadeOut();
		$("#ricAv").fadeOut();
		$("#striscia").html("<img src=\"images/loading.gif\" />");
		$("#striscia").fadeIn();
		$("#ricAv").fadeIn();
		$("#ricAv").attr("idLinea",$(this).attr("idLinea"));

		$("#cursoreScroll")[0].style.left="1px";
		
		if(($.browser.msie==true) && ($.browser.version < "9.0")) 
		{
			$("#striscia").addClass("content-compat");
			
			$.ajax({
			type:"POST",
			url:"ajax/strip.php",
			data:"IDLinea="+$(this).attr("idLinea"), 
			success: function(response) {				
				$("#striscia").html(response);
			},
			complete: function(jqXHR, textStatus) {
				/*if($("#theStriscia").width() > $("#striscia").width())*/ $("#scroll").fadeIn();
			}
		});	
		}
		else {
		$.ajax({
			type:"POST",
			url:"ajax/strip.php",
			data:"IDLinea="+$(this).attr("idLinea")+"&NumProdotti=20&start=0", 
			success: function(response) {				
				$("#striscia").html(response);
			},
			complete: function(jqXHR, textStatus) {
				/*if($("#theStriscia").width() > $("#striscia").width())*/ $("#scroll").fadeIn();
			}
		});
		}
		$(".category").removeClass("selected");
		$(this).addClass("selected");
	});
	
	$(".content")[0].style.height=($("footer").position().top - contentSpaceHeight).toString()+"px";
	//alert($("footer").position().top - contentSpaceHeight);
	
	
	//Scroll
	$("#cursoreScroll").draggable({ 
		axis: "x" ,
		containment: "parent",
		drag: function(event, ui) {
			if(($.browser.msie==true) && ($.browser.version < "9.0")) 
			{
				var leftCursor=$("#cursoreScroll").position().left;
				var ratio=$("#theStriscia").width()/$("#striscia").width();
				
				var myStriscia=$("#theStriscia")[0];
				$("#theStriscia")[0].style.left=(-leftCursor*ratio/10000).toString()+"px";
				
				return;
			}
		 	var leftCursor=$("#cursoreScroll").position().left;
			var ratio=$("#theStriscia").width()/$("#striscia").width();
			var rightLimit=$("#striscia").width()+$("#striscia").position().left-100;
			
			$("#theStriscia")[0].style.left="-"+(leftCursor*ratio).toString()+"px";
			
			if($(".more").position() == null) return;
			if($(".more").position().left - leftCursor*ratio < rightLimit) {
				var start=$(".more").first().attr("nextStart");
				var id=$(".more").first().attr("id");
				var idLinea=$(".more").first().attr("idLinea"); 
				
				$("#"+id).removeClass("more");
				$("#"+id).attr("class", "");
				$("#"+id).attr("start", "");
				$("#"+id).attr("idLinea", "");
				$("#"+id).attr("nextStart", "");
				
				$.ajax({
					type:"POST",
					url:"ajax/reduced.php",
					data:"IDLinea="+idLinea+"&NumProdotti=20&start="+start, 
					success: function(response) {				
						$("#"+id).after(response);
						$("#"+id).remove();
					},
				});
			
			}
		 }
	});
	
	$("#vcursoreScroll").draggable({ 
		axis: "y" ,
		containment: "parent",
		drag: function(event, ui) {
		 	var topCursor=$("#vcursoreScroll").position().top;
			var ratio=$("#thePage").height()/$("#content").height();
			
			$("#thePage")[0].style.top=(20-topCursor*ratio).toString()+"px";
		 }
	});
	
	$("#scroll").click(function(e) {
			var ratio=$("#theStriscia").width()/$("#striscia").width();
			$("#cursoreScroll")[0].style.left=(e.pageX-100).toString()+"px";
			var leftCursor=$("#cursoreScroll").position().left;
			
			$("#theStriscia")[0].style.left="-"+(leftCursor*ratio).toString()+"px";
			if($(".more").position() == null) return;
			if($(".more").position().left - leftCursor*ratio < rightLimit) {
				var start=$(".more").first().attr("nextStart");
				var id=$(".more").first().attr("id");
				var idLinea=$(".more").first().attr("idLinea"); 
				
				$("#"+id).removeClass("more");
				$("#"+id).attr("class", "");
				$("#"+id).attr("start", "");
				$("#"+id).attr("idLinea", "");
				$("#"+id).attr("nextStart", "");
				
				$.ajax({
					type:"POST",
					url:"ajax/reduced.php",
					data:"IDLinea="+idLinea+"&NumProdotti=20&start="+start, 
					success: function(response) {				
						$("#"+id).after(response);
						$("#"+id).remove();
					},
				});
			
			}
		});
	
	$("#vscroll").click(function(e) {
			var ratio=$("#thePage").height()/$("#content").height();
			$("#vcursoreScroll")[0].style.top=(e.pageY-30).toString()+"px";
			var topCursor=$("#vcursoreScroll").position().top;
			
			$("#thePage")[0].style.top=(20-topCursor*ratio).toString()+"px";
		});
	//Form di login
	$.ajax({
			type:"POST",
			url:"ajax/login.php",
			success: function(response) {				
				$("#auth").html(response);
			}
		});
	
	$("#ricAv").click(function(){
		$.ajax({
			type:"POST",
			url:"ajax/estesa.php",
			data:"IDLinea="+$(this).attr("idLinea"),
			success: function(response) {				
				$("#contentLightBox").html(response);
				$("#lightBox").fadeIn();
			}
		});
	});
	
	$("#lightBox").click(function() {
		$(this).fadeOut();
		
		$("#contentLightBox").html("<img src=\"images/loading.gif\">");
		});
	
	$(".backgroundImage").css('height',$(document).height());
	$(".backgroundImage").css('width',$(document).width());
 });
 
 $(window).resize(function() {
	 //$("#thePage").html($(window).height());
	//if($("footer").position().top < 573 )
	
	$("#lightBox")[0].style.width=$(document).width()+"px";
	$("#lightBox")[0].style.height=$(document).height()+"px";
	
	if($(window).height() > 820) 
	{
		$("footer")[0].style.bottom = "0px";
		$("footer")[0].style.top = "";
	}
	else  $("footer")[0].style.top = "573px";
	
	$(".content")[0].style.height=($("footer").position().top - contentSpaceHeight).toString()+"px";
	
	if($("#theStriscia").position() != null)
	{
		var topProduct;
		var heightProduct;
		
		if(!$.browser.msie || $.browser.version > "8.0" ) {
				topProduct=$("#theStriscia").position().top + $("#theStriscia").height() + 47;
				heightProduct=$("footer").position().top - topProduct -9;
			}
			else {
				topProduct=$("#theStriscia")[0].offsetTop + 116 + 47;
				var TopOfFooter=new Number($("footer")[0].offsetTop);
				heightProduct= TopOfFooter - topProduct -9;
			}
	
		$("#product")[0].style.top=topProduct.toString()+"px";
		$("#product")[0].style.height=Math.round(heightProduct).toString()+"px";
		$("#product")[0].style.width=Math.round(heightProduct*1.891).toString()+"px";
	
		$("#product img")[0].style.height=Math.round(heightProduct).toString()+"px";
		
		
	}
	
	$(".backgroundImage").css('height',$(document).height());
	$(".backgroundImage").css('width',$(document).width());
 });
 
 
function showZoom(img) {
		if(img==null || img==undefined) return;
		
		$("#lightBox").fadeIn();
		$('<img />')
			.attr('src', img.toString())
			.load(function(){ 
				$("#contentLightBox img").attr("src","imageBig.php?Immagine="+$(this).attr("src")+"&h=600");
				
				$("#contentLightBox").animate({
					top: "100px",
					left: (($("#lightBox").width() - 500)/2).toString()
				},2000,function() {
					});
			});
}

function addToCart(idProdotto){
	if(idProdotto==null || idProdotto==undefined) return;
	
	$.ajax({
			type:"POST",
			url:"ajax/cart.php",
			data: "IDProdotto="+idProdotto,
			success: function(response) {				
				$("#camp").html(response);
				$("#"+idProdotto).addClass("inCart");
			}
		});
}

function goToContattaci() {
		showAziendale(false);
		showStriscia(false);
		showGaranzia(false);
		showContatti(true);
		showContattaci(true);
}

function selectCategory(idLinea, success) {
		$("#scroll").fadeOut();
		$("#striscia").fadeOut();
		$("#ricAv").fadeOut();
		$("#striscia").html("<img src=\"images/loading.gif\" />");
		$("#striscia").fadeIn();
		$("#ricAv").fadeIn();
		$("#ricAv").attr("idLinea",idLinea);
				
		if(($.browser.msie==true) && ($.browser.version != "9.0")) 
		{
			$("#striscia").addClass("content-compat");
		}
		$.ajax({
			type:"POST",
			url:"ajax/strip.php",
			data:"IDLinea="+idLinea, 
			success: function(response) {				
				$("#striscia").html(response);
			},
			complete: function(jqXHR, textStatus) {
				if($("#theStriscia").width() > $("#striscia").width()) $("#scroll").fadeIn();
				success();
			}
		});
		$(".category").removeClass("selected");
		$('.category[idLinea="'+idLinea+'"]').addClass("selected");
	}
	
function showBigImage(IDImg){
			$("#theStriscia img").removeClass("Sel");
			$(IDImg).addClass("Sel");
			
			var mySrc=$(IDImg).attr("immagine");
			var topProduct;
			var heightProduct;
			//Determino le dimensioni di #product in modo dinamico
			if(!$.browser.msie || $.browser.version > "8.0" ) {
				topProduct=$("#theStriscia").position().top + $("#theStriscia").height() + 47;
				heightProduct=$("footer").position().top - topProduct -9;
			}
			else {
				topProduct=$("#theStriscia")[0].offsetTop + 116 + 47;
				var TopOfFooter=new Number($("footer")[0].offsetTop);
				heightProduct= TopOfFooter - topProduct -9;
			}
			
			
			$("#product")[0].style.top=topProduct.toString()+"px";
			$("#product")[0].style.height=Math.round(heightProduct).toString()+"px";
			$("#product")[0].style.width=Math.round(heightProduct*1.891).toString()+"px";
			
			if($.browser.msie && $.browser.version <= "8.0" ) {
				//Sono arrivato qui
			}
			
			
			var height=Math.round(heightProduct).toString();
			
			var cart='<div id="camp" align="left"><a href="javascript:addToCart('+$(IDImg).attr("idProdotto")+')">SELEZIONA PER CAMPIONATURA</a></div>';
			if($(IDImg).hasClass("inCart")) cart='<div id="camp" align="left"><a href="javascript:goToContattaci()">RICHIEDI CAMPIONATURA</a></div>';
			$("#product").html('<table><tr><td valign="middle" height="'+height+'"><!--<img src="'+$(IDImg).attr("src")+'" />--><img src="images/loading.gif" /></td></tr></table><div id="zoom"><a href=\"javascript:showZoom(\''+mySrc+'\')\">ZOOM</a></div>'+cart+'<div id="fbook"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.omworks.net%2Fclic%2F%3FIDProdotto%3D'+$(IDImg).attr("idProdotto")+'&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp;font=tahoma&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true" ></iframe></div><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.omworks.net/clic/?IDProdotto='+$(IDImg).attr("idProdotto")+'" data-count="none" id="tweet">&nbsp;</a>');
			//Qui forse bisogna caricare un loader
			
			/*$("#product img").attr("height", height);
			if($(IDImg).height()<100)  {
				$("#product img").attr("height", "");
				$("#product img").attr("width", 700);
			}*/
			$("#product img").attr("src", "imageshow.php?Immagine="+mySrc+"&h="+height);
			$("#product img").draggable({ containment: "parent" }); 
			$("#product img").bind("contextmenu", function(e) {
      				alert('Copiright Clic s.r.l.');
   			});
			
			$("#product").fadeIn();
			$("#detailProduct").fadeIn();
			$.ajax({
				type:"POST",
				url:"ajax/detailProduct.php",
				data:"IDProdotto="+$(IDImg).attr("idProdotto"), 
				success: function(response) {		
					$("#detailProduct").html(response);
				}
			});
		}
