if ( typeof(Libero) == "undefined" ) {
	Libero = {
		Version: '1.0.2'
    };
}

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;
}

