//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Pressbox JS
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//Document Ready +++++
$(function() {		
	//Ticker Update Modal
	$("a.pressbox-edit").click(function (e) {
		e.preventDefault();
		$("#modal-pressbox").modal({ containerCss:{ width:660, height:425 }, opacity:50, overlayClose:true });
	});
	//Modal Character Limit
	$("#modal-pressbox textarea").keyup(function() {               
               var len = $(this).val().length;

                //Show the number of characters left
                if(120 - len <= 0){ 
                	//Too many characters
                	$(this).prev().text("Max");
                	$(this).prev().addClass("maxed"); 
                }
                else{
                	//Character length okay
                	$(this).prev().removeClass("maxed");
                	$(this).prev().text(120 - len);
                }
            });
   	//Set the initial length according to what's in the database
        $("#modal-pressbox textarea:first").focus();

         //Show/hide character limit badge
        $("#modal-pressbox textarea").focus(function() {
        	$(this).prev().show();
        	if ($(this).val().length == 120){ 
        		$(this).prev().addClass("maxed"); 
        		$(this).prev().text("Max"); 
        	} else {
        		$(this).prev().removeClass("maxed"); 
        		$(this).prev().text(120 - $(this).val().length);
        	}
        });
        $("#modal-pressbox textarea").blur(function() {
        	$(this).prev().hide();
        });
        //Pressbox Ajax --------------------------------
	$("a.pressbox-save").click(function(e) {
		e.preventDefault();
		
		//Clear stuff from previous submission
		$(".pressbox-saved").hide();	
		
		//Get the textarea values
		var dataToSend = $("#form-pressbox").serialize();
		dataToSend = dataToSend+"&direction=push";
		//Show the loading spinner
		$("#modal-pressbox .loading").show();
			
		//Send the message to the PHP script
		$.ajax({
			type: "POST",
		    	url: "ui/scripts/pressbox.php",
		    	data: dataToSend,
			    success: function(msg){
				    	//Hide the spinner
				    	$("#modal-pressbox .loading").hide();
				    	//Show success message
				    	$("#modal-pressbox .saved").show();	  	
					//Reload the page
					location.reload(true);				
			    }
		 });
	});
});

