pname=document.location.pathname;
pattern = new RegExp("([i|I]ndex\\.*)[a-z]*");
var unit_type =pname.replace(pattern,"")+document.location.search;
//var unit_type = document.location.pathname+document.location.search;
var unit_id = "1";

var commentPageSize = 10;

function moreComments(){
	$("#comment-navigation").html("<img src=\"http://www.muchmusic.com/mmconnect/images/ajax-loader.gif\" />");
	offset += commentPageSize;

	var useremail=getCookie("mml").substring(0,getCookie("mml").indexOf('|'));
	var usersession=getCookie("mml").substring(getCookie("mml").indexOf('|')+1).split("|")[0];

	var moreComments = "";

	$.getJSON("http://webservices.muchmusic.com/connect/elastica.php?act=getcmts&jsoncallback=?",
		{unittype: unit_type, unitid: unit_id, offset: offset},
		function(data){
			if(data.success == 1){

				var counter=0;

				for(var propertyName in data.comments) {

					moreComments += "<div class=\"cmttop\"></div>";

					if( (counter%2) == 0){
						moreComments += "<div id=\"Comment" + data.comments[propertyName].cmtid + "\" class=\"Comment\" style=\"background-color: #eee;\">";				
					} else {
						moreComments += "<div id=\"Comment" + data.comments[propertyName].cmtid + "\" class=\"Comment\">";
					}

					moreComments += "<div class=\"CommentSubject\">" +  data.comments[propertyName].cmtsubj;
						
					if(useremail.length > 0 && usersession.length > 0){
						moreComments += "<a onclick=\"CommentReply(" +  data.comments[propertyName].cmtid + ");return false;\" class=\"CommentReply\" href=\"#reply"+data.comments[propertyName].cmtid+"\" style=\"display: inline;\">REPLY</a>";
					} else {
						moreComments += "<a onclick=\"window.open('http://webservices.muchmusic.com/connect/frontend/login.php?brand=" + brand + "','login','scrollbars=0,resizable=no,width=790, height=420');return false;\" class=\"CommentReply\" href=\"#reply"+data.comments[propertyName].cmtid+"\" style=\"display: inline;\">LOGIN TO REPLY</a>";
					}
					moreComments += "</div>";

					moreComments += "<div class=\"CommentUsername\">Posted by " + data.comments[propertyName].displayname + "</div>";
					moreComments += "<div class=\"CommentTimestamp\">" + data.comments[propertyName].cmtdate + "</div>";

					moreComments += "<div class=\"userStuff\"><img src=\"" + data.comments[propertyName].displaypic + "\" title=\"User Image\" alt=\"User Image\">";
					moreComments += "<div class=\"CommentText\">" + data.comments[propertyName].cmttxt + "</div>";

					moreComments += "</div>"; 

					moreComments += "<div id=\"ReplyBox" +  data.comments[propertyName].cmtid + "\"></div>";
					moreComments += "<div class=\"SubCommentReply\"></div>"

					for(var subPropertyName in data.comments[propertyName].subcomments) {  
						moreComments += "<div class=\"SubComment\">";

						moreComments += "<div class=\"CommentSubject\">";
							
						if(useremail.length > 0 && usersession.length > 0){
							moreComments += "<a onclick=\"CommentReply(" +  data.comments[propertyName].cmtid + ");return false;\" class=\"CommentReply\" href=\"#reply"+data.comments[propertyName].cmtid+"\" style=\"display: inline;\">REPLY</a>";
						} else {
							moreComments += "<a onclick=\"window.open('http://webservices.muchmusic.com/connect/frontend/login.php?brand=" + brand + "','login','scrollbars=0,resizable=no,width=790, height=420');return false;\" class=\"CommentReply\" href=\"#"+data.comments[propertyName].cmtid+"\" style=\"display: inline;\">LOGIN TO REPLY</a>";
						}
						moreComments += "</div>";

						moreComments += "<div class=\"CommentUsername\">Posted by " + data.comments[propertyName].subcomments[subPropertyName].displayname + "</div>";
						moreComments += "<div class=\"CommentTimestamp\">" + data.comments[propertyName].subcomments[subPropertyName].cmtdate + "</div>";

						moreComments += "<div class=\"userStuff\"><img src=\"" + data.comments[propertyName].subcomments[subPropertyName].displaypic + "\" title=\"User Image\" alt=\"User Image\">";

						moreComments += "<div class=\"CommentText\">" + data.comments[propertyName].subcomments[subPropertyName].cmttxt + "</div>";
						moreComments += "</div></div>";
					}

					moreComments += "</div>";
					moreComments += "<div class=\"cmtbot\"></div>";

					counter++;
				}
				$("#CommentSection").html($("#CommentSection").html() + moreComments );

				if(offset + commentPageSize < numcomments){
					$("#comment-navigation").html("<a href=\"#\" onclick=\"moreComments(); return false;\"> MORE COMMENTS <img src=\"http://www.muchmusic.com/mmconnect/images/down_arrow_small.gif\" /></a>");
				} else {
					$("#comment-navigation").html("");
				}
			}
		}
	);
}

function CommentReply(comment_id){
	$("#ReplyBox"+comment_id).show();
	$("#ReplyBox"+comment_id).html('<div class="CommentInput"><label for="cmttxt'+comment_id+'" class="CommentLabel">Comment:</label><textarea cols="30" rows="4" class="cmttxt" id="cmttxt'+comment_id+'" name="cmttxt"></textarea><input type="button" value="Submit Comment" class="SubmitComment" onclick="postReplyComment('+comment_id+')"/><input type="button" value="Cancel" class="CancelComment" onclick="CloseCommentReply('+comment_id+')"/></div>');
}
function CloseCommentReply(comment_id){
	$("#ReplyBox"+comment_id).hide();	
}
function postComment(){
	if ($("#cmttxtTop").val()){
		var username=getCookie("mmd").substring(0,getCookie("mmd").indexOf('|'));
		var userimage=getCookie("mmd").substring(getCookie("mmd").indexOf('|')+1);
		var useremail=getCookie("mml").substring(0,getCookie("mml").indexOf('|'));
		var usersession=getCookie("mml").substring(getCookie("mml").indexOf('|')+1).split("|")[0];
		var userid=getCookie("mml").substring(getCookie("mml").indexOf('|')+1).split("|")[1];

		$("#NewComment").html('<div class="cmttop"></div><div class="Comment"><div class="CommentSubject">'+$("#cmtsubjectTop").val()+'</div><img alt="User Image" title="User Image" src='+userimage+'><div class="CommentTimestamp" style="color:#FF0000">Comment is awaiting moderation</div><div class="CommentUsername">'+username+' says:</div><div class="CommentText">'+$("#cmttxtTop").val()+'</div></div><div class="cmtbot"></div>');

		$.getJSON("http://webservices.muchmusic.com/connect/elastica.php?act=postcmt&jsoncallback=?", { unittype: unit_type, unitid: unit_id, inreplyto: "", cmtsubj: $("#cmtsubjectTop").val(), cmttxt: $("#cmttxtTop").val(), email: useremail, brand:brand, session: usersession, originalurl:location.href } ,function(data){
		});
		$("#cmtsubjectTop").val("");
		//$("#cmtsubjectTop").attr("disabled", true)
		$("#cmttxtTop").val("");
		//$("#cmttxtTop").attr("disabled", true)
		$("#SubmitComment").hide();
		
	} else {
		alert('Comment is empty');
	}
}
function postReplyComment(comment_id){
	if ($("#cmttxt"+comment_id).val()){
		var username=getCookie("mmd").substring(0,getCookie("mmd").indexOf('|'));
		var userimage=getCookie("mmd").substring(getCookie("mmd").indexOf('|')+1);
		var useremail=getCookie("mml").substring(0,getCookie("mml").indexOf('|'));
		var usersession=getCookie("mml").substring(getCookie("mml").indexOf('|')+1).split("|")[0];
		var userid=getCookie("mml").substring(getCookie("mml").indexOf('|')+1).split("|")[1];

		$("#Comment"+comment_id+" .SubCommentReply").html('<div class="SubComment"><img alt="User Image" title="User Image" src='+userimage+'><p class="CommentTimestamp" style="color:#FF0000">Comment is awaiting moderation</p><p class="CommentUsername">'+username+' says:</p><p class="CommentText">'+$("#cmttxt"+comment_id).val()+'</p>');

		$.getJSON("http://webservices.muchmusic.com/connect/elastica.php?act=postcmt&jsoncallback=?", { unittype: unit_type, unitid: unit_id, inreplyto: comment_id, cmtsubj: "", cmttxt: $("#cmttxt"+comment_id).val(), email: useremail, brand:brand, session: usersession, originalurl:location.href } ,function(data){
			});
		$("#cmttxt"+comment_id).val("");
		$("#ReplyBox"+comment_id).hide();
		
	} else {
		alert('Comment is empty');
	}
}
