$(document).ready(function(){
	/*Menu top
		
	$(".menutop > ul > li").each(function(){
		
		if(!$(this).hasClass("active")){
	
			$(this).hover(function(){
				$(this).find("ul").show();
				$(this).addClass("active");
			},
			function(){
				$(this).find("ul").hide();
				$(this).removeClass("active");
			});
		}
	});*/
	
	$(".menutop ul:first > li").hover(function(){
		$(this).addClass("active");
		$(this).find("ul:first").show();				
	}, function(){
		$(this).removeClass("active");
		$(this).find("ul:first").hide();
	});
	
	
	/*Header Info*/
	$("a.info").mouseover(function(){
		$("div.info").show();
	});
	$("a.info").mouseout(function(){
		$("div.info").hide();
	});
	/*Header Login
	$(".btn_login").click(function(){
		$("div.login div.off").hide();
		$("div.login div.on").show();
	});*/
	
	
	/*Menu Fist Level label current*/
	var page = location.pathname;
	page = page.split("business/testtm/").join("");
	
	$("#menu > ul > li").each(function(){
		
		var a = $(this).find("a:first").attr("href");
		var node = a.replace("/index.html","");
		var node = node.substring(node.lastIndexOf("/")+1,node.length);
		if($(this).attr("class") == "hp"){		
			var hpPage = node+"/index.html";
			if(page.indexOf(hpPage) != -1){
				$(this).find("a:first").addClass("active");		
			}	
		}
		else if(page.indexOf("/"+node)==0){
			$(this).find("a:first").addClass("active");	
			if($(this).hasClass("promo")){
				$(this).addClass("active");
			}	
		}
	});
	
	/*Menu Second Level*/
	if(!$("#cc a:first").hasClass("active")){
	
		$("#cc").hover(function(){
			$(this).find("ul").show();
			$(this).find("a:first").addClass("active");
		},
		function(){
			$(this).find("ul").hide();
			$(this).find("a:first").removeClass("active");
		});
	}
	else{
		$("#cc").hover(function(){
			$(this).find("ul").show();
		},
		function(){
			$(this).find("ul").hide();
		});
	}
	
	
	if(!$("#pp a:first").hasClass("active")){
		$("#pp").hover(function(){
			$(this).find("ul").show();
			$(this).find("a:first").addClass("active");
		},
		function(){
			$(this).find("ul").hide();
			$(this).find("a:first").removeClass("active");
		});
	}
	else{
		$("#pp").hover(function(){
			$(this).find("ul").show();
		},
		function(){
			$(this).find("ul").hide();
		});
	}
	if(!$("ul li.promo a:first").hasClass("active")){
		$("ul li.promo a").hover(function(){
			$(this).parent().addClass("active");
		},
		function(){
			$(this).parent().removeClass("active");
		});
	}
	
	/*Toggle*/
	$("div.toggle a.open").click(function(){
	
		if($(this).hasClass("close")){
			$(this).parent().find("p").hide();
			$(this).removeClass("close");
		}
		else{
		$(this).addClass("close");
		$(this).parent().find("p").show();
		}
	});
	
	/*MapSite OLD
	$("div.mapsite > ul > li:last").css("display","none");
	
	$("div.mapsite > ul > li > ul > li").each(function(){
		
		$(this).find("a:first").css("font-weight","bold");
		
	});	*/
	
	/*MapSite*/
	$("div.mapsite span.open a").click(function(){
	
		if($(this).parent().hasClass("close")){
			$(this).parent().parent().find("ul").hide();
			$(this).parent().removeClass("close");
		}
		else{
		$(this).parent().addClass("close");
		$(this).parent().parent().find("ul").show();
		}
	});
	
	
	/*Menu Lat Sx*/
	$("div.colsx > ul > li > span:first").css("padding-top","0");

});

$.fn.bgIframe = function()
{
	// private
	var m_this = this;
	
	// constructor
	setUp();
	
	function setUp()
	{
		// check if already called
		if(m_this.data("__tguiSetup.BgIframe"))
			return;
		m_this.data("__tguiSetup.BgIframe", true);	
		
		if ( $.browser.msie )
		{
			var ifr = '<iframe class="bgIframe" style="position:absolute; z-index:1; filter: alpha(opacity=0)" frameborder="0" src="javascript:false"></iframe>';
			m_this.before(ifr);
			m_this.prev().width(m_this.outerWidth());
			m_this.prev().height(m_this.outerHeight());
			m_this.prev().css("left",m_this.css("left"));
			m_this.prev().css("top",m_this.css("top"));
		}
	}
	
	return this;
}

PhishingAlarmWindow = new function()
{
	var w;
	var m;
	
	this.setup = function()
	{
		var toLoad = "/servizi/popup_phishing.html";
		if (document.location.href.indexOf("testtm/")!=-1)
			toLoad = "/business/testtm/servizi/popup_phishing_alarm.html";
		if (document.location.href.indexOf("file:/")!=-1)
			toLoad = "servizi/popup_phishing_alarm.html";
		$.get(toLoad, PhishingAlarmWindow.draw);
	}
	
	this.draw = function(data)
	{
		if (data && data.split(" ").join("")!="")
		{
			if (getCookie("fdphpop")!="set")
			{
				setCookie("fdphpop", "set");
				
				m = $("<div id=modalDialog style='position:absolute;left:0;top:0;display:none;background-color:#000;z-index:1000;'>&nbsp;</div>");	
				$(document.body).append(m);
				if ($.browser.ie)
					m.css("filter", "alpha(opacity='50')");
				else
					m.css("opacity",0.5);
				sizeAndPosition();
				m.bgIframe();

				$(window).resize(sizeAndPosition);
				$(window).scroll(sizeAndPosition);
	
				w = $("<div id='phishingAlarm' align='center' style='position:absolute;display:none;width:100%;left:0;top:80px;z-index:1001'></div>");
				$(document.body).append(w);
				w.html(data);
	
				$("embed").hide();
				$("object").hide();
				m.fadeIn("normal", function(){w.show()});
			}
		}
	}
	
	this.close = function()
	{
		m.remove();
		w.remove();
		if ( $.browser.msie )
			$(".bgIframe").remove();
		$("embed").show();
		$("object").show();
	}
	
	function sizeAndPosition()
	{
		var dlg = $("#modalDialog");
		dlg.width($(window).width());
		dlg.height($(window).height()+$(document).scrollTop());
	}
	
	function getCookie(name)
	{
		if (document.cookie.length > 0)
		{
			begin = document.cookie.indexOf(name+"=");
			if (begin != -1)
			{
				begin += name.length+1;
				end = document.cookie.indexOf(";", begin);
				if (end == -1) end = document.cookie.length;
				return unescape(document.cookie.substring(begin, end));
			}
		}
		return "";
	}

	function setCookie(name, value)
	{
		document.cookie = name + "=" + escape(value) + "; path=/;";
	}
}

$(window).load(PhishingAlarmWindow.setup);
