var Comments =
{
	pages : new Array(),
	current_page  : 1,
	current_video : null,
	request_url : '/comments/listbyvideo/hash/',
	parent_dom  : 'comment_container',
	dom_prefix  : 'comments_page_',

	filter  : function(item) { var regexp = new RegExp(Comments.dom_prefix); return (item.id.match(regexp)); },
	set_nav : function(item) {
								if (Comments.filter(item)) 
									{ $('#'+item.id).click( function() { Comments.load(this); return false;} );}
							 },
	set_page : function(item) {
								if ('next' == item) { Comments.current_page ++; }
								if ('prev' == item) { Comments.current_page --; }
								if ( !isNaN(item) ) { Comments.current_page = item; }
							  },
	get_page : function() { return '/page/'+ Comments.current_page; },
	get_link : function() { return Comments.request_url + Comments.current_video + Comments.get_page(); },
    load : function(item) {
							if (item)
                            {
                                Comments.set_page( item.id.split(Comments.dom_prefix)[1] );
                            }
                            
							$('#'+Comments.parent_dom).load(
                                    Comments.get_link(), 
                                    '', 
                                    function(){
                                        Comments.initialize(); 
                                        //CommentForm.init(); 
                                        
                                    });
						  },
	initialize : function(param) {
                                    if (param)
                                    {
                                        Comments.current_video = param.hash;
                                    }
									$('#paginator').children().filter( function(){ Comments.set_nav(this) } );
									
									$('#comment').focus(function(){
										if ($(this).text() == 'Deja tu comentario...')
											$(this).text('');
									});
									$('#comment').blur(function(){
											//$(this).text('Deja tu comentario...');
									});
                                     try
                                        {
                                            CommentDeleteAction.add_bind();
                                        }catch(e){
                                            
                                        }
								 }
}

var CommentForm = 
{
    action : null,
    container: null,
    id_dom:null,
    temp:null,
    data_send:'',

    submit : function()
    {
        this.temp = $("#"+this.id_dom).find(':input');
        this.temp.each( function (){ CommentForm.data_send += this.name+'='+escape(this.value)+'&';});
        this.send();
    },
    send : function()
    {
            $.post(this.action, CommentForm.data_send, function(data){ 
                                        CommentForm.callback(data);
                                    } ,'html');
    },
    callback:function(data){
               $('#'+this.container).html(data);
                this.init();
                Comments.load();
    },
    init: function(param){
       this.data_send = '';
        if(!this.action){
            this.action     = param.action;
            this.container  = param.container;
            this.id_dom     = param.id_dom;
         }
          try
            {
                Captcha.init();
            }catch(e){
                
            }  
        $('#'+this.id_dom).submit( function(){
        			// verificar el valor del input
        	
		    		try{
							if ($('#comment').text() == 'Deja tu comentario...'){
								//$('#comment').text('');
								alert('Debe escribir un comentario');
								$('#comment').focus();
								return false;
							}
		    		}catch(e)
		    		{
		    			alert('error');
		    		}
        	
                    CommentForm.submit();
                    return false;
        });
    }
}

var CommentDeleteAction= {
    message:'',

		callback:function(data){
        
		$(data).each(
                function() {	
                if($(this).attr('class')== 'error')
                {
                    if($(this).text()=='ok')
                    {
                        CommentDeleteAction.message = "El comentario ha sido eliminado";
                        Comments.load();
                    }
                    else
                        CommentDeleteAction.message = "No se pudo eliminar "+$(this).text();
                    alert(CommentDeleteAction.message);
                }
               }
            );
		},
		add:function (){
            if(confirm("¿ Está seguro de eliminar el comentario?"))
            {
                $.post(this.href,CommentDeleteAction.callback);
            }
			return false;
	
		},
		add_bind:function(){
			$("#comment_container a.delete").bind("click", CommentDeleteAction.add);
		}
}

$(document).ready( 
	function()
	{
            Comments.initialize(jsonParameters);
            CommentForm.init({"action":"/comments/add","id_dom":"dom_comment_form", "container":"comment_form"});
		//	Rating.initialize(jsonParameters);
            CommentDeleteAction.add_bind();
       
       
	}
);
