/*
// 	Wall post manipulation script
//  uses ajax method to write and read to wall databases (for users, venues, and events)
//  deletion behavor are attached to an achor of this form:  <a class="walldeletelink" id="123:user">delete</a>
//  @requires jquery 1.2
//  @author Nathan Letsinger
//  @version 2.0
//  @todo: When deleting we should throw up a modal window to confirm the deletion.
*/
function ajaxdelete() {
							
							// @TODO : confirm deletion with model window
							var answer = confirm("Are you sure? There is no undo!");
							if (!answer) return false;

							// get the anchor's id and extract it's information
							var input = $(this).attr('id');
							var input_array = input.split(":");
							// we display our intention to delete the post
							var selector = 'div#post_'+input_array[0];
							$(selector).prepend('<div id="deletionmsg"><p  class="form_success_msg"><img title="deleting..." src="/public/images/ajax-loader-green.gif"  alt="loading..."/>deleting...</p></div>');
							// we initiate the ajax call, and 
							$.ajax({
					          	type: "POST", 
					            data: {wall_id : input_array[0] , wall_type : input_array[1] , owner_id : input_array[2]},
					            datatype: "html",
					            url: '/walls/ajaxdelete',  
					            timeout: 2000,
					            error: function() 
					            		{
					              			console.log("Failed to submit");
					              			$('div#deletionmsg').html("<p  class=\"form_error_msg\"><img  src=\"/public/shared/images/icons/exclamation.png\" alt=\"error\" /> Sorry, couldn't connect to the site. Maybe try it again.</p>");
					            		},
					            success: function(html) 
					            		{ 
					            			$('div#deletionmsg').html(html);
					              			if ($('p#deletionresponse').attr('name') == 'success') 
					              			{
					              			    $(selector).fadeOut(3000,function(){$(this).remove()});
					              			    
					         				}else{
					         					$('div#deletionmsg').fadeOut(3000,function(){$(this).remove()});
					         				}
					            		}
							}); 
							// by default - we'll always return false so it doesn't redirect the user.
          					return false;	
          										
			};



			function confirmDelete(){
			
				var answer = confirm('Delete post? There is no undo!');
				
				if (answer) {
					ajaxDelete;
					return false;
				}else{
					return false;
				}
			
			
			}
		


$(document).ready(function(){	

			// assign the deletion behavior to our delete anchors
			$('a.walldeletelink').bind('click',ajaxdelete);
		



		// when the form is submitted we use the jquery ajax method to send all the form data
		// then we display the new post or an error.
        $('#wallpostform').submit(function() {
          
          // display a msg saying we are writing the data
          var writingmsg = '<div id="writingmsg"><p class="form_success_msg"><img title="deleting..." src="/public/images/ajax-loader-green.gif"  alt="loading..."/>Writing...</p></div>';
          $('#wallposts').prepend(writingmsg);
          // now we're going to capture *all* the fields in the
          // form and submit it via ajax.
          
          // :input is a macro that grabs all input types
          var inputs = [];
          $(':input', this).each(function() {
            inputs.push(this.name + '=' + escape(this.value));
          })
          
          // now if I join our inputs using '&' we'll have a query string that we can also use for POST
          $.ajax({
          	type: "POST",
            data: inputs.join('&'),
            datatype: "html",
            url: this.action,
            timeout: 2000,
            error: function() {
              console.log("Failed to submit");
             	$('div#writingmsg').html('<p  class="form_error_msg"><img  src="/public/shared/images/icons/exclamation.png" alt="error" /> Sorry, could not connect to the site. Maybe try it again.</p>');
					            		
            },
            success: function(input) { 
              // update the value of the text field, display the new post (input), bind the delete action again, and remove the "user has no posts" msg if it exisits.
              $("textarea#walltext").val('Write another message on the wall...');
              $('div#wallposts').prepend(input).fadeIn(4000, function()
              			{
              			 	$('a.walldeletelink').bind('click',ajaxdelete);
             				$('div#writingmsg').remove();
              				$('p#nopostsmsg').remove();
              			}); 

            }
          }) // checkout http://jquery.com/api for more syntax and options on this method.
          
          // by default - we'll always return false so it doesn't redirect the user.
          return false;
        });

}); 