var currentActiveArticleID = null;
var maxRatingStars=5;

$(document).ready(function() {
	//When page loads...
	if ($(".tab_content").length > 0)
	{
		$(".tab_content").hide(); //Hide all content
		
		var $firstTab = $("ul.tabs li:first");
		$firstTab.addClass("active").show(); //Activate first tab
		var tmp = $firstTab.find('a').attr("href");
		var parts = tmp.split('_');
		
		if (initialObjectID == null)
			{
				currentActiveArticleID = parts[1];
				$("#tab_"+currentActiveArticleID).show(); //Show first tab content
			}
		else
			{
				currentActiveArticleID = initialObjectID;
				setActiveTab(currentActiveArticleID);
			}
		
		initProductRatingWidget();
		updateProductRatingWidget(currentActiveArticleID);
		loadProductReviews(currentActiveArticleID);
		
//		$(".tab_content:first").show(); //Show first tab content
		
		
	
		//On Click Event
		$("ul.tabs li").click(function() {
			var tmp = $(this).attr('id');
			var thisTabObjectID = tmp.replace(/tabButton_/ , '');
			setActiveTab(thisTabObjectID);
			return false;
		});

	}
});



function setActiveTab(objectID)
{
	var $tabButton = $('#tabButton_'+objectID);
	$('#previousReviewWrapper').html('<center><br /><br /><img src="/i/loading.gif" /><br /><b>Loading...</b></center>');
	
	$("ul.tabs li").removeClass("active"); //Remove any "active" class
	$tabButton.addClass("active"); //Add "active" class to selected tab
	$(".tab_content").hide(); //Hide all tab content

	var activeTab = $tabButton.find("a").attr("href"); //Find the href attribute value to identify the active tab + content
	$(activeTab).fadeIn(); //Fade in the active ID content
	
	var parts = activeTab.split('_');
	currentActiveArticleID = parts[1];
	
//	updateProductRatingWidget(currentActiveArticleID);
//	loadProductReviews(currentActiveArticleID);
//	$('#ratingForm').show();
//	$('#ratingForm').css({'overflow':'hidden'}).animate({height: 300}, 700);
}

function initProductRatingWidget()
{
	$("#productRating").children().not("select, #rating_title").hide();
	var $caption = $('<div id="caption"/>');

	$("#productRating").stars({
		inputType: "select",
		oneVoteOnly: false
	});
	
	$("#productRating").stars("select", 0);
	$('#btnPostReview').button();

}

function updateProductRatingWidget(productArticleID)
{
	currentActiveArticleID = productArticleID;
}

function loadProductReviews(productArticleID, lastReviewID)
{
	if (typeof lastReviewID == 'undefined')
		{
			lastReviewID=0;
		}
	
	var requestVariableMap = {
			'mode':'list',
			'contentID':productArticleID,
			'contentTypeID':5
	};
	
	
	var submissionURI = '/apps/review/';
	
	$.ajax({
		type: 'POST',
		url : submissionURI,
		data : requestVariableMap,
		dataType : 'json',
		cache : false,
		error : function(request, statusText, myErr) {
			switch (statusText) {
			case 'parsererror':
				pfaxSetStatus(
						'ProductReviewSubmission: Unparsable JSON received on URL '
								+ submissionURI, true);
				break;
			default:
				pfaxSetStatus('ProductReviewSubmission: Unhandled Error "'
						+ statusText + '"');
				break;
			}
		},
		success : function(contentResponseObject) {
			pfaxSetStatus('ProductReviewList: SUCCESS');
			var newHtml='';
			var numReviews = contentResponseObject.dataSet.length;
			if (numReviews == 0)
				{
					newHtml = '<div class="rating"><h3>Be the first to review this product!</h3></div>';
				}
			else
				{
					newHtml = generateReviewListHtml(contentResponseObject.dataSet)
				}
			
			$('#previousReviewWrapper').html(newHtml);
		}
	});	
	
}


function generateReviewListHtml(dataSet)
{
	var newHtml = '';
	
	var numReviews = dataSet.length;
	
	
	
	for (var i=0; i<numReviews; i++)
		{
			var thisReview = dataSet[i];
			var rPercent = thisReview['ratingPercent'];
			var numStars = Math.round((rPercent/100)*maxRatingStars);
			var starHtml = '';
			for (var s=0; s<numStars; s++)
				{
					starHtml += '<div class="starOn">&nbsp;</div>';
				}
			for (var so=s; so<maxRatingStars; so++)
				{
					starHtml += '<div class="starOff">&nbsp;</div>';
				}
			
			newHtml += '<div class="rating"><div class="stars" style="width:80px;margin-top:10px;float:right;clear:none;height:16px;">';
			newHtml += starHtml;
			newHtml += '</div>';
			newHtml += '<h3>'+thisReview['Subject']+'</h3>';
			newHtml += '<div class="byPosted"><span>Posted: '+thisReview['postTime']+'</span>By:  '+thisReview['userName']+'</div></div>';
			
			newHtml += '<div class="content"><p>';
			newHtml += thisReview['Body'];
			newHtml += '</p></div>';
			newHtml += '<div style="clear:both;"></div>';
		}
	return newHtml;
}


function productReview_Submit()
{
	var submissionURI = '/apps/review/';
	
	var reviewUser = $('#review_Name').val();
	var reviewEmail = $('#review_Email').val();
	var objStars = $("#productRating").data("stars");
	var reviewRating = objStars.options.value;
	var reviewTitle = $('#review_Title').val();
	var reviewBody = $('#review_Body').val();
	var pathname = $('#websiteURL').html();	
	 	pathname = pathname+'i/logo/logoReview.jpg';	
	if (reviewRating == 0)
	{
			alert("Please use the stars to rate this product.");
			return;
	}
	
	if (reviewTitle.length < 3 || reviewBody.length < 10 || reviewEmail.length < 10)
	{
		alert("Please provide an email address and a valid title and body for this review.");
		return;
	}
	
	
	var requestVariableMap = {
			'mode':'submit',
			'reviewUser': reviewUser,
			'reviewEmail': reviewEmail,
			'reviewTitle': reviewTitle,
			'reviewBody': reviewBody,
			'reviewRating': reviewRating,
			'pathname': pathname,				
			'contentID':currentActiveArticleID,
			'contentTypeID':5
	};
	
	
	$.ajax({
		url : submissionURI,
		data : requestVariableMap,
		dataType : 'json',
		cache : false,
		error : function(request, statusText, myErr) {
			switch (statusText) {
			case 'parsererror':
				pfaxSetStatus(
						'ProductReviewSubmission: Unparsable JSON received on URL '
								+ submissionURI, true);
				break;
			default:
				pfaxSetStatus('ProductReviewSubmission: Unhandled Error "'
						+ statusText + '"');
				break;
			}
		},
		success : function(contentResponseObject) {
			pfaxSetStatus('ProductReviewSubmission: SUCCESS : ' + contentResponseObject.statusText);
			alert("Thank you\nYour review has been received and is awaiting moderation and spam checking");			
			$('#ratingForm').css({'overflow':'hidden'}).animate({height: 0}, 700, function() { 
					$(this).hide();
					$('#review_Name').val('');
					$('#review_Email').val('');
					$("#productRating").stars("select", 0);
					$('#review_Title').val('');
					$('#review_Body').val('');
					});
		}
	});
}
