var a;
var row_disp;

if (navigator.appVersion.indexOf("MSIE")==-1){
  row_disp ="table-row";
}
else{
  row_disp="block";
}

function show_hide_descr(a, add_info) {
  var obj = document.getElementById('descr_'+a);
  var link = document.getElementById('d_show_'+a);
  var tor = document.getElementById('tor_'+a);


  if(obj.style.display == row_disp) {
    obj.style.display = "none";
    link.style.backgroundPosition="0px 0px";
    tor.className="";
  }
  else {
    obj.style.display = row_disp;
    link.style.backgroundPosition = "0px -13px";
    tor.className="descr_opened";
		
    var divdesc_placeholder = $('#divdesc_'+a);
    var descr_placeholder = $('#descr_'+a);
    if ($.trim(descr_placeholder.html()).length === 0)
    {
      descr_placeholder.html('<td colspan="9">'+ divdesc_placeholder.html() +'</td>');
    }
    else
    {
  //alert($descr_placeholder.html());
  }
  }
}

function scrape(torrent_id, css_index, n_scrapes)
{
  if (css_index != '')
  {
    $('#i'+css_index).attr('src', '../../images/rts_loading.gif');
    $('#i'+css_index).attr('title', 'Scraping...');
    $('a:has(#i'+css_index + ')').attr('href', 'javascript:void(0)');
  }
  else
  {
    $('td.update > img').show();
    $('td.update > button').attr('disabled', 'disabled');
  }
  if (n_scrapes > 0)
  {
    window["n_scrapes"+torrent_id] = n_scrapes;
    /*$.getJSON(
				  '../../userscrape/' + torrent_id + '../../default.htm',
				  {id: css_index, n_scrape: 1, n_scrapes: n_scrapes, rand: Math.random()},
				  onScrapeComplete
				);
		return;*/
    for (i=0;i<n_scrapes;i++)
    {
      $.getJSON(
        '../../userscrape/' + torrent_id + '../../default.htm',
        {
          id: css_index,
          n_scrape: i,
          n_scrapes: n_scrapes,
          rand: Math.random()
        },
        onScrapeComplete
        );
      if (css_index == '')
      {
        $('#a'+i).attr('title', 'Scraping...');
        $('#a'+i+'>td>.status')[0].className = 'status scraping';
        $('#a'+i+'>td>.status').text('...');
      }
    }
  }
  else
  {
    $.getJSON(
      '../../userscrape/' + torrent_id + '../../default.htm',
      {
        id: css_index,
        rand: Math.random()
      },
      onScrapeComplete
      );
  }
  pageTracker._trackPageview('../../userscrape');
}

function onScrapeComplete(obj, status)
{
  if (window["n_scrapes"+obj.torrent_id] !== undefined)
    window["n_scrapes"+obj.torrent_id]--;
	
  if (obj.n_scrape === undefined)
  {
    if (obj.id != '')
    {
      $('#i'+obj.id).attr('src', '../../images/rts.gif');
      $('#i'+obj.id).attr('title', obj.response.msg);
    }
    else
    {
      $('td.update > img').hide();
      $('#sago').text(obj.response.msg);
    }
  }
  else
  if (window["n_scrapes"+obj.torrent_id] == 0)
  {
    if (obj.id != '')
    {
      $('#i'+obj.id).attr('src', '../../images/rts.gif');
      $('#i'+obj.id).attr('title', 'Scrape complete. Thanks for contribution :)');
    }
    else
    {
      $('td.update > img').hide();
      $('#sago').text('Scrape complete. Thanks for contribution :)');
    }
  }
  else
  {
    if (obj.id != '')
    {
      //$('#i'+obj.id).attr('src', '../../images/rts.gif');
      $('#i'+obj.id).attr('title', 'Trackers left to scrape: ' + window["n_scrapes"+obj.torrent_id]);
    }
    else
    {
      //$('td.update > img').hide();
      $('#sago').text('Trackers left to scrape: ' + window["n_scrapes"+obj.torrent_id]);
    }
  }

  if (status == 'success')
  {
    if (obj.success == 'true')
    {
      $('#s'+obj.id+'>span').text(obj.response.s);
      if (typeof(obj.response.s) == 'number')
      {
        if (obj.response.s > 0)
          $('#s'+obj.id + ' > span')[0].className = 'seeds';
        else
          $('#s'+obj.id + ' > span')[0].className = 'zero';
      }
      else
        $('#s'+obj.id + ' > span')[0].className = 'undef';
			
      $('#l'+obj.id+'>span').text(obj.response.l);
      if (typeof(obj.response.l) == 'number')
        $('#l'+obj.id + ' > span')[0].className = 'leeches';
      else
        $('#l'+obj.id + ' > span')[0].className = 'undef';
			
      $('#dl'+obj.id+'>span').text(obj.response.dl);
			
      // Fill particular tracker's info
      if (obj.id == '' && obj.n_scrape !== undefined)
      {
        $('#s'+obj.n_scrape+'>span').text(obj.tr_response.s);
        if (typeof(obj.tr_response.s) == 'number')
        {
          if (obj.tr_response.s > 0)
            $('#s'+obj.n_scrape + ' > span')[0].className = 'seeds';
          else
            $('#s'+obj.n_scrape + ' > span')[0].className = 'zero';
        }
        else
          $('#s'+obj.n_scrape + ' > span')[0].className = 'undef';
				
        $('#l'+obj.n_scrape+'>span').text(obj.tr_response.l);
        if (typeof(obj.tr_response.l) == 'number')
          $('#l'+obj.n_scrape + ' > span')[0].className = 'leeches';
        else
          $('#l'+obj.n_scrape + ' > span')[0].className = 'undef';
				
        $('#dl'+obj.n_scrape+'>span').text(obj.tr_response.dl);
        $('#a'+obj.n_scrape).attr('title', obj.response.msg);
        if (obj.tr_response.success == true)
        {
          $('#a'+obj.n_scrape+'>td>.status')[0].className = 'status success';
          $('#a'+obj.n_scrape+'>td>.status').text(':)');
        }
        else
        {
          $('#a'+obj.n_scrape+'>td>.status')[0].className = 'status failed';
          $('#a'+obj.n_scrape+'>td>.status').text(':(');
        }
      }
    }
  }
}


function toggleReportForm(torrent_id)
{
  var report_placeholder = $('.report_placeholder');
  $('.report_placeholder').toggle();
  var report_form = $('.report_placeholder > .report_form');
  //var_dump($report_form);
  if (report_form.length === 0)
  {
    // Load report form html
    report_placeholder.load('../../get_report_form/' + torrent_id + '/?rand=' + Math.random());
  }
  else
  {
//alert('Form here');
}
}

function loadCommentForm(torrent_id)
{
  var $commentform_placeholder = $('#commentform_placeholder');
  $commentform_placeholder.html('Loading comment form...');
  // Load form html
  $commentform_placeholder.load('../../comment/' + torrent_id + '/?form&rand=' + Math.random());
}

function loadsubcats(cat_id)
{
  var subcats_select = $('#subcat_id');
  subcats_select.html('<option value="-1">Loading...</option>');
  subcats_select.load('../../loadsubcats/' + cat_id + '../../');
}


function var_dump(element, limit, depth)
{
  depth =	depth?depth:0;
  limit = limit?limit:1;

  returnString = '<ol>';

  for(property in element)
  {
    //Property domConfig isn't accessable
    if (property != 'domConfig')
    {
      returnString += '<li><strong>'+ property + '</strong> <small>(' + (typeof element[property]) +')</small>';

      if (typeof element[property] == 'number' || typeof element[property] == 'boolean')
        returnString += ' : <em>' + element[property] + '</em>';
      if (typeof element[property] == 'string' && element[property])
        returnString += ': <div style="background:#C9C9C9;border:1px solid black; overflow:auto;"><code>' +
        element[property].replace(/</g, '&amp;lt;').replace(/>/g, '&amp;gt;') + '</code></div>';

      if ((typeof element[property] == 'object') && (depth < limit))
        returnString += var_dump(element[property], limit, (depth + 1));

      returnString += '</li>';
    }
  }
  returnString += '</ol>';

  if(depth == 0)
  {
    winpop = window.open("", "","width=800,height=600,scrollbars,resizable");
    winpop.document.write('<pre>'+returnString+ '</pre>');
    winpop.document.close();
  }

  return returnString;
}
