function showModalDialog()
{
	var modalHtml = "<div id=modalDialog style=\"position:absolute;left:0;top:0;display:none;background-color:#000;z-index:100;\">&nbsp;</div>";
	if ($("#modalDialog").length==0)
	{
		$(document.body).append(modalHtml);
		$(window).resize(sizeAndPosition);
		$(window).scroll(sizeAndPosition);
	}
	var dlg = $("#modalDialog");
	dlg.css("opacity",0.5);
	dlg.css("filter", "alpha(opacity='50')");
	sizeAndPosition();
	dlg.show();
}

function sizeAndPosition()
{
	var dlg = $("#modalDialog");
	dlg.width($(window).width());
	dlg.height($(window).height()+$(document).scrollTop());
}

(function($){
	
	if (!window.console)
	{
		window.console = {
			log:function() {},
			dir:function() {}
		}
	}
	
	$.fn.extend( {
	
		/*Toggle*/
		boiToggle: function()
		{		
			$(this).click(function(){
				
				if($(this).hasClass("close")){
					$(this).parent().find(".ctn").hide();
					$(this).removeClass("close");
				}
				else
				{
					$(this).addClass("close");
					$(this).parent().find(".ctn").show();
				}
			});
			
			var openFirst = true;
			var h = document.location.hash;
			if (h.length>0)
			{
				$(this).each(function()
				{
					if($(this).attr("href")==h)
					{
						openFirst = false;
						$(this).click();
					}
				})
			}
			
			if (openFirst)
				$(this).closest(".toggle").find("a:first").click();
	
		},
		/*Carousel*/
		boiCarousel: function()
		{
		$(this).jcarousel({
		  	 vertical: true,
       		 scroll: 1
		});
		},
		/*Slide Box Hp*/
		boiSlideBox: function()
		{
		 $(this)
			.cycle({
		        fx:      'scrollLeft',
		        timeout:  5000,
		        pager:   '#nav'
		    });
		},
		/*Slide Claim Hp*/
		boiSlideBoxHp: function()
		{
			$(this).each(function(i) {
				$(this).hover(function() {
					$(this).addClass("active");
					if($(this).hasClass("k")){
						$(this).find(".carousel").animate({top: 120},400)
					}
				},function() {
					$(this).removeClass("active");
					if($(this).hasClass("k")){
						$(this).find(".carousel").animate({top: 210},400)
					}
				})
			});
		},
		/*Share*/
		boiShareToolbar: function(options)
		{
			$(this).each(function(){
	
				var URL = $(this).attr("href");
				URL = URL.split("${url}").join(document.location.href);
				URL = URL.split("${title}").join($("head title").text());
				$(this).attr("href",URL);
			});
		},
		/*Open Popup*/
		boiOpenPopup: function(options)
		{		
			$(this).click(function()
			{
				showModalDialog();		
				$("div" + $(this).attr("href")).css("left", ($(window).width()-$("div.layer").width())/2);
				$("div" + $(this).attr("href")).css("top", 120 + $(document).scrollTop());		
				$("div" + $(this).attr("href")).fadeIn("fast");
			});
		},
		/*Opacity Box HP*/
		boiOpacityHp: function(options)
		{
			$(this).animate({
			    opacity: 0.7
			}, 10);
		},
		boiMenu: function()
		{
			$(this).each(function()
			{
				var url = document.location.href;
				var href = $(this).attr("href").split("../").join("");
				var hrefPrefix = href.indexOf("/")!=-1 ? href.substring(0, href.lastIndexOf("/"))+"/" : href;
		
				if (href!="" && url.indexOf(href)!=-1)
				{
					$(this).addClass("current");
				}
				else if (hrefPrefix!="" && url.indexOf(hrefPrefix)!=-1)
				{
					$(this).addClass("active");
				}				
			})
		},
		boiPlayer: function()
		{
			
			$(this).click(function(e)
			{
				e.preventDefault();		 
				
				var href = $(this).attr('href') ;
				var ytUrl = href.replace(new RegExp("watch\\?v=", "i"), 'v/');
				if (ytUrl.indexOf("&")!=-1)
					ytUrl = ytUrl.split("&")[0];
				
				var objectHtml = "<object width=\"600\" height=\"340\">" +
								 "<param name=\"movie\" value=\"" + ytUrl + "?fs=1&amp;hl=it_IT&amp;rel=0\"></param>" +
								 "<param name=\"allowFullScreen\" value=\"true\"></param>" +
								 "<param name=\"allowscriptaccess\" value=\"always\"></param>" +
								 "<embed src=\""+ ytUrl + "?fs=1&amp;hl=it_IT&amp;rel=0\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"600\" height=\"340\"></embed>" +
								 "</object>";

				$("div.layer").find("#player").append(objectHtml);
				showModalDialog();
				$("div.layer").css("left", ($(window).width()-$("div.layer").width())/2);
				$("div.layer").css("top", 120 + $(document).scrollTop());		
				$("div.layer").fadeIn("fast");				
			})
		},
		/*Close Popup*/
		boiClosePopup: function(options)
		{
			$(this).click(function()
			{
				$("div.layer").fadeOut("fast");
				$("#modalDialog").remove();
				$("div.layer").find("#player").empty();
			})
		},
		boiGaTrack: function()
		{
			$(this).each(function()
			{
				var tn = $(this).get(0).tagName.toLowerCase();
				if (tn=="a")
				{
					$(this).click(function(e)
					{
						var href = $(this).attr("href");
						if (href.indexOf("#")==0 && document.location.hash!=href)
							track(href.substring(1),true);
						if (href.indexOf(".pdf")!=-1)
							track(href.substring(1),false);
						return true;
					})
				}
				if (tn=="form")
				{
					$(this).submit(function()
					{
						var t = $(this).attr("title");
						if (t)
						{
							t = t.toLowerCase().split(" ").join("-");
							document.location.hash=t;
							track(t, true);
						}
						return true;
					})
				}
			})
			
			function track(href, prependBase)
			{
				if (href.length==0)
					return;
				var toTrack;
				href = href.split("../").join("");
				if (prependBase)
				{
					var base = document.location.pathname + "";
					if (base.indexOf(".")!=-1)
						base = base.substring(0, base.lastIndexOf("."));
					toTrack = base + "/" + href;
				}
				if (toTrack && toTrack.length>0 && toTrack.charAt(0)!="/")
					toTrack = "/" + toTrack;

				if (toTrack)
				{
					try
					{
						_gaq.push(['_trackPageview',toTrack]);
						console.log("Track '" + toTrack + "'");
					}
					catch(e)
					{
						console.log("Could not track '" + toTrack + "'");
					}
				}
				return true;
			}
			
			if (document.location.hash.length>1)
				track(document.location.hash.substring(1), true);
		}
	});
})(jQuery);


$(document).ready(function(){

	$("body a").click(function(e)
	{							
		if (($(this).attr("onclick")+"").indexOf("return false")!=-1)
		{
			e.preventDefault();
			return false;
		}
	});

	$("div.mappa > ul.map li:nth-child(1) a.first").addClass("blue");
	$("div.mappa > ul.map li:nth-child(2) a.first").addClass("yellow");
	$("div.mappa > ul.map li:nth-child(3) a.first").addClass("red");
	
	$("#header a[accesskey=1]").attr("id", "first");
	$("#header a[accesskey=2]").attr("id", "center");
	$("#header a[accesskey=3]").attr("id", "last");
	
	// trigger plugins
	$("#header div.menu a").boiMenu();
	$("div.toggle a.open, ul.map a.open").boiToggle();
	$("#mycarousel").boiCarousel();
	$("#slideshow").boiSlideBox();
	$("#hp div.ctn").boiSlideBoxHp();	
	$("div.share a").boiShareToolbar();	
	$(".boiplayer").boiPlayer();	
	$("#private a").boiOpenPopup();	
	$("div.layer div.close a").boiClosePopup();	
	$("#hp div.ctn p, #hp div.ctn ul").boiOpacityHp();	
	$("a, form.kform").boiGaTrack();
	$("#gallery a").lightBox();
});
