function Contact(subject) { window.location.href='mai'+'lto'+':'+'Son'+'ic19'+'80'+'@'+'gm'+'ail.c'+'om'+'?subject='+escape(subject); return false; }
function squote(str) { return str.split('"').join("'"); }
function yesno(i) { return (i==1)?STR_YES:STR_NO; }
function nowww(s) { if (s.slice(0,4)=='www.') return s.slice(4); else return s; }
function cut(s,n) {
  if (s.length<=n) return s;
  if (s[s.length-1]=='.') n--;
  return s.slice(0,n)+'…';
}

function getUrlVars() {
  var vars = [], hash;
  var hashes = window.location.href.slice(window.location.href.indexOf('?')+1).split('&');
  for(var i=0; i<hashes.length; i++) {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
  }
  return vars;
}
var urlvars = getUrlVars();

var TC = 5; // tabs count
if (urlvars['w']) { T = parseInt(urlvars['w']); if (0>T>=TC) T=0; } else T=0;
var TD = [];  // tabs data

function tab(id,engines_count,tabname) {
  var tab=this; // private
  tab.id=id;
  tab.page='#PAGE'+id;
  tab.thead='';
  tab.engines_count=engines_count;
  tab.results=0;
  tab.progress=0;
  tab.tabname=tabname;
  tab.lastquery='';
  tab.lastqueryvars='';
  tab.engines=new Array;
  if (tab.engines_count>0) for (var i=0;i<engines_count;i++) tab.engines[i]=$.ajax();

  tab.ShowProgress=function() {  // +Math.floor((100/tab.engines_count)*tab.progress)+'%'
    var p = $(tab.page+' .PROGRESS');
    // in progres...
    if (tab.progress<tab.engines_count) p.html('<img src="style/progressbar.gif"/>');
    // finished
    else {
      p.fadeOut(0);
      p.html('<span>'+STR_SEARCH_RESULTS+': '+tab.results+'</span>'); 
      if (tab.results>0) {
        p.append('<span> | '+
                 '<a href="http://www.lolp2p.com/?w='+tab.id+'&q='+escape(tab.lastquery)+tab.lastqueryvars+'">'+
                 STR_SEARCH_PERMALINK+'</a></span>');
      }
      p.fadeIn(1500);
    }
  };

  tab.Search=function() {
    if (tab.engines_count<1) return false;
    if ($(tab.page+' .Q').val()=='') return false;
    tab.lastquery=$(tab.page+' .Q').val();   
    //$('#LOGO').animate({marginBottom:'0px'},200).animate({marginTop:'4px'},400);
    $('#LOGO').css({marginTop:'4px',marginBottom:'0px'});
    // cancelo anteriores busquedas
    for (var i=0;i<tab.engines_count;i++) tab.engines[i].abort();
    // limpio resultados
    $(tab.page+' .RESULTS').html('');
    tab.results=0;
    tab.progress = 0;
    tab.ShowProgress();
    // se empieza a buscar
    for (var i=0;i<engines_count;i++) tab.SearchEngine(i);
    return false;                   
  };

  tab.SearchEngine=function(id) {
    var t =($(tab.page+' .CBT').length>0)?($(tab.page+' .CBT').is(':checked')?'&t=1':''):'';
    var l =($(tab.page+' .CBL').length>0)?($(tab.page+' .CBL').is(':checked')?('&l='+STR_LANGUAGE):''):'';
    var o =($(tab.page+' .OPT').length>0)?('&o='+$(tab.page+' .OPT').val()):'';
    tab.lastqueryvars=t+l+o;
    tab.engines[id]=$.ajax({
      type: 'GET',
      url: 'ajax/'+tab.tabname+'.php',
      data: 'id='+id+'&q='+escape(tab.lastquery)+tab.lastqueryvars+'&nocache='+Math.random(),
      success: function(data) {
        tab.ParseResults(data);
      }
    });
  };

  tab.CreateTable=function() {
    var tableid = 'RESTBL'+tab.id;
    if ($('#PAGE'+tab.id+' .RESULTS').html()=='') {
      $('#PAGE'+tab.id+' .RESULTS').html('<table id="'+tableid+'" class="RESULTS"><thead><tr>'+tab.thead+'</tr></thead><tbody></tbody></table>');
      if ($.browser.msie) {       
        $('th').mouseover(function(){ if ($(this).hasClass('header')) $(this).css('background-color','#e8e8d0').css('background-image','url("style/bg1.gif")');});
        $('th').mouseout (function(){ if ($(this).hasClass('header')) $(this).css('background-color','#fff').css('background-image','url("style/bg0.gif")');});
      }
      if (tab.tabname=='torrents') $('#'+tableid).tablesorter({headers:{3: {sorter: false}}});
      else $('#'+tableid).tablesorter();
    }
  } 

  tab.ParseResults=function(data) {
    var tableid = '#RESTBL'+tab.id;
    tab.progress++;
    if (data=='') { tab.ShowProgress(); return false; }  
    var lines = data.split('\n');
    var lastresults = tab.results;
    var l=''; // line
    var r=''; // parsed results (table rows)
    var rc=0; // results count
    for (var i in lines) if ((l=tab.ParseLine(lines[i]))!=''){r+=l;rc++;}
    if (rc>0) {
      if (tab.results==0) tab.CreateTable();
      $(tableid+' tbody').append(r);
      tab.results+=rc;
      if ($.browser.msie) {
        $('tbody tr').css('background-color','#fff');
        $('tbody tr').mouseover(function(){ $(this).css('background-color','#e8e8d0');});
        $('tbody tr').mouseout (function(){ $(this).css('background-color','#fff');});
      }
      $(tableid).trigger("update"); 
      if (tab.tabname=='torrents') $(tableid).trigger("sorton",[[[4,1]]]); 
      else $(tableid).trigger("sorton",[[[0,0]]]); 
    }  
    tab.ShowProgress();
  };
}

function settabs() {
// SETUP ED2K TAB
TD[0]=new tab(0,7,'ed2k');
TD[0].thead='<th>'+STR_THEAD_TITLE+'</th><th>'+STR_THEAD_WEB+'</th><th class="C">'+STR_THEAD_REGISTER+'</th>';
$('#PAGE0 .FLR').html('<input id="CBT0" class="CBT" type="checkbox"/><label for="CBT0">'+STR_OPTION_TITLESONLY+'</label><br/>');
TD[0].ParseLine=function(line) {
  var cols = line.split('\t');
  if (cols.length!=4) return '';
  var title = cols[0].split('<>');
//$txt."\t".$url['host']."\t".$url['path']."\t1
  return '<tr><td><a target="_blank" href="http://'+cols[1]+cols[2]+'" target="_BLANK" title="'+squote(cols[0])+'">'+title[0].substr(0,80)+'</a></td><td>'+nowww(cols[1])+'</td><td class="C">'+yesno(cols[3])+'</td></tr>';
};

// SETUP TORRENTS TAB
TD[1]=new tab(1,2,'torrents');
TD[1].thead='<th class="R">'+STR_THEAD_TYPE+'</th><th>'+STR_THEAD_TITLE+'</th><th>Tracker</th><th class="R">'+STR_THEAD_SIZE+'</th><th class="R">SE</th><th class="R">LE</th>';
$('#PAGE1 .FLR').html('<input id="CBT1" class="CBT" type="checkbox"/><label for="CBT1">'+STR_OPTION_TITLESONLY+'</label><br/>');
TD[1].ParseLine=function(line) {
  var cols = line.split('\t');
  if (cols.length!=7) return '';
//$type\t{$url['host']}\t{$url['path']}\t$txt\t$siz\t$see\t$lee
  return '<tr><td class="R">'+cols[0]+'</td><td><a target="_blank" href="http://'+cols[1]+cols[2]+'" title="'+squote(cols[3])+'">'+cols[3].substr(0,55)+'</a></td><td>'+nowww(cols[1])+'</td><td class="R">'+cols[4]+'</td><td class="R">'+cols[5]+'</td><td class="R">'+cols[6]+'</td></tr>';
};

// SETUP DDs TAB
TD[2]=new tab(2,1,'dds');
TD[2].thead='<th class="R">Rank</th><th>'+STR_THEAD_TITLE+'</th><th>'+STR_THEAD_WEB+'</th>';
$('#PAGE2 .FLR').html('<input id="CBT2" class="CBT" type="checkbox"/><label for="CBT2">'+STR_OPTION_TITLESONLY+'</label><br/>'+
                      '<input id="CBL2" class="CBL" type="checkbox"/><label for="CBL2">'+STR_OPTION_LOCALONLY+'</label>');
TD[2].ParseLine=function(line) {
  var cols = line.split('\t');
  if (cols.length!=5) return '';
//$txt\t{$url['host']}\t{$url['path']}\t$des
  return '<tr>'+
         '<td class="R">'+cols[4]+'</td><td><a target="_blank" href="http://'+cols[1]+cols[2]+'" title="'+squote(cols[3])+'">'+cols[0]+'</a></td>'+
         '<td>'+cut(nowww(cols[1]),20)+'</td>'+
         '</tr>';
};

// SETUP COVERS TAB
TD[3]=new tab(3,2,'covers');
TD[3].thead='<th>'+STR_THEAD_TITLE+'</th><th>'+STR_THEAD_AVAILABLEPARS+'</th><th>'+STR_THEAD_WEB+'</th>';
$('#PAGE3 .FLR').html('<select class="OPT">'+
                        '<option value="all">'+STR_OPTION_ALL+'</option>'+ 
                        '<option value="poster">'+STR_OPTION_POSTERS+'</option>'+
                        '<option value="dvd">DVD</option>'+
                        '<option value="bluray">BluRay</option>'+
                        '<option value="hddvd">HD-DVD</option>'+
                        '<option value="vcd">VCD</option>'+
                        '<option value="vhs">VHS</option>'+
                      //'<option value="bollywood">Bollywood</option>'+
                        '<option value="audio">'+STR_OPTION_MUSIC+'</option>'+
                      //'<option value="karaoke">Karaoke</option>'+    
                        '<option value="pc">PC</option>'+
                        '<option value="psp">PSP</option>'+
                        '<option value="psx">Playstation</option>'+
                        '<option value="ps2">Playstation 2</option>'+
                        '<option value="ps3">Playstation 3</option>'+
                        '<option value="gba">Gameboy Advance</option>'+
                        '<option value="ds">Nintendo DS</option>'+
                        '<option value="gamecube">GameCube</option>'+
                        '<option value="wii">Wii</option>'+
                        '<option value="xbox">XBox</option>'+
                        '<option value="xbox360">XBox 360</option>'+
                        '<option value="dreamcast">Dreamcast</option>'+
                        '<option value="cdi">CDi</option>'+
                      '</select>');

TD[3].ParseLine=function(line) {
  var cols = line.split('\t');
  if (cols.length!=3) return '';
//$txt\t$urls\t{website}
  cols[1]=cols[1].split('\r').join(', ');
  return '<tr><td>'+cols[0]+'</td><td>'+cols[1]+'</td><td>'+cols[2]+'</td></tr>';
};

// SETUP SUBS TAB
TD[4]=new tab(4,0,'subs');
}

function setnav(id) {
  function IE6tabWorkAroud() {
    if ($.browser.msie) {
      $('#NAV a').css('top','0px');
      $('#NAV a.ON').css('top','1px');
      $('#NAV ul').css('margin','0px');
    }
  }
  $('#NAV a').each(function(i){
    $(this).click(function() {
      if ($(this).hasClass('ON')) return false;
      $('#NAV a').removeClass('ON');
      var clicked=this;
      $('#NAV a').each(function(j){
        if (this==clicked) {
          var lastq = $('#PAGE'+T+' .Q').val();
          T=j;
          $(this).addClass('ON');
          IE6tabWorkAroud();
          $('.PAGE').hide();
          $('#PAGE'+T).show();
          if ($('#PAGE'+T+' .Q').val()=='') $('#PAGE'+T+' .Q').val(lastq);
          $('#PAGE'+T+' .Q').focus();
        }
      });
      return false;
    });
    $(this).focus(function(){$(this).blur();});
  });
  $($('#NAV a')[T]).addClass('ON');
  IE6tabWorkAroud();
}

$(document).ready(function(){
  $('.PAGE').html($('#PAGE0').html());
  settabs();
  setnav();
  $('input') .click(function(){$('#PAGE'+T+' .Q').focus()});
  $('select').change(function(){$('#PAGE'+T+' .Q').focus()});
  $('#PAGE'+T).show();
  if (urlvars['q']) {
    $('#LOGO').css({marginTop:'4px',marginBottom:'0px'});
    $('#PAGE'+T+' .Q').val(unescape(urlvars['q']));
    TD[T].Search();
  }
  $('.FRM form').submit(function(){return TD[T].Search();});
  $('#PAGE'+T+' .Q').focus();
});


