/*
 * Public JavaScripts for ShoutBox.
 * @author markos
 */

ShoutBoxPublic = Class.create();
ShoutBoxPublic.prototype = {
	    categoryId : null,
	    target : null,
	    params : null,
	    maxlength: 100,
	    expiretime: 10,
	    cookiename: "SB_ticker",
	    cookievalue: "SB",
	    language : "FI",
	    messages : {
	        "FI" : {"user_required" : "Nimimerkki on pakollinen tieto",
	                "content_required" : "Kommenttiteksti puuttuu",
	                "length_exceeded" : "Viestin maksimipituus on 100 merkkiä",
	                "please_wait" : "Odota hetki ennen uuden viestin lähettämistä"},
	        "EN" : {"user_required" : "Name is required",
	                "content_required" : "Comment text is required",
	                "length_exceeded" : "Messages maximum length is 100 merkkiä",
	                "please_wait" : "Please wait before sending new message"}                          
	    },
		
	    initialize : function (categoryId, target) {
		    this.categoryId = categoryId;
	        this.target = (typeof(target) == "string") ? $(target):target;
	        this.render();
	    },
	    
	    render : function () {
	    	this.busyArea(this.target);
	    	var element = this.target;
	    	RemoteShoutService.listShouts(this.categoryId,function(str){
	    		element.update(str);
	    	})
	    },
		
	    busyArea : function (element) {
	    	element.update('<div style="background:transparent url(/pics/contentmanager/ShoutBox/loading.gif) no-repeat top center"><div style="opacity:0.4; filter: alpha(opacity=40)">'+element.innerHTML+'</div></div>');
		},
		
		sendShoutout : function (shoutout_content,nickname) {
	    	this.busyArea(this.target);
	    	var element = this.target;
	    	RemoteShoutService.addShout(this.categoryId,nickname,shoutout_content, function(str){
	    		element.update(str);
	    	})
		},
		
		submitShoutout : function (shoutout_content,nickname) {
			var errors = "";
			if (shoutout_content == null || shoutout_content=="")
				errors+=this.messages[this.language]["content_required"]+"\n";
			if (nickname == null || nickname=="")
				errors+=this.messages[this.language]["user_required"]+"\n";
			
			if (shoutout_content != null && shoutout_content.length > this.maxlength)
				errors+=this.messages[this.language]["length_exceeded"]+"\n";
			
			if ( this.readCookie() != null ) {
				errors+=this.messages[this.language]["please_wait"]+"\n";
			}
			
			if (errors=="") {
				this.sendShoutout(shoutout_content,nickname);
				this.sendCookie();
			} else {
				alert(errors);
			}
		},
		
		sendCookie : function() {
			var date = new Date();
			date.setTime(date.getTime()+(this.expiretime*60*1000));
			var expires = "; expires="+date.toGMTString();
			document.cookie = this.cookiename+"="+this.cookievalue+expires+"; path=/";
		},
		
		readCookie : function() {
			var name = this.cookiename + "=";
			var tmp = document.cookie.split(';');
			for(var i=0;i < tmp.length;i++) {
				var c = tmp[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
			}
			return null;
		}
}



