var Comments =
{
	pages : new Array(),
	current_page  : 1,
	current_video : null,
	request_url : '/comments/listbyvideo/hash/',
	parent_dom  : 'comments-box',
	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); } );}
							 },
	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;
                                    }
									$('#paginador').children().filter( function(){ Comments.set_nav(this) } );
                                     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;
         }
        $('#'+this.id_dom).submit( function(){ 
                    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);
		}
}
