/*
http://isohunt.com Interface Javascript
by Gary Fung - email: gary{REPLACE_WITH_THE_AT_SIGN}isohunt.com

Feel free to use / mod this to your heart's content,
but keep these lines to acknowledge where this code originated.
Comments, mods or additions you'd like add to this script can be posted here:
http://isohunt.com/forum/viewforum.php?f=1

Tip popup functions adapted from AWStats: http://awstats.sourceforge.net/
*/
var smooth_timer;
function go2(selID, link) {
  var selOBJ = (document.getElementById) ? document.getElementById(selID) : eval("document.all['" + selID + "']");
  window.location=link + selOBJ.options[selOBJ.selectedIndex].value;
}
function toclip(string) { window.clipboardData.setData('Text', string); }
function ShowTip(fArg) {
  var tooltipOBJ = (document.getElementById) ? document.getElementById('ih' + fArg) : eval("document.all['ih" + fArg + "']");
  if (tooltipOBJ != null) {
    var tooltipLft = (document.body.offsetWidth?document.body.offsetWidth:document.body.style.pixelWidth) - (tooltipOBJ.offsetWidth?tooltipOBJ.offsetWidth:(tooltipOBJ.style.pixelWidth?tooltipOBJ.style.pixelWidth:$TOOLTIPWIDTH)) - 5;    var tooltipTop = 10;

    if (navigator.appName == 'Netscape') {
      if (parseFloat(navigator.appVersion) >= 5) tooltipTop = (document.body.scrollTop>=0?document.body.scrollTop+10:event.clientY+10);
      tooltipOBJ.style.left = tooltipLft; tooltipOBJ.style.top = tooltipTop;
    }
    else {
      tooltipLft -= 30;
      tooltipTop = (document.body.scrollTop?document.body.scrollTop:document.body.offsetTop) + event.clientY - (tooltipOBJ.scrollHeight?tooltipOBJ.scrollHeight:tooltipOBJ.style.pixelHeight) - 30;
      if (tooltipTop < (document.body.scrollTop?document.body.scrollTop:document.body.offsetTop) + 10) {
        if (event.clientX > tooltipLft) tooltipTop = (document.body.scrollTop?document.body.scrollTop:document.body.offsetTop) + event.clientY + 30;
        else tooltipTop = (document.body.scrollTop?document.body.scrollTop:document.body.offsetTop) + 10;
      } 
      tooltipOBJ.style.pixelLeft = tooltipLft; tooltipOBJ.style.pixelTop = tooltipTop;
    }
    tooltipOBJ.style.visibility = "visible";
  }
}
function HideTip(fArg) {
  var tooltipOBJ = (document.getElementById) ? document.getElementById('ih' + fArg) : eval("document.all['ih" + fArg + "']");
  if (tooltipOBJ != null) tooltipOBJ.style.visibility = "hidden";
}
function smoothHeight(id, curH, targetH, stepH, mode) {
  diff = targetH - curH;
  if (diff != 0) {
    newH = (diff > 0) ? curH + stepH : curH - stepH;
    ((document.getElementById) ? document.getElementById(id) : eval("document.all['" + id + "']")).style.height = newH + "px";
    if (smooth_timer) window.clearTimeout(smooth_timer);
    smooth_timer = window.setTimeout( "smoothHeight('" + id + "'," + newH + "," + targetH + "," + stepH + ",'" + mode + "')", 36);
  }
  else if (mode != "o") ((document.getElementById) ? document.getElementById(mode) : eval("document.all['" + mode + "']")).style.display="none";
}
function rowOver(i, nColor) {
  if (!nColor) nColor = "#ECECD9";
  var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
  if (nameObj != null) nameObj.style.background=nColor;
}

function getBrowser(){
var BrowserDetect = {
  init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
            || this.searchVersion(navigator.appVersion)
            || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
  },
  searchString: function (data) {
    for (var i=0;i<data.length;i++) {
      var dataString = data[i].string;
      var dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].identity;
      if (dataString) {
        if (dataString.indexOf(data[i].subString) != -1)
          return data[i].identity;
      }
      else if (dataProp)
      return data[i].identity;
    }
  },
  searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  },
  dataBrowser: [
          {       string: navigator.userAgent,
                  subString: "OmniWeb",
                  versionSearch: "OmniWeb/",
                  identity: "OmniWeb"
          },
          {
                  string: navigator.vendor,
                  subString: "Apple",
                  identity: "Safari"
          },
          {
                  prop: window.opera,
                  identity: "Opera"
          },
          {
                  string: navigator.vendor,
                  subString: "iCab",
                  identity: "iCab"
          },
          {
                  string: navigator.vendor,
                  subString: "KDE",
                  identity: "Konqueror"
          },
          {
                  string: navigator.userAgent,
                  subString: "Firefox",
                  identity: "Firefox"
          },
          {
                  string: navigator.vendor,
                  subString: "Camino",
                  identity: "Camino"
          },
          {               // for newer Netscapes (6+)
                  string: navigator.userAgent,
                  subString: "Netscape",
                  identity: "Netscape"
          },
          {
                  string: navigator.userAgent,
                  subString: "MSIE",
                  identity: "Explorer",
                  versionSearch: "MSIE"
          },
          {
                  string: navigator.userAgent,
                  subString: "Gecko",
                  identity: "Mozilla",
                  versionSearch: "rv"
          },
          {               // for older Netscapes (4-)
                  string: navigator.userAgent,
                  subString: "Mozilla",
                  identity: "Netscape",
                  versionSearch: "Mozilla"
          }
  ],
  dataOS : [
          {
                  string: navigator.platform,
                  subString: "Win",
                  identity: "Windows"
          },
          {
                  string: navigator.platform,
                  subString: "Mac",
                  identity: "Mac"
          },
          {
                  string: navigator.platform,
                  subString: "Linux",
                  identity: "Linux"
          }
  ]
};
  BrowserDetect.init();
  return BrowserDetect.browser;
}

function rowOut(i, nColor) {
  var trObj = (document.getElementById) ? document.getElementById('ihtr' + i) : eval("document.all['ihtr" + i + "']");
  var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
  if (trObj == null || trObj.style.display=="none") nameObj.style.background=nColor;
}

function serv(i, href, nColor) {
  var trObj = (document.getElementById) ? document.getElementById('ihtr' + i) : eval("document.all['ihtr" + i + "']");
  var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
  var ifObj = (document.getElementById) ? document.getElementById('ihif' + i) : eval("document.all['ihif" + i + "']");

  //var ageID = document.getElementById('row_6_' + i);
  var browser = getBrowser(); 

  if (browser == "Firefox" || browser == "Mozilla" || browser == "Netscape" || browser == "Gecko" || browser == "Seamonkey"){
    //ageID.addEventListener("click", stopEvent, false);
    trObj.addEventListener("click", stopEvent, false);
    nameObj.addEventListener("click", stopEvent, false);
    //nameObj.addEventListener("click", stopEvent, false);
  }
  else {
    window.event.cancelBubble = true;
  }; 
 if (trObj != null) {
    if (trObj.style.display=="none") {
      ifObj.style.height = "0px";
      trObj.style.display="";
      nameObj.style.background="#ECECD9";
      if (browser == "Safari" || browser == "Konquerors"){
         setTimeout("toggleDownUp('triangle_"+i+"','down');",36);
      }   else {
        toggleDownUp('triangle_'+i,'down');
      }
      //if (!ifObj.src) ifObj.src = href;
      smoothHeight('ihif' + i, 0, 210, 42, 'o');
    }
    else {
      nameObj.style.background=nColor;
	//trObj.style.display="none";
      smoothHeight('ihif' + i, 210, 0, 42, 'ihtr' + i);
      if (browser == "Safari" || browser == "Konquerors"){
      setTimeout("toggleDownUp('triangle_"+i+"','up');",36);
      } else {
        toggleDownUp('triangle_'+i,'up');
      }
     /* triangleID.setAttribute('src', '/img/serp-toggle-up.gif');*/
    }
  }
}

function toggleDownUp(ele,pos){
var ele = document.getElementById(ele);
ele.setAttribute('src', '/img/serp-toggle-'+pos+'.gif');
}

function stopEvent(ev) {
  // this ought to keep t-daddy from getting the click.
  ev.stopPropagation();
//return false;
// alert("event propagation halted.");
//  ev.cancelBubble = true;
}
function trOC(idHL, idOC, idArrow) {
  var trObj = (document.getElementById) ? document.getElementById(idOC) : eval("document.all['" + idOC + "']");
  var hlObj = (document.getElementById) ? document.getElementById(idHL) : eval("document.all['" + idHL + "']");
  var arrowObj = (document.getElementById) ? document.getElementById(idArrow) : eval("document.all['" + idArrow + "']");
  if (trObj != null && hlObj != null) {
    if (trObj.style.display=="none") {
      trObj.style.display="";
      hlObj.style.background="#003366";
      hlObj.style.color="#EDF6F9";
      arrowObj.innerHTML="v";
    }
    else {
      trObj.style.display="none";
      hlObj.style.background="#ECECD9";
      hlObj.style.color="#000000";
      arrowObj.innerHTML="^";
    }
  }
}
function firstFocus()
{
   if (document.forms.length > 0)
   {
      var TForm = document.forms[0];
      for (i=0;i<TForm.length;i++)
      {
         if ((TForm.elements[i].type=="text")||
           (TForm.elements[i].type=="textarea")||
           (TForm.elements[i].type.toString().charAt(0)=="s"))
         {
            document.forms[0].elements[i].focus();
            break;
         }
      }
   }
}
function HighlightAll(theField) {
  var tempval=eval("document."+theField);
  tempval.focus();
  tempval.select();
  if (document.all) {
    therange=tempval.createTextRange();
    therange.execCommand("Copy");
    window.status="Contents highlighted and copied to clipboard!";
    setTimeout("window.status=''",36);
  }
}
function NewWindow(mypage, myname, w, h, scroll) {
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
  win = window.open(mypage, myname, winprops)
  if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
startList = function() {
  if (document.all&&document.getElementById) {
    navRoot = document.getElementById("nav");
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (node.nodeName=="LI") {
        node.onmouseover=function() {
          this.className+=" navOver";
        }
        node.onmouseout=function() {
          this.className=this.className.replace(" navOver", "");
        }
      }
    }
  }
}
function setImageDimensions(gotImage) {
  if(gotImage.width > 600) gotImage.width = 600;
}
function changeImageDimensions(gotImage, type) {
  if(gotImage.width > 600 && type == 'out') {
	  gotImage.width = 600;
    return;
  }
  if(type == 'over') gotImage.removeAttribute('width');
}

function toggleprob(obj) {
  var el = document.getElementById(obj);
  el.style.display = (el.style.display != 'none' ? 'none' : '' );
  el.style.display = '';  
}

function jqtoggle(obj) {
  $(obj).toggle();
}

startTabList = function(tab) {
   if (document.all&&document.getElementById) {
     navRoot = document.getElementById("detailsTabs");
     for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if (node.nodeName=="LI") {
		  node.onmouseover=function() {
		    this.className+=" detailsTabsOver";
		  }
		  node.onmouseout=function() {
		    this.className=this.className.replace(" detailsTabsOver", "");
		  }
		}
     }
   }
}

function cancelReply(reply,current_user_id,btid){
  var replyForm = document.getElementById(reply);
  replyForm.innerHTML = '';
}


function showQuotedReply(comment){
  $.get("/getcomment.php",
  { id: comment },
    function(data){
    $("#reply").val('**Quoting [Comment #'+comment+'](#commentanch_'+comment+')**\n> '+data.replace(/\n/g,"\n> ")+'\n\n');
  }
  );
}

function randomString() {
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  var string_length = 8;
  var randomstring = '';
  for (var i=0; i<string_length; i++) {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
    }
  return randomstring;
}

function toMarkdown(text){
  var converter = new Showdown.converter();
  var html = converter.makeHtml(text);
  return html;
}

function countdown(secs, commentid, hash, classification) {
  countdownele = "#countdown"+commentid;
  $(countdownele).html("<b>You have "+secs+"s to <a href=\"#\" onClick=\"editcomment("+commentid+",'"+hash+"','"+classification+"');return false;\">edit</a> your comment.</b><br><br>");
  secs--;
  if (secs<1) {
    $(countdownele).remove();
    canceleditcomment(commentid)
    } else
  setTimeout("countdown("+secs+","+commentid+",'"+hash+"','"+classification+"')",36);
};


function editcomment(id,hash,classification){  
  var commentele = '#comment_'+id;
 // var currentComment = getComment(id);//$('#mdcomment_'+id).text();
  $.get("/getcomment.php",
    { id: id },
    function(data){
  $(commentele).html("<textarea id=\"content_"+id+"\" class=\"commentTextarea\">"+data+"</textarea>&nbsp;<br><select name=\"classification\" id=\"classification_"+id+"\"><option value=\"none\">Flag this torrent as:</option><option value=\"none\">None</option><option value=\"misnamed\">Misnamed</option><option value=\"pwed\">Passworded</option><option value=\"spam\">Spam / Malware</option><option value=\"fake\">Fake</option></select>&nbsp;&nbsp;&nbsp;<input type=\"submit\" onClick=\"updatecomment("+id+",'"+hash+"')\" value=\"Update\" class=liteoption>&nbsp;<a href=\"#\" onClick=\"canceleditcomment("+id+");return false;\">cancel</a>");
  $("#classification_"+id).val(classification);
    }
  );
}

function canceleditcomment(id){
  var commentele = '#comment_'+id;
  $.get("/getcomment.php",
    { id: id , tohtml: "yes"},
    function(data){
      $(commentele).html(data);
      }
  );
}

function getComment(id){
  var _content;
  $.get("/getcomment.php",
    { id: id, tohtml: "no" },
    function(data){
      _content = data;
     return _content;
    }
  );
  return _content;
}

function updatecomment(id,hash){
  var contentele = '#content_'+id;
  var classele = '#classification_'+id;
  var content = $(contentele).val();
  var classification = $(classele).val();
  $.post("/updatecomment.php",
  { commentid: id, content: content, hash: hash, classification: classification });
  if (content!=''){
  $('#mdcomment_'+id).text(content);
  $('#comment_'+id).html(toMarkdown(content));
  }
}

function postComment(hash){
  var content = $('#reply').val();
  var classification = $('#classification').val();
  $("#reply").val('');
  $.post("/comment.php",
  { hash: hash, classification: classification, content: content },
  function(data){
    if (data.indexOf("<___no_comment___>") == -1){ 
      $("#commentsList").append(data);
      var latestid = $('#latest').text();
      $('#latest').remove();
      countdown(900,latestid,hash,classification);
    }
  }  
  );
}

function deletecomment(id,hash){
  var deleteBool = confirm('Are you sure you want to delete this comment?');
  if (deleteBool==true){
    $.post("/deletecomment.php",
    { commentid: id, hash: hash });
    $('#row_'+id).remove();
    $('#spacerow_'+id).remove();
  }
}


function voteTorrent(vote,hash){
  $.post("/votetorrent.php",
  { hash: hash, vote: vote });
   $('#saythanks').html('You\'ve said Thanks to this torrent.');
}

function voteComment(vote,comment_id,hash,cvotes){
  $.post("/votecomment.php",
  { comment_id: comment_id, vote: vote, hash: hash });
  $('#commentvote_'+comment_id).html('Thanks for voting.');
  $('#current_score_'+comment_id).html(vote+cvotes || '0');
}
