/*STRIPS*/

function loadstrip(url,imgn){
	var img=$(imgn);
		if(img==null) {
			setTimeout("loadstrip('"+url+"','"+imgn+"')",500);	
		} else {
			img.src=url;
		}
}

function showstrip(imgn, spinimg){
	var img=getthediv(imgn);
	var spin=getthediv(spinimg);

	if (spin==null||img==null) {
		setTimeout("showstrip('"+imgn+"','"+spinimg+"')",500);
		return;		
	}
	spin.display='none';
	img.display='block';
}

/*TAGS*/
function handletags(num,max,div1,div2,param){
	var i;
	var lnk = $(div2+num);
	var st = lnk.className;
	
	for(i=0;i<max;i++){
		var alnk=$(div2+i);
		alnk.className='clink';
		}
	changediv(div1,"<div><img src=\"images/ajax-loader-mini.gif\" style=\"border: 0px;margin-top:"+($('thecloud').offsetHeight/2)+"px;\"/></div>");
	lnk.className='tlink';
	grabFile("tags.php?param="+param,div1);	
	toggleLayer(div1,1);
}


function showtag(num,max,tag){
	handletags(num,max,"tagresult","tagclink",tag);
	toggleLayer('thetags',1);
}

function showtag2(num,max,tag){
	handletags(num,max,"tagresult","tagclink",tag);
	toggleLayer('thetags',1);
}


/*STATS*/
function encode64(inp){
var key="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var chr1,chr2,chr3,enc3,enc4,i=0,out="";
while(i<inp.length){
chr1=inp.charCodeAt(i++);if(chr1>127) chr1=88;
chr2=inp.charCodeAt(i++);if(chr2>127) chr2=88;
chr3=inp.charCodeAt(i++);if(chr3>127) chr3=88;
if(isNaN(chr3)) {enc4=64;chr3=0;} else enc4=chr3&63;
if(isNaN(chr2)) {enc3=64;chr2=0;} else enc3=((chr2<<2)|(chr3>>6))&63;
out+=key.charAt((chr1>>2)&63)+key.charAt(((chr1<<4)|(chr2>>4))&63)+key.charAt(enc3)+key.charAt(enc4);
}
return encodeURIComponent(out);
}

function stats(our_url){
   var referer=encode64(document.referrer);
   var thispage=encode64(our_url);
   var date=new Date();
   var time=date.getTime();
   var resolution= screen.width + "x" + screen.height;
   var sid=39411;
   document.writeln("<img src=\"http://www.atrabile.net/phptraffic/count.php?sid="+sid+"&p="+thispage+"&r="+referer+"&t="+time+"&res="+resolution+"\" alt=\"\" border=\"0\" />\n");
  }



/*PAGE*/
function menubar(thediv){
	var st=isvisible(thediv);
	toggleLayer('about',-1);
	toggleLayer('arch',-1);
	if(!st) toggleLayer(thediv,1);
}

function waitdiv(thediv){
	changediv(thediv,"<div><img src=\"images/ajax-loader-mini.gif\" style=\"border: 0px\"/></div>");
}

function changediv(thediv,cnt){$(thediv).innerHTML=cnt;}

function getthediv(thediv){//get the style
  var elem, vis;
  elem=$(thediv);
  vis = elem.style;
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  return vis;
}

function isvisible(thediv){return (getthediv(thediv).display!='none');}

function toggleLayer( thediv, flag )
{
	if(flag==0&&isvisible(thediv)) flag=-1;
	else if(flag==0) flag=1;
		
	if ((thediv=="about"||thediv=="arch")){
		var url= (thediv=="about") ? ("info.php?pid="+pid):("archive.php?pid="+pid);
		//var li=thediv+"_li";
		if(flag==-1) {getthediv(thediv).display='none'; /*if($(li)) $(li).setStyle('color','green');*/return;}
		if(flag==1) {/*if($(li)) $(li).setStyle('color','blue');*/waitdiv(thediv);getthediv(thediv).display='block';grabFile(url,thediv);}
		return;
	}
	if (flag==-1) getthediv(thediv).display='none';
	if (flag== 1) getthediv(thediv).display='block';
}

function grabFile(file,thediv) {
var req = new Request.HTML({
	url: file,
	method: 'get',
	evalScripts: true,
	update: $(thediv),
	onFailure: function(responseText, responseXML) {
		alert("Ajax Failed requesting:"+file);
	}
	});
	req.get();
}


function grabFilePost(file,params,thediv) {
var req = new Request.HTML({
	url: file,
	method: 'post',
	update: $(thediv),
	onFailure: function(responseText, responseXML) {
		alert("Ajax Failed requesting:"+file);
	}
	});

	req.send(params);
}

/*POST COMMENT*/

function postcomment(){

var the_name=$("postname").value;
var the_locn=$("postlocn").value;
var the_cmnt=$("postcmnts").value;
var the_page=$("pageid").value;
var the_url=$("returl").value;
var the_mail=$("postmail").value;

var params="name="+escape(encodeURI(the_name))+"&locn="+escape(encodeURI(the_locn))+"&page_id="+escape(encodeURI(the_page))+"&ret_url="+escape(encodeURI(the_url))+"&comments="+escape(encodeURI(the_cmnt))+"&usermail="+escape(encodeURI(the_mail));
grabFilePost("comments/comments_process.php",params,"wrapcmn");

}

/*POST MAIL*/

function postmail(){

var the_name=$("the_mail").value;
var the_page=$("pageid").value;
var the_url=$("returl").value;

var params="address="+escape(encodeURI(the_name))+"&page_id="+escape(encodeURI(the_page))+"&ret_url="+escape(encodeURI(the_url));
grabFilePost("simple/process.php",params,"wrapcmn");
}


function getTag(name,num,lng){
grabFile("get_tag.php?tagname="+name+"&tagfrom="+num+"&lng="+lng,"the_bors");	
}
function getArchive(show){
grabFile("archive.php?pid="+pid+"&show="+show,"arch");	
}


function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

function scrollme(){window.scrollTo(0,9999999);}

function eq(div1,div2){
		var ay,by;
		ay=by=0;
		ay=$(div1).offsetHeight;
		by=$(div2).offsetHeight-4;
  		//alert("eq: "+div1+":"+ay+" "+div2+":"+by);
		if(ay<by) $(div1).setStyle('min-height',by+"px");
		if(ay<by) $(div1).setStyle('height',by+"px");
}


function initialize_rating(votes, avg, votable){
	var raters=new Array('rater1','rater2','rater3','rater4','rater5');

	if(votable){
	$(raters[0]).addEvents({mouseenter: function(){ draw_rating(1,votes,avg); for(k=1;k<=1;k++) $(raters[k-1]).src= "images/star_on.png";}});
	$(raters[1]).addEvents({mouseenter: function(){ draw_rating(2,votes,avg); for(k=1;k<=2;k++) $(raters[k-1]).src= "images/star_on.png";}});
	$(raters[2]).addEvents({mouseenter: function(){ draw_rating(3,votes,avg); for(k=1;k<=3;k++) $(raters[k-1]).src= "images/star_on.png";}});
	$(raters[3]).addEvents({mouseenter: function(){ draw_rating(4,votes,avg); for(k=1;k<=4;k++) $(raters[k-1]).src= "images/star_on.png";}});
	$(raters[4]).addEvents({mouseenter: function(){ draw_rating(5,votes,avg); for(k=1;k<=5;k++) $(raters[k-1]).src= "images/star_on.png";}});
	$(raters[0]).addEvents({mouseleave: function (){draw_rating(0,votes,avg);}});
	$(raters[1]).addEvents({mouseleave: function (){draw_rating(0,votes,avg);}});
	$(raters[2]).addEvents({mouseleave: function (){draw_rating(0,votes,avg);}});
	$(raters[3]).addEvents({mouseleave: function (){draw_rating(0,votes,avg);}});
	$(raters[4]).addEvents({mouseleave: function (){draw_rating(0,votes,avg);}});
	} else {
	$(raters[0]).addEvents({mouseenter: function(){draw_rating(0,votes,avg);}});
	$(raters[1]).addEvents({mouseenter: function(){draw_rating(0,votes,avg);}});
	$(raters[2]).addEvents({mouseenter: function(){draw_rating(0,votes,avg);}});
	$(raters[3]).addEvents({mouseenter: function(){draw_rating(0,votes,avg);}});
	$(raters[4]).addEvents({mouseenter: function(){draw_rating(0,votes,avg);}});
	}
	
	draw_rating(0,votes,avg);
}

function draw_rating_msg(id,votes,avg){

	txt = new Array();

	if(lang=="it") {
		txt[0]=''+votes+' vot'+( (votes==1) ? ('o'):('i') )+' | '+avg;
		txt[1]='oribbile';
		txt[2]='passabile';
		txt[3]='ahahah';
		txt[4]='geniale';
		txt[5]='fammi tua';
	} else {
		txt[0]=''+votes+' vote'+( (votes==1) ? (''):('s') )+' | '+avg
		txt[1]='terrible';
		txt[2]='so and so';
		txt[3]='ahahah';
		txt[4]='genius!';
		txt[5]="let's have sex";
	}

	changediv('rate_text',txt[id]);

}

function draw_rating(id,votes,avg){
	var star;
	
	for(i=1;i<=5;i++) {
		star='rater'+i;
		if(i<=avg) $(star).src="images/star_full.png";
		else if((avg-i)<0&&(avg-i)>-1) $(star).src="images/star_half.png";
		else $(star).src="images/star_off.png";
	}
	draw_rating_msg(id,votes,avg);
}

function rateit(id,vote){
grabFile("rating.php?pid="+pid+"&vote="+vote,"rate_panel");	
}


