/**
 * By FTM
 */

$(document).ready(function (){
	
	var dataType = "collection";
	try{
		
		if( $('#typeCont').val()==3){
			dataType = "user";
		}
	}catch(e){}
	
	$('#edit').live('click', function() {
	   	$('li.thread').each(function () 
	   	{
	   		var sid = $(this).attr('id');
	    		
	   		$('div#ec-' + sid + '.edit-comment').hide();
	    	$('textarea#text-' + sid).val('');
	    	$('div#u-' + sid + '.user').show();
	    	$('div#uc-' + sid + '.user-comment').show();
	   	});
	   	
	   	var id = $(this).attr('name'),
	   		comment = $('pre.comment-' + id).text();
	   	
	  	$('div#ec-' + id + '.edit-comment').show();
	   	$('textarea#text-' + id).val(comment);
	   	$('div#u-' + id + '.user').hide();
	   	$('div#uc-' + id + '.user-comment').hide();
	});
	
	$('#submit').live('click', function() 
	{
		var cmt = $('#comment').val(),
			id_image = $('#nav-image').prop('class').split('/');
						
		if(cmt != '' && cmt != 'Deja tu comentario')
		{
	        $('<div class="layer-stopEditComment">').prependTo('#to-comment');
	        $.post(CI_ROOT + dataType+'/add_comment/' + id_image[1], {comment: cmt}, function (e)
			{
				if(e=="nologin"){
					
					window.open(CI_ROOT+'login/index/'+dataType+'/image/'+$('#id_item').val()+'/12/'+$('#id_image').val()+'/1',"_self");
				}else{
					 var show = $('#comments > p > a#show_allComments').attr('title');
		            if(show == "0")
		            	$('#comments').load(CI_ROOT +dataType +'/show_comments/' + id_image[1] + '/-1' + '/' + new Date().getTime());
		            else
		            	$('#comments').load(CI_ROOT + dataType+'/show_comments/' + id_image[1] + '/3' + '/' + new Date().getTime());
		            $('#comment').val('Deja tu comentario');
				}
	           
	        });
	    }
        return false;
    });
	
	$('#cancel').live('click', function() 
	{
	   	var id = $(this).attr('name');
		    	
	   	$('div#ec-' + id + '.edit-comment').hide();
	   	$('textarea#text-' + id).val('');
	   	$('div#u-' + id + '.user').show();
	   	$('div#uc-' + id + '.user-comment').show();
    });
	
	$('#save').live('click', function()
	{

		var id = $(this).attr('name'),
			comment = $('textarea#text-' + id).val();
			
	    if(comment != '' && comment != 'Deja tu comentario')
	    {
			$('<div class="layer-stopEditComment">').prependTo('div#ec-' + id);
	        $.post(CI_ROOT + dataType+'/save_comment', {id_comment: id, comment: comment}, 
	        	function(e)
	        	{
	        		if(e=="nologin"){
						window.open(CI_ROOT+'login/index/'+dataType+'/image/'+$('#id_item').val()+'/12/'+$('#id_image').val()+'/1',"_self");
					}else{
						$('#comments li#' + id + '.thread').load(CI_ROOT +dataType +'/reload_comment/' + id);
					}
	        		
			    }
	        );
		}
			
		    return false;
	});
	$('#delete').live('click', function()
	{
		var id = $(this).attr('name'),
			id_image = $('#nav-image').prop('class').split('/');
						
		$('<div class="layer-stopEditComment">').prependTo('div#ec-' + id);
		$.post(CI_ROOT + 'item/delete_comment', {id_comment: id}, 
			function(e)
			{
				if(e=="nologin"){
					window.open(CI_ROOT+'login/index/'+dataType+'/image/'+$('#id_item').val()+'/12/'+$('#id_image').val()+'/1',"_self");
				}else{
					var show = $('#comments > p > a#show_allComments').attr('title');
			
					if(show == "0")
						$('#comments').load(CI_ROOT +dataType+ '/show_comments/' + id_image[1] + '/-1' + '/' + new Date().getTime());
				    else
				       	$('#comments').load(CI_ROOT +dataType +'/show_comments/' + id_image[1] + '/3' + '/' + new Date().getTime());
				}
				
		    }
		);
		
		return false;
	});
});
