
YAHOO.namespace('moviezen'); 

var pagelinks;
var sortlinks;
var genrelinks;

YAHOO.moviezen.PageLinks = function (type, id) { 
    this.type = type;
    this.id = id;
    this.bind();
};
YAHOO.moviezen.PageLinks.prototype = {
    states: { READY : 0, LOADING : 1 , TIMEOUT : 2, FAILURE : 3 },
    bind: function() {
        this.state = this.states.READY;
	if (getEl('top-comments-index')) {
	        var pages = getEl('top-comments-index').getElementsByTagName('a');
    		YAHOO.util.Event.addListener(pages, "click", this.fnCallback, this); 
        	pages = getEl('bottom-comments-index').getElementsByTagName('a');
    		YAHOO.util.Event.addListener(pages, "click", this.fnCallback, this); 
       }
    },
    fnCallback: function(e, el) {
        YAHOO.util.Event.stopEvent(e);
        if (el.state == el.states.LOADING) {
            return;   
        }
        el.state = el.states.LOADING;
        el.makeBrowseRequest(this.getAttribute('page'));
        //this.makeBrowseRequest(this.getAttribute('genre'));
        getEl("comments").innerHTML = "<p class=\"loading\">Loading comments...</p>";
    },
    makeBrowseRequest: function(page){
        var postData = "page="+page+"&id="+this.id+"&type="+this.type;
        //alert("postdata=" + postData);
        //alert("language = " + this.language);
    	var request = YAHOO.util.Connect.asyncRequest('POST', '/talk/getcomments', browseCallback, postData);
    }
};

    var browseSuccess = function(o){
        //alert("Got response = " + o.responseText);
        getEl("comments").innerHTML = o.responseText;
        pagelinks.bind();
    };

    var browseFailure = function(o){
        getEl("comments").innerHTML = "There was a temporary issue - please refresh your browser and try again";
        pagelinks.state = pagelinks.states.READY;
    };

    var browseCallback =
    {
      success:browseSuccess,
      failure:browseFailure,
      argument:['foo','bar']
    };




    YAHOO.moviezen.TalkWidget = function (talkCfg) { 
        this.init(talkCfg);
    };
    YAHOO.moviezen.TalkWidget.prototype = {
        init: function(talkCfg) {
            var oThis = this;
            
            // Create the config object
     		this.cfg = new YAHOO.util.Config(this);
            this.cfg.addProperty("formId", {value:'comment_form'});
            this.cfg.addProperty("textareaId", {value:'comment_text'});
            this.cfg.addProperty("countId", {value:'comment_char_count'});
            this.cfg.addProperty("submitId", {value:'comment_post_it'});
            this.cfg.addProperty("url", {value:'/talk/actor'});
            this.cfg.addProperty("limit", {value:1000});
            this.cfg.addProperty("minChars", {value:10});
            this.cfg.addProperty("guestAllowed", {value:1});

            getEl(this.cfg.getProperty("textareaId")).disabled = false;
            getEl(this.cfg.getProperty("textareaId")).value = ''; 
            getEl(this.cfg.getProperty("submitId")).disabled = false;

            var links = YAHOO.util.Dom.getElementsByClassName('commentpost', 'a');
            YAHOO.util.Event.addListener(links, "click", show_comment_box_bottom);
            	    	
            if(talkCfg) {
     			this.cfg.applyConfig(talkCfg);
 	    	}
 	    	
            YAHOO.util.Event.addListener(this.cfg.getProperty("submitId"), "click", this.handleSubmit, this); 
        },
	    handleCount : function() {
	        //alert("lenght of textarea=" + getEl(this.textareaId).value.length);
    		if (getEl(this.cfg.getProperty("textareaId")).value.length <= this.cfg.getProperty("limit")) {
	    		getEl(this.cfg.getProperty("countId")).innerHTML = this.cfg.getProperty("limit")-getEl(this.cfg.getProperty("textareaId")).value.length;
		    	return true;
    		} else {
	    		alert("Please keep your comments under "+this.cfg.getProperty("limit")+" characters.  Thanks.");
		    	getEl(this.cfg.getProperty("textareaId")).value = getEl(this.cfg.getProperty("textareaId")).value.substring(0, this.cfg.getProperty("limit"));
			    return false;
    		}
	    },
	    
    	handleSubmit : function(e, el) {
    	    YAHOO.util.Event.stopEvent(e);

	    if (current_user == '') {
                if (el.cfg.getProperty("guestAllowed") == 0) {
			alert("You need to sign-in or register before posting a message");
	                return false;
                }

		if (getEl("catpchaResponse").value.length == 0) {
			alert("Please type in the characters corresponding to the image ");
			return false;
	        }

		if (getEl("postName").value.length == 0) {
			alert("Please type in your name ");
			return false;
	        }
		if (getEl("postName").value.length > 16) {
			alert("Your name cannot be more than 16 characters ");
			return false;
	        }

            }

	if (getEl(el.cfg.getProperty("textareaId")).value.length <= 
	el.cfg.getProperty("minChars")) {
	alert("Your comment needs to be at least " + el.cfg.getProperty("minChars") + " characters long");
	return false;
}

            YAHOO.util.Connect.setForm(el.cfg.getProperty("formId"));
            var request = YAHOO.util.Connect.asyncRequest('POST', el.cfg.getProperty("url"), 
        	                                              {
        	                                               success: function(o) {
                                                                if (o.responseText == -1) {
                                                                    alert("Your comment could not be submitted");
                                                                } else if (o.responseText == -2) {
    alert("Please type in the characters in the image");
    getEl(el.cfg.getProperty("textareaId")).disabled = false;
    getEl(el.cfg.getProperty("submitId")).disabled = false;

} else if (o.responseText == -3) {
    alert("Please enable cookies on your machine");
    getEl(el.cfg.getProperty("textareaId")).disabled = false;
    getEl(el.cfg.getProperty("submitId")).disabled = false;
} else {
        
        var sURL = unescape(window.location.pathname);
        window.location.href = sURL;


                                                                    //alert("Your comment has been succesfully submitted");
                                                                }
        	                                                
        	                                                },
        	                                                failure : function(o) {
        	                                                    alert("Your comment could not be submitted");
        	                                                }
        	                                               });
    	    getEl(el.cfg.getProperty("textareaId")).disabled = true;
	    	getEl(el.cfg.getProperty("submitId")).disabled = true;
		    return true;
    	}
    };
    






    function show_comment_box_bottom(e, el)
    {
       YAHOO.util.Event.stopEvent(e);
	   if (current_user != '') {
         var box_elem = document.getElementById("comment_box_bottom");
         box_elem.style.display="block";
         
         getEl('comment_text').focus();
       } else { 
         show_login_error();
       } 
       return false;
    };
    
    function hide_comment_box_bottom()
    {
         getEl("comment_box_bottom").style.display="none";
         return false;
    };

    YAHOO.util.Event.onDOMReady(
        function() {
            var links = YAHOO.util.Dom.getElementsByClassName('commentpost', 'a');
            YAHOO.util.Event.addListener(links, "click", show_comment_box_bottom);
        }
    
    );    
