// $.ajaxSetup({async:true, cache:true});

function createCookie(name,value) {
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


tt = null;
 
document.onmousemove = updateTT;
 
function updateTT(e) {
  var scrTop = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
  var scrLeft = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
  x = (document.all) ? window.event.x + scrLeft : e.pageX;
  y = (document.all) ? window.event.y + scrTop : e.pageY;
  if (tt != null) {
	tt.css('left',((x * 1) + 20) + "px");
	tt.css('top',((y * 1) + 20) + "px");
  }
}

 
function showTT(id) {

	tt = $('#'+id);

	if (tt.html().length != 0)
	tt.show();
	else
	tt.hide();

	if (tt != null) {
	tt.css('left',((x * 1) + 20) + "px");
	tt.css('top',((y * 1) + 20) + "px");
	}

}
 
function hideTT() {
    tt.hide();
}


function showSC(e) {

if (e == 0)
$('.category-sub').hide();
else {
$('.category-sub').addClass("category-subm");
$('.category-sub').show();
}
sm = $('.sc'+e).html();

$('.category-sub').html(sm);

}

function Tpp(e) {
createCookie('tpp', e);

location.reload();

return false; 
}

function JCat() {
if (readCookie('DCat') == 'block')
createCookie('DCat', 'none');
else
createCookie('DCat', 'block');

location.reload();

return false;
}

$(document).ready(function() {

$("a[@class^='JView']").click(function(){

createCookie('view', $(this).attr('id'));

location.reload();

return false; 
}); 


$("a[@class^='JOpis']").click(function(){


if (readCookie('opis') == 'block')
createCookie('opis', 'none');
else
createCookie('opis', 'block');

location.reload();

return false;
});

$("a[@class^='JCheck']").click(function(){
var num = $(this).attr('id').substring(1);

for(i=0; i<num; i++){
getPeer(i);
continue;
}

return false;
});

$("a[@class^='JId']").click(function(){
var i = $(this).attr('id').substring(1);

getPeer(i);

return false;
});

$("a[@class^='JTime']").click(function(){

createCookie('lastseen', mktime());

location.reload();

return false;
});


});

function getPeer(i) {

var data = {};
data['announce'] = strtolower($('.'+i+'tracker').html());
data['infohash'] = $('.'+i+'hash').html();


$.ajax({
type: "POST",
url: "/check_page_new.php",
dataType: "json",
data: data,
beforeSend: function(x){
if(x && x.overrideMimeType) {
x.overrideMimeType("application/j-son;charset=UTF-8;");
}
$(".tnptable tr.top:eq("+i+") td:eq(3)").html('<img src="/img/28-1.gif">');
$(".tnptable tr.top:eq("+i+") td:eq(4)").html('<img src="/img/28-1.gif">');
},
success: function(obj,status) {

if(typeof(obj.error) != "undefined") {
	
	if(obj.error != ""){
		$(".tnptable tr.top:eq("+i+") td:eq(3)").html('---');
		$(".tnptable tr.top:eq("+i+") td:eq(4)").html('---');
		//alert(obj.error);
	}
	else{
		$(".tnptable tr.top:eq("+i+") td:eq(3)").html(obj.s);
		$(".tnptable tr.top:eq("+i+") td:eq(4)").html(obj.l);
	}
}

}//,
//error: function(xhr, status, errorThrown) {
//			alert(errorThrown+'\n'+status+'\n'+xhr.statusText);
//	}

});

}

function getStat(i,h,t) {

var data = {};
data['announce'] = t;
data['infohash'] = h;


$.ajax({
type: "POST",
url: "/check_page_new.php",
dataType: "json",
data: data,
beforeSend: function(x){
if(x && x.overrideMimeType) {
x.overrideMimeType("application/j-son;charset=UTF-8;");
}
$(".torrent"+i+" span.seed").html('<img src="/img/28-1.gif">');
$(".torrent"+i+" span.leech").html('<img src="/img/28-1.gif">');
$(".torrent"+i+" span.finish").html('<img src="/img/28-1.gif">');
},
success: function(obj,status) {

if(typeof(obj.error) != "undefined") {
	
	if(obj.error != ""){
		$(".torrent"+i+" span.seed").html('---');
		$(".torrent"+i+" span.leech").html('---');
		$(".torrent"+i+" span.finish").html('---');
		//alert(obj.error);
	}
	else{
		$(".torrent"+i+" span.seed").html(obj.s);
		$(".torrent"+i+" span.leech").html(obj.l);
		$(".torrent"+i+" span.finish").html(obj.f);
	}
}

}//,
//error: function(xhr, status, errorThrown) {
//			alert(errorThrown+'\n'+status+'\n'+xhr.statusText);
//	}

});

}

function check(a,b) {

var data = {};
data['announce'] = strtolower(b);
data['infohash'] = a;

$.ajax({
type: "POST",
url: "/check_page_new.php",
dataType: "json",
data: data,
beforeSend: function(){
$(".js").html('<img src="/img/28-1.gif">');
$(".jl").html('<img src="/img/28-1.gif">');
$(".jf").html('<img src="/img/28-1.gif">');
},
success: function(obj,status) {

if(typeof(obj.error) != "undefined") {
	
	if(obj.error != ""){
		$(".js").html('---');
		$(".jl").html('---');
		$(".jf").html('---');
		$(".je").html('<br/>Błąd: '+obj.error);
	}
	else{
		$(".js").html(obj.s);
		$(".jl").html(obj.l);
		$(".jf").html(obj.f);
		$(".jd").html(obj.d);
		
	}
}

}
});

}

function mktime() {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: baris ozdil
    // +      input by: gabriel paderni 
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FGFEmperor
    // +      input by: Yannoo
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: jakes
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Marc Palau
    // *     example 1: mktime(14, 10, 2, 2, 1, 2008);
    // *     returns 1: 1201871402
    // *     example 2: mktime(0, 0, 0, 0, 1, 2008);
    // *     returns 2: 1196463600
    // *     example 3: make = mktime();
    // *     example 3: td = new Date();
    // *     example 3: real = Math.floor(td.getTime()/1000);
    // *     example 3: diff = (real - make);
    // *     results 3: diff < 5
    // *     example 4: mktime(0, 0, 0, 13, 1, 1997)
    // *     returns 4: 883609200
    // *     example 5: mktime(0, 0, 0, 1, 1, 1998)
    // *     returns 5: 883609200
    // *     example 6: mktime(0, 0, 0, 1, 1, 98)
    // *     returns 6: 883609200
    
    var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
 
    if (argc > 0){
        d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
    }
 
    var dateManip = {
        0: function(tt){ return d.setHours(tt); },
        1: function(tt){ return d.setMinutes(tt); },
        2: function(tt){ var set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
        3: function(tt){ var set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
        4: function(tt){ return d.setDate(tt+mb); },
        5: function(tt){ return d.setYear(tt+ma); }
    };
    
    for( i = 0; i < argc; i++ ){
        no = parseInt(argv[i]*1);
        if (isNaN(no)) {
            return false;
        } else {
            // arg is number, let's manipulate date object
            if(!dateManip[i](no)){
                // failed
                return false;
            }
        }
    }
 
    return Math.floor(d.getTime()/1000);
}

function strtolower( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman
    // *     example 1: strtolower('Kevin van Zonneveld');
    // *     returns 1: 'kevin van zonneveld'
 
    return (str+'').toLowerCase();
}