﻿$j(document).ready(function() {
	RefreshComments('',1, true,'');
	
	$j('#sortByDefault').click(function(){
		RefreshComments('',1,true,'commentsWrapper');
		$j('#hfCurrentSelectCategory').val('');
	});
	
	$j('#sortByReply').click(function(){
		RefreshComments('updated',1,true,'commentsWrapper');
		$j('#hfCurrentSelectCategory').val('updated');
	});
	
	$j('#sortByMember').click(function(){
		RefreshComments('member',1,true,'commentsWrapper');
		$j('#hfCurrentSelectCategory').val('member');
	});
	
	
	$j('#lnkAddToFavs').click(function(){
    var url = "comments/commentpost.aspx";
    var button = $j(this);
    $j.ajax
		({
			type: 'POST',
			url: url,
			data: 'AddFavArticle=1',
			success: function (data) 
			{
			     button.hide();
			}
		});
		
		return false;
    });
	
});

function RefreshComments(sort,page, paging, location) {
    $j('#ajaxLoadingImage').show();

    $j.ajax
	({
	    type: 'GET',
	    url: 'comments/commentsloader.aspx',
	    data: 'page=' + page + '&sort=' + sort,
	    success: function (data) {
	        $j('#commentsPanelOuter').remove();
	        $j('#commentgoeshere').after(data);
	        if (paging) {
	            LoadPaging(page);
	        }
	        $j('#CommentsPaginationWrapper a').removeClass('active');
	        $j('#CommentsPaginationWrapper a:eq(' + (page - 1) + ')').addClass('active');

	        // roll to the top of comments containor
	        if (location != '') {
	            //FindContent(location);
	            scroller(location, 0);
	        }

	        BindEvents();
	        $j('#ajaxLoadingImage').hide();
	    }
	});
}

function LoadPaging(page)
{
	var index = 1
	if(page != 0)
	{
		index = page;
	}
	$j('#CommentsPaginationWrapper').html($j('#temp_paging').html());
	
	$j('#CommentsPaginationWrapper a').click(function(){
		var newindex = $j(this).attr('id').replace('pagenumber_','');
		RefreshComments($j('#hfCurrentSelectCategory').val(),newindex,false,'commentsWrapper');
		return false;
	});
}

function BindEvents()
{
				$j('.userCommentBtns li button[id^="userReportAbuseBtn"]').click(function(){
					var index = $j(this).attr('id').replace('userReportAbuseBtn_','');
					
					$j('#reportAbuseClickedWrapper_' + index).slideDown();
					$j(this).addClass('btnCommentsInactive');
					$j(this).removeClass('btnComments');
					$j(this).removeClass('btnDefault');
					$j(this).removeClass('TwoColSpan');
					$j(this).removeClass('hoverState_01');
					$j(this).attr('disabled','disabled');
					$j('#userReplyBtn_' + index).removeClass('btnDefault');
					$j('#userReplyBtn_' + index).removeClass('TwoColSpan');
					$j('#userReplyBtn_' + index).addClass('btnCommentsInactive');
					$j('#userReplyBtn_' + index).attr('disabled','disabled');
					
				});
				
				// Open reply area
				$j('#commentsPanelInner .userCommentBtns button[id^="userReplyBtn"]').click(function(){
					var index = $j(this).attr('id').replace('userReplyBtn_','');
					$j('#replyClickedWrapper_' + index).slideDown();
					$j(this).addClass('btnCommentsInactive');
					$j(this).removeClass('btnDefault');
					$j(this).removeClass('TwoColSpan');
					$j(this).attr('disabled','disabled');
					$j(this).removeClass('hoverState');
					$j('#userReportAbuseBtn_' + index).removeClass('btnComments');
					$j('#userReportAbuseBtn_' + index).addClass('btnCommentsInactive');
					$j('#userReportAbuseBtn_' + index).attr('disabled','disabled');
				});
				
				// open reply report area
				$j('button[id^="userReportReplyAbuseBtn"]').click(function(){
				
				var index = $j(this).attr('id').replace('userReportReplyAbuseBtn_','');
						$j('#reportAbuseClickedWrapper_' + index).slideDown();
						$j(this).addClass('btnCommentsInactive');
						$j(this).removeClass('btnComments');
						$j(this).removeClass('btnDefault');
						$j(this).removeClass('TwoColSpan');
						$j(this).removeClass('hoverState');
						$j(this).attr('disabled','disabled');
						$j('#userReplyBtn_' + index).removeClass('btnDefault');
						$j('#userReplyBtn_' + index).removeClass('TwoColSpan');
						$j('#userReplyBtn_' + index).addClass('btnCommentsInactive');
						$j('#userReplyBtn_' + index).attr('disabled','disabled');
				});
		
		
				$j('#commentsPanelInner .userCommentBtns button[id^="userReplyClose"]').click(function(){
					var index = $j(this).attr('id').replace('userReplyClose_','');
					$j('#replyClickedWrapper_' + index).slideUp();
					$j('#userReplyBtn_' + index).removeClass('btnCommentsInactive');
					$j('#userReplyBtn_' + index).addClass('btnDefault');
					$j('#userReplyBtn_' + index).addClass('TwoColSpan');
					$j('#userReportAbuseBtn_' + index).removeClass('btnCommentsInactive');
					$j('#userReportAbuseBtn_' + index).addClass('btnComments');
					$j('#userReplyBtn_' + index).attr('disabled','');
					$j('#userReportAbuseBtn_' + index).attr('disabled','');
					$j('#userReply_' + index).val('');
				});
		
				$j('button[id^="userReportAbuseClose"]').click(function(){
					
					var index = $j(this).attr('id').replace('userReportAbuseClose_','');
					$j('#reportAbuseClickedWrapper_' + index).slideUp();
					$j('#userReportAbuseBtn_' + index).removeClass('btnCommentsInactive');
					$j('#userReportAbuseBtn_' + index).addClass('btnComments');
					$j('#userReportAbuseBtn_' + index).attr('disabled','');
					
					$j('#userReportReplyAbuseBtn_' + index).removeClass('btnCommentsInactive');
					$j('#userReportReplyAbuseBtn_' + index).addClass('btnComments');
					$j('#userReportReplyAbuseBtn_' + index).attr('disabled','');
					
					$j('#userReplyBtn_' + index).removeClass('btnCommentsInactive');
					$j('#userReplyBtn_' + index).addClass('btnDefault');
					$j('#userReplyBtn_' + index).addClass('TwoColSpan');
					$j('#userReplyBtn_' + index).attr('disabled','');
				});
				
				
				
				// User submit new comments
				$j('#userCommentSubmitSingle').click(function(){
					
					if($j('#userCommentSingle').val() == ''){return false;}
				
					var body = $j('#userCommentSingle').val();
					if(body.length >= 5000)
					{
					    alert('Your post contains more than 5000 characters.');
					    return false;
					}
				
				
					$j(this).attr('disabled','disabled');
					if($j('#hfAction').val() == 'false')
					{
						return false;
					}
					else
					{
						$j('#hfAction').val('false');
					}

					$j.ajax
					({
						type: 'POST',
						url: 'comments/commentpost.aspx',
						data: 'SubmitComment=1'  + '&commenttext=' + ParseString($j('#userCommentSingle').val()) + '&subscription=' + $j('#chkEmailMe').attr('checked'),
						success: function (data) 
						{
							$j('#userCommentsSingle').hide();
							$j('#userCommentSingle').val('');
							$j('#userCommentsThankyouMessageSingle').show();
							$j('#userCommentsThankyouMessageSingle').html(data);
							$j('#userCommentSubmitSingle').removeAttr('disabled');
							$j('#hfAction').val('');
							
							$j('#hfCurrentSelectCategory').val('');
							$j('#selectCategory').html('Most recent comments');
							var count = $j('#lblyoursay').val();
							$j('#lnkyoursay').html("Your say - " +  (parseInt(count) + 1) + " comments");
							$j('#lblyoursay').val((parseInt(count) + 1));
							RefreshComments($j('#hfCurrentSelectCategory').val(),1,true,'commentsWrapper');
							if ($j.browser.msie) {
							    window.location.reload();
							} 
                        }
					});
				});
		
				// User submit a comments report
				$j('#commentsPanelInner button[id^="userReportAbuseSubmit"]').click(function(){
				var index = $j(this).attr('id').replace('userReportAbuseSubmit_','');
				if($j('#userReportAbuse_' + index).val() == ''){return false;}
				
				var button = $j(this);
				button.attr('disabled','disabled');
				
					$j.ajax
					({
						type: 'POST',
						url: 'comments/commentpost.aspx',
						data: 'SubmitCommentAbuse=1'  + '&commenttext=' + ParseString($j('#userReportAbuse_' + index).val()) + '&commentid='  + $j('#hfcomment_id_' + index).val(),
						success: function (data) 
						{
							$j('#reportAbuseClicked_' + index).hide();
							$j('#userReportAbuse_' + index).val('');
							$j('#reportAbuseClickedThankyouMessage_' + index).show();
							button.removeAttr('disabled');
						}
					});
				});
				
				// User submit a reply report
				$j('#commentsPanelInner button[id^="userReportReplyAbuseSubmit"]').click(function(){
				var index = $j(this).attr('id').replace('userReportReplyAbuseSubmit_','');
				if($j('#userReportAbuse_' + index).val() == ''){return false;}
				
				var button = $j(this);
				button.attr('disabled','disabled');
					$j.ajax
					({
						type: 'POST',
						url: 'comments/commentpost.aspx',
						data: 'SubmitReplyAbuse=1'  + '&commenttext=' + ParseString($j('#userReportAbuse_' + index).val()) + '&replyid='  + $j('#hfreply_id_' + index).val(),
						success: function (data) 
						{
							$j('#reportAbuseClicked_' + index).hide();
							$j('#userReportAbuse_' + index).val('');
							$j('#reportAbuseClickedThankyouMessage_' + index).show();
							button.removeAttr('disabled');
						}
					});
				});
				
				// User submit reply
				$j('#commentsPanelInner button[id^="userReplySubmit"]').click(function(){
					
					var index = $j(this).attr('id').replace('userReplySubmit_','');
					if($j('#userReply_' + index).val() == ''){return false;}
					
					var body = $j('#userReply_' + index).val();
					if(body.length >= 5000)
					{
					    alert('Your post contains more than 5000 characters.');
					    return false;
					}
					
					var button = $j(this);
					button.attr('disabled','disabled');
					$j.ajax
					({
						type: 'POST',
						url: 'comments/commentpost.aspx',
						data: 'SubmitReplay=1'  + '&commenttext=' + ParseString($j('#userReply_' + index).val()) + '&commentid='  + $j('#hfcomment_id_' + index).val(),
						success: function (data) 
						{
							$j('#replyClickedWrapper_' + index).hide();							  	
							$j('#newReplyWrapper_' + index).show();
							$j('#userReplyBtn_' + index).removeClass('btnCommentsInactive');
							$j('#userReplyBtn_' + index).addClass('btnDefault');
							$j('#userReplyBtn_' + index).addClass('TwoColSpan');
							$j('#userReportAbuseBtn_' + index).removeClass('btnCommentsInactive');
							$j('#userReportAbuseBtn_' + index).addClass('btnComments');
							$j('#userReplyBtn_' + index).attr('disabled','');
							$j('#userReportAbuseBtn_' + index).attr('disabled','');
							$j('#userReply_' + index).val('');
							
							//load by updated replies
							$j('#hfCurrentSelectCategory').val('updated');
							$j('#selectCategory').html('Most recent replies');
							
							//var count = $j('#lblyoursay').val();
							//$j('#lnkyoursay').html("Your say - " +  (parseInt(count) + 1) + " comments");
							//$j('#lblyoursay').val((parseInt(count) + 1));
							
							RefreshComments('updated',1,true,'commentsWrapper');
							
							button.removeAttr('disabled');
						}
					});
				});
				
				// User submit comment up and down
				$j('#commentsPanelInner a[id^="userCommentDown"]').click(function(){
				var index = $j(this).attr('id').replace('userCommentDown_','');
				
				if(CheckCookie($j('#hfcomment_id_' + index).val()))
				{
					return false;
				}
				
				var button = $j(this);
					$j.ajax
					({
						type: 'POST',
						url: 'comments/commentpost.aspx',
						data: 'SubmitCommentsUpDown=down'  + '&commentid='  + $j('#hfcomment_id_' + index).val(),
						success: function (data) 
						{
							var current = button.prev('span').html();
							var newRate = parseInt(current) - 1;
							if(newRate > 0)
							{
								newRate =  '+' + newRate;
							}
							button.prev('span').html( newRate);
						}
					});
				});
				
				
				$j('#commentsPanelInner a[id^="userReplyDown"]').click(function(){
				var index = $j(this).attr('id').replace('userReplyDown_','');
				
				if(CheckCookie($j('#hfreply_id_' + index).val()))
				{
					return false;
				}
				
				var button = $j(this);
					$j.ajax
					({
						type: 'POST',
						url: 'comments/commentpost.aspx',
						data: 'SubmitReplyUpDown=down'  + '&replyid='  + $j('#hfreply_id_' + index).val(),
						success: function (data) 
						{
							var current = button.prev('span').html();
							var newRate = parseInt(current) - 1;
							if(newRate > 0)
							{
								newRate =  '+' + newRate;
							}
							button.prev('span').html( newRate);
						}
					});
				});
				
				
				$j('#commentsPanelInner a[id^="userCommentUp"]').click(function(){
				var index = $j(this).attr('id').replace('userCommentUp_','');
				
				if(CheckCookie($j('#hfcomment_id_' + index).val()))
				{
					return false;
				}
				
				var button = $j(this);
					$j.ajax
					({
						type: 'POST',
						url: 'comments/commentpost.aspx',
						data: 'SubmitCommentsUpDown=up'  + '&commentid='  + $j('#hfcomment_id_' + index).val(),
						success: function (data) 
						{
							var current = button.prevAll('span').html();
							var newRate = parseInt(current) + 1;
							if(newRate > 0)
							{
								newRate =  '+' + newRate;
							}
							button.prevAll('span').html( newRate);
						}
					});
				});
				
				$j('#commentsPanelInner a[id^="userReplyUp"]').click(function(){
				var index = $j(this).attr('id').replace('userReplyUp_','');
				
				if(CheckCookie($j('#hfreply_id_' + index).val()))
				{
					return false;
				}
				
				var button = $j(this);
					$j.ajax
					({
						type: 'POST',
						url: 'comments/commentpost.aspx',
						data: 'SubmitReplyUpDown=up'  + '&replyid='  + $j('#hfreply_id_' + index).val(),
						success: function (data) 
						{
							var current = button.prevAll('span').html();
							var newRate = parseInt(current) + 1;
							if(newRate > 0)
							{
								newRate =  '+' + newRate;
							}
							button.prevAll('span').html( newRate);
						}
					});
				});
}

function CheckCookie(id)
{
	if($j.cookies.get('ChoiceCommentsCookie')== null)
	{
		$j.cookies.set('ChoiceCommentsCookie', id); 
		return false;
	}
	else
	{
		var ids = $j.cookies.get('ChoiceCommentsCookie');
		if(ids.indexOf(id) < 0)
		{
			var newids = ids + '|' + id;
			$j.cookies.set('ChoiceCommentsCookie', newids);
			return false;
		}
		else
		{
			return true;
		}
	}
}

function FindContent(div)
{
    window.location.hash = "#" + div;    
}

function intval(v)
{
	 v = parseInt(v);
	 return isNaN(v) ? 0 : v;
}


function getPos(e)
{
	 var l = 0;
	 var t = 0;
	 var w = intval(e.style.width);
	 var h = intval(e.style.height);
	 var wb = e.offsetWidth;
	 var hb = e.offsetHeight;
	 while (e.offsetParent){
		 l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);
		 t += e.offsetTop + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0);
		 e = e.offsetParent;
	 }
	 l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);
	 t += e.offsetTop + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0);
	 return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
}


function getScroll()
{
 var t, l, w, h;

 if (document.documentElement && document.documentElement.scrollTop) {
	 t = document.documentElement.scrollTop;
	 l = document.documentElement.scrollLeft;
	 w = document.documentElement.scrollWidth;
	 h = document.documentElement.scrollHeight;
 } else if (document.body) {
	 t = document.body.scrollTop;
	 l = document.body.scrollLeft;
	 w = document.body.scrollWidth;
	 h = document.body.scrollHeight;
 }
 return { t: t, l: l, w: w, h: h };
}


function scroller(el, duration)
{
 if(typeof el != 'object') { el = document.getElementById(el); }

 if(!el) return;

 var z = this;
 z.el = el;
 z.p = getPos(el);
 z.s = getScroll();
 z.clear = function(){window.clearInterval(z.timer);z.timer=null};
 z.t=(new Date).getTime();

 z.step = function(){
 var t = (new Date).getTime();
 var p = (t - z.t) / duration;
 if (t >= duration + z.t) {
 z.clear();
 window.setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
 } else {
 st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
 sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
 z.scroll(st, sl);
 }
 };
 z.scroll = function (t, l){window.scrollTo(l, t)};
 z.timer = window.setInterval(function(){z.step();},13);
}

     function ParseString(value) {
         while (value.indexOf("&") >= 0) { 
            value = value.replace("&","%26");
         }
        return value;
     }
