function gd(el){
	return document.getElementById(el);
}

function t(el){
	gd(el).style.display = gd(el).style.display != 'none'?'none':'';
}

function wopen(wdt,hgt,durl){
	var left = (screen.availWidth/2) - (wdt/2);
	var top = (screen.availHeight/2) - (hgt/2);
	var dwin = window.open(durl,'Insert','width='+wdt+',height='+hgt+', left='+left+', top='+top);
	dwin.focus();
}

function open_npo(npo_link){
	npo = window.open(npo_link,'newsmemory','left=0,top=0,height='+screen.availWidth+',width='+screen.availWidth+',resizable=yes');
	return false;
	/*npo.moveTo(0,0);
	npo.resizeTo(screen.availWidth,screen.availHeight);*/
}


function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function signs(num){
	switch(num) {
		case 0:
			return "ariete";
		case 1:
			return "toro";
		case 2:
			return "gemelli";
		case 3:
			return "cancro";
		case 4:
			return "leone";
		case 5:
			return "vergine";
		case 6:
			return "bilancia";
		case 7:
			return "scorpione";
		case 8:
			return "sagittario";
		case 9:
			return "capricorno";
		case 10:
			return "acquario";
		case 11:
			return "pesci";
		default:
			return false;
	}

}

function fpos(obj) {
	if (obj){
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
			return [curleft,curtop];
		}
	}
	return false;
}

function togglesize(objid, defsize, deflineheight){
	if (gd(objid)){
		var currentSize = gd(objid).style.fontSize.replace("px","");
		if (currentSize && currentSize != defsize){
			gd(objid).style.fontSize = defsize + 'px';
			gd(objid).style.lineHeight = deflineheight + 'px';
		} else {
			gd(objid).style.fontSize = defsize + 2 + 'px';
			gd(objid).style.lineHeight = deflineheight + 3 + 'px';
		}
		with (gd('tools_size').style) {
			if (backgroundColor != 'rgb(126, 122, 111)' && backgroundColor.toLowerCase() != '#7e7a6f'){
				backgroundColor = '#7E7A6F';
			} else {
				backgroundColor = '';
			}
		}
	}
}

function check_email(eml){
	return (eml.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1) ? true : false;
}

function send_to_friend(art_id){
	if (art_id != '' && gd('mail_address')){
		if(check_email(gd('mail_address').value)){
			xget('sendto=' + gd('mail_address').value + '&article=' + art_id , 'float_xbox_mail');
		} else {
			alert("Si prega di inserire un indirizzo email valido");
		}
	}
}

function print_content(content_id){
	if (content_id && gd('print_preview')){
		gd('print_preview').src = '/print.php?q=' + content_id + '';
	}
}

function tabshow(ident,tabid){
	if (gd(tabid) && ident){
		var divs = document.getElementsByTagName("div");
		for(i=0; i < divs.length; i++){
			if (divs[i].id.substr(0,ident.length) == ident){
				divs[i].style.display = 'none';
			}
		}
		gd(tabid).style.display = '';
	}
}

function get_aff(){

	if (gd('afm') && gd('aff_sign_1') && gd('aff_sign_2') && gd('aff_date') && gd('aff_percent')){
		i = gd('aff_sign_1').value+'.';
		i += gd('aff_sign_2').value+'.';
		i += gd('aff_date').value;
		gd('afm').style.display = 'block';
		gd('aff_percent').innerHTML = '<img style="margin-top:2px;" alt="" src="/images/loading.gif" />';
		aff_timer = setTimeout('xget(\'affinita='+i+'\',\'aff_percent\')',2000);
		return false;
	}
}

function get_asdf(){
	if (gd('asdf') && gd('zodiac_birth_day') && gd('zodiac_today')){
		i = gd('zodiac_birth_day').value+'.';
		i += gd('zodiac_today').value;
		gd('asdf').style.display = 'block';
		xget('asdf='+i,'asdf');	
		//gd('aff_percent').innerHTML = '<img style="margin-top:2px;" alt="" src="/images/loading.gif" />';
		//aff_timer = setTimeout('xget(\'affinita='+i+'\',\'aff_percent\')',2000);
		return false;
	}
}

function set_bgim(obj,imgpath){
	obj.style.backgroundImage = 'url('+imgpath+')';
}


show_menu = function(menu_id){
	opener_id = menu_id.substr(6,menu_id.length-6);
	if (gd(opener_id) && gd(menu_id)){
		gd(opener_id).style.backgroundColor = '#7E7A6F';
		gd(opener_id).style.color = '#FFFFFF';
		if (gd(menu_id).style.display != 'block'){
			with (gd(menu_id).style){
				zIndex = 99;
				left = fpos(gd(opener_id))[0] + 'px';
				top = (fpos(gd(opener_id))[1] + gd(opener_id).offsetHeight) + 'px';
			
				display = 'block';
				if (gd(menu_id).parentNode){
					el_right = fpos(gd(menu_id))[0] + gd(menu_id).offsetWidth;
					container_right = fpos(gd(menu_id).parentNode)[0] + gd(menu_id).parentNode.offsetWidth;
					if (el_right > container_right){
						left = (fpos(gd(opener_id))[0] - (gd(menu_id).offsetWidth - gd(opener_id).offsetWidth)) + 'px';
					}
				}
			}
		}
	}
}

hide_menu = function(menu_id){
	opener_id = menu_id.substr(6,menu_id.length-6);
	if (gd(menu_id) && gd(menu_id)){
		gd(menu_id).style.display = 'none';
		gd(opener_id).style.backgroundColor = '';
		gd(opener_id).style.color = '';
	}
}

function xget(x_params,target_id) {
	/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
	window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
	@end @*/
	
	if (x_params){
		var x_url = '/xrequest.php?'+x_params;	
		this.req = new XMLHttpRequest();
		req.open('GET', x_url, true);
		if (target_id && gd(target_id)){
			req.onreadystatechange = function (aEvt) {
				if (req.readyState == 4) {
					if(req.status == 200) {
						gd(target_id).innerHTML = req.responseText;
					}
				}
			}
		}
		req.send(null);
	}
}

function fbs_click(){
	u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function bookmarklink(url,btitle){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,btitle);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(btitle,url,"");
  } else {
    alert("per aggiungere questa pagina ai preferiti\npremere CTRL-D (Netscape) o CTRL-T (Opera)");
  }
}

function loadMainMenu(){
	
	// caricamento di tutti i menu a tendina presenti in pagina
	// ma non di quelli già elaborati
	
	var a_all = document.getElementsByTagName("a");
	for(var i=0; i < a_all.length; i++){
		if (a_all[i].id.substr(0,5) == 'menu_'){
			if (!a_all[i].onmouseover && !gd('float_'+a_all[i].id).onmouseover){
				a_all[i].onmouseover = function(evt){ show_menu('float_'+this.id) }	
				a_all[i].onmouseout = function(evt){ hide_menu('float_'+this.id) }	
				gd('float_'+a_all[i].id).onmouseover = function(evt){ show_menu(this.id) }	
				gd('float_'+a_all[i].id).onmouseout = function(evt){ hide_menu(this.id) }	
			}
		}
	}	
	
}

window.onload = function(){

	try {
  	document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	
	// caricamento dei menu
	loadMainMenu();
	
	var img_all = document.getElementsByTagName("img");
	for(var i=0; i < img_all.length; i++){
		if (img_all[i].id.substr(0,5) == 'xbox_'){
			if (gd('float_'+img_all[i].id)){
				img_all[i].onclick = function(evt){ 
					if (gd('float_'+this.id) && gd('float_'+this.id).style.display != 'block'){
						show_menu('float_'+this.id);
					} else {
						hide_menu('float_'+this.id);
					}
				}	
			}
		}
	}
	
	if (gd('gallery_thumbs') && gd('gallery_right') && gd('gallery_left') && gd('content_details_gallery')){
	
		var gx = gd('gallery_thumbs').offsetLeft;
		var gy = gd('gallery_thumbs').offsetTop;
		var gw = gd('gallery_thumbs').offsetWidth;
		var gv = 52;
		
		var imgs = gd('gallery_thumbs').getElementsByTagName('img');
		var imglen = imgs.length;

		maxleft = 0 - ((imglen*gv) - (6*gv));
		
		moveLeft = function(){
			if (gd('gallery_thumbs').offsetLeft<gx){
				gd('gallery_thumbs').style.left = gd('gallery_thumbs').offsetLeft+gv+"px";
				gd('gallery_thumbs').style.width = gd('gallery_thumbs').offsetWidth-gv+"px";
			}
		}
		
		moveRight = function() {
			if (gd('gallery_thumbs').offsetLeft > maxleft) {
				gd('gallery_thumbs').style.left = gd('gallery_thumbs').offsetLeft-gv+"px";
				gd('gallery_thumbs').style.width = gd('gallery_thumbs').offsetWidth+gv+"px";
			}
		}

		gd('gallery_left').onmouseout = function(evt) { this.src = '/images/arrow_left.gif'; return false; }		
		gd('gallery_left').onclick = function(evt) { moveLeft(); return false; }
		gd('gallery_left').ondblclick = function(evt) {	moveLeft();	return false;	}
		gd('gallery_left').onmouseover = function(evt) { this.src = '/images/arrow_left_hover.gif'; return false; }
		
		gd('gallery_right').onmouseout = function(evt) { this.src = '/images/arrow_right.gif'; return false; }
		gd('gallery_right').onclick = function(evt) {	moveRight(); return false; }
		gd('gallery_right').ondblclick = function(evt) { moveRight();	return false;	}
		gd('gallery_right').onmouseover = function(evt) { this.src = '/images/arrow_right_hover.gif'; return false; }	

	}

	
	if (gd('hover_box') && gd('image_full')){
		
		// Lore: 20090417 Anti trascinamento delle foto
		gd('image_full').onmousedown = function(evt){
			return false;
		}
		// Lore: 20090417 Anti tasto destro sulle foto
		gd('image_full').oncontextmenu = function(evt){
			return false;
		}
		
		gd('hover_box').onmousemove = function(evt){
			gd('image_full').onmousemove;
		}
		gd('hover_box').onmouseover = function(evt){
			gd('image_full').onmousemove;
		}
		gd('image_full').onmouseover = function(evt){
			gd('hover_box').style.display = 'block';
		}
		gd('image_full').onmouseout = function(evt){
			gd('hover_box').style.display = 'none';
		}
		gd('image_full').onmousemove = function(evt){
			
			gd('hover_box').style.display = 'block';
			var e = (evt) ? evt : window.event;
    	var scx = f_scrollLeft();
			var scy = f_scrollTop();
    	cx = (e.pageX) ? e.pageX : (e.clientX + scx);
    	cy = (e.pageY) ? e.pageY : (e.clientY + scy);	
    	
    	cx = cx + 12;
    	cy = cy + 12;
    	   	
    	if ((cx+12+gd('hover_box').offsetWidth) >= (gd('image_full').offsetWidth + fpos(gd('image_full'))[0])){
    		cx = (fpos(gd('image_full'))[0] + gd('image_full').offsetWidth) - gd('hover_box').offsetWidth - 10;
    	}
    	
    	gd('hover_box').style.left = cx + 'px';
    	gd('hover_box').style.top = cy + 'px';
    	
		}
	}
	
	if (gd('sign_1')){
		for (var i = 0; i < 12;i++){
			elsign = i+1;
			elsign = 'sign_'+elsign;
			if (gd(elsign)){
				//elname = ;
				gd(elsign).onmouseover = function (evt){
					if (this.style.color != 'rgb(208, 208, 208)' && this.style.color.toLowerCase() != '#d0d0d0'){
						set_bgim(this,'/images/zodiac/hover/'+this.title+'_hover.jpg');
						this.style.color='#FF8500';
					}
				}
				gd(elsign).onmouseout = function (evt){
					if (this.style.color != 'rgb(208, 208, 208)' && this.style.color.toLowerCase() != '#d0d0d0'){				
						set_bgim(this,'/images/zodiac/normal/'+this.title+'_normal.jpg');
						this.style.color='#808080';
					}
				}
				gd(elsign).onmousedown = function (evt){
					for (var q = 0; q < 12;q++){
						set_bgim(gd('sign_'+(q+1)),'/images/zodiac/normal/'+signs(q)+'_normal.jpg');
						gd('sign_'+(q+1)).style.color='#808080';
					}
					set_bgim(this,'/images/zodiac/selected/'+this.title+'_selected.jpg');
					this.style.color='#D0D0D0';
					//tabshow('zodiacfull_','zodiacfull_'+(i+1));
					return false;
				}
			}	
		}
	}
}