if ( typeof(Libero) == "undefined" ) {
	Libero = {
		Version: '1.0.3',
		Browser: (function(){
			var ua = navigator.userAgent;
			var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';
			return {
				IE:             !!window.attachEvent && !isOpera,
				Opera:          isOpera,
				WebKit:         ua.indexOf('AppleWebKit/') > -1,
				Gecko:          ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1,
				MobileSafari:   /Apple.*Mobile/.test(ua)
			}
			})()
    };
}

Libero.Stash = {};

// test prototype library
if ( typeof(Prototype) == "undefined" ) {
	var Class = {
		create: function() {
			return function() {
				this.initialize.apply(this, arguments);
			}
		}
	};
	Object.extend = function(destination, source) {
		for (var property in source) {
			destination[property] = source[property];
  		}
		return destination;
	};
}

// Libero.HF: header footer class utilities  
Libero.HF = Class.create();

Libero.HF.show = function (id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		if (el) {
			el.style.display = 'block';
		}
	}
}

Libero.HF.hide = function (id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		if (el) {
			el.style.display = 'none';
		}
	}
}

Libero.HF.toggle = function(id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		if (el) {
			if (el.style.display == 'block') {
				el.style.display = 'none';
			}
			else {
				el.style.display = 'block';
			}
		}
	}
}

Libero.HF.ariannaSearch = function (url, empty_url, cksrv_a, cksrv_b, cksrv_c, q, encode, target) {
	encode = encode == undefined ? 1 : encode;
	target = target == undefined ? null : target;
	// empty query
	if ( q.match(/^\s*$/) ) {
		if ( empty_url != undefined ) {
			ckSrv(empty_url,cksrv_a,cksrv_b,cksrv_c,target);
			return false; 		
		}
		else 
			return false;
	}
	else {
		ckSrv(url + ( encode ? encodeURIComponent(q) : q ),cksrv_a,cksrv_b,cksrv_c,target);
		return false; 
	}
}

// Adv
function adv_flashDisp (flashobj) {
	if (typeof(flashobj)!='undefined'&&flashobj!='') {
		document.write(flashobj);
	}
}

Libero.HF.setCookie = function (name, value, date, path, domain, secure) {
	var expires = 0;

	// expires is a Date object
	if (date.constructor.toString().match(/function\s+Date\(/)) {
		expires = date;
	}
	// expires is a anonymous Object
	else if (date.constructor.toString().match(/function\s+Object\(/)) {
		date = Object.extend({ day: 0, hour: 0, min: 0, sec: 0 }, date);
		if (date.day == 0 && date.hour == 0 && date.min == 0 && date.sec == 0) {
			expires = 0;
		}
		else {
			expires = new Date();
			Libero.HF.fixDate(expires);
			expires.setTime(expires.getTime() + (((date.day * 24 + date.hour) * 60 + date.min) * 60 + date.sec) * 1000);
		}
	}

	// set cookie
	var curCookie = name + "=" + escape(value) 
		+ ((expires) ? "; expires=" + expires.toGMTString() : "") 
		+ ((path) ? "; path=" + path : "") 
		+ ((domain) ? "; domain=" + domain : "") 
		+ ((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

Libero.HF.getCookie = function (name) {
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1) {
		return null;
	}
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1) {
		cookieEndIndex = document.cookie.length;
	}
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length,cookieEndIndex));
}

Libero.HF.deleteCookie = function (name, path, domain) {
	if (Libero.HF.getCookie(name)) {
		Libero.HF.setCookie(name, '', new Date(0), path, domain);
	}
}

Libero.HF.fixDate = function (date) {
	var base = new Date(0)
	var skew = base.getTime()
	if (skew > 0) {
		date.setTime(date.getTime() - skew);
	}
}

Libero.HF.extractSummary = function () {
	var args = Object.extend({
		elem: 'lh_email',
		max_length: 13,
		min_width: 900
	}, arguments[0]);

	if ( document.getElementById(args.elem).title.length > args.max_length ) { 
		if ( window.document.body.clientWidth < args.min_width ) {
			document.getElementById(args.elem).innerHTML = document.getElementById(args.elem).title.substring(0,args.max_length - 1) + '...';
		} 
		else {
			document.getElementById(args.elem).innerHTML = document.getElementById(args.elem).title 
		}
	}
}

Libero.HF.adjustInputLabel = function () {
	var args = Object.extend({
		elem_src: 'top-query',
		elem_dest_1: 'lh_comment_col-1',
		elem_dest_2: 'lh_comment_col-2',
		offset: 20
		//,min_width: 280
	}, arguments[0]);

	if (document.getElementById) {
		var el = document.getElementById(args.elem_src);
		if ( el.clientWidth > args.min_width || args.min_width == undefined ) {
			document.getElementById(args.elem_dest_1).style.width = el.clientWidth + args.offset + 'px';
		}
	}
	Libero.HF.show(args.elem_dest_2);
}

Libero.HF.addLoadEvent = function (func) {
	var oldonload = window.onload;
	if ( typeof window.onload != 'function' ) {
    	window.onload = func;
	}
	else {
		window.onload = function() {
		oldonload();
		func();
		}
	}
}

Libero.HF.currentURL = function (host_only) {
	return window.location.protocol + '//' + location.host + ( host_only ? '' : (location.pathname + location.search) ); 
}

function write_HF_login (service_id) {
	var def_conf = {
		aiuto: {
			ret_url_raise: 1,
			url_member: 'http://aiuto.libero.it',
			cksrv_b: 'aiuto'
		},
		oroscopo: {
			cksrv_b: 'oroscopo',
			anagrafica: 1
		},
		meteo: {
			anagrafica: 1,
			cksrv_b: 'meteo'
		},
		search: {
			cksrv_b: 'search'
		}
	};

	var HF_login_el_l = document.getElementById('HF_login_l');
	var HF_login_el_r = document.getElementById('HF_login_r');

	var service_conf = {};
	if ( Libero.Stash.write_HF_login != undefined && typeof(Libero.Stash.write_HF_login) == 'object' ) {
		service_conf = Libero.Stash.write_HF_login;
	}
	else if ( def_conf[service_id] != undefined ) {
		service_conf = def_conf[service_id];
	}

	if (HF_login_ar[0]) {
		HF_login_el_r.innerHTML = '<a href="https://login.libero.it/logout.php?service_id=' + service_id + 
			'&ret_url=' + escape( service_conf.ret_url ? service_conf.ret_url : Libero.HF.currentURL(service_conf.ret_url_raise) ) + '" class="lh_br">Esci</a>';
		HF_login_el_l.innerHTML	= ( (service_conf.url_member || service_conf.anagrafica) ? ( '<a href="http://selfcare.libero.it/anagrafica/?url_member=' + escape(service_conf.url_member ? service_conf.url_member : Libero.HF.currentURL(1) ) + '" onclick="return ckSrv(this,\'' + Libero.Stash.HF.cksrv_a + '\',\'' + service_conf.cksrv_b + '\',\'anagrafica\',null)">' + HF_login_ar[1] + '</a>' )
			: ('<div class="lh_onebox">' + HF_login_ar[1] + '</div>') )
	}
	else {
		HF_login_el_r.innerHTML = '<a href="https://login.libero.it?service_id=' + service_id + '&ret_url=' + escape(Libero.HF.currentURL()) + '" onclick="return ckSrv(this,\'' + Libero.Stash.HF.cksrv_a + '\',\'' + service_conf.cksrv_b + '\',\'accedi\',null)" class="lh_br">Accedi</a><a href="http://registrazione.libero.it/" onclick="return ckSrv(this,\'' + Libero.Stash.HF.cksrv_a + '\',\'' + service_conf.cksrv_b + '\',\'registrati\',null)">Registrati</a>';
	}
}

Libero.HF.autoSuggestion = function() {
	var args = Object.extend({
		as_on_id: 'libero_auto_suggest_on',
		as_off_id: 'libero_auto_suggest_off',
		click: false,
		cookie_name: 'auto_suggest',
		close: false,
		popup_id: 'lh_options',
		cksrv: undefined
	}, arguments[0]);
	
	var rv; 
	
	if ( Libero.HF.getCookie(args.cookie_name) == 'false' ) {
		if ( args.click ) {
			Libero.HF.setCookie(args.cookie_name, true, { day: 365 * 20	}, '/', '.libero.it');
			if ( args.cksrv != undefined && args.cksrv.constructor.toString().match(/function\s+Object\(/) ) {
				ckSrv(document.location.href, args.cksrv.a, args.cksrv.b, args.cksrv.c,'_ajax');
			}
		}			
		rv = 0;
	}
	else {
		if ( args.click ) {
			Libero.HF.setCookie(args.cookie_name, false, { day: 365 * 20 }, '/', '.libero.it');
			if ( args.cksrv != undefined && args.cksrv.constructor.toString().match(/function\s+Object\(/) ) {
				ckSrv(document.location.href, args.cksrv.a, args.cksrv.b, args.cksrv.c,'_ajax');
			}
		}
		rv = 1;
	}
	
	// Arrange div status
	if ( Libero.HF.getCookie(args.cookie_name) == 'false' ) {
		Libero.HF.show(args.as_on_id);
		Libero.HF.hide(args.as_off_id);
	}
	else {
		Libero.HF.show(args.as_off_id);
		Libero.HF.hide(args.as_on_id);
	}
	 
	if ( args.close ) {
		Libero.HF.hide(args.popup_id);
	}
	
	return rv;
}

Libero.HF.changeTheme = function(){
	var args = Object.extend({
		theme: 'blue',
		theme_id: 'hf_theme',
		base_uri: 'themes/',
		save: true,
		cookie_name: 'prefs',
		current_theme: Libero.HF.getCookie('prefs'),
		set_active: true
	}, arguments[0]);
	
	// remove skin
	var skin_el = document.getElementById(args.theme_id);
	if (skin_el != undefined) {
		skin_el.parentNode.removeChild(skin_el);
	}

	// Restore default	
	if (args.theme == 'default') {
		Libero.HF.setCookie(args.cookie_name, args.theme, { day: 365 }, '/', '.libero.it');
		return;
	}

	// Add theme
	Libero.HF.makeGetPostRequest({ 
		url: args.base_uri + args.theme + '/theme1024.css',
		status_200: function (text) { 
			Libero.HF.addStyle(text.replace(/url\(img/g, 'url(' + args.base_uri + args.theme + '/img'),args.theme_id)
			if ( args.save ) {
				Libero.HF.setCookie(args.cookie_name, args.theme, { day: 365 }, '/', '.libero.it');
			}
			// Set active style
			if ( args.set_active ) {
				document.getElementById('lh_theme_' + args.current_theme).className = 'lh_theme_inactive';
				document.getElementById('lh_theme_' + args.theme).className = 'lh_theme_active';
			}
		}
	});

}

Libero.HF.addStyle = function (text, theme_id) {
	var styleObj = document.createElement('STYLE');
	styleObj.setAttribute('type', 'text/css');
	styleObj.setAttribute('id', theme_id);
	if ( Libero.Browser.IE ) 
		styleObj.styleSheet.cssText = text;
	else 
		styleObj.appendChild(document.createTextNode(text));	
	var headObj = document.childNodes[1].childNodes[0];
	headObj.appendChild(styleObj);
}

Libero.HF.makeGetPostRequest = function () {
	var args = Object.extend({
		url: undefined,
		method: 'get',
		status_200: undefined
	}, arguments[0]);
	
	var xhr;
	if ( window.XMLHttpRequest ) { // Mozilla, Safari,...
		xhr = new XMLHttpRequest();
		if ( xhr.overrideMimeType ) {
		// set type accordingly to anticipated content type
		// http_request.overrideMimeType('text/xml');
			xhr.overrideMimeType('text/css');
		}
	} 
	else if ( window.ActiveXObject ) { // IE
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if ( ! xhr ) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	xhr.onreadystatechange = function () { 
		// controllo risposte
		if ( xhr.readyState == 1 ) {
			// alert(http_request.readyState);
		}
		else if ( xhr.readyState == 2 ) {
		}
		else if ( xhr.readyState == 3 ) {
			// alert(http_request.readyState);
		}	
		else if ( xhr.readyState == 4 ) {
			// alert(http_request.readyState);
			// alert(http_request.status);
			if ( xhr.status == 200 ) {
				args.status_200(xhr.responseText);
			}
			else {
				// alert('Non possibile effettuare la richiesta.');
			}
		}
	};
	//xhr.onreadystatechange = function() { alertContents(xhr) };

	xhr.open(args.method, args.url, true);
//	http_request_array[numeroRequest].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//	http_request_array[numeroRequest].setRequestHeader("Content-length", parameters.length);
	xhr.setRequestHeader("Connection", "close");
	xhr.send();
}
