// Preload Images
var base = 'http://www.mrtv4.net.mm';
var img1 = new Image(16, 16);  
img1.src=base+"/img/front_images/spinner.gif";

var img2 = new Image(220, 19);  
img2.src=base+"/img/front_images/ajax-loader.gif";

var img3 = new Image(32, 30);
img3.src = base+'/img/front_images/cm_indicator.gif';

$(document).ready(function(){

	$('#comment').keypress(function(event) {
  		if (event.which == '13') {
     		event.preventDefault();
			$("#mycomment").submit();
   		}   
	});

	
	$("#mycomment").submit(function(){ 				
	var $form = $( this ),
        url = $form.find( 'input[name="url"]' ).val();

	var str = $(this).serialize(); 
	$.ajax({  
    	type: "POST",
    	url: base+"/comment",  // Send the login info to this page
    	data: str,  
    	success: function(msg){  
   		$("#mycomment").ajaxComplete(function(event, request, settings){   
 			 	$('input[name=title]').val('');
				$('textarea[name=comment]').val('');
				$('#comment').value='';
				$('#comment_response').html(img3);				
				$('#comment_response').fadeIn();				
				$('#comment_response').fadeOut();
				$('#comment_response').html('');																	        
 		}); 
		setTimeout( function(){ paginate(1); }, 1000); 		
 		},
	complete: function(msg){  
   		$("#mycomment").ajaxComplete(function(event, request, settings){   
 			 	$('input[name=title]').val('');
				$('textarea[name=comment]').val('');
				$('#comment').value='';
				$('#comment_response').html(img3);				
				$('#comment_response').fadeIn();				
				$('#comment_response').fadeOut();
				$('#comment_response').html('');																	        
 		}); 
		setTimeout( function(){ paginate(1); }, 1000); 		
 		}
	
  	});  
  
	// -- End AJAX Call --
	return false;

	}); // end submit event		
	

});

function paginate(pageNumber){						
				var controller = $('#controller').val();
				var id = $('#contid').val();				
				$('#loader').fadeIn();				
				$.post(base+'/comment', {page:pageNumber, id:id, controller:controller }, function(data){																																	
						setTimeout( function() { $('#show_comments').html(data); }, 2000);
						$('#show_comments').fadeIn();							
				});
			return false;							
}



function delComment(id, pageId){					
				$('#delete_'+id).hide();				
				$('.cm_del_'+id).html(img1);			
				$.post( base+'/comment', {deletecomment:id, delete:'DELETE'},
					 setTimeout( function(){ paginate(pageId); }, 1000) 	
					);						
}
