/**
 * Comments object for public views
 *
 * @author Henri Meltaus
 * @version 1.0.1
 * 
 * prototype (http://www.prototypejs.org/) JavaScript library is required
 */
 
CommentsPublic = Class.create(); 
CommentsPublic.prototype = {

    commentsModuleId : null,
    target : null,
    params : null,
    language : "FI",
    url : "/portal/",
    messages : {
        "FI" : {"user_required" : "Nimimerkki on pakollinen tieto",
                "content_required" : "Kommenttiteksti puuttuu"},
        "EN" : {"user_required" : "Name is required",
                "content_required" : "Comment text is required"}                          
    },
    

    /**
     * Initializes comments
     *
     * @param commentsModuleId Category id of comments module
     * @param target Target element where comments is added, possible values are:
     *     - a string containing id of target element
     *     - object reference to target element (for example: div-element)
     * @param namespace Target namespace
     * @param targetId Id of target that is commented
     * @param params Optional paramters in array, possible values are:
     *     
     *     namespace       = target namespace
     *     targetId        = id of target whose comments are shown
     *     itemsPerPage    = how many items are displayed per page  
     *     sortDir         = sorting direction (asc|desc)
     *     sortProperty    = sorting property (date|visible|n.name|rating|user|content)
     */
    initialize : function (commentsModuleId, target, namespace, targetId, params) {
	
	    this.commentsModuleId = commentsModuleId;
        this.target = (typeof(target) == "string") ? $(target):target;
        this.params = (params != null) ? params:{};
	
		this.setProperty("namespace", namespace);
		this.setProperty("targetId", targetId);
		
		this.render();
    },


    /**
     * Renders comments area
     *
     * @param page Page number (optional, default is 1)
     */
    render : function (page) {

		if (page == null || page < 1) {
			page = 1;
		}

		this.setProperty("page", page);        
	    var queryString = "a=shared-listComments&visible=true&" + this.getQueryString();
		
        this.ajax(queryString);          
    },


    /**
     * Submits new comment
     *
     * @param content Content of comment
     * @param user User 
     * @param rating Rating
     */
    submitComment : function (content, user, rating) {
        
        if (content == null) content = "";
        if (user == null) user = "";
        if (rating == null) rating = "0";
    
    	if (user == "") {
    		alert(this.messages[this.language]["user_required"]);
    		return;
    	}

    	if (content == "") {
    		alert(this.messages[this.language]["content_required"]);
    		return;
    	}
    
    	// Replace &-characters
    	user = user.replace(/&/g,"%26");
    	content = content.replace(/&/g,"%26");
    
        var queryString = "a=shared-saveComment&visible=true";
	    queryString += "&content=" + encodeURI(content) + "&user=" + encodeURI(user) + "&rating=" + rating;
        queryString += "&" + this.getQueryString();
        
        this.ajax(queryString);    
    },
    
    
    /**
     * Creates ajax request
     *
     * @param url Target url
     * @param params Parameter string
     */
    ajax : function (params) {
    	this.busyArea(this.target);
    	if ( params != '' ) params = '?' + params;
    	
    	var qs = this.url + this.commentsModuleId + params + '&noRedirect=true';
    	jQuery( '#' + this.target.id ).load( qs );
    },
    
    
    /**
     * Indicates that element is busy
     */
    busyArea : function (n) {
		n.innerHTML = '<div style="background:transparent url(/pics/contentmanager/Comments/loading.gif) no-repeat top center"><div style="opacity:0.4; filter: alpha(opacity=40)">'+n.innerHTML+'</div></div>';
	},
	
	
	/**
	 * Creates rating selection
	 *
	 * @param target Id of target element where rating selector is placed
	 * @param maxRating Max rating value
	 */
    createRating : function (target, maxRating) {
        new Starry(target, {maxLength: maxRating, startAt: 0, name:'rating', id: 'rating'});
    },
    
    
    /*
     * Sets language
     *
     * @param lang Language code, f.ex. "FI"
     */
    setLanguage : function(lang) {
        if (lang == "") lang = "FI";
        this.language = lang.toUpperCase();
    },
    
    
    /**
     * Sets property of parameters. If parameters does not contain property,
     * it is created.
     *
     * @param name Name of property
     * @param value New value
     * @return reference to itself
     */
    setProperty : function (name, value) {
        this.params[name] = value;
        return this;
    },
    
       	
    /**
     * @return Query string generated from parameters
     */
    getQueryString : function() {
        return $H(this.params).toQueryString(); 
    }	
}