$(function() {
	_mpInit();
});

function _mpInit(){
	/*Layout*/
	
	$(".BoxLabels .Label").corner("6px tl tr Hover");
	$(".cssBBox.Corner").corner("6px");
	$(".casehistory").corner("6px");
	$(".cssMenuNL").corner("6px");
	//$(".cssBBox").find(".cssArticle:last").css({border: "none"});

	/*Allineamento altezze box in box che ne mantiene il layout*/
	//$(".BOXColumnChildrens>tbody>tr").each(function(){var _h=$(this).height(); $(this).find("td>.cssBBox").each(function(){$(this).height(_h)})});
	
	var perc = 100 / $(".MENU_TAB").length+"";
	
	$(".MENU_TAB").width(perc+"%");
	//arrotonda in eccesso l'ultimo per correggere sbavature a destra
	
	if ($.browser.msie) $("#menuarea").find(".MENU_TAB:last").width(Math.round(perc-1)+"%"); 
	//$(".MENU_TAB").width(perc.split('.')[0]+"%");
	
	$(".MENU_TAB .pages a").parent().width("200px");
	
	if ($.browser.msie && $.browser.version<7) $("br[@type=_moz]").hide();
	
	/*MENU*/
	
	_mpMenu();
	_mpNL();
	_mpBBoxLabel();
	
	_mpSlide()
	
	$(".cssMenuNL .page").each(function(){
		if ($("#jTip_"+this.id).html()!="")
			$(this).jHelperTip({ trigger: "hover",  dC: "#jTip_"+this.id,   autoClose:true, topOff: -100, leftOff: -350})
	});
	
	if ($("#Main-Right *").length==0) {
		$(".cssPageIndex").width("266px");
		$("#Main-Left").width("100%");
		
		
	}
	//_mpBoxSlides();
	$("td").attr("vAlign", "top");
	
	$(".CompanyInfo").click(function(){$(this).find("span").toggle(300)});
	
}

function NLPageIndex(){
	var html = "";
	$(".jTip").each(function(){
			html+="<div class=\"cssArticle\">"+$(this).clone().removeClass("jTip").html()+"</div>";
		});
	alert(html);
	$(".cssPageTitle").after(html);
}
function _mpMenu(){
	$(".MENU_TAB").hover(MenuHover, MenuOut);
	
	/*Evidenzia il Menu corrente*/
	var _kp = getStringVar('KeyPub')
	$("#gp_"+_kp.split('|')[0]).parent().addClass("MENU_CURRENT");
}
function _mpNL(){
	/*NAVIGAZIONE LOCALE*/
	$(".cssMenuNL").find(".page:last").css({border: "none"});
	var _kp = getStringVar('KeyPub');
	
	_kp = _kp.replace('|', '_sep_');
	$("div.page[@id='kp_"+_kp+"']").addClass("current");
	$("div.page[@id='kp_"+_kp+"']").next(".subpages").removeClass("hide");
	
	//$("div.page").hover(function(){$(this).next(".subpages").removeClass("hide")}, function(){$(this).next(".subpages").addClass("hide")});
	
		
}

function _mpBBoxLabel(){
	$(".LabelObjects").corner("3px");

	$(".cssBBox.LABEL .Label").eq(0).addClass("CurrentLabel");
	$(".LabelObjects>div").css({"display": "none"});
	$(".LabelObjects>div").eq(0).css({"display": ""});
	
	$(".cssBBox.LABEL .Label").click(
		function(){
			var pos = jQuery.inArray(this, $(this).parent().find(".Label").siblings().siblings());
			
			//Label corrente
			$(this).parent().find(".Label").removeClass("CurrentLabel");
			$(this).addClass("CurrentLabel");
			//nasconde tutti i contenuti
			$(this).parent().parent().find(".LabelObjects>div").css({"display": "none"}); 
			//contenuto corrente
			$(this).parent().parent().find(".LabelObjects>div").eq(pos).css({"display": ""});	
		}
	);
	
	return;
	$(".cssBBox.LABEL").each(function(){
		var _ran = "banner_"+Math.random();
		$(this).attr("myid", _ran);
		$(this).attr("current", 0);
		$(this).attr("max", $(this).find(".LabelObjects>div").size()-1);

		var s = setInterval("AnimBox('"+_ran+"')" , 4000);
	});

}

function _mpBoxSlides(){
	$(".cssBBox.LABEL").each(function(){
			$(this).find(".Slides").css({"position": "relative"});
			$(this).find(".Slides>div").css({"position": "relative", "top": "0px", "display": "none"});
			var _ran = "banner_"+Math.random();
			$(this).attr("myid", _ran);
			$(this).attr("current", 0);
			$(this).attr("max", $(this).find(".Slides>div").size()-1);
			
			//var s = setInterval("AnimBox('"+_ran+"')" , 2000);
	});

}

function AnimBox(){
	var _this = $(".cssBBox.LABEL[@myid='"+arguments[0]+"']");
	
	var _current = _this.attr("current");
	_this.find(".Slides>div").eq(_current).animate({opacity: 'hide'}, 2000)
	var _max = _this.attr("max");
	if (_current<_max) _current++; else _current=0;
	_this.attr("current", _current);
	_this.find(".Slides>div").eq(_current).animate({opacity: 'show'}, 2000);

}

function MenuHover(){
	
	$(this).addClass("MENU_HOVER");
	
	
	//$(this).find(".GP").css({backgroundImage: "url(skins/IST/images/menu/menu-hover-centro.gif", background-repeat: "repeat-x")"});
	//$(this).find(".GP").css({BACKGROUND-IMAGE: "skins/IST/images/menu/menu-hover-centro.gif"});
	
	return;
	//pages anim
	$(this).find(".pages").fadeIn();

}

function MenuOut(){
	$(this).removeClass("MENU_HOVER");
	
	//pages anim
	$(this).find(".pages").fadeOut();
}



/*
 * mpSlide
 *
 *
 *
 *
 */
function _mpSlide(){
	var _set = {interval: 5000, corner: 6};
	$(".mpSlide").each(function(){InitSlide(this, _set)})

}

function InitSlide(slide, settings){
	var s = $(slide);

	s.find(".slide:first").show(1000);
	s.find(".slide:first").attr("current", 1);
	s.find(".slide:last").attr("last", 1);
	if (settings.corner!=undefined)s.corner(settings.corner+"px");

	var _this = this;
	var animID = setInterval(function(){SlideNext(s)}, settings.interval);
	s.find(".slide").mouseover(function(){clearTimeout(animID)})
	s.find(".slide").mouseout(function(){animID = setInterval(function(){SlideNext(s)}, settings.interval);})
}
function SlideNext(s){
	var curr = s.find(".slide[current=1]");
	var next;

	if (curr.attr("last")==1)
		next = s.find(".slide:first");
	else
		next = s.find(".slide[current=1]").next();
	curr.attr("current", 0);
	next.fadeIn(2000);
	curr.fadeOut(1500);
	next.attr("current", 1);
}

//social network
function condividi(id) {
	var url;
	var dtitle;
	dtitle = escape(document.title);
	url = escape(document.location);
	wtitle = document.title.substring(0,document.title.indexOf('- Il Sole 24 ORE'));
	wtitle = escape(wtitle.replace(/\s+$/g,''));
	var description;
	var metas = document.getElementsByTagName('meta');
	for (var x=0,y=metas.length; x<y; x++) {
	  if (metas[x].name.toLowerCase() == "description") {
	    description = metas[x];
	  }
	}
	switch(id)
	{
		case "linkedin":
		  url = "http://www.linkedin.com/shareArticle?mini=true&url=" + url + "&title=" + dtitle + "&summary=" + escape(description.content) + "&source=Il Sole 24 ORE";
		  break;  
		case "oknotizie":
		  url = "http://oknotizie.alice.it/post?url=" + url + "&title=" + dtitle;
		  break;  
		case "facebook":
		  url = "http://www.facebook.com/sharer.php?u=" + url;
		  break; 
		case "wikio":
		  url = "http://www.wikio.it/vote?domain=ilsole24ore.com&title=" + wtitle;
		  break;	
		case "delicious":
		  url = "http://del.icio.us/post?v=4&noui&jump=close&url=" + url + "&title=" + dtitle;
		  break;    
		case "digg":
		  url = "http://digg.com/submit?phase=2&url=" + url + "&title=" + dtitle;
		  break;
		case "technorati":
		  url = "http://technorati.com/faves?add=" + url;
		  break;
		case "yahoo":
		  url = "http://myweb.yahoo.com/myresults/bookmarklet?t=" + dtitle + "&u=" + url + "&ei=UTF";
		  break;
	}
	//document.location = url;
	window.open(url);
}


/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jHelpertip
 * Version: 1.0 (Jun 2, 2008)
 * Requires: jQuery 1.2+
 */
(function($) {
		  
	$.fn.jHelperTip = function(options) {
		// merge users option with default options
		var opts = $.extend({}, $.fn.jHelperTip.defaults, options);
		
		// default actions
		// create a ttC is not found
		if ($(opts.ttC).length == 0)
			$('<div id="'+opts.ttC.slice(1)+'"></div>').appendTo("body");
		
		// create a dC is not found
		if ($(opts.dC).length == 0)
			$('<div id="'+opts.dC.slice(1)+'"></div>').appendTo("body");
		
		if ($(opts.aC).length == 0)
			$('<div id="'+opts.aC.slice(1)+'"></div>').appendTo("body");

		
		// initialize our tooltip and our data container and also the close box
		$(opts.ttC).add(opts.aC).css({
			position: "absolute",
			display: "inline"
		}).hide();
		
		$(opts.dC).hide();
		
		// close the tooltip box
		var closeBox = function(){
			if (opts.source == "attribute")
				$(opts.aC).hide().empty();
			else
				$(opts.ttC).hide().empty();
		};
		
		$(".jHelperTipClose").bind("click", closeBox);
		$(opts.ttC).bind("mouseover",function(){
			$(opts.ttC).show();
			return false;
		});

		// the sources of getting data
		var getData = function(obj,e){
			if (opts.source == "ajax") {
				getPosition(e);
				$(opts.ttC).html('<div><img src="'+opts.loadingImg+'"/> '+opts.loadingText+'</div>').show();
				
				$.ajax({
					type: opts.type,
					url: opts.url,
					data: opts.data,
					success: function(msg){
						$(opts.ttC).html(msg);
						// reInitialize the close controller
						$(".jHelperTipClose").unbind("click", closeBox); 
						$(".jHelperTipClose").bind("click", closeBox);
					}
				});
			}
			
			else if (opts.source == "container"){
				$(opts.ttC).show().empty();
				$(opts.dC).clone(true).show().appendTo(opts.ttC).corner("6px");
			}
			
			if (opts.source == "attribute"){
				$(opts.aC).html($(obj).attr(opts.attrName));
			}
		};
		
		// used to position the tooltip
		var getPosition = function (e){
			var top = e.pageY+opts.topOff;
			var left = e.pageX+opts.leftOff;
			
			if (opts.source == "attribute"){
				$(opts.aC).css({
					top: top,
					left: left,
					opacity: opts.opacity
				}).show();
			}
			else {
				$(opts.ttC).css({
					top: top,
					left: left,
					opacity: opts.opacity
				}).show();
			}
		};

		// just close tool tip when not needed usually trigger by anything outside out tooltip target
		if (opts.trigger == "hover") {
			$(this).bind("mouseover", function(e){
				e.preventDefault();
				getData(this, e);
				return false;
			});
			$(this).bind("mousemove", function(e){
				getPosition(e);
				return false;
			});
			
			$(this).bind("mouseout", function(e){
			    if (opts.source == "attribute")
					$(opts.aC).hide().empty();
				else
					$(opts.ttC).hide().empty();
				return false;
			});
		}
		
		else if (opts.trigger == "click") {
			$(this).bind("click", function(e){
				getData(this, e);
				getPosition(e);
				$(document).bind("click", function(e){
					if (opts.autoClose) {
						if (opts.source == "attribute")
							$(opts.aC).hide().empty();
						else
							$(opts.ttC).hide().empty();
					}
				});
				
				return false;
			});

		}
	};
	
	$.fn.jHelperTip.defaults = {
		trigger: "click",
		topOff: 3,
		leftOff: 10,
		source: "container", /* attribute, container, ajax */
		attrName: '',
		ttC: "#jHelperTipContainer", /* tooltip Container*/
		dC: "#jHelperTipDataContainer", /* data Container */
		aC: "#jHelperTipAttrContainer", /* attr Container */
		opacity:  1.0,
		loadingImg: "ajax-loader.gif",
		loadingText: "Loading...",
		type: "GET", /* data can be inline or CSS selector */
		//url: '',
		//data: '',
		autoClose: true
	};
		
	
		  

})(jQuery);

