/**
 * name: com.aol.music.spinner.comments.js $LastChangedDate: 2009-06-02 17:20:28
 * -0400 (Tue, 02 Jun 2009) $ $Rev: 45492 $ author: Ali Hasan
 * 
 * dependencies: 1. jquery-1.32 2. com.aol.music.dynatrriapi.js
 */

if (typeof com === "undefined") {
	var com = {};
}
if (typeof com.aol === "undefined") {
	com.aol = {};
}
if (typeof com.aol.music === "undefined") {
	com.aol.music = {};
}
if (typeof com.aol.music.spinner === "undefined") {
	com.aol.music.spinner = {};
}

com.aol.music.spinner.Comments = function(options) {
	var self = this;

	this.settings = {
		firstPass : true,
		numberShown : 20,
		currentPage : 1,
		maxPage : 1
	};

	this.initModule = function(options) {
		this.assetUri = options.assetUri;
		this.div_container = $("div#commentsContainer");
		this.div_comments = this.div_container.find("div.comments")[0];
		this.div_pagination = this.div_container.find("div.commentsPagination")[0];
		this.ol_pagination = this.div_container.find("div.commentsPagination ol")[0];
		this.textbox_comments = this.div_container.find("textarea#commentBox")[0];
		this.button_add = this.div_container.find("div#addComment")[0];
		this.button_prev = this.div_container.find("div.prev")[0];
		this.button_next = this.div_container.find("div.next")[0];

		if (options.guid) {
			this.guid = options.guid;
			this.displayName = options.displayName;
		} 
		else {
			$(this.textbox_comments).bind("click", function(e) {
				$("#loginLink").trigger('click');
			});
		}

		$(this).bind("getCommentsSuccess", function(e) {
			self.processComments(e);
		});
		$(this.button_add).bind("click", function(e) {
			self.addAComment(e);
		});
		$(this.button_prev).bind("click", function(e) {
			self.prev(e);
		});
		$(this.button_next).bind("click", function(e) {
			self.next(e);
		});

		this.getComments(
			this.assetUri,
			{
				'start' : ((this.settings.currentPage - 1) * this.settings.numberShown),
				'count' : this.settings.numberShown	
			}
		);
	};

	this.goToPageClicked = function(e) {
		var pageLink = e.target;
		var page = parseInt($(pageLink).text());
		this.goToPage(page);
	};
	
	this.goToPage = function(page){	
		var scrollToTarget = parseInt($(this.div_container).position().top);
		$('body,html').animate({scrollTop:scrollToTarget}, 250, null, function(e){ 	
			$(self.div_comments).slideUp(250, function(e){
				self.getComments(self.assetUri, {
					'start' : ((page - 1) * self.settings.numberShown),
					'count' : self.settings.numberShown
				}); 
			});
		});
	};

	this.next = function() {
		if (this.settings.currentPage < this.settings.maxPage) {
			this.goToPage(this.settings.currentPage+1);
		}
	};

	this.prev = function() {
		if (this.settings.currentPage > 1) {
			this.goToPage(this.settings.currentPage - 1);
		}
	};

	this.processComments = function(commentsObj) {
		$(this.div_comments).empty();
		this.settings.currentPage = (commentsObj.start / this.settings.numberShown) + 1;
		var comments = commentsObj.comments;
		$.each(comments, function() {
			self.buildComment(this, "bottom");
		});

		// Set up pagination.
		if (this.settings.firstPass) {
			var numAvailable = commentsObj.numAvailable;
			this.settings.maxPage = Math.ceil(commentsObj.numAvailable / this.settings.numberShown);

			if (this.settings.maxPage > 1) {
				$(this.div_pagination).css( {
					'display' : 'block'
				});

				for ( var i = 1; i <= this.settings.maxPage; i++) {
					var li_p = $("<li />");
					if (i === 1) {
						$(li_p).addClass("first");
					} 
					else if (i === this.settings.maxPage) {
						$(li_p).addClass("last");
					}

					var a_p = $("<a />").attr({	'href' : '#commentsContainerTop' }).text(i).appendTo(li_p);
					a_p.bind('click', function(e) {
						e.preventDefault();
						self.goToPageClicked(e);
					});
					$(li_p).appendTo(this.ol_pagination);
				}
			}
			this.settings.firstPass = false;
			this.highlightCurrentPage();
		} 
		else {
			this.highlightCurrentPage();
			$(this.div_comments).slideDown(1000);			
		}
	};

	/*
	 * author authorDisplayName date reviewId reviewText
	 */
	this.buildComment = function(o, position) {
		var c = $("<div />").addClass("comment");
			/* create left description section */
			var cd = $("<div />").addClass("commentDesc").appendTo(c);
				var cd_bi = $("<img />").addClass("buddyIcon").attr(
					{	src : "http://api.oscar.aol.com/expressions/get?t="
							+ o.author
							+ "&f=native&type=buddyIcon&defaultId=00052b00003089",
						title : o.author
					}).appendTo(cd);
				var cd_un = $("<div />").addClass("userName").appendTo(cd);
					var cd_un_bp = $("<img />").addClass("buddyPresence").attr(
						{	src : "http://big.oscar.aol.com/"
								+ o.author
								+ "?on_url=http://o.aolcdn.com/art/spinner_interface_art/aimpresence-on.gif&off_url=http://o.aolcdn.com/art/spinner_interface_art/aimpresence-off.gif",
							title : "Online Status"
						}).appendTo(cd_un);
					var cd_un_dn = $("<span />").text(o.authorDisplayName).appendTo(cd_un);
				if (o.rawDate){
					try {
						var dte = new Date(parseInt(o.rawDate));
						o.date = getPrintDate(dte);
					} catch(err){}
				}
				var cd_dt = $("<div />").addClass("dateTime").text(o.date).appendTo(cd);
			
			/* create right main section */
			var ct = $("<div />").addClass("commentText").appendTo(c);
				if (o.reviewId){
					var umtSpan = $("<span />").addClass("umtLink").appendTo(ct);
						var reportCommentUrl = "/dyna/form/reportComment?reviewId="	+ encodeURIComponent(o.reviewId) + "&violator_url="	+ encodeURIComponent(document.location);
						var umt = $("<a />").addClass("commentReport").attr( 
								{ 	href : reportCommentUrl,
									target : "_new"
								}).appendTo(umtSpan).append(
										$("<img />").attr(
												{ 	src : "http://o.aolcdn.com/art/spinner_interface_art/interface-comment-vote-flag.gif",
													alt :"report this comment"
												}));
						$(umt).bind("click", function(e) {
							e.preventDefault();
							self.reportAComment(e);
						});
				}
				var ctSpan = $("<span />").addClass("commentText").html(o.reviewText).appendTo(ct);

		if (position === "top") {
			$(c).prependTo(this.div_comments);
		} else {
			$(c).appendTo(this.div_comments);
		}
	};

	this.reportAComment = function(e) {
		var x = (screen.width - 515) / 2;
		var y = (screen.height - 490) / 2;
		var notifyUrl = $(e.currentTarget).attr("href");
		window.open(notifyUrl, 'NotifyUs', 'width=515,height=490,directories=no,titlebar=no,status=no,menubar=no,toolbar=no,location=no,left='	+ x + ',top=' + y);
	};
	
	this.addAComment = function(e) {
		var commentText = this.textbox_comments.value;
		this.submitComment(this.assetUri, commentText)
		this.textbox_comments.value = "";

		var o = {};
		o.author = this.guid;
		o.authorDisplayName = this.displayName;
		o.date = getPrintDate();
		o.reviewText = commentText;

		this.buildComment(o, "top");
		var scrollToTarget = parseInt($(this.div_container).position().top);
		$('body,html').animate({scrollTop:scrollToTarget}, 250);		
	};

	this.highlightCurrentPage = function() {
		if (this.highlightedPage) {
			if ($(this.highlightedPage).hasClass("selected")) {
				$(this.highlightedPage).removeClass("selected");
			}
		}
		
		var highlightedPage = $("li", this.ol_pagination)[this.settings.currentPage - 1];
		
		if (highlightedPage) {
			if (!$(highlightedPage).hasClass("selected")) {
				$(highlightedPage).addClass("selected");
			}
			this.highlightedPage = highlightedPage;
		}
		
		if (this.settings.currentPage === this.settings.maxPage) {
			if (!$(this.button_next).hasClass("disabled")) {
				$(this.button_next).bind("click", function(e) {
					e.preventDefault();
				});
				$(this.button_next).addClass("disabled");
			}
		} 
		else {
			if ($(this.button_next).hasClass("disabled")) {
				$(this.button_next).unbind("click");
				$(this.button_next).bind("click", function(e) {
					self.next(e);
				});
				$(this.button_next).removeClass("disabled");
			}
		}

		if (this.settings.currentPage <= 1) {
			if (!$(this.button_prev).hasClass("disabled")) {
				$(this.button_prev).unbind("click");
				$(this.button_prev).bind("click", function(e) {
					e.preventDefault();
				});
				$(this.button_prev).addClass("disabled");
			}
		} 
		else {
			if ($(this.button_prev).hasClass("disabled")) {
				$(this.button_prev).unbind("click");
				$(this.button_prev).bind("click", function(e) {
					self.prev(e);
				});
				$(this.button_prev).removeClass("disabled");
			}
		}
	}
	
	function getPrintDate(d) {
		if (!d) { d = new Date(); }
		var month = d.getMonth() < 9? "0"+(d.getMonth()+1) : d.getMonth();
		var day = d.getDate() < 10? "0" + d.getDate() : d.getDate();
		var hours = d.getHours() < 10? "0" + d.getHours() : d.getHours();
		var minutes = d.getMinutes() < 10? "0"+d.getMinutes() : d.getMinutes();
		var printDate = d.getFullYear() + "-" + month + '-' + day + ' '	+ hours + ':' +minutes;
		return printDate;
	};

	this.initModule(options);
};

com.aol.music.spinner.Comments.prototype = new com.aol.music.DynaTrriApi("/dyna/api");
