﻿// IMPLEMENTAZIONE DI Array_indexOf
var Array_indexOf = function( array, obj ) {
	for( var i = 0; i < array.length; i++ )
		if( array[ i ] == obj ) return i;
	return -1
}
// IMPLEMENTAZIONE DI appendEvent
var appendEvent = function( element, event, callback ) {
	var evt = "on" + event;
	var fnc = element[ evt ];
	if( typeof fnc != undefined ) {
		if( fnc )
			element[ evt ] = function() {
				fnc();
				callback();
			}
		else element[ evt ] = callback;
	}
}
// External for NO file_generali
function ifExt_preHost() {
	var dom = location.hostname.match( /\.motorionline\.com$/ ) ? true : false;
	if( dom && ! window.QN ) dom = false;
	var ext_hosts = [ "meteo\\.quotidiano\\.net" ];
	for( var i = 0; i < ext_hosts.length; i++ ) {
		var re = new RegExp( ext_hosts[i], "" );
		if( location.hostname.match( re ) ) {
			dom = false;
			break;
		}
	}
	return dom ? "" : "http://qn.quotidiano.net";
}

fedHead = {
	menu : {
		id : "fedHeadMenu",
		items : [
			{
				id : "fedHead_qnet",
				type : "link",
				href : "http://qn.quotidiano.net",
				text : "Quotidiano.net"
			},
			{
				id : "fedHead_gio",
				type : "link",
				href : "http://www.ilgiorno.it",
				text : "Il Giorno"
			},
			{
				id : "fedHead_naz",
				type : "link",
				href : "http://www.lanazione.it",
				text : "La Nazione"
			},
			{
				id : "fedHead_rdc",
				type : "link",
				href : "http://www.ilrestodelcarlino.it",
				text : "Il Resto del Carlino"
			},
			{
				id : "fedHead_qs",
				type : "link",
				href : "http://qn.quotidiano.net/sport",
				text : "QS Sport",
				rel : "nofollow"
			},
			{
				id : "fedHead_net",
				type : "menu",
				href : "http://qn.quotidiano.net/mappa_sito.php",
				text : "Network",
				menu : {
					id : "fedHead_network",
					items : [
						[
							{
								tagName : "strong",
								innerHTML : "Cultura e spettacolo"
							},
							{
								id : "fedHead_caffe",
								type : "link",
								href : "http://qn.quotidiano.net/caffe",
								text : "il caffè",
								rel : "nofollow"
							},
							{
								id : "fedHead_spettacoloit",
								type : "link",
								href : "http://www.lospettacolo.it/",
								text : "lospettacolo.it",
								rel : "nofollow"
							},
							{
								tagName : "strong",
								innerHTML : "Motori"
							},
							{
								id : "fedHead_motori",
								type : "link",
								href : "http://qn.quotidiano.net/motori",
								text : "QN Motori",
								rel : "nofollow"
							},
							{
								id : "fedHead_motorionline",
								type : "link",
								href : "http://www.motorionline.com",
								text : "MotoriOnline",
								rel : "nofollow"
							}
						], [
							{
								tagName : "strong",
								innerHTML : "Sport"
							},
							{
								id : "fedHead_sport",
								type : "link",
								href : "http://qn.quotidiano.net/sport",
								text : "QS Sport",
								rel : "nofollow"
							},
							{
								id : "fedHead_informacalcio",
								type : "link",
								href : "http://informacalcio.quotidiano.net",
								text : "informacalcio.it",
								rel : "nofollow"
							},
							{
								id : "fedHead_ubi",
								type : "link",
								href : "http://www.ubitennis.com",
								text : "UbiTennis",
								rel : "nofollow"
							},
							{
								id : "fedHead_cavallo",
								type : "link",
								href : "http://www.cavallomagazine.it",
								text : "Cavallo Magazine",
								rel : "nofollow"
							}
						], [
							{
								tagName : "strong",
								innerHTML : "Servizi"
							},
							{
								id : "fedHead_pronto",
								type : "link",
								href : "http://www.prontoimprese.it",
								text : "ProntoImprese",
								rel : "nofollow"
							},
							{
								id : "fedHead_lavoro",
								type : "link",
								href : "http://lavoro.quotidiano.net",
								text : "Quotidiano.net Lavoro",
								rel : "nofollow"
							},
							{
								id : "fedHead_homeit",
								type : "link",
								href : "http://home.it",
								text : "Home.it",
								rel : "nofollow"
							},
							{
								tagName : "strong",
								innerHTML : "Magazine"
							},
							{
								id : "fedHead_ecquo",
								type : "link",
								href : "http://www.ecquo.it",
								text : "ecquo",
								rel : "nofollow"
							},
							{
								id : "fedHead_dieta",
								type : "link",
								href : "http://dietaclub.quotidiano.net",
								text : "Dietaclub",
								rel : "nofollow"
							}
						], [
							{
								tagName : "strong",
								innerHTML : "Tecnologia",
								className : "double"
							},
							{
								id : "fedHead_hwupgrade",
								type : "link",
								href : "http://www.hwupgrade.it/",
								text : "Hardware Upgrade",
								rel : "nofollow"
							},
							{
								id : "fedHead_gamemag",
								type : "link",
								href : "http://www.gamemag.it/",
								text : "Game magazine",
								rel : "nofollow"
							},
							{
								id : "fedHead_business",
								type : "link",
								href : "http://www.businessmagazine.it/",
								text : "Business magazine",
								rel : "nofollow"
							},
							{
								id : "fedHead_hwfiles",
								type : "link",
								href : "http://www.hwfiles.it/",
								text : "HW Files",
								rel : "nofollow"
							}
						], [
							{
								tagName : "strong",
								innerHTML : "&nbsp;",
								className : "white"
							},
							{
								id : "fedHead_tvtech",
								type : "link",
								href : "http://www.tvtech.it/",
								text : "TV tech",
								rel : "nofollow"
							},
							{
								id : "fedHead_fotografi",
								type : "link",
								href : "http://www.fotografidigitali.it/",
								text : "Fotografi digitali",
								rel : "nofollow"
							},
							{
								id : "fedHead_appunti",
								type : "link",
								href : "http://www.appuntidigitali.it/",
								text : "Appunti digitali",
								rel : "nofollow"
							},
							{
								id : "fedHead_avmagazine",
								type : "link",
								href : "http://www.avmagazine.it/",
								text : "AVMagazine",
								rel : "nofollow"
							}
						]
					]
				}
			},
			{
				id : "fedHead_search",
				type : "menu",
				href : "http://www.motorionline.com",
				text : "Cerca",
				menu : {
					id : "fedHead_searchbox",
					items : [
						{
							id : "fedHead_searchsite",
							type : "form",
							method : "get",
							action : "http://motorsport.motorionline.com/",
							fields : [
								{
									tagName : "input",
									type : "text",
									name : "s",
									value : ""
								}
							]
						}
					]
				}
			}/*,
			{
				id : "fedHead_user",
				type : "link",
				href : "#",
				text : "Ciao Ospite"
			}*/
		]
	},
	cont : false,
	tabIndex : 1,
	init : function() {
		var id = "fedHead";
		if( document.getElementById( id ) ) return;
		this.cont = document.createElement( "div" );
		this.cont.id = id;
		var body = this.getBody();
		body.insertBefore( this.cont, body.firstChild );
		this.setTop();
		this.callStyle();
		this.addMenu( this.menu, this.cont );
		appendEvent( window, "load", this.transTabIndex );
	},
	callStyle : function() {
		if( ! this.cont ) return;
		var url = "http://www.motorionline.com/wp-content/themes/motorionline-qn/file_generali/css/liz/fedHead.css";
		var head = document.getElementsByTagName( "head" )[0];
		var link = document.createElement( "link" );
		link.type = "text/css";
		link.rel = "Stylesheet";
		link.href = url;
		head.appendChild( link );
		/*
		var tmp = document.createElement( "div" );
		var style = '<style type="text/css">@import url("' + url + '");</style>';
		tmp.innerHTML = '<p>x</p>' + style;
		style = tmp.getElementsByTagName( "style" )[0];
		var body = this.getBody();
		body.insertBefore( style, this.cont );
		//document.writeln( style );
		*/
	},
	setTop : function( yTop ) {
		if( ! yTop ) yTop = 20;
		var body = this.getBody();
		var paddingTop = 0, paddingSum = [ "marginTop", "paddingTop" ];
		for( var i = 0; i < paddingSum.length; i++ ) {
			var tmp = this.getStyle( body, paddingSum[ i ] );
			if( tmp ) paddingTop += parseFloat( tmp.match( /\d+/ )[0] );
		}
		body.style.marginTop = "0px";
		if( paddingTop < yTop ) body.style.paddingTop = yTop + "px";
	},
	getStyle : function( el, css ) {
		var ret = false;
		if( window.getComputedStyle ) ret = window.getComputedStyle( el, null )[ css ];
		else if( el.currentStyle ) ret = el.currentStyle[ css ];
		return ret;
	},
	getBody : function() {
		return document.getElementsByTagName( "body" )[0];
	},
	addMenu : function( ul, cont ) {
		if( ! this.cont || ! cont ) return;
		var menu = document.createElement( "ul" );
		menu.id = ul.id;
		for( var i = 0; i < ul.items.length; i++ ) {
			var itemL = ul.items[ i ];
			var li = document.createElement( "li" );
			li.id = itemL.id || cont.id + "__item" + i;
			if( i + 1 == ul.items.length )
				li.className = "fedHead_last";
			if( !( itemL instanceof Array ) ) itemL = [ itemL ];
			for( var x = 0; x < itemL.length; x++ ) {
				var item = itemL[ x ];
				var submenu = a = field = false;
				if( item.href && item.text ) {
					var a = document.createElement( "a" );
					a.href = item.href;
					if( item.rel ) a.rel = item.rel;
					if( item.id && item.id != li.id ) a.id = item.id;
					a.target = "_blank";
					a.tabIndex = this.tabIndex++;
					li.appendChild( a );
					var span = document.createElement( "span" );
					span.innerHTML = item.text;
					a.appendChild( span );
				}
				switch( item.type ) {
					case "menu" :
						var submenu = this.addMenu( item.menu, li );
						submenu.isSubMenu = true;
						li.submenu = submenu;
						appendEvent( li, "mouseover", function(){ fedHead.menuaction( this ); } );
						appendEvent( li, "mouseout", function(){ fedHead.menuaction( this, true ); } );
						appendEvent( submenu, "mouseover", function(){ fedHead.menuaction( this ); } );
						appendEvent( submenu, "mouseout", function(){ fedHead.menuaction( this, true ); } );
						break;
					case "form" :
						var form = document.createElement( "form" );
						form.method = item.method || "post";
						form.action = item.action;
						for( var f = 0; f < item.fields.length; f++ ) {
							var field = document.createElement( item.fields[ f ].tagName );
							for( var k in item.fields[ f ] ) {
								if( k != "tagName" )
									field.setAttribute( k, item.fields[ f ][ k ] );
							}
							field.submenu = menu;
							appendEvent( field, "focus", function(){ fedHead.menuaction( this ); } );
							appendEvent( field, "blur", function(){ fedHead.menuaction( this, true ); } );
							if( field.type != "hidden" ) field.tabIndex = this.tabIndex++;
							form.appendChild( field );
						}
						li.appendChild( form );
						break;
					case "link" :
						break;
					default :
						if( ! item.tagName ) break;
						var el = document.createElement( item.tagName );
						for( var t in item )
							if( t != "tagName" )
								el[ t ] !== undefined ? el[ t ] = item[ t ] : el.setAttribute( t, item[ t ] );
						li.appendChild( el );
						break;
				}
				if( a ) {
					if( submenu ) a.href = "javascript:void(0);";
					a.submenu = submenu || menu;
					appendEvent( a, "focus", function(){ fedHead.menuaction( this ); } );
					appendEvent( a, "blur", function(){ fedHead.menuaction( this, true ); } );
				}
				menu.appendChild( li );
			}
		}
		cont.appendChild( menu );
		return menu;
	},
	menuaction : function( source, out ) {
		var menu = source.submenu || source;
		if( ! menu.isSubMenu ) return;
		if( ! menu.over ) menu.over = [];
		var i = Array_indexOf( menu.over, source );
		if( ! out && i < 0 ) menu.over.push( source );
		else if( out && i >= 0 ) menu.over.splice( i, 1 );
		var time = out ? 333 : 500;
		if( menu.timeout ) clearTimeout( menu.timeout );
		menu.timeout = setTimeout( function(){ fedHead.menuaction_do( menu ); }, time );
	},
	menuaction_do : function( menu ) {
		var li = menu.parentNode;
		while( li.tagName != "LI" && li.parentNode ) li = li.parentNode;
		var cl = " hover";
		if( menu.over.length && li.className.indexOf( cl ) < 0 ) li.className += cl;
		else if( ! menu.over.length ) li.className = li.className.replace( cl, "" );
	},
	transTabIndex : function() {
		var all = fedHead.cont.getElementsByTagName( "*" );
		for( var i = 0; i < all.length; i++ ) all[ i ].isFromFedHead = true;
		var all = fedHead.getBody().getElementsByTagName( "*" );
		for( var i = 0; i < all.length; i++ )
			if( ! all[ i ].isFromFedHead
				&& all[ i ] != fedHead.cont
				&& all[ i ].getAttribute( "tabindex" ) > 0 )
				all[ i ].tabIndex += fedHead.tabIndex;
	}
}
fedHead.init();

