$(document).ready(function() {
 

	/*
	* Controls the opening and closing of the category tabs
	*/
	$("img.category_sign").toggle(function(){
	$(this).attr("src", "/img/site_images/minus.png");
	$("div#"+this.id+'s').show(); 
	}, function (){ 
	$(this).attr("src", "/img/site_images/plus.png"); 
	$("div#"+this.id+'s').hide();
	}); 
	/*
	* End Control for opening and closing of category tabs
	*/ 

	
	
	/* 
	* end Standard nzbnation post size and click functions 
	*/ 
	
	
$("input.check_change").click(function(){
	if ($(this).is(":checked"))
	{
		current_number = parseInt($("b.items").text()); 
		new_number = current_number + 1; 
		$("b.items").html(new_number); 	
		
		size = parseInt(this.id.match(/\d+/)); 
		current_size = parseInt($("b.size").text()); 
		new_size = current_size + size; 
		$("b.size").html(new_size); 
		
		div = $(this).parents("div:first");
		$(div).css({"background-color" : "#f68e8e"}); 
		
		
	}
	else if (!$(this).is(":checked"))
	{
		current_number = parseInt($("b.items").text()); 
		new_number = current_number - 1; 
		if(new_number == 0) {
			new_number = " 0"; 
		}; 
		$("b.items").html(new_number);
		$(this).removeAttr("checked");  
		
		current_size = parseInt($("b.size").text()); 
		size = parseInt(this.id.match(/\d+/)); 
		new_size = current_size - size; 
		if(new_size == 0)
		{
			new_size = " 0"; 
		}
		$("b.size").html(new_size); 

		div = $(this).parents("div:first");
		$(div).css({"background-color" : ""}); 
		
	}

	}); 
	
	/* 
	* end Standard nzbnation post size and click functions 
	*/ 
	
	
	
	
	/* 
	*Special download size and click function
	*/ 
	
	$("input.check_part").click(function(){
	if ($(this).is(":checked"))
	{
		current_number = parseInt($("b.items").text()); 
		new_number = current_number + 1; 
		$("b.items").html(new_number); 	
		
		size = this.id.split("_");  
	
		size = parseFloat(size[1]); 
		
		current_size = parseFloat($("b.size").text()); 
		new_size = current_size + size; 
		$("b.size").html(new_size); 
		
		div = $(this).parents("div:first");
		$(div).css({"background-color" : "#f68e8e"}); 
	//f68e8e 
		
	}
	else if (!$(this).is(":checked"))
	{
		current_number = parseInt($("b.items").text()); 
		new_number = current_number - 1; 
		if(new_number == 0) {
			new_number = " 0"; 
		}; 
		$("b.items").html(new_number);
		$(this).removeAttr("checked");  
		
		current_size = parseFloat($("b.size").text()); 
		
		size = this.id.split("_");  
		size = parseFloat(size[1]); 
		
		new_size = current_size - size; 
		if(new_size == 0)
		{
			new_size = " 0"; 
		}
		$("b.size").html(new_size); 
		
		div = $(this).parents("div:first");
		$(div).css({"background-color" : ""}); 
		
	}

	}); 
	
	/* 
	* End special download and click functions
	*/ 
	
	
	
	
	
	
	$("form#add_comment").submit(function(){
		
		//function for ajax comment system 
		var body = $("#body").val();
		var nzb_id = $("#nzb_id").val(); 
		var comment_id = $("#hidden_val").val(); 
		var maxCharacters = 500; 
		if(body === "")
		{
			//if the comment body is empty, do nothing
			$("#comment_error").html("Oops, please enter a comment..."); 
			$("#comment_error").show("medium"); 
			return false; 
		}
		else if(body.length > maxCharacters)
		{
			$("#comment_error").html("Whoops, a comment can only be 500 characters long!"); 
			$("#comment_error").show("medium");
			return false; 
		}
		else
		{
			
	$.post('/nzbs/add_comment/', { 'data[Comment][body]' : body, 'data[Comment][nzb_id]' : nzb_id, 'data[Comment][parent_id]' : comment_id} , function(data){
			$('div.add_comment_container').hide();
			$('p.empty').hide();  
			$('div.comments').append(data); 
		}); 
		 return false;
		 
		}	
		
	}); 
		
		//controls the image to be shown during ajax request
		$("div#show_on_ajax").ajaxStart(function(){
			  $(this).show(); 
			});
		//controls the image to be hidden after the ajax request is complete 
		 $("div#show_on_ajax").ajaxStop(function(){  
		 $(this).hide();  
			});  
			
			
		//hides the download link after it is finished completing
		$("a#download_link").click(function(){
			$(this).hide(); 
			$("td#download_td").html("Downloading file..."); 
			 
		}); 
			
	
		

		
		
		
 });

 
 
function showStatus(){
	window.status = 'Check All'; 
	return true; 
}


function add_bookmark(nzb_id){
	$.get("/bookmarks/add_bm/"+nzb_id, function(){
		$("a#add_bookmark_"+nzb_id).hide(); 
		$("img#add_bookmark_success_"+nzb_id).show(); 
		alert("Bookmark Successfully Added"); 
	})
}

function add_watch(wid) { 
	$.get("/searches/set_watch/"+wid, function(data){
		alert(data); 
		
	})
}

function checkAll(){
	$("input.check_change").attr("checked", "checked");
	size = $("input.check_change").size(); 
	$("b.items").html(size); 	
	$("div.nzb_odd").css({"background-color" : "#f68e8e"});
	$("div.nzb_even").css({"background-color" : "#f68e8e"});

	
	big_size = 0; 
	
	$("input.check_change").each(function(){
		
		size = this.id.split("_");  
	
		size = parseFloat(size[1]);
		
		big_size = big_size + size;  
		
	}); 
	
	$("b.size").html(big_size); 
	
}

function uncheckAll(){
	$("input.check_change").removeAttr("checked");
	
	$("b.items").html("0"); 
		
	
	$("div.nzb_odd").css({"background-color" : ""});
	$("div.nzb_even").css({"background-color" : ""});
	
	$("b.size").html(" 0"); 
	
			
}

function invert(){

	big_size = 0; 

	items_check = 0; 
	
	$("input.check_change").each( function() {
                $(this).attr('checked', !$(this).attr('checked'));
                
                	
                
                
                	if ($(this).is(":checked"))
                	{
                				
						div = $(this).parents("div:first");
						$(div).css({"background-color" : "#f68e8e"}); 
			
							size = this.id.split("_");  
						
							size = parseFloat(size[1]);
							
							big_size = big_size + size; 
							
							
							items_check = items_check + 1; 
					
                	}
                	else
                	{
                		div = $(this).parents("div:first");
						$(div).css({"background-color" : ""}); 
						
						
                	}      
                
            }); 
   			
            if(items_check == 0)
            {
            	items_check = " 0"; 
            }
            
            if(big_size == 0)
            {
            	big_size = " 0"; 
            }
            
            
            $("b.items").html(items_check); 
            
            $("b.size").html(big_size); 
            
            return false;
            
 

}


function new_checkAll(){
	$("input.check_part").attr("checked", "checked");
	size = $("input.check_part").size(); 
	$("b.items").html(size); 	

	$("div.nzb_odd").css({"background-color" : "#f68e8e"});
	$("div.nzb_even").css({"background-color" : "#f68e8e"});
	
	
		
	big_size = 0; 
	
	$("input.check_part").each(function(){
		
		size = this.id.split("_");  
	
		size = parseFloat(size[1]);
		
		big_size = big_size + size;  
		
	}); 
	
	big_size = Math.round(big_size * 100)/100; 
	
	$("b.size").html(big_size); 
	

}

function new_uncheckAll(){
		$("input.check_part").removeAttr("checked");
		$("b.items").html("0"); 
		
	
	$("div.nzb_odd").css({"background-color" : ""});
	$("div.nzb_even").css({"background-color" : ""});
	
	$("b.size").html(" 0"); 
}

function new_invert(){
	
	
	big_size = 0; 

	items_check = 0; 
	
	        $("input.check_part").each( function() {
                $(this).attr('checked', !$(this).attr('checked'));
                
                	if ($(this).is(":checked"))
                	{
                				
						div = $(this).parents("div:first");
						$(div).css({"background-color" : "#f68e8e"}); 
						
							size = this.id.split("_");  
						
							size = parseFloat(size[1]);
							
							big_size = big_size + size; 
							
							
							items_check = items_check + 1; 
						
                	}
                	else
                	{
                		div = $(this).parents("div:first");
						$(div).css({"background-color" : ""}); 
                	}
                
                
            }); 
            
            
            big_size = Math.round(big_size*100)/100; 
            
             if(items_check == 0)
            {
            	items_check = " 0"; 
            }
            
            if(big_size == 0)
            {
            	big_size = " 0"; 
            }
            

            
            
            $("b.items").html(items_check); 
            
            $("b.size").html(big_size); 
            
            
            return false;
}



function removeBookmark(bookmark_id, actual_bookmark_id){
	$("div#bookmark_"+bookmark_id).hide(); 
	$.get("/bookmarks/remove/"+actual_bookmark_id); 
}


//function that controls viewing more comments
function comment_request(nzb_id){
			
			$.get("/nzbs/view_more_comments/"+nzb_id, function(data){
				$("a#view_rest_link").hide(); 
				$("div.comments").append(data); 
			}); 
		
}

//function that determines how many characters are left when posting a comment
function characterCount(){ 

	var body = $("#body").val(); 
	var maxCharacter = 500; 
	var charactersLeft = 500 - body.length; 
	
	if(charactersLeft <= 0)
	{
		var charactersLeft = "0"; 
	}
	
	$("span.character_count").html(charactersLeft); 
} 


function getArticleReply(post_id, article_id){
	$.get("/updates/replies/"+post_id+"/"+article_id, function(data){
		$("div#"+post_id).append(data); 
		$("a#closelink_"+post_id).hide(); 
	}); 
}


function add_reply(comment_id, nzb_id){
	$("a#replylink_"+comment_id).hide(); 
	$("input#hidden_val").attr("value", comment_id); 
}

function get_nzb_reply(post_id, nzb_id){
	$("a#viewreply_"+post_id).hide(); 
	$.get("/nzbs/get_children/"+post_id+"/"+nzb_id, function(data){
		$("div#container_"+post_id).append(data); 
	})
}
