
var todel = '';

var checkeverysec = 15;
var lastupdate = 0;
var checkdownloader = 0;

jQuery().ready(function(){
	
	 if($("#action-download").hasClass("action-download")) {
	 $("#action-download").load("download.php?ie="+Math.random());
	 }
	
	$(".refresh").click(function(event){
		  $("#captcha2").attr("src", "captcha.php?rnd="+Math.random());
	 });	
	 
	 $(".qcheck").click(function(event){
		  $("#contqueue input[type=checkbox]").attr("checked","checked");
		  return false;
	 });
	 
	 $(".switch1").click(function(event){
		  top.location = 'files-static,save.html';
		  return false;
	 });	 
	 
	 $(".switch2").click(function(event){
		  top.location = 'files,save.html';
		  return false;
	 });	 
	 
	 $(".fcheck").click(function(event){
		  $("#contfiles input[type=checkbox]").attr("checked","checked");
		  return false;
	 });

	 $(".qdel").click(function(event){
		if(confirm("Czy aby na pewno chcesz usunąć te pliki?")) {  
		  $("#contqueue input:checked").filter(function(){
			
			todel = todel + '['+ $(this).attr("id") +']';
			$(this).parent().parent().fadeOut();
			$(this).parent().parent().remove();
		  });
		  $("#action-del").load("delete.php?ie="+Math.random()+"&type=q&ids="+todel);
		  todel = '';
		}
		  return false;
	 });

	 $(".fdel").click(function(event){
		if(confirm("Czy aby na pewno chcesz usunąć te pliki?")) {
		  $("#contfiles input:checked").filter(function(){
			
			todel = todel + '['+ $(this).attr("id") +']';
			$(this).parent().parent().fadeOut();
			$(this).parent().parent().remove();
		  });
		  $("#action-del").load("delete.php?ie="+Math.random()+"&type=f&ids="+todel);
		  todel = '';
		}
		  return false;
	 });
	 
	 $(".flinks").click(function(event){
		
		$("#linkslist").html("");
		newtextarea = $("<textarea>");
		newtextarea.attr("name","llist");
		newtextarea.attr("id","llist");
		newtextarea.attr("class","llist");
		newtextarea.attr("rows","10");
		newtextarea.attr("cols","95");
		newtextarea.css("font-size","9pt");
		
		$("#contfiles > #files_row > #files_link > .server").filter(function(){
			$(newtextarea).append($(this).attr("value")+"\n");
		});
		
		$("#linkslist").append(newtextarea);
		
		  return false;
	 });	 


	 $("#action-check").load("checksize.php?ie="+Math.random());
	 

	 
	 $("#action-checkfiles").load("checkfiles.php?ie="+Math.random());
	 
	 $("#payment_offers div").filter(function(){
	 $(this).fadeTo("slow", 0.6);
	 });
	 
	 
	 $("#payment_offers div").hover(function(){
	 $(this).fadeTo("normal", 1);
	 },function(){
	 $(this).fadeTo("slow", 0.6);
	 });
	 
	 $("#logs-navi div").click(function() {
		var page = $(this).children("a").attr("href");

		
		if($(this).attr("id") != "navipageselect") {
		//show ajaxgif
		$("#navipageajaxgif").html("<img src=\"styles/zajepro/images/icons/ajaxgif2.gif\" class=\"naviajaxgif\">");
			
			if(page != "#" && page != "") {
				$("#logs-list").load("showlogs.php?ie="+Math.random()+"&page="+page,function() {
				$("#navipageajaxgif").html("");
				});
			} else {
				$(this).attr("id", "navipageselect");
				$("#navipageajaxgif").html("");

				var addselect;
				for(i=6; i<=parseInt(pagemax)-1; i++) {
				addselect = addselect + '<option>'+i+'</option>';
				}
				
				$(this).html("<select style=\"width: 45px\" id=\"pageselect\" class=\"pageselect\" onchange=\"reloadlogspage(this)\">"+addselect+"</select>");
				
			}
		}
	 return false;
	 });
	 

if($("#load").hasClass("load")) {
var newdiv1 = $("<div>");
newdiv1.attr("id","ajaxgif");
newdiv1.attr("class","ajaxgif1");
newdiv1.html("<img src=\"styles/zajepro/images/icons/ajaxgif2.gif\"> Pobieram dane..");

var newdiv2 = $("<div>");
newdiv2.attr("id","ajaxgif");
newdiv2.attr("class","ajaxgif2");
newdiv2.html("<img src=\"styles/zajepro/images/icons/ajaxgif2.gif\"> Pobieram dane..");

$("#contqueue").append(newdiv1);
$("#contfiles").append(newdiv2);
		
var i1=0, i2=0;
var downloaderactive = 0;
var activeuniq;
var lastprogress=-1;
		
time = setInterval(function() {

$("#load").load("update.php?ie="+Math.random(), function() {
					lastupdate = parseInt($("div#lastupdate").text());
					$(".ajaxgif1").remove();
					
					
					$("div#load > div#queue").children("span#file").children("label#name").filter(function() {
						element = this;
						
						newdiv = QueueDiv($(element).html(), $(element).parent().children("label#uniq").text(), $(element).parent().children("label#size").text(),'queue','','',$(element).parent().children("label#link").html());
						$("#contqueue").append(newdiv);
						newdiv.fadeIn((Math.random()*500)+500);
						i1++;
					});
					
					$(".ajaxgif2").remove();
					
					$("div#load > div#files").children("span#file").children("label#name").filter(function() {
						element = this;
						
						newdiv = QueueDiv($(element).html(), $(element).parent().children("label#uniq").text(), $(element).parent().children("label#size").text(),'files', $(element).parent().children("label#time").html(), '',$(element).parent().children("label#link").html());
						$("#contfiles").append(newdiv);
						newdiv.fadeIn((Math.random()*500)+500);
						i2++;
					});

					var newdiv3 = $("<div>");
					newdiv3.attr("id","nofilesq");
					newdiv3.html("<i>Brak plików</i>");
					
					var newdiv4 = $("<div>");
					newdiv4.attr("id","nofilesf");
					newdiv4.html("<i>Brak plików</i>");
					
					if(i1 == 0)
					$("#contqueue").append(newdiv3);
					
					if(i2 == 0)
					$("#contfiles").append(newdiv4);
					
					$("div#load > div#downloader").children("span#file").children("label#name").filter(function() {
						element = this;
						
						newdiv = QueueDiv($(element).html(), $(element).parent().children("label#uniq").text(), $(element).parent().children("label#size").text(),'downloader','',$(element).parent().children("label#progress").text(),$(element).parent().children("label#link").html());
						newdiv.fadeIn((Math.random()*500)+500);
						activeuniq = $(element).parent().children("label#uniq").text();
						lastprogress = $(element).parent().children("label#progress").text();
						$("#contdown").append(newdiv);
						$("#contdown").fadeIn((Math.random()*500)+1000);
						downloaderactive = 1;
						i1++;
					
					});

					
				
});

clearInterval(time);

},1000);
			
time2 = setInterval(function() {

	$("#load").load("update.php?ie="+Math.random()+"&u=1&lastupdate="+lastupdate, function() {
		
					$("div#load > div#queue").children("span#file").children("label#name").filter(function() {
					
						$("div#nofilesq").remove();
						
						element = this;
						newdiv = QueueDiv($(element).html(), $(element).parent().children("label#uniq").text(), $(element).parent().children("label#size").text(),'queue','','',$(element).parent().children("label#link").html());
						$("#contqueue").append(newdiv);
						newdiv.fadeIn((Math.random()*500)+500);
						
					});

					$("div#load > div#files").children("span#file").children("label#name").filter(function() {
					
						$("div#nofilesf").remove();
						
						element = this;
						if($('.'+$(element).parent().children("label#uniq").text()).parent().attr("id") == "contdown") {
						$("#contdown").hide();
						downloaderactive = 0;
						activeuniq = 0;
						lastprogress = '';
						}
						
						$('.'+$(element).parent().children("label#uniq").text()).remove();
						
						newdiv = QueueDiv($(element).html(), $(element).parent().children("label#uniq").text(), $(element).parent().children("label#size").text(),'files', $(element).parent().children("label#time").html(), '',$(element).parent().children("label#link").html());
						$("#contfiles").append(newdiv);
						newdiv.fadeIn((Math.random()*500)+500);
						
					});


					$("div#load > div#downloader").children("span#file").children("label#name").filter(function() {
						
						element = this;

						if($(element).parent().children("label#uniq").text() == activeuniq) {
						//update last
						$("div#contdown div#downloading div#progress").text($(element).parent().children("label#progress").text());
						$("div#contdown div#downloading div#progress").fadeOut((Math.random()*500)+500);
						$("div#contdown div#downloading div#progress").fadeIn((Math.random()*500)+500);
							if(lastprogress == $(element).parent().children("label#progress").text() && $(element).parent().children("label#progress").text() != "100%" && checkdownloader == 0) {
							$("#action-check-status").load("checkdownloader.php?ie="+Math.random());
							checkdownloader = 1;
							}
							
							lastprogress = $(element).parent().children("label#progress").text();
						}
						
						if(downloaderactive == 0) {
							
							$('.'+$(element).parent().children("label#uniq").text()).remove();
							downloaderactive = 1;
							lastprogress = $(element).parent().children("label#progress").text();
							activeuniq = $(element).parent().children("label#uniq").text();

							newdiv = QueueDiv($(element).html(), $(element).parent().children("label#uniq").text(), $(element).parent().children("label#size").text(),'downloader','',$(element).parent().children("label#progress").text(),$(element).parent().children("label#link").html());
							$("#contdown").append(newdiv);
							$(newdiv).fadeIn((Math.random()*500)+1000);
							$("#contdown").fadeIn((Math.random()*500)+1000);
						}
					});

					$("div#load > div#size-update").children("span#file").children("label#size").filter(function() {

						$("." + $(this).parent().children("label#uniq").text() + " > .size").text($(this).text());

					});

					
	});

	lastupdate = lastupdate + checkeverysec;
}, checkeverysec * 1000);

		
}	 
	 
});



function reloadlogspage(handler) {
$("#navipageajaxgif").html("<img src=\"styles/zajepro/images/icons/ajaxgif2.gif\" class=\"naviajaxgif\">");

var page = $(handler).children(":selected").text();

$("#logs-list").load("showlogs.php?ie="+Math.random()+"&page="+page,function() {
				$("#navipageajaxgif").html("");
				});

}

function QueueDiv(filename, uniq, size, typ, time, progress, linkcode) {
var newdiv = $("<div>");
newdiv.attr("id",typ+"_row");
newdiv.attr("class",uniq);

var newdiv2 = $("<div>");
newdiv2.attr("id",typ+"_name");
newdiv2.html(filename);

var newdiv3 = $("<div>");
newdiv3.attr("id",typ+"_size");
newdiv3.attr("class","size");
newdiv3.html(size);

var newdiv4 = $("<div>");
newdiv4.attr("id",typ+"_adds");
newdiv4.html("<a href=\"#delete\" onclick=\"javascript:delone(this);\"><img src=\"styles/zajepro/images/icons/btn_delete.png\"></a> <a href=\"#showlinks\" onclick=\"javascript:httpone(this);\"><img src=\"styles/rapidscript/images/btnhttp.png\"></a> ");

if(typ == 'files') {
var newdiv5 = $("<div>");
newdiv5.attr("id",typ+"_time");
newdiv5.html(time);

var linkhref = $(linkcode+" > input[class=server]").attr("value");
newdiv2.html("<a href=\""+linkhref+"\">"+filename+"</a>");
}

if(typ == 'downloader') {
var newdiv7 = $("<div>");
newdiv7.attr("id","progress");
newdiv7.html(progress);
}

var sep1 = $("<div>");
sep1.attr("id","sepfiles");

var sep2 = $("<div>");
sep2.attr("id","sepfiles");

var sep3 = $("<div>");
sep3.attr("id","sepfiles");

var checkbox1 = $("<input>");
checkbox1.attr("id","f"+uniq);
checkbox1.attr("name","f"+uniq);
checkbox1.attr("type","checkbox");
checkbox1.attr("value","ON");

var newdiv6 = $("<div>");
newdiv6.attr("id",typ+"_checkbox");
newdiv6.append(checkbox1);

var newdiv8 = $("<div>");
newdiv8.attr("id",typ+"_link");
newdiv8.attr("class","link");
newdiv8.html(linkcode);


if(typ != 'downloader')
newdiv.append(newdiv6);
newdiv.append(newdiv2);
newdiv.append(sep1);
newdiv.append(newdiv3);
newdiv.append(sep2);
if(typ == 'files') {
newdiv.append(newdiv5);
newdiv.append(sep3);
}
if(typ != 'downloader')
newdiv.append(newdiv4);

if(typ == 'downloader')
newdiv.append(newdiv7);

if(typ == 'downloader')
newdiv.attr("id","downloading");

newdiv.append(newdiv8);

newdiv.css("display","none");

return newdiv;

}

function delone(sender) {
		if(confirm("Czy aby na pewno chcesz usunąć ten plik?")) {
			
			todel = todel + '[f'+ $(sender).parent().parent().attr("class") +']';
			$(sender).parent().parent().fadeOut();
			$(sender).parent().parent().remove();
			
		  dname = ''+$(sender).parent().parent().attr("id");
		  var astring = new String(dname);
		  $("#action-del").load("delete.php?ie="+Math.random()+"&type="+ astring.charAt(0) +"&ids="+todel);
		  todel = '';
		}
		return false;
}

function httpone(sender) {

		$(sender).parent().parent().css("height","55");
		return false;
}


/*function addFilesInactive(button) {
button.disabled = true;
document.getElementById('addfiles_form').submit();
}*/
