/* 
    WorldCasinoDirectory comments class
    Comments.js
    All rights reserved.
    
    contains
	    WCD.Comments.addCommentBox

*/

/*extern WCD, Ext */

// first, declare the namespace if it does not exist
if (WCD == null || typeof(WCD) != "object") { var WCD = {}; }

//WCD.Comments = {};
WCD.Comments = function () {

    // return a public interface
    return {
    
        init : function(){
		this.addBehaviors();
        },
	
	addBehaviors : function() {
		Ext.addBehaviors({
			// add a listener
			'#wcdCommentSubmitButton@click' : this.submitComment
		});
		
		//attach the voting up/down behaviors
		Ext.select('img[@id*=wcdCommentVoteDown-]').on('click', this.vote );
		Ext.select('img[@id*=wcdCommentVoteUp-]').on('click', this.vote );
	},
		
	splitDocumentUrl : function() {
		var here = document.location.href;
		//extract the protocol, host and path 
		//and create a location object with the data 
		var parts = here.replace('//', '/').split('/'); 
		var loc = { 
			'protocol' : parts[0], 
			'host' : parts[1] 
		} 
		return loc;
	},
	
	vote : function (e) {
		e.preventDefault();
		var target = e.getTarget();
		
		var parts = target.id.split('-'); 
		var voteDownId = parts[1];
		
		var voteUpDownInteger;
		if(parts[2] == 'up') { voteUpDownInteger = '1'; }
		else { voteUpDownInteger = '-1'; }

		//var loc = this.splitDocumentUrl();
		var here = document.location.href;
		//extract the protocol, host and path 
		//and create a location object with the data 
		var parts = here.replace('//', '/').split('/'); 
		var loc = { 
			'protocol' : parts[0], 
			'host' : parts[1] 
		} 

		var posturl = 'http://'+loc['host']+'/comments';
		var con = new Ext.data.Connection();
		
		con.request({
			url: posturl,
			method: 'post',
			params: { 	'action' : 'vote',
					'vote' : voteUpDownInteger,
					'commentid' : voteDownId
			},
			callback: function(opts,success,response) {
				var voteResponse = response.responseText.split(':')
				
				if(voteResponse[0] == 'voted') {
					WCD.Comments.doVoteFade(voteResponse[1], voteResponse[2]);
					WCD.Comments.disableVoteButtons(voteResponse[1]);
					return false;
				} else if(response.responseText == 'toomanyuser') {
					alert('Sorry, you\'ve exceeded the maximum 15 minute voting rate. Please try again in a little while.');
				} else if(response.responseText == 'toomanyip') {
					alert('Sorry, your computer or network\'s IP address has exceeded the maximum 15 minute voting rate. Please try again in a little while.');
				} else {
					alert('Sorry, there was an error processing your vote!');
				}

				return false;
			}
		});
	},
	
	submitComment : function() {
		
		//disable the input elements once button is clicked
		Ext.get('wcdCommentTextArea').dom.disabled = true;
		Ext.get('wcdCommentSubmitButton').dom.disabled = true;
		
		var con = new Ext.data.Connection();
		
		//var loc = this.splitDocumentUrl();
		var here = document.location.href;
		//extract the protocol, host and path 
		//and create a location object with the data 
		var parts = here.replace('//', '/').split('/'); 
		var loc = { 
			'protocol' : parts[0], 
			'host' : parts[1] 
		} 

		var posturl = 'http://'+loc['host']+'/comments';
		con.request({
			url: posturl,
			method: 'post',
			params: { 	'action' : 'add',
					'type' : Ext.get('wcdCommentType').getValue(),
					'body' : Ext.get('wcdCommentTextArea').getValue(),
					'item' : Ext.get('wcdCommentItemId').getValue()
			},
			callback: function(opts,success,response) {
				if(response.responseText == '1') {
					WCD.Comments.showSubmittedComment();
					return false;
				} else if(response.responseText == 'toomanyuser') {
					alert('Sorry, you\'ve exceeded the maximum hourly user comment rate. Please try again in a little while.');
				} else if(response.responseText == 'toomanyip') {
					alert('Sorry, your computer or network\'s IP address has exceeded the maximum hourly comment rate. Please try again in a little while.');
				} else {
					alert('Sorry, there was an error submitting your comment!');
				}
				
				//re-enable the input elements
				Ext.get('wcdCommentTextArea').dom.disabled = false;
				Ext.get('wcdCommentSubmitButton').dom.disabled = false;

				return false;
			}
		});
	}
    };
};

WCD.Comments.showSubmittedComment = function() {

	this.hideReplyBox();
}

WCD.Comments.hideReplyBox = function() {
	var replyBox = Ext.get('wcdCommentReplyBox');
	//blind closed the reply
	replyBox.setHeight(0, {
			duration: 1,
			callback: WCD.Comments.showCommentBox
			}
	);
}

WCD.Comments.showCommentBox = function() {
	var submittedBox = Ext.get('wcdCommentSubmittedBox');
	var submittedCommentBody = Ext.get('wcdCommentSubmittedBody');
	var commentTextArea = Ext.get('wcdCommentTextArea');
	if(submittedCommentBody) {
		submittedCommentBody.update(commentTextArea.getValue().replace(/\r\n/g, "<br />").replace(/\r/g, "<br />").replace(/\n/g, "<br />"));
	} else { alert('error: id wcdCommentSubmittedBody not found'); }
	submittedBox.setHeight(0);
	submittedBox.enableDisplayMode();
	submittedBox.show();
	//blind open the new comment
	submittedBox.autoHeight(true, 1);
}

WCD.Comments.doVoteFade = function(commentid, newtotal) {
	var ratingEl = Ext.get('wcdCommentVotes-'+commentid);
	ratingEl.setOpacity(0, {
				duration: 1,
				callback: function() {
					var prefix;
					if(newtotal > 0) { prefix = '+'; } else { prefix = ''; }
					ratingEl.update(prefix+newtotal+' votes');
					ratingEl.setOpacity(100, {duration: 1});
				} 
			}
	);
}


WCD.Comments.disableVoteButtons = function(commentid) {
	var buttonEl = Ext.get('wcdCommentVoteButtons-'+commentid);
	buttonEl.setOpacity(0, {
				duration: 1,
				callback: function() {	
					buttonEl.update('<img class="wcd-comment-vote-down-active" width="18" height="18" style="margin-top: 2px; margin-right: 3px; cursor: default;" alt="Vote down" src="http://www.worldcasinodirectory.com/images/vote-down-off.png"/><img class="wcd-comment-vote-up-active" width="18" height="18" alt="Vote up" style="margin-top: 2px; cursor: default;" src="http://www.worldcasinodirectory.com/images/vote-up-off.png"/>');
					buttonEl.setOpacity(100, {duration: 1});
				} 
			}
	);
}