// Handle comment zones.
// start by getting some informations about the comments.
// the comment zones exist inside a div with id "commentZone"..
// we must define the type of comments and the target id
// when the page loads we are always at the page 1.

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}



$(document).ready(function(){

	// get the targetID and type
	var commentZoneTargetID;
	var commentZoneType;
	var commentZonePage = 1;
	var commentZoneItemsPerPage;
	var $maxChars = 500;
	
	commentZoneTargetID = $('#commentsZone').attr("targetID");
	commentZoneType = $('#commentsZone').attr("commentType");
	commentZoneItemsPerPage = $('#commentsZone').attr("itemsPerPage");
	
	
	
	function generatePager(currentPage, totalRecords, recordsPerPage)
	{
		var pager;
		var totalNumberOfPages;
		
		totalNumberOfPages = Math.ceil(parseInt(totalRecords) / parseInt(recordsPerPage));
		
		
		
		if (totalNumberOfPages ==1) return "";
		
		
		
		pager = "<div id='commentsPager'>";
		for (f=1;f<=totalNumberOfPages;f++)
		{
			pager = pager+"<span >"+f+"</span>&nbsp;";
		}
		
		// create the live query event.
		$('#commentsPager span').livequery('click', function(event) 
		{
			var page;
		
			page = $(this).text();
			if (page != commentZonePage)
			{
				commentZonePage = page;
				
				// keep the div with the same height
				currentHeight = $('#commentsZone').css('height');
				//alert(currentHeight);
				$('#commentsZone').css('height', currentHeight);
				
				// put the loader gif
				$('#commentsZone').html('<img src="/images/ajax-loader.gif" border="0">');
				// use ajax to load the first page of comments	
				$.get("/data_access/getComments.php", 
						{
								id: commentZoneTargetID, 
							  type: commentZoneType, 
							  page: commentZonePage, 
							nItems: commentZoneItemsPerPage
						}, 
						function(xml) {
			
							updateCommentZone(xml);
		
						}
				);
			}

	
		});
		
		pager = pager+"</div>\n";
		return pager;
	}
	
	
	// update the comment Zone
	function updateCommentZone(xml)
	{
	
		var output;
		var commentSTR;
		
		// get the number of items in page and total items
		items = $(xml).find('comments').attr("items");
		total_items = $(xml).find('comments').attr("total_items")
		//alert($items);
		
		
		// define the templates for the comments
		switch (commentZoneType)
		{
		
			// models comments
			case "1":
				output = "";
				$(xml).find('comments').children('comment').each(function()
				{
					id = $(this).find("ID").text();
					user_id = $(this).find("user_id").text();
					username = $(this).find("username").text();
					target_id = $(this).find("target_id").text();
					comment = stripslashes($(this).find("comment").text());
					date = $(this).find("date").text();
			
			
			
					commentSTR = "\t\t\t<div class='comment'><h3><strong>"+username+"</strong> - "+date+"</h3><p>"+comment+"</p></div>\n";
					output = output + commentSTR;
				});
				break;


			// photosets comments
			case "2":
				output = "";
				$(xml).find('comments').children('comment').each(function()
				{
					id = $(this).find("ID").text();
					user_id = $(this).find("user_id").text();
					username = $(this).find("username").text();
					target_id = $(this).find("target_id").text();
					comment = stripslashes($(this).find("comment").text());
					date = $(this).find("date").text();
			
			
			
					commentSTR = "\t\t\t<div class='comment'><h3><strong>"+username+"</strong> - "+date+"</h3><p>"+comment+"</p></div>\n";
					output = output + commentSTR;
				});
				break;


				


			// videos comments
			case "3":
				output = "";
				$(xml).find('comments').children('comment').each(function()
				{
					id = $(this).find("ID").text();
					user_id = $(this).find("user_id").text();
					username = $(this).find("username").text();
					target_id = $(this).find("target_id").text();
					comment = stripslashes($(this).find("comment").text());
					date = $(this).find("date").text();
			
			
			
					commentSTR = "\t\t\t<div class='comment'><h3><strong>"+username+"</strong> - "+date+"</h3><p>"+comment+"</p></div>\n";
					output = output + commentSTR;
				});
				break;



			// videoitems comments
			case "4":
				output = "";
				$(xml).find('comments').children('comment').each(function()
				{
					id = $(this).find("ID").text();
					user_id = $(this).find("user_id").text();
					username = $(this).find("username").text();
					target_id = $(this).find("target_id").text();
					comment = stripslashes($(this).find("comment").text());
					date = $(this).find("date").text();
			
			
			
					commentSTR = "\t\t\t<div class='comment'><h3><strong>"+username+"</strong> - "+date+"</h3><p>"+comment+"</p></div>\n";
					output = output + commentSTR;
				});
				break;
				
				

				
		}
		
		// generate pager and add to the output
		//alert($output);
		
		output = output+generatePager(commentZonePage, total_items, items);
		
		// update the comments
		$('#commentsZone').html(output);
	}

	// use ajax to load the first page of comments	
	/*
	$.get("/data_access/getComments.php", 
		{
				id: commentZoneTargetID, 
			  type: commentZoneType, 
			 $page: commentZonePage, 
			nItems: commentZoneItemsPerPage
		}, 
		function(xml) {
			
			updateCommentZone(xml);
		
		}
	);
	*/
	
	$('#commentsWantToComment').click(function() {
		$(this).fadeOut(function(){
			$('#commentsForm').fadeIn();
		});
	});
	
	
	// handle the submit
	$('#commentsForm form').submit(function() {
		/*
		switch (commentZoneType)
		{
		
			// models comments
			case "1":
			
			break;
		}
		*/
		
		// get the comment
		commentSTR = $('#comment').val();
		usernameSTR = $('#username').val();
		user_idVal = $('#user_id').val();
	
		if (usernameSTR == "")
		{
			usernameSTR = "Visitor";
			user_idVal = 0;
		}
		
		if (user_idVal == "" || user_idVal == 0 || user_idVal=="0")
		{
			usernameSTR = "Visitor";
			user_idVal = 0;		
		}
		//alert(commentZoneTargetID);
	
		// use ajax to send the comment	
		$.post("/data_access/addComment.php", 
						{
								id: commentZoneTargetID, 
							  type: commentZoneType, 
						   comment: commentSTR, 
						  username: usernameSTR,
						   user_id: user_idVal
						}, 
				function(xml) {
					//alert(xml);
					/*
					message = "There was a problem adding your comment. Please try later!";
					if (xml == "OK")
					{
						message = "Comment sent! Will be revised by one of our editors before appearing live.";
					}
					*/
					//alert(xml);
					if (xml == "OK") $('#commentsMessage #OK').show();
					else $('#commentsMessage #ERROR').show();
					
					$('#commentsForm').fadeOut(function(){
						//$('#commentsMessage').html(message);
						$('#commentsMessage').fadeIn();
					});
	
		});

		
		return false;
	});
	
	
	
		
	$('#comment').keyup(function(){
		$comment = stripslashes($('#comment').val());
		//alert($comment);
		
		if ($comment.length > $maxChars) {
			$comment = $comment.substr(0, $maxChars);
			$('#comment').val($comment);
		}		
		$nChars = $comment.length;
		$remainingChars = $maxChars - $nChars;
		if ($remainingChars == 0) $remainingChars='<b>0</b>';
		
		$('#charactersCount').html($remainingChars);

		//alert($nChars);
		
	});
	
});