/*
	StarRatings.js
	
	Wraps the majority of the star rating functionality. 
	Requires working pointers in the same page to the current jquery.ui.stars.css and jquery.ui.stars.js files.

	Requirements:
	- Requires JQuery 1.4.2 or better.
	
	Revision History:
	
	11/08/2010	kn		Created.	
	11/10/2010	kn		Rewrote this crap.

*/

// Globals ================================
var gStarSaveCallback = null;
var gStarRatingBasis = 1.0;		// Set to 0.5 for half stars but you'll need to fix the spacing issue!
var gStarMaxRating = 5.0;		

// New Code ====================================================

// For quick display versions (no label, no functionality).
function gStarCreateSmallDisplay( inContainerID, inRatingValue )
{
	gStarCreateRating( inContainerID, inRatingValue, true, true, 0, 0, "");
	
}//end gStarCreateSmallDisplay

function gStarCreateLargeDisplay( inContainerID, inRatingValue )
{
	gStarCreateRating( inContainerID, inRatingValue, false, true, 0, 0, "");
	
}//end gStarCreateLargeDisplay


// New version. The rating ID, event ID and rating title are optional.
// Creates a single self-contained rating component.
function gStarCreateRating( inContainerID, inRatingValue, inSmall, inReadOnly, inRatingID, inEventID, inRatingTitle, inRatingCallback)
{
	
	$("#" + inContainerID).html(gStarGetRatingHTML(inRatingValue, inRatingID, inEventID, inRatingTitle));
	
	gStarActivateRatings(inContainerID, inSmall, inReadOnly, inRatingCallback);
	
}//end gStarCreateRating

// Creates the HTML for a single rating item. Call gStarActivateRatings to activate it.
function gStarGetRatingHTML(inRatingValue, inRatingID, inEventID, inRatingTitle)
{
	var sWork = '';
	var fRatingValue = gGetFloatFromDB(inRatingValue);
	var iSCREventID = gGetIntFromDB(inEventID);
	var iSCRRatingID = gGetIntFromDB(inRatingID);
	
	sWork += '<div class="RatingWrapper">';
	
	if (inRatingTitle != null && inRatingTitle.length > 0)
		sWork += '<label>' + inRatingTitle + '</label>';
	
	sWork += '<div class="RatingItem">';
	
	for (var i=gStarRatingBasis; i < (gStarMaxRating + gStarRatingBasis); i += gStarRatingBasis)
	{
		var sID = '';
		var sControlID = (inRatingTitle != null ? inRatingTitle : 'Rating') + iSCREventID + '' + i;
		
		if (iSCRRatingID > 0)
			sID = 'Rating[' + iSCREventID + '|' + iSCRRatingID + '|' + i + ']';
		else
			sID = 'Rating[' + iSCREventID + '|0|' + i + ']';
		
		sWork += '<input type="radio" name="' + sControlID + '" id="' + sControlID + '" value="' + sID + '" ' + 
			( fRatingValue >= i ? ' checked="checked"' : '') + ' />' + 
			'\r\n';
		
		
	}//end for
	
	
	sWork += '</div>' +
			(iSCRRatingID > 0 ? '<div class="Status" id="Status_' + iSCREventID + '_' + iSCRRatingID + '"></div>' : '') + 
			'</div>\r\n';
	
	return sWork;

}//end gStarGetRatingHTML

// Activates the rating code for anything withing the container specified. Use the callback to find out when the save is done.
function gStarActivateRatings(inContainerID, inSmall, inReadOnly, inRatingCallback)
{
	
	if (inSmall)
	{
		$("#" + inContainerID + " .RatingItem").stars({
			
			split: 0, cancelShow: false, starWidth: 17,
			disabled: inReadOnly,
			callback: function(ui, type, value) { gStarSaveRating(ui, type, value, inRatingCallback); }
			
		})//end stars
		.css("width", "100px");
	}//end if
	else
	{
		$("#" + inContainerID + " .RatingItem").stars({
			
			split: 0, cancelShow: false, starWidth: 23,
			disabled: inReadOnly,
			cancelClass: "ui-stars-cancel-lg",
			starClass:"ui-stars-star-lg",
			starOnClass:"ui-stars-star-on-lg",
			starHoverClass:"ui-stars-star-hover-lg",
			cancelHoverClass:"ui-stars-cancel-hover-lg",
			callback: function(ui, type, value) { gStarSaveRating(ui, type, value, inRatingCallback); }
			
		});//end stars
	}//end else
	
}//end gStarActivateRatings



function gStarSaveRating( ui, type, value, inRatingCallback)
{
	var sWork = value.substring(value.indexOf("[")+1, value.indexOf("]"));
	var aPieces = sWork.split("|");
	
	
	var sEID = aPieces[0];
	var sRID = aPieces[1];
	var sRat = aPieces[2];
	
	var sURL = "/events/Services/RatingsService.asmx/SaveEventRating";
	var postdata = { inEID: sEID, inRID: sRID, inRating: sRat };
	
	gStarSaveCallback = (inRatingCallback ? inRatingCallback : null);
	
	gDoAjaxJSON(sURL, $.toJSON(postdata), gStarSaveFinish, gStarSaveFail);
	

}//end gStarSaveRating

function gStarSaveFinish(inData)
{
	var data = inData.d;
	var sStatusID = "";
	var bSuccess = false;
	
	if (data != null)
	{
		if (data.Status != null)
		{
			bSuccess = (data.Status == "Success");
			
			if (!bSuccess && data.Message && data.Message.length > 0)
				alert(data.Message);
			else
				sStatusID = data.Payload;
				
		}//end data
	}//end if
	
//	alert(gStarSaveCallback + " : " + bSuccess + " : " + sStatusID + " : " + $.find("#Status_" + sStatusID).length);
	
	if (gStarSaveCallback != null)
	{
		gStarSaveCallback(bSuccess);
		
		gStarSaveCallback = null;
		
	}//end if
	else
	{
		if (bSuccess && sStatusID.length > 0)
		{
			if ($.find("#Status_" + sStatusID).length > 0)
			{
				$("#Status_" + sStatusID).html("Saved!");
				setTimeout('$("#Status_' + sStatusID + '").html("");', 1000);
				
			}//end if
		}//end if
	}//end else
	
}//end gStarSaveFinish

function gStarSaveFail(inError)
{
	if (gProcessReportError(inError, true, "errormsg"))
		return;
		
}//end gStarSaveFail




