//cycle thru comments one by one- see main.php product-range.php, view-range.php, view-product.php
commentsLoop = function(){
	var o = this;
	function init(){
		o.timer = setInterval( function(){ nextComment(); }, 10000 );
		o.comments = $('.comments-loop');
		o.commentItems  = $('.comments-loop-item', o.comments);
		o.currentComment = 0;
	}
	function nextComment(){
		o.currentComment++;
		if( o.currentComment == (o.commentItems.length) ){
			o.currentComment = 0;
		}
		o.commentItems.removeClass('active').addClass('hidetext').hide();
		o.commentItems.eq(o.currentComment).removeClass('hidetext').show();
	}
	return { start: init }
}();
	

function getComments(url, callback) {
	$.getJSON(url, function (data) {
		if (data.result == 'success')
		{
			// reset all
			$('div.comment blockquote p').html('');
			$('div.big-comment span.commentelement').html(''); 
			$('div.big-comment span.open-quote').hide(); 
			$('div.big-comment span.close-quote').hide(); 
			
			$.each(data.comments, function(i,item) {
				element = i+1;
				$('div.comment:nth-child('+element+') blockquote p').html(item.comment); // sidebar
				$('div.big-comment:nth-child('+element+') span.commentelement').html(item.comment); // commentbox
				$('div.big-comment:nth-child('+element+') span.open-quote').show(); 
				$('div.big-comment:nth-child('+element+') span.close-quote').show(); 
				// item.name
				// item.locality
			});
			
			callback(data.link);
			
			return true;
		}
		else 
		{
			$('#comments').html(data.comments);
		}
	});
	
	return false;
	
}

function updatePagnation(data)
{
	if (data.next.url.length > 1) $('#comments-pagination a#next').attr('href',data.next.url);
	if (data.prev.url.length > 1) $('#comments-pagination a#previous').attr('href',data.prev.url);
	
	if (data.next.disabled) $('#comments-pagination a#next').addClass('disabled');
	else $('#comments-pagination a#next').removeClass('disabled');
	
	if (data.prev.disabled) $('#comments-pagination a#previous').addClass('disabled');
	else $('#comments-pagination a#previous').removeClass('disabled');	
	
}

$(document).ready(function() {

	// popup comment box
	var limit = 3;
	
	$('#dialog').dialog({
			autoOpen: false,
			modal: true,
			resizable: false,
			width: 924
	});
	
	$('#dialog a#previous').livequery('click', function() {
		var href = $(this).attr('href');
		href = href.replace('commentbox', 'get_comment');
		
		data = getComments(href, function(data) {
			updatePagnation(data);
		});
		
		return false;
	});
	
	$('#dialog a#next').livequery('click', function() {
		var href = $(this).attr('href');
		href = href.replace('commentbox', 'get_comment');

		data = getComments(href, function(data) {
			updatePagnation(data);
		});
		
		return false;
	});
	
	$('#dialog #post-comment').livequery('submit', function() {
		// JS VALIDATION !! ?
		
		$.post(
			   $(this).attr('action'), 
			   $(this).serialize(), 
			   function(data) { 
			   		if (data.result == 'fail')
					{
						$('#dialog div#FormErrors').html(data.html);
					}
					else 
					{
						// put this is a different div ?
						$('#dialog #post-comment').html(data.html);
					}
			   }, 
			   "json"
		);
		
											   
		return false;
	});
	

	$('.commentbox, .quotebox_button').livequery('click',function() {
													  
		$('#dialog').dialog('open');
	
		return false;
	});
	
	$('.viewmore_comments').click(function() {
		var href = $(this).attr('href');
		href = href.replace('commentbox', 'get_comment');
		
		data = getComments(href, function(data) {
			if (data.next.url.length > 1) $('.viewmore_comments').attr('href',data.next.url);
			updatePagnation(data);
		});		
		
		return false;
	});
	

});

