var s_uid, req;
function $(id) { return document.getElementById(id); }
function setOpacity(num,id) {
  $(id).style.opacity = (num / 100);
  $(id).style.MozOpacity = (num / 100);
  $(id).style.KhtmlOpacity = (num / 100);
  $(id).style.filter = "alpha(opacity=" + num + ")";
}
function openWin(id,name,code) {
  setOpacity(55,'bg');
  setSize(); $('bg').style.display='';
  setPos('box1'); 

  var url='http://www.youtube.com/v/'+code; var vars=''; 
  if(!code) { url='/player.swf'; vars='file=rtmp://sharetv.org/oflaDemo&id='+id+'&displayheight=480'; }
  $('cname').innerHTML='Watching '+name;
  $('votebox').src="/votebox.php?name=_"+id;
  if($('myshow')) { $('myshow').href="/shows/"+id; $('myshow').innerHTML="Download "+name; }

  
  $('video_clip').innerHTML='<embed src="'+url+'" width="425" height="350" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"  allowfullscreen="true" flashvars="'+vars+'" />';


  $('box1').style.display='';
}
function closeWin() {
  $('video_clip').innerHTML='';
  $('box1').style.display='none';
  $('bg').style.display='none';
}
function scTop() {
  var of1 = window.pageYOffset ? window.pageYOffset : 0;
  var of2 = document.documentElement ? document.documentElement.scrollTop : 0;
  var of3 = document.body ? document.body.scrollTop : 0;
  return Math.max(of1,of2,of3);
}
function setPos(bx) {
  var de = document.documentElement;
  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  var b_wd=650; var b_ht=400;
  var x=Math.round((w-b_wd)/2);
  var y=Math.round((h-b_ht)/2);
  $(bx).style['left']=x+'px';
  $(bx).style['top']=(y+scTop()-10)+'px';
}
function setSize() {
  if(window.innerHeight && window.scrollMaxY) ht = window.innerHeight + window.scrollMaxY;
  else if(document.body.scrollHeight > document.body.offsetHeight) ht = document.body.scrollHeight;
  else ht = document.body.offsetHeight;

  if(ht) $('bg').style['height'] = ht +"px";
  else $('bg').style['height']='100%';
}

function sendVote(id,uid,num) {
  s_uid=uid; if(!uid) { alert("ERROR: You must login to vote"); return; }

  $('voting').style.display='none';
  $('loader').style.display='';
  cancel(); ajaxSend('type=6&id='+id+'&vote='+num,3);
}
function showVote(dat) {
  var d=dat.split('|');
  if(d[2]!="done") { alert("ERROR: unknown error placing vote"); return; }
  var love=0, hate=0;
  if(d[0]>0) hate=d[0];
  if(d[1]>0) love=d[1];
  $('hate').innerHTML=hate;
  $('love').innerHTML=love;
  $('loader').style.display='none';
  $('voting').style.display='';
}

function addShow(id,uid) {
  s_uid=uid; if(!uid) { alert("ERROR: You must login to add a show"); return; }

  $('fav').innerHTML='loading ...';
  cancel(); ajaxSend('type=5&id='+id,4);
}

function addCeleb(celeb,uid) {
  s_uid=uid;
  $('char_load').innerHTML="loading ...";
  ajaxSend('type=13&celeb='+celeb,7);
}

function showCeleb(dat) {
  if(dat!="done") { alert('ERROR: unable to reach server'); $('char_load').innerHTML="<b style='color:red'>ERROR</b>"; }
  else $('char_load').innerHTML='<a href="/user/'+s_uid+'">[View My Celebs]</a>';
}

function showFav(dat) {
  if(dat=="done" && s_uid) $('fav').innerHTML='<a href="/user/'+s_uid+'" target="_parent">[View My Shows]</a>';
  else alert("ERROR: Unable to reach server");
}

function cVote(rev,uid,num) {
  s_uid=uid; if(!uid) { alert("ERROR: You must login to vote"); return; }

  var obj=$('cnt_'+rev);
  obj.innerHTML="...";
  obj.style.color="#999";
  cancel(); ajaxSend('type=9&rev='+rev+'&vote='+num,5);
}
function showRev(dat) {
  var d=dat.split('|'); var co="#999";
  if(d[2]!="done") { alert("ERROR: unknown error placing vote"); return; }
 
  if(d[0]<0) co="red"; 
  else if(d[0]>0) { co="green"; d[0]="+"+d[0]; }
  else d[0]="+"+d[0];
  var obj=$('cnt_'+d[1]);

  obj.innerHTML=d[0];
  obj.style.color=co;
}


function addVid(cid,uid) {
  s_uid=uid;
  $('char_load').innerHTML="loading ...";
  ajaxSend('type=11&cid='+cid,6);
}
function showVid(dat) {
  if(dat!="done") { alert('ERROR: unable to reach server'); $('char_load').innerHTML="<b style='color:red'>ERROR</b>"; }
  else $('char_load').innerHTML='<a href="/user/'+s_uid+'">[View My Vids]</a>';
}
function addChar(show,celeb,uid) {
  s_uid=uid;
  $('char_load').innerHTML="loading ...";
  ajaxSend('type=2&id='+show+'&celeb='+celeb,1);
}
function showRes(dat) {
  if(dat!="done") { alert('ERROR: unable to reach server'); $('char_load').innerHTML="<b style='color:red'>ERROR</b>"; }
  else $('char_load').innerHTML='<a href="/user/'+s_uid+'">[View My Characters]</a>';
}

function sendComm(obj) {
  var data=obj.data.value;
  var id=obj.id.value;
  if(!data) { alert('oops, you forgot your comment'); return; }
  if(!id) { alert('unkown error, contact admin'); return; }

  $('sub_comm').innerHTML="loading ...";
  ajaxSend('type=4&db=shows_com&id='+id+'&data='+escape(data),2);
}
function showComm(dat) {
  if(dat=="e_spammer") alert('ERROR: Your Account is currently blocked from posting show reviews or in the forum');
  else if(dat!="done") alert('ERROR: unable to reach server');
  else { 
    var data=$('data').value;
    var id=$('userid').value;
    $('data').value='';
    var r=$('ctbl').insertRow(0); 
    var tmp=r.insertCell(0);
    tmp.style.padding='10px';
    tmp.style.width='130px';
    tmp.innerHTML='<a href="/user/'+id+'"  class="user" style="text-decoration:none"><img src="/images/'+id+'-user.jpg" border=0 width=48 height=48><br><b>'+id+'</b></a><br>just now<br>';
    r.insertCell(1).innerHTML=data.replace(/\n/g,"<br>");
    r.insertCell(2).innerHTML="<b style='font-size:11px;font-family:verdana;color:#999'>+0</b>";
  }

  $('sub_comm').innerHTML='<input type="submit" name="button" value="Add Comment">';
  location.hash = 'comments';
}
function cancel() {
  if(req) { 
    req.onreadystatechange = function () {} 
    req.abort(); 
  }
}
function ajaxSend(vars,func) {
  function ajaxParse() {
      if(req.readyState==4 && req.status==200)  {
        var data=req.responseText; 
  	if(func==1) showRes(data); //favorite character
  	else if(func==2) showComm(data); //comment/review
  	else if(func==3) showVote(data); //vote
  	else if(func==4) showFav(data); //favorite show
  	else if(func==5) showRev(data); //review vote
  	else if(func==6) showVid(data); //favorite vid
  	else if(func==7) showCeleb(data); //favorite celeb
      }
  }
  
  req=null;
  if(window.XMLHttpRequest) req = new XMLHttpRequest();
  else if(window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
  else return false;
  
  req.open("POST","/include/serv.php",true);
  req.onreadystatechange = ajaxParse;
  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  req.send(vars);
}
