var CommentsMessenger = {
	main_url: "http://au.weather.yahoo.com/weather_comments/",
			
	sendContentHeight: function() {
			cHeight = document.body.scrollHeight;
		 	cdate = new Date;
  		document.getElementById('commentsMessenger').src = this.main_url + "cheight.html?" + cdate.getTime() + "#" + cHeight;
	},

	login: function() {
			cdate = new Date;
  	  document.getElementById('commentsMessenger').src = this.main_url + "cheight.html?" + cdate.getTime() + "#login";
	}

};

var loginSuccessHandler = function(o) {
	if (loginId.length > 0) {
			YAHOO.util.Dom.get('c-yid').innerHTML = loginId;
			YAHOO.util.Dom.get('addcommentarea').style.display = 'block';
			YAHOO.util.Dom.get('loginarea').style.display = 'none';
	} else {
		YAHOO.util.Dom.get('loginarea').style.display = 'block';
		YAHOO.util.Dom.get('addcommentarea').style.display = 'none';
	}
}
YAHOO.util.Get.POLL_FREQ = 3;
YAHOO.util.Get.PURGE_THRESH = 3;
YAHOO.util.Get.script("http://au-phugc1.aue.yahoo.com/au/weather_comments/backend/weatherLogin.php", {onSuccess:loginSuccessHandler});

var webserviceController = 'http://au-phugc1.aue.yahoo.com/au/weather_comments/backend/weatherController.php';
var AjaxComment = {
	action: null,
	current_page: 1,
	handleSuccess: function(o){
		var commentsArea = YAHOO.util.Dom.get('commentsarea');
		commentsArea.innerHTML = '';
		if (typeof comments != 'undefined' && comments.total_rows > 0) {
			YAHOO.util.Dom.get('c-navigation').style.display='block';
			for(i=0; i<comments.info.length; i++) {
				var innerHTML = "<li class='c-single";
				innerHTML += "'><div class='c-profile'><span class='user-icon'><img src='" + comments.info[i].profile.avatar.url +  "'/></div><div class='c-details'><h3><span class='c-alias'>" + comments.info[i].profile.nickname + "</span> posted a comment</h3><div class='c-ts'>" + comments.info[i].ts + "</div><div class='comments_text'>" + comments.info[i].text + "</div></div>";
				commentsArea.innerHTML += innerHTML;
			}
			commentsArea.innerHTML = "<ul>"+ commentsArea.innerHTML + "</ul>";
			maxItem = Math.min((this.current_page * comments.comments_per_page), comments.total_rows);
			YAHOO.util.Dom.get('c-navinfo').innerHTML = (((this.current_page - 1) * comments.comments_per_page)+1) + ' - '+ maxItem + ' of ' + comments.total_rows + ' Entries' 
			YAHOO.util.Dom.get('commentBody').value = '';
			YAHOO.util.Dom.get('commentBody').disabled = false;
			YAHOO.util.Dom.get('commentSubmit').disabled = false;
			this.action = null;
			this.init_prev_page_link();
			this.init_next_page_link();
			
		} else {
			YAHOO.util.Dom.get('c-navigation').style.display='none';
			commentsArea.innerHTML = "<ul><li class='c-single'><center>What is the weather like in your area?</center></li></ul>";
		}
		CommentsMessenger.sendContentHeight();
	},
	
	handleFailure: function(o) {
		var comments = '';
	},

	post: function(e, formObj) {
		this.action = 'post';
    	ctext = encodeURIComponent(YAHOO.util.Dom.get('commentBody').value);
		crumb = YAHOO.util.Dom.get("ycommentscmb").value;
		YAHOO.util.Dom.get('commentBody').disabled = true;
		YAHOO.util.Dom.get('commentSubmit').disabled = true;
		YAHOO.util.Get.POLL_FREQ = 3;
		YAHOO.util.Get.PURGE_THRESH = 3;

		YAHOO.util.Get.script(webserviceController + '?ycommentscmb=' + crumb + '&a=post&comment=' + ctext, callback);
	},

	load: function(p) {
		this.action='load';
		this.curent_page = p;
		YAHOO.util.Dom.get('commentsarea').innerHTML = 'Loading comments...';
		YAHOO.util.Get.POLL_FREQ = 3;
		YAHOO.util.Get.PURGE_THRESH = 3;

		setTimeout(function() {YAHOO.util.Get.script(webserviceController + '?a=load&p=' + p, callback);}, 700);
	},
	
	prevpage: function() {
		this.current_page--;
		this.load(this.current_page);
	},
	
	nextpage: function() {
		this.current_page++;
		this.load(this.current_page);

	},
	
	init_next_page_link: function() {
		if (comments.total_rows > (comments.comments_per_page * (this.current_page))) {
			YAHOO.util.Dom.get('c-nav-next').innerHTML = "<a href=\"javascript:AjaxComment.nextpage();\">Next Page</a>";		
		} else {
			YAHOO.util.Dom.get('c-nav-next').innerHTML = 'Next Page';
		}
	},
	
	init_prev_page_link: function() {
		if (this.current_page > 1) {
			YAHOO.util.Dom.get('c-nav-prev').innerHTML = "<a href=\"javascript:AjaxComment.prevpage();\">Prev Page</a>";
		} else {
			YAHOO.util.Dom.get('c-nav-prev').innerHTML = 'Prev Page';
		}
	}
	
};

var callback = {
		onSuccess:AjaxComment.handleSuccess,
		onFailure: AjaxComment.handleFailure,
		scope: AjaxComment
};

YAHOO.util.Event.addListener('commentSubmit', 'click', AjaxComment.post, YAHOO.util.Dom.get('frmComments'));
AjaxComment.load(1);

