$.fn.extend({
  reduceUl: function(num,expandLabel,reduceLabel){
	if($(this).children("li").length > num) {
		$(this).children("li.reduce").remove();
		var i = 1;
		$.each($(this).children("li"), function() {
			if(i > num) {
				$(this).hide();
			} else if(i == num) {
				$(this).after("<li class='expand'><a href='#' onclick='$(this).expandUl(\""+reduceLabel+"\");return false;'><span>"+expandLabel+"</span><span class='img'></span></a></li>");
			}
			i++;
		});
	}
  },
  expandUl: function(reduceLabel){
	$ul = $(this).closest("ul");
	$ul.append("<li class='reduce'><a href='#' onclick='$($ul).reduceUl(\""+$ul.children("li:visible:not(.expand)").length+"\",\""+$(this).text()+"\",\""+reduceLabel+"\");return false;'><span>"+reduceLabel+"</span><span class='img'></span></a></li>");
	$ul.children("li").show();
	$(this).closest("li").remove();
  }
});
$.tooltipReggiani = {
	init : function(obj) {
		$.each($("a.open-tooltip"), function(){
			$a = $(this);
			var id = $a.attr("href");
			id = id.split("#");
			var $id = $("#tooltip-"+id[1]);
			var content = $id.html();
			$id.empty().html('<table><tr><td class="corner" id="topleft"></td><td class="top"></td><td class="corner" id="topright"></td></tr><tr><td class="left"></td><td class="content">'+content+'</td><td class="right"></td></tr><tr><td id="bottomleft" class="corner"></td><td class="bottom"><div></div></td><td class="corner" id="bottomright"></td></tr></table>');
		});
		$("a.open-tooltip").mouseover(function () {
			$.tooltipReggiani.show($(this));
			return false;
		});
		$("a.open-tooltip").mouseleave(function () {
			$.tooltipReggiani.close();
			return false;
		});
	},
	show : function(obj) {
		$obj = $(obj);
		var id = $obj.attr("href");
		id = id.split("#");
		var $id = $("#tooltip-"+id[1]);
		if($id.css("display")=="block")return false;
		var idWidth = $id.outerWidth();
		var idHeight = $id.outerHeight();	
		
		var pos = $obj.offset();
		var posRelative = $obj.position();
		var objWidth = $obj.outerWidth();
		var idWidth = $id.outerWidth();
		var idHeight = $id.outerHeight();
		var height = $id.height();
		console.log(idWidth);
		leftPosition = pos.left-((idWidth-objWidth)/2);
		topPosition = pos.top-idHeight;
		$id.css("top",topPosition);
		$id.css("left",leftPosition);  
		$id.css("background-color","pink");  
		$id.show();	
	},
	close : function() {
		$(".tooltip-reggiani").hide();
	}
}
$.documentReggiani = {
	init : function(obj) {
		$("a.document-open").click(function () {
			$.documentReggiani.toggle($(this));
			return false;
		});
	},
	toggle : function(obj) {
		$obj = $(obj);
		var id = $obj.attr("href");
		id = id.split("#");
		var $box = $("#document-"+id[1]);
		if($box.css("display")=="block") {
			$box.slideUp("normal",function(){
				$obj.removeClass("document-close");			
			});
			return false;
		}
		var idWidth = $box.outerWidth();
		var objHeight = $obj.outerHeight();
		var pos = $obj.offset();
		var posRelative = $obj.position();
		var objWidth = $obj.outerWidth();
		var idWidth = $box.outerWidth();
		leftPosition = pos.left;
		topPosition = pos.top+objHeight-1;
		$box.css("top",topPosition);
		$box.css("left",leftPosition);  
		$box.slideDown("normal");
		$obj.addClass("document-close");
	}
}
var seiInteressatoMinHeight = 0;
var seiInteressatoMaxHeight = 0;
$.seiInteressato = {
	init : function(obj) {
		var height = 0;
		$.each($("#sei-interessato > li > a"), function() {
			if($(this).innerHeight()>height) height = $(this).innerHeight();
		});
		seiInteressatoMinHeight = height;
		height = 0;
		$.each($("#sei-interessato div.st-box"), function() {
			if($(this).innerHeight()>height) height = $(this).innerHeight();
		});
		seiInteressatoMaxHeight = seiInteressatoMinHeight+height;
		$("#sei-interessato > li").eq(0).addClass("selected").height(seiInteressatoMaxHeight);
		$("#sei-interessato").find("li:not(.selected)").height(seiInteressatoMinHeight);
		$("#sei-interessato > li > a").click(function () {
			$.seiInteressato.toggle($(this));
			return false;
		});
		var url = document.URL;
		var urlMail = $("#sei-interessato li").eq(1).find(".st-box a").attr("href");
		$("#sei-interessato li").eq(1).find(".st-box a").attr("href",urlMail+"&url="+url);
	},
	toggle : function(obj) {
		$(obj).closest("ul").find("li").removeClass("selected");
		$(obj).parent().addClass("selected");
		$("#sei-interessato").find("li").dequeue();
		$("#sei-interessato").find("li:not(.selected)").animate({
			height: seiInteressatoMinHeight
		}, 200);
		$(obj).parent().animate({
			height: seiInteressatoMaxHeight
		}, 200);
	},
	ajax : function(obj) {
		var url = document.URL;
		var email = $(obj).prev().val();
		if(!$.seiInteressato.checkEmail(email)) {
			alert("inserire un indirizzo mail valido");	
			return false;
		}
		$(obj).parent().parent().find(".st-box").fadeOut();
		$(obj).parent().parent().find(".st-box-loader").fadeIn();
		var type = $(obj).prev().attr("name");
		switch(type) {
			case "demo":
				$.ajax({
					type: "POST",
					url: "/php/ajax.php",
					data: "type=demo&email="+email+"&url="+url,
					success: function(msg){
						if(msg=="1") {
							$(obj).parent().parent().find(".st-box-loader").fadeOut();
							$(obj).parent().parent().find(".st-box-feedback").fadeIn().delay("3000").fadeOut("normal",function(){
								$(obj).parent().parent().find(".st-box").fadeIn();
							});
						} else {
							$(obj).parent().parent().find(".st-box-loader").fadeOut();
							$(obj).parent().parent().find(".st-box-error").fadeIn().delay("3000").fadeOut("normal",function(){
								$(obj).parent().parent().find(".st-box").fadeIn();
							});
						}
					}
				});
				break;
			case "newsletter":
				$.ajax({
					type: "POST",
					url: "/php/ajax.php",
					data: "type=newsletter&email="+email,
					success: function(msg){
						if(msg=="1") {
							$(obj).parent().parent().find(".st-box-loader").fadeOut();
							$(obj).parent().parent().find(".st-box-feedback").fadeIn().delay("3000").fadeOut("normal",function(){
								$(obj).parent().parent().find(".st-box").fadeIn();
							});
						} else {
							$(obj).parent().parent().find(".st-box-loader").fadeOut();
							$(obj).parent().parent().find(".st-box-error").fadeIn().delay("3000").fadeOut("normal",function(){
								$(obj).parent().parent().find(".st-box").fadeIn();
							});
						}
					}
				});
				break;
			case "invia":
				$.ajax({
					type: "POST",
					url: "/php/ajax.php",
					data: "type=invia&email="+email+"&url="+url,
					success: function(msg){
						if(msg=="1") {
							$(obj).parent().parent().find(".st-box-loader").fadeOut();
							$(obj).parent().parent().find(".st-box-feedback").fadeIn().delay("3000").fadeOut("normal",function(){
								$(obj).parent().parent().find(".st-box").fadeIn();
							});
							return false;							
						} else {
							$(obj).parent().parent().find(".st-box-loader").fadeOut();
							$(obj).parent().parent().find(".st-box-error").fadeIn().delay("3000").fadeOut("normal",function(){
								$(obj).parent().parent().find(".st-box").fadeIn();
							});
						}
					}
				});
				break;
		}
	
	},
	checkEmail : function(email) {
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(email)) return false;
		return true;
	}
}
