//// Big shout out to Cd-MaN, thank you for all your help!
var opened_box = null;
var opened_image = null;
var submitted = 0;
var open_box = '1';

var m_iInterval;
var m_iIntervalSet = false;
var m_Height;
var iScroll=0;
var arrTitles;
var arrCursor = 0;
var arrMax = arrTitles.length-1;
var isNS4 = (navigator.appName=="Netscape")?1:0;
var special_close = 0;
var donate_amount;

function tab_toggle ( new_toggle ) {
	if (new_toggle == 'browse') {
		document.getElementById("browse").style.display = "";
		document.getElementById("search_advance").style.display = "none";
		document.getElementById("search_basic").style.display = "none";		 
		document.getElementById("tab_browse").style.background = "#00FF33";
		document.getElementById("tab_search").style.background = "#CDDAE9";
	}
	if (new_toggle == 'search') {
		if (on_tab_search == 'advance') { new_toggle = 'search_advance'; }
		else { 
			if (advanced == 1) { new_toggle = 'search_advance'; }
			else { new_toggle = 'search_basic'; }
			
		}
	}
	if (new_toggle == 'search_basic')  {
		on_tab_search = 'basic';
		document.form_search_basic.q.value = document.form_search_advance.q.value;
		document.getElementById("tab_browse").style.background = "#CDDAE9";
		document.getElementById("tab_search").style.background = "#00FF33";
		document.getElementById("browse").style.display = "none";
		document.getElementById("search_advance").style.display = "none";
		document.getElementById("search_basic").style.display = "";		
		if (!nofocus) { document.form_search_basic.q.focus(); }
	}
	if (new_toggle == 'search_advance')  {
		on_tab_search = 'advance';
		document.getElementById("tab_browse").style.background = "#CDDAE9";
		document.getElementById("tab_search").style.background = "#00FF33";  // Old color: 7CFC00
		document.form_search_advance.q.value = document.form_search_basic.q.value;
		document.getElementById("browse").style.display = "none";
		document.getElementById("search_advance").style.display = "";
		document.getElementById("search_basic").style.display = "none";		
		if (!nofocus) { document.form_search_advance.q.focus(); }
	}
}
function href(url) {
		window.location = url;
}
function init(tab_select,unsup_bro) {
	if (tab_select == 'search_basic' && !nofocus) {
		document.form_search_basic.q.focus();	
	}
	if (tab_select == 'search_advance' && !nofocus) {
		document.form_search_advance.q.focus();	
	}
	if (tab_select == 'browse' && !nofocus) {
//		document.form_browse.c.focus();
	}
    	if (unsup_bro != 1) {
		wl();
	}	
}
function validate(theform) {
	if (theform == 'search_advance')  {
		if (document.form_search_advance.q.value.length < 2) {
			alert(lang[0]);
			return false;
		}
	}
	if (theform == 'search_basic')  {
		if (document.form_search_basic.q.value.length < 2) {
			alert(lang[0]);
			return false;
		}
	}
	if (theform == 'browse')  {
		if (document.form_browse.c && document.form_browse.c.value == '0') {
			alert(lang[1]);
			return false;
		}
	}
}
function toggle( targetId ){ 
  if (document.getElementById){
        target = document.getElementById( targetId );
           if (target.style.display == "none"){
              target.style.display = "";
           } else {
              target.style.display = "none";
           }
     }
}

function move_box(an, box, mi_left, mi_top) {
  var cleft = 0;
  var ctop = 0;
  var obj = an;
  while (obj.offsetParent) {
    cleft += obj.offsetLeft - mi_left;
    ctop += obj.offsetTop - mi_top;
    obj = obj.offsetParent;
  }
  box.style.left = cleft + 'px';
  ctop += an.offsetHeight + 8;
  if (document.body.currentStyle &&
    document.body.currentStyle['marginTop']) {
    ctop += parseInt(
      document.body.currentStyle['marginTop']);
  }
  box.style.top = ctop + 'px';
}

function child_iframe_load(child_frame) {
	var getFFVersion = navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
	var FFextraHeight = parseFloat(getFFVersion)>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers

	if (child_frame.contentDocument && child_frame.contentDocument.body.offsetHeight) //ns6 syntax
		child_frame.height = child_frame.contentDocument.body.offsetHeight+FFextraHeight; 
	else if (child_frame.Document && child_frame.Document.body.scrollHeight) //ie5+ syntax
		child_frame.height = child_frame.Document.body.scrollHeight;
}
function show_hide_box(an, width, height, borderStyle, minus_left, minus_top, jdfkjd) {
  var href = an.href;
  var boxdiv = document.getElementById(href);
  if (boxdiv != null) {
    if (boxdiv.style.display=='none') {
      move_box(an, boxdiv, minus_left, minus_top);
      boxdiv.style.display='block';
    } else
      boxdiv.style.display='none';
    return false;
  }
  boxdiv = document.createElement('div');
  boxdiv.setAttribute('id', href);
  boxdiv.style.display = 'block';
  boxdiv.style.position = 'absolute';
  boxdiv.style.width = width + 'px';
  boxdiv.style.height = height + 'px';
  boxdiv.style.border = borderStyle;
  boxdiv.style.backgroundColor = '#fff';
  var contents = document.createElement('iframe');
  contents.scrolling = 'no';
  contents.frameBorder = '0';
  contents.style.width = width + 'px';
  contents.style.height = height + 'px';
  contents.src = href;
  boxdiv.appendChild(contents);
  document.body.appendChild(boxdiv);
  move_box(an, boxdiv, minus_left, minus_top);
  if (open_box != '1') {
      open_box.style.display='none';
  }
  open_box = boxdiv;
  return false;
}
function listfiles(an, width, height, borderStyle, minus_left, minus_top, img_name) {
	//changed by Cd-MaN
    	  
  var href = an.href;
        
  //element not found, need to create one
  if (img_name) { document[img_name].src='/images/shrink.gif'; }
  
	//get the parent tr
  var parentTr = an.parentNode;
  while (null != parentTr && 'TR' != parentTr.tagName)
  	parentTr = parentTr.parentNode;
  parentTr = parentTr.parentNode;
  while (null != parentTr && 'TR' != parentTr.tagName)
  	parentTr = parentTr.parentNode;
  
  //create a new row under it
  var newTr = document.createElement('TR');
  newTr.id = href;
  
  var newTd = document.createElement('TD');
  newTd.colSpan = 7;
  newTd.style.backgroundColor = "#fff";
  newTd.height = 5;
 	newTr.appendChild(newTd);
  
  //add the iframe in it
  var newIframe = document.createElement('IFRAME');
  newIframe.style.width = '100%';
  newIframe.scrolling = 'no';
  newIframe.frameBorder = 0;
  newIframe.height = height;
  
  newTd.appendChild(newIframe);
  
  //create an event listener special for this iframe resize
  var iframe_resizer = function () { child_iframe_load(newIframe); }
 
	//attach the event listener
	if (newIframe.addEventListener)
		newIframe.addEventListener("load", iframe_resizer, false);
	else if (newIframe.attachEvent)
		newIframe.attachEvent("onload", iframe_resizer)
	else
		newIframe.onload = iframe_resizer;
  //only now set the source, so that the event listener surely fires
  newIframe.src = href;
    
  parentTr.parentNode.insertBefore(newTr, parentTr.nextSibling);
  
  //change the event handler for the A  
  an.onclick = function () {
  	var boxdiv = document.getElementById(href);
    if (null != boxdiv) { //this shouldn't happen!
    	if ('none' == boxdiv.style.display) {      	
      	if (null != opened_box) {
        	opened_box.style.display = 'none';
        	opened_image.src='/images/expand.gif';
        }
      
      	document[img_name].src='/images/shrink.gif';
        boxdiv.style.display='';
        
        opened_image = document[img_name];
        opened_box = boxdiv;
      } else {      	
      	document[img_name].src='/images/expand.gif';
        boxdiv.style.display='none';
        
        opened_box = null; opened_image = null;
      }
    }   	
    return false;
  }
  
  if (null != opened_box) {
  	opened_box.style.display = 'none';
    opened_image.src='/images/expand.gif';
  }  
	opened_image = document[img_name];
	opened_box = newTr;
  
  return false;
}
function dosubmit(theform,thekey,thevalue) {
	if (submitted) {
		alert(lang[6]);
	}
	submitted = 1;
    if (thekey) {
	    thekey.value = thevalue;
    }
	theform.submit();
}
function wl() {
  if (m_iIntervalSet) pause();
  m_iInterval = setInterval(ontimer, 10);
  m_iIntervalSet = true;
  var base = document.getElementById("jump_base");
  m_Height = base.offsetHeight;
  var divi = parseInt(m_Height/5);
  m_Height = divi*5;
  var td1 = document.getElementById("td1");
  var td2 = document.getElementById("td2");
  var td3 = document.getElementById("td3");
  td1.height = m_Height-5;
  td2.height = m_Height-5;
  td3.height = m_Height-5;
  setLink();
}
function setLink() {
  document.getElementById("td2").innerHTML = arrTitles[arrCursor];
}
function ontimer() {
  var base = document.getElementById("jump_base");
  iScroll+=5;
  if (iScroll>(m_Height*2)) {
    iScroll=0;
    arrCursor++;
    if (arrCursor>arrMax)
      arrCursor=0;
    setLink();
  }
  if (iScroll==m_Height) {
    pause();
    m_iInterval = setTimeout(resume, 5000);
    m_iIntervalSet = true;
  }
  base.scrollTop=iScroll;
}
function pause() {
  clearInterval(m_iInterval);
  m_iIntervalSet = false;
}
function resume() {
  if (m_iIntervalSet) pause();
  m_iInterval = setInterval(ontimer, 10);
  m_iIntervalSet = true; 
}
function changepage(new_page) {
    var new_href = location.href;
    if (new_href.match(/\/page/)) {
        new_href = new_href.replace(/\/page[0-9]*/g, "/page" + new_page);
    }
    else if (new_href.match(/p=/)) {
        new_href = new_href.replace(/p=[0-9]*/g, "p=" + new_page);
    }
    else if (new_href.match(/browse\/[A-Za-z0-9]*$/)) {
	new_href = new_href + '/page' + new_page;
    }
    else {
        new_href = new_href + "&p=" + new_page;
    }
    location.href = new_href;
}
function wopen(url, w, h, scroll)
{
        w += 32;
        h += 96;
	var win = window.open(url,
		'popup',
		'width=' + w + ', height=' + h + ', ' +
		'location=no, menubar=no, ' +
		'status=no, toolbar=no, scrollbars=' + scroll + ', resizable=no');
	win.resizeTo(w, h);
	win.focus();
}
function changelang(newlang,newurl) {
	var answer = confirm("Language: " + newlang);
	if (answer) {
		window.location = newurl;
	}
}
function change_category(category) {
	var newurl = location.href;
	if (!newurl.match(/browse\/[A-Za-z0-9]*/)) {
		newurl = 'http://btjunkie.org/browse/All/page1/?o=52&t=0&s=1';
	}
	window.location = newurl.replace(/browse\/[A-Za-z0-9]*/,"browse/" + category).replace(/\/page[0-9]+/,"/page1");
}
function addfirefox() {
	if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function")) {
		window.sidebar.addSearchEngine("http://btjunkie.org/btjunkie.src","http://mycroft.mozdev.org/install.php/2999/btjunkie.gif", "BTJunkie", "File Sharing");
	}
	else {
		alert("Netscape 6 or Mozilla is needed to install a sherlock plugin");
	}
}
///////////////////// SLIDING AD
var _ad; var _tb; var _adTop; var _maxScroll; var _lastPos=0;
function adinit() {
if(document.getElementById('ad-right')) {
        _ad=document.getElementById('ad-right');
        _adTop=_ad.offsetTop;
        _maxScroll=document.getElementById('main').offsetHeight-document.getElementById('ad-extra').offsetHeight+_adTop-_ad.offsetHeight-10;
        if(_maxScroll>_adTop) adScroll();
}
}

function pos(){ if(window.innerHeight)
return window.pageYOffset; if(document.documentElement&&document.documentElement.scrollTop)
return document.documentElement.scrollTop; if(document.body)
return document.body.scrollTop; return 0;}
function adScroll(){ if(_lastPos!=pos()){ _lastPos=pos(); if(pos()<_maxScroll&&pos()>_adTop)
_ad.style.top=pos()+'px'; else if(pos()<_maxScroll)
_ad.style.top=_adTop+'px'; else _ad.style.top=_maxScroll+'px';}
setTimeout('adScroll()', 10);}

window.onresize=function(){ if(_ad){ _adTop=document.getElementById('main').offsetTop;
_maxScroll=document.getElementById('main').offsetHeight+_adTop-_ad.offsetHeight;}
};
