// IMPLEMENTAZIONE DI Array.indexOf
if( ! Array.indexOf ) {
	Array.prototype.indexOf = function( obj ) {
		for( var i = 0; i < this.length; i++ )
			if( this[ 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;
	}
}

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 : [
						{
							id : "fedHead_caffe",
							type : "link",
							href : "http://qn.quotidiano.net/caffe",
							text : "il caff√®",
							rel : "nofollow"
						},
						{
							id : "fedHead_motori",
							type : "link",
							href : "http://qn.quotidiano.net/motori",
							text : "QN Motori",
							rel : "nofollow"
						},
						{
							id : "fedHead_cavallo",
							type : "link",
							href : "http://www.cavallomagazine.it",
							text : "Cavallo Magazine",
							rel : "nofollow"
						},
						{
							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"
						},
						{
							id : "fedHead_homeit",
							type : "link",
							href : "http://home.it",
							text : "Home.it",
							rel : "nofollow"
						},
						{
							id : "fedHead_pronto",
							type : "link",
							href : "http://www.prontoimprese.it",
							text : "ProntoImprese",
							rel : "nofollow"
						},
						{
							id : "fedHead_motorionline",
							type : "link",
							href : "http://www.motorionline.com",
							text : "MotoriOnline",
							rel : "nofollow"
						},
						{
							id : "fedHead_informacalcio",
							type : "link",
							href : "http://informacalcio.quotidiano.net",
							text : "informacalcio.it",
							rel : "nofollow"
						}
					]
				}
			},
			{
				id : "fedHead_search",
				type : "menu",
				href : "http://ricerca.quotidiano.net",
				text : "Cerca",
				menu : {
					id : "fedHead_searchbox",
					items : [
						{
							id : "fedHead_searchsite",
							type : "form",
							method : "get",
							action : "http://ricerca.quotidiano.net/mnricerca.php",
							fields : [
								{
									tagName : "input",
									type : "text",
									name : "query",
									value : "Cerca nel sito"
								}
							]
						}
					]
				}
			}/*,
			{
				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 = "/fedHead/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 = 36;
		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 item = ul.items[ i ];
			var li = document.createElement( "li" );
			li.id = ul.items[ i ].id;
			if( i + 1 == ul.items.length )
				li.className = "fedHead_last";
			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;
				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;
				default :
					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 = menu.over.indexOf( source );
		if( ! out && i < 0 ) menu.over.push( source );
		else if( out && i >= 0 ) menu.over.splice( i, 1 );
		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();

