/*--------------------------------------------------
A L P H A T E S T
--------------------------------------------------*/


/*--------------------------------------------------
topmenu
--------------------------------------------------*/
function topMenu(){
	$(".menu li:last").css({"margin-right":0});
}


/*--------------------------------------------------
login
--------------------------------------------------*/
function popupLogin(){
	var popLogin = false;
	$(".top_accedi").click(function(){
		popLogin = true;
		$(".box_login").show();
		return false;
	});
}


/*--------------------------------------------------
tabs
--------------------------------------------------*/
function tabs(){
	$(".tabs_wrap").each(function(){
		var $this = $(this);
		var maxH = 0;
		
		// cambio tab
		$this.find(".tabs_links a").click(function(){
			$this.find(".tabs_links a.on").removeClass("on");
			$(this).addClass("on");
			$this.find(".tab").hide();
			$($(this).attr("href")).show();
			return false;
		});
		$this.find(".tabs_links a:eq(0)").click();
	});
	
	$(".tabs_search").each(function(){
		var $this = $(this);
		var maxH = 0;
		
		// cambio tab
		$this.find(".tabs_links a").click(function(){
			$this.find(".tabs_links a.on").removeClass("on");
			$(this).addClass("on");					
			$this.find(".tab").hide();
			$($(this).attr("href")).show();
			
			var ind_href = $(this).attr("href")
			
			$this.find(".tabs_links_down a.on").removeClass("on");
			$this.find(".tabs_links_down a[href='"+ind_href+"']").addClass("on");
			
			return false;
		});
		
		// cambio tab
		$this.find(".tabs_links_down a").click(function(){
			$this.find(".tabs_links_down a.on").removeClass("on");
			$(this).addClass("on");				
			$this.find(".tab").hide();
			$($(this).attr("href")).show();
			
			var ind_href = $(this).attr("href")
			
			$this.find(".tabs_links a.on").removeClass("on");
			$this.find(".tabs_links a[href='"+ind_href+"']").addClass("on");
			
			return false;
		});
		
		$this.find(".tabs_links a:last").click();
	});
}


/*--------------------------------------------------
scroller
--------------------------------------------------*/
function scroller() {
	$(".scroller").each(function(){
		var $this = $(this);
		var time = 500;
		var w1 = 55;
		var b1 = 40;
		var w2 = 82;
		var b2 = 23;
		var w3 = 110;
		var b3 = 0;
		var m = 55 + 10;
		
		$this.find(".scroller_item").css({"width":w1,"bottom":b1});
		$this.find(".scroller_item:eq(1), .scroller_item:eq(3)").css({"width":w2,"bottom":b2});
		$this.find(".scroller_item:eq(2)").css({"width":w3,"bottom":"0px"});
		$this.find(".scroller_popup").html($this.find(".scroller_item:eq(2) .scroller_item_detail").html());
		// left click
		$this.find(".scroller_arr_left").click(function(){
			$this.find(".scroller_item:last").css({"width":w1,"bottom":b1,"margin-left":-m}).prependTo($this.find(".viewport")).animate({"margin-left":"0"},time);
			$this.find(".scroller_item:eq(4)").animate({"width":w1,"bottom":b1},time);
			$this.find(".scroller_item:eq(1), .scroller_item:eq(3)").animate({"width":w2,"bottom":b2},time);
			$this.find(".scroller_item:eq(2)").animate({"width":w3,"bottom":"0px"},time);
			setTimeout(function(){
				$this.find(".scroller_popup").html($this.find(".scroller_item:eq(2) .scroller_item_detail").html());
				addToBasket();
			},time+100);
			return false;
		});
		// right click
		$this.find(".scroller_arr_right").click(function(){
			$this.find(".scroller_item:first").animate({"margin-left":-m},time,function(){
				$(this).appendTo($this.find(".viewport")).css({"margin-left":0});
			});
			$this.find(".scroller_item:eq(1), .scroller_item:eq(5)").animate({"width":w1,"bottom":b1},time);
			$this.find(".scroller_item:eq(2), .scroller_item:eq(4)").animate({"width":w2,"bottom":b2},time);
			$this.find(".scroller_item:eq(3)").animate({"width":w3,"bottom":"0px"});
			setTimeout(function(){
				$this.find(".scroller_popup").html($this.find(".scroller_item:eq(2) .scroller_item_detail").html());
				addToBasket();
			},time+100);
			return false;
		});
		// click sulle immagini
		$this.find(".scroller_item:eq(0) a").live("click",function(){
			$this.find(".scroller_arr_left").click();
			setTimeout(function(){
				$this.find(".scroller_arr_left").click();
			},time);
			return false;
		});
		$this.find(".scroller_item:eq(1) a").live("click",function(){
			$this.find(".scroller_arr_left").click();
			return false;
		});
		$this.find(".scroller_item:eq(3) a").live("click",function(){
			$this.find(".scroller_arr_right").click();
			return false;
		});
		$this.find(".scroller_item:eq(4) a").live("click",function(){
			$this.find(".scroller_arr_right").click();
			setTimeout(function(){
				$this.find(".scroller_arr_right").click();
			},time);
			return false;
		});
	});
}


/*--------------------------------------------------
codice sconto
--------------------------------------------------*/
function codiceSconto(){
	$(".carrello .sconto").click(function(){
		$(this).hide();
		$(this).parent().find(".codice_sconto, .codice_sconto input").show();
		return false;
	});
}


/*--------------------------------------------------
codice piumeno libri
--------------------------------------------------*/
function piuMenoLibri(){
	$(".carrello a.increase").click(function(){
		var value = parseInt($(this).parent().find("input").val());
		$(this).parent().find("input").val(value + 1);
		return false;
	});
	
	$(".carrello a.decrease").click(function(){
		var value = parseInt($(this).parent().find("input").val());
		if (value > 0) {
			$(this).parent().find("input").val(value - 1);
		}
		return false;
	});
	
}


/*--------------------------------------------------
tabella corsi
--------------------------------------------------*/
function tabellaCorsi(){
	$(".corsi .apri_corso").click(function(){
		$(".corsi .chiudi").removeClass("chiudi").addClass("apri").parents(".corso").next().hide();
		if ($(this).is(".apri")){
			$(this).removeClass("apri").addClass("chiudi").parents(".corso").next().show();
		} else if ($(this).is(".chiudi")) {
			$(this).removeClass("chiudi").addClass("apri").parents(".corso").next().hide();
		}
		return false;
	});
}


/*--------------------------------------------------
box toggle
--------------------------------------------------*/
function boxToggle(){
	$(".box_toggle a.apri_box").click(function(){
		$(this).next().toggle();
		return false;
	});
}


/*--------------------------------------------------
open commenti
--------------------------------------------------*/
function openCommenti(){
	$(".opencommenti").click(function(){
		$(".commenti").toggle();
		return false;
	});
}


/*--------------------------------------------------
open carrello
--------------------------------------------------*/
function openCarrello(){
	var cartOpen = false;
	var closeTimer;
	
	function openCart(){
		clearTimeout(closeTimer);
		$(".top_carrello a").addClass("on");
		$(".box_carrello").fadeIn(500);
	}
	
	function closeCart() {
		closeTimer = setTimeout(function(){
			if (!cartOpen){
				$(".top_carrello a.on").removeClass("on");
				$(".box_carrello").hide();
			}
		},1000);
	}
	
	$(".top_carrello a, .box_carrello").mouseover(function(){
		cartOpen = true;
		openCart();
	}).mouseout(function(){
		cartOpen = false;
		closeCart();
	});
}


/*--------------------------------------------------
tooltip
--------------------------------------------------*/
function tooltip(){
	$(".tooltip").mouseover(function(e){
		if ($("#tooltip").size() == 0){
			$("body").append('<div id="tooltip">' + $(this).attr("rel") + '</div>');
		}
		$("#tooltip").css({"top":e.pageY-$("#tooltip").height()-25+"px","left":e.pageX-50+"px"})
	}).mousemove(function(e){
		$("#tooltip").css({"top":e.pageY-$("#tooltip").height()-25+"px","left":e.pageX-50+"px"})
	}).mouseout(function(){
		$("#tooltip").remove();
	});
	$(".tooltip_down").mouseover(function(e){
		if ($("#tooltip_down").size() == 0){
			$("body").append('<div id="tooltip_down">' + $(this).attr("rel") + '</div>');
		}
		$("#tooltip_down").css({"top":e.pageY+25+"px","left":e.pageX-50+"px"})
	}).mousemove(function(e){
		$("#tooltip_down").css({"top":e.pageY+25+"px","left":e.pageX-50+"px"})
	}).mouseout(function(){
		$("#tooltip_down").remove();
	});
}

/*--------------------------------------------------
INIT
--------------------------------------------------*/
$(function(){
	topMenu();
	popupLogin();
	tabs();
	scroller();
	codiceSconto();
	piuMenoLibri();
	tabellaCorsi();
	boxToggle();
	openCommenti();
	openCarrello();
	$(".col_3 .col:nth-child(3n+1)").addClass("clear");
	tooltip();
});
