/*********************************************************************************/
/**			copyright (c) PlanetCalc.com, 2007-2010			**/
/**		    	User activity functions 				 */
/*********************************************************************************/

function Commenter(outerdivid, url ) {
	this.OuterDivID = outerdivid;
	this.URL = url;

	this.SetTable = function ( tbl ) {
		this.Comments = tbl;
	}

	this.initdialog =  function (dialog) { 
		this.Dialog = dialog;
	}; 

	this.onchanged = function ( controlid ) {	
	}; 	

	this.onkeypressed = function ( controlid, evt ) { 
		return true; 
	};

	this.oncommand = function ( buttonid ) { 
		clearLastError(); 
		if ("ok" == buttonid) {
			if ( this.Dialog.Validate() ) {
				this.Send();
			}
		} else if ("cancel" == buttonid)
			this.Discard();
	}; 

	this.Send = function ( ) {
		if ( this.Dialog.Validate() ) {
			this.Dialog.Busy();
			BSMakePOSTRequest( this.URL, this, GetDialogData(this.Dialog) );			
		}
	}

	this.Discard = function ( ) {
		SetVisible(this, false);
	}                   

	function SetVisible(commenter, show) {
		var elem = document.getElementById(commenter.OuterDivID);
		elem.style.display = show ? "block" : "none";		
	}

	this.OnResponse = function( obj ) {
		this.Dialog.Free();
		ShowAndReload(this);
	}

	this.OnError = function( obj ) {
		this.Dialog.Free();
		this.ResetCaptcha();
		this.Dialog.ShowError(null, obj);
		return true;
	}                                    

	this.ResetCaptcha = function () {
		if ( commenter.Dialog.captcha ) {
			commenter.Dialog.captcha.SetValue("");
			document.getElementById(this.Dialog.GetElement().id + '_captcha_image').src = '/captcha/?' + Math.random();
		}
	}

	function GetDialogData(dialog) {
		var data = { "id" : dialog.comment_id.GetValue(),
			     "message" : dialog.message_text.GetValue(), 
			     "parent" : dialog.parent.GetValue() };					
		if (undefined != dialog.captcha) { 
			data.captcha = dialog.captcha.GetValue();
		}
		if ( undefined != dialog.sender ) {
			data.sender = dialog.sender.GetValue();
		}
	        return data;		
	}

	function ShowAndReload(commenter) {
		commenter.ResetCaptcha();
		commenter.Dialog.message_text.SetValue("");
		commenter.Comments.Reload();
	}
}    


function CommentsRenderer( placeholderid, maxNavigationPages ) {
	this.inheritFrom = RecordsetRenderer;
	this.inheritFrom(placeholderid, maxNavigationPages);

	var me = this;

	this.GetEmptyLines = function ( linesCount ) {
		return "";
	}

	this.CreateTableHeader = function ( sortcolumn, sortdirection, renderSorting ) {
		return "";
	}

	this.CreateRowData = function( rowclass, record ) {

		/* useful stuff
	    var property, propCollection = "";

    	for(property in record)
	    {
    	    propCollection += (property + ": " + record[property] + "\n");
	    }    
	    alert(propCollection);
		*/

		var data = '<tr>';
		data += '<td colspan=2 class="comment_created">';
		data += Spanner('comment_posted', me.Header.GetFormatter('posted') ? me.Header.GetFormatter('posted').Format('posted', record ) : record['posted']);
		if (me.Header.GetFormatter('spamVotes')) { //can report spam
			data += '<span class="spam_control">'; 
			if (me.Header.GetFormatter('spamDelete')) { //can delete spam
				data += me.Header.GetFormatter('spamDelete').Format('spamDelete', record );
				data += ' ';
			}
			data += me.Header.GetFormatter('spamControl').Format('spamControl', record ) + ' ' + record['spamVotes'] + '/' + record['spamLimit']
			data += '</span>';
		}
		data += "</td>";
		data += '</tr>';
		data += '<tr class="comment_data">';
		data += '<td class="author_info">';
		if (0 == record['role'])
			data += this.RenderAnonimousInfo(record);
		else {
			data += Paragrapher('nick', me.Header.GetFormatter('nickname') ? me.Header.GetFormatter('nickname').Format('nickname', record ) : record['nickname']);		
			data += Paragrapher('role', me.Header.GetFormatter('role') ? me.Header.GetFormatter('role').Format('role', record ) : record['role']);
			data += Paragrapher('photo', me.Header.GetFormatter('photo') ? me.Header.GetFormatter('photo').Format('photo', record ) : record['photo'])
			data += Paragrapher('regdate', me.Header.GetFormatter('created') ? me.Header.GetFormatter('created').Format('created', record ) : record['created']);
			data += Paragrapher('posts', me.Header.GetFormatter('posts') ? me.Header.GetFormatter('posts').Format('posts', record ) : record['posts']);		
		}
		data += '</td>';
		data += '<td class="message">';
		data += record['message'];		
		data += '</td>';
		data += '</tr>';
		return data;
	}	

	this.RenderAnonimousInfo = function( record ) {
		record['photo'] = '/img/none.jpg';
		var res = Paragrapher('nick', me.Header.GetFormatter('nickname') ? me.Header.GetFormatter('nickname').Format('nickname', record ) : record['nickname']);		
		res += Paragrapher('role', me.Header.GetFormatter('role') ? me.Header.GetFormatter('role').Format('role', record ) : record['role']);
		res += Paragrapher('photo', me.Header.GetFormatter('photo') ? me.Header.GetFormatter('photo').Format('photo', record ) : record['photo']);
		return res;
	}	

	function Paragrapher(classname, text) {
		return '<p class="'+ classname + '">' + text + '</p>';
	}

	function Spanner(classname, text) {
		return '<span class="'+ classname + '">' + text + '</span>';
	}
}


function ArtefactRater(url, contentid, artefactid) {
	this.ArtefactID = artefactid;
	this.ContentID = contentid;
	this.URL = url;
	var me = this;
	this.Forbid = false;
	this.Score = 0;
	this.Votes = 0;
	var rated_on_img = "/img/rated_on.gif";
	var rated_off_img = "/img/rated_off.gif";
	var rate_on_img = "/img/rate_on.gif";
	var rate_off_img = "/img/rate_off.gif";

	this.setInfo = function(score, votes) {
		this.Score = score;
		this.Votes = votes;
		var elem = document.getElementById(this.ContentID);		
		if (null == elem) {
			alert("Error message " + this.ContentID + " is not included by server-side code. Contact site support");
			return;
		}

		var loginDiv = FindChild(elem, "rating_login_id");
		if (null != loginDiv) 
			loginDiv.style.display = "none";

        var ratingScore = FindChild(elem, "rating_score_id");
		if (null != ratingScore)
			ratingScore.innerHTML = "(" + (new FormatterNumber(2)).Format(score) + ")";

        var ratingVotes = FindChild(elem, "rating_votes_id");
		if (null != ratingVotes)
			ratingVotes.innerHTML = votes;

		RefreshImages(elem, score, rated_on_img , rated_off_img );	
	}

	this.Disable = function() {
		this.Forbid = true;
	}

	this.setRate = function(score) {
		if (!this.Forbid)
			BSMakePOSTRequest( this.URL, this, {"id":this.ArtefactID, "mark" : score } );
	}

	this.setNormal = function() {
		var elem = document.getElementById(this.ContentID);		
		if (null == elem) {
			alert("Error message " + this.ContentID + " is not included by server-side code. Contact site support");
			return;
		}
		if (!this.Forbid)
			RefreshImages(elem, this.Score, rated_on_img, rated_off_img);	
	}

	this.setHighlight = function(score) {
		var elem = document.getElementById(this.ContentID);		
		if (null == elem) {
			alert("Error message " + this.ContentID + " is not included by server-side code. Contact site support");
			return;
		}
		if (!this.Forbid)
			RefreshImages(elem, score, rate_on_img, rate_off_img );	
		else {
			var loginDiv = FindChild(elem, "rating_login_id");
			if ( null != loginDiv ) {
				loginDiv.style.display = "inline";			
				loginDiv.style.position = "absolute";			
			}
		}
	}

	this.OnResponse = function( obj ) {
		me.setInfo( obj.score, obj.votes );
	}

	function RefreshImages(elem, score, setimage, resetimage) {
		for(var i=1; i<=5; ++i) {
			var rateimg = FindChild(elem, "rate" + i);
			if (null != rateimg)
				rateimg.src = (score >= i) ? setimage : resetimage;
		}						
	}

	function FindChild(parent, id) {
		var children = parent.childNodes;
		for( var i=0;i<children.length;++i ) {
			var elem = children[i];
			var attrs = elem.attributes;
			if (null != attrs) {
				var attr = elem.attributes.getNamedItem("id");
				if (null != attr) {
					if (attr.value == id)
						return elem;
				}
			}
			var out = FindChild(elem, id);
			if (null != out)
				return out;				
		}           	
		return null;		
	}
}

function FormTrackerHandler(url) {
	this.Dialog = null;
	this.URL = url;

	this.onchanged = function ( controlid ) {
		BSMakePOSTRequest( this.URL, this, GetDialogData(this.Dialog));			
	}

	this.initdialog =  function (dialog) {
		this.Dialog = dialog;
	}

	this.oncommand = function ( buttonid ) {
		clearLastError();
		return;
	}

	this.onkeypressed = function ( controlid, evt ) {
		return true;
	}                                   

	this.OnResponse = function( obj ) {
	}

	/* internal */
	function GetDialogData(dialog) {
	        return { "id" : dialog.item_id.GetValue() , "op" : dialog.op.GetValue() };		
	}
}    


function SpamControl(url, comments) {
	this.URL = url;
	var me = this;
	this.Comments = comments;

	this.Request = function ( op, id ) {
		BSMakePOSTRequest(this.URL, this, {"id" : id , "op" : op });
	}

	this.Report = function ( msgid ) {
		this.Request( "report", msgid );
	}

	this.Close = function ( msgid ) {
		this.Request( "close", msgid );
	}

	this.Delete = function ( msgid ) {
		this.Request( "delete", msgid );
	}

	this.OnResponse = function( obj ) {
		me.Comments.Reload();
	}

}    

function Requester(url) {
	this.URL = url;

	this.Request = function ( id ) {
		BSMakePOSTRequest(this.URL, this, {"id" : id});
	}

	this.Close = function ( id ) {
		this.Request( id );
	}

	this.OnResponse = function( obj ) {
		alert("Done");
	}
}    

