// GLOBAL VARIABLES & CONSTANTS
var _AltImagePath = "/share/com/ugc/Img/y30.gif,/share/com/ugc/Img/y20.gif,/share/com/ugc/Img/y10.gif"
var _Tag1 = "Poor";
var _Tag2 = "Great";
var _IntroText = "Rate this item";
var _ExistingRatingText = "You rated this item ";
var _CurrentRatingText = "Current user rating by";
var _AverageRatingText = "Overall average rating by ";
var _EnforceSecurity = true;
var _CookieExpiry = 1;

function showRatings(objectId,onclickcode)
{	
	var _maxRating = eval("_maxRating_" + objectId);
	var _rated = eval("_rated_" + objectId);
	var _arrImages = eval("_arrImages_" + objectId);
	var _ratedValue = null;
	var _aspect = eval("_aspect_" + objectId);
	if(_arrImages  && _arrImages == "")
	{
		_arrImages = _AltImagePath.split(",");
	}
	var _currentRating = eval("_currentRating_" + objectId);
	var _ratingTotal = eval("_ratingTotal_" + objectId);
	var _averageRating = Math.floor(parseInt(_currentRating) / parseInt(_ratingTotal));
	
	var _lDivObject;
	if(objectId != "m")
	{
		_lDivObject = document.getElementById("RatingContainer_" + objectId);
	}
	else
	{
		_lDivObject = document.getElementById("RatingContainer_multi");
	}	
	var _lCode = "";
	var _uCode = "";
	if(_blnNoPost == false)
	{
		_uCode = "<div class='UserRating'><span id='UserRateMsg_" + objectId + "' class='UserRating_text'>" + ((_aspect==undefined || _aspect=='') ? ((JS_RATING_RATETHISITEM==undefined || JS_RATING_RATETHISITEM=='') ? _IntroText : JS_RATING_RATETHISITEM) : _aspect) + " </span><span class=UserRating_low>" + ((JS_RATING_LOW==undefined || JS_RATING_LOW=='') ? _Tag1 : JS_RATING_LOW) + "</span><span class='UserRating_img'>" + " ";			
	}
	else
	{
		_uCode = "<div class='UserRating'><span id='UserRateMsg_" + objectId + "' class='UserRating_text'></span>";
	}
	try
	{
		if(_EnforceSecurity == true)
		{
			_ratedValue = checkRatingCookie(objectId);
		}
		
		for(var cnt=0;cnt<_maxRating;cnt++)
		{
			if(objectId != "m" && _blnNoPost == false)  
			{
				_uCode = _uCode + "<img class=ratingImages id=img_" + objectId + "_" + cnt + " src='" + _arrImages[1] + "' " + onclickcode + " alt='Click to rate: " + (cnt+1) + " out of " + _maxRating + "' >";
			}
			if(cnt < _averageRating)
			{
				_lCode = _lCode + "<img class=avgImages  src='" + _arrImages[0] + "' alt='Rating value: " + (cnt+1) + " out of " + _maxRating + "' >";
			}
			else
			{
				_lCode = _lCode + "<img class=avgImages  src='" + _arrImages[1] + "' alt='Rating value: " + (cnt+1) + " out of " + _maxRating + "' >";
			}
		}
		if(_ratedValue != null)
		{	
			var _uCode = "<div class='UserRating'><span id='UserRateMsg_" + objectId + "' class='UserRating_text'>" + ((JS_RATING_YOURATEDTHISITEM==undefined || JS_RATING_YOURATEDTHISITEM=='') ? _ExistingRatingText : JS_RATING_YOURATEDTHISITEM) + "</span><span class=UserRating_low>" + ((JS_RATING_LOW==undefined || JS_RATING_LOW=='') ? _Tag1 : JS_RATING_LOW) + "</span><span class='UserRating_img'>" + " ";

			for(var cnt=0;cnt<_maxRating;cnt++)
			{
				if(cnt<parseInt(_ratedValue))
				{
					_uCode = _uCode + "<img class=avgImages  src='" + _arrImages[0] + "' alt='Rating value: " + (cnt+1) + " out of " + _maxRating + "' >";
				}
				else
				{
					_uCode = _uCode + "<img class=avgImages  src='" + _arrImages[1] + "' alt='Rating value: " + (cnt+1) + " out of " + _maxRating + "' >";
				}			
			}
		}
		if(objectId != "m" && _blnNoPost == false)
		{
			_lCode = "<div class='RatingContainer'>" +  _uCode + " " + "</span><span class=UserRating_high>" + ((JS_RATING_HIGH==undefined || JS_RATING_HIGH=='') ? _Tag2 : JS_RATING_HIGH) + "</span></div><div id=ratingAvg_" + objectId + " class=ratingAvg><span class='ratingAvg_text'>" + ((JS_RATING_CURUSERRATEBY==undefined || JS_RATING_CURUSERRATEBY=='') ? _CurrentRatingText : JS_RATING_CURUSERRATEBY) + " " + _ratingTotal +" users: </span><span class='ratingAvg_img'>" + _lCode + "</span></div>";					

		}
		else if(objectId != "m" && _blnNoPost == true)
		{
			_lCode = "<div class='RatingContainer'>" +  _uCode + "</div><div id=ratingAvg_" + objectId + " class=ratingAvg><span class='ratingAvg_text'>" + ((JS_RATING_CURUSERRATEBY==undefined || JS_RATING_CURUSERRATEBY=='') ? _CurrentRatingText : JS_RATING_CURUSERRATEBY) +" "+ _ratingTotal +" users: </span><span class='ratingAvg_img'>" + _lCode + "</span></div>";					
		}
		else
		{
			//get the total number of aspects for this multi-rating
			var _NoOfMultiRatingAspects = window["_NoOfMultiRatingAspects_" + objectId];
			var _totalNoOfUsers = 0; //default value is zero total no. of users on error
			if (!_NoOfMultiRatingAspects || _NoOfMultiRatingAspects <= 0)
			{
				alert("Multi Rating Exception: variable '_NoOfMultiRatingAspects' is missing or has an invalid value.");
			}
			else
			{
				_totalNoOfUsers = Math.ceil(_ratingTotal / _NoOfMultiRatingAspects);
			}
			_lCode = "<div class='RatingContainer'><div id=ratingAvg_" + objectId + " class=ratingAvg>"+ _AverageRatingText + _totalNoOfUsers +" users: " + _lCode + "</span></div>";
		}
		_lDivObject.innerHTML = _lCode + "</div>";
	}
	catch(e)
	{
	}
}

function renderAjaxRating(objectId, contentTypeId, divId)
{
	var callbackFunction = function (responseElement)
	{
		if (responseElement &&
		(typeof(responseElement.Rating) != "undefined") && 
		(typeof(responseElement.Rating.length) != "undefined") &&
		(responseElement.Rating.length > 0))
		{
			var mRatingData = responseElement.Rating;
			var mRatingMarkup = "";
			var mRatingScript = "";
			for (var i = 0; i < mRatingData.length; i++)
			{
				mRatingMarkup += mRatingData[i].Markup;
				mRatingScript += mRatingData[i].Script;
			}
			
			var ratingDiv = document.getElementById(divId);
			if (ratingDiv)
			{
			    if (!isNullOrEmpty(mRatingMarkup))
			    {
				    ratingDiv.innerHTML = mRatingMarkup;
				}
				if (!isNullOrEmpty(mRatingScript))
			    {
				    evalGlobalJavaScript(mRatingScript);
				}	
			}
		}
	}
	
	//Get the markup and script to render for rating attached to the given Revver Media										
	var fetchUrl = "/share/com/ugc/ajaxcontrols/rating.aspx?" +
	    "objectid=" + objectId + "&contentTypeId=" + contentTypeId;
	var jsonPropList = new Ninemsn.Global.ContentManager.JsonProp (10000, -1, null, 
		"JSONResult", Ninemsn.Global.ContentManager.Json.ResponseFunction.Unique);	
	Ninemsn.Global.ContentManager.GetContent(fetchUrl, callbackFunction, jsonPropList);
}

function refreshRatings(objectId, contentTypeId, renderFunctionName)
{
	renderFunctionName = renderFunctionName ? renderFunctionName : "showRatings";
	var _lCurrentRating;
	var _currentRating;	
	var _ratingTotal;	
	var _securityKey;	
	var _securityMsg;	
	try
	{	
		var fetchUrl = getLocationUrl("/UGCPortal.aspx?tool=1&action=0&objectId=" + objectId +"&ContentTypeId="+ contentTypeId);

		var _oSwap = function (responseElement) {
			
			if(responseElement && responseElement != null && responseElement.UGCPortalResult && responseElement.UGCPortalResult.RatingDS)
			{
				if(responseElement.UGCPortalResult.RatingDS && responseElement.UGCPortalResult.RatingDS.rating.length)
				{
					for(var cnt=0; cnt < responseElement.UGCPortalResult.RatingDS.rating.length; cnt++)
					{
						objectId = responseElement.UGCPortalResult.RatingDS.rating[cnt].objectId;
						_securityKey = responseElement.UGCPortalResult.RatingDS.rating[cnt].key;			
						_securityMsg = responseElement.UGCPortalResult.RatingDS.rating[cnt].Msg;			
						_ratingTotal = responseElement.UGCPortalResult.RatingDS.rating[cnt].ratingTotal;
						_lCurrentRating = parseInt(_ratingTotal) * parseInt(responseElement.UGCPortalResult.RatingDS.rating[cnt].ratingCount);
						
						if(parseInt(_lCurrentRating) != parseInt(eval("_currentRating_" + objectId)))
						{
							eval("_currentRating_" + objectId + "=" + _lCurrentRating + ";");
							eval("_ratingTotal_" + objectId + "=" + _ratingTotal + ";");
							window[renderFunctionName](objectId,eval("_onClickCode_" + objectId));
						}
						eval("_securityKey_" + objectId + "='" + _securityKey + "';");						
						eval("_securityMsg_" + objectId + "='"  + _securityMsg + "';");		
					}
				}
				else
				{
					objectId = responseElement.UGCPortalResult.RatingDS.rating.objectId;
					_securityKey = responseElement.UGCPortalResult.RatingDS.rating.key;			
					_securityMsg = responseElement.UGCPortalResult.RatingDS.rating.Msg;			
					_ratingTotal = responseElement.UGCPortalResult.RatingDS.rating.ratingTotal;	
					_lCurrentRating = parseInt(_ratingTotal) * parseInt(responseElement.UGCPortalResult.RatingDS.rating.ratingCount);		
	
					if(parseInt(_lCurrentRating) != parseInt(eval("_currentRating_" + objectId)))
					{
						eval("_currentRating_" + objectId + "=" + _lCurrentRating + ";");
						eval("_ratingTotal_" + objectId + "=" + _ratingTotal + ";");
						window[renderFunctionName](objectId,eval("_onClickCode_" + objectId));
					}
					eval("_securityKey_" + objectId + "='" + _securityKey + "';");						
					eval("_securityMsg_" + objectId + "='" + _securityMsg + "';");					
				}
			}
		} 	
		// Setup Property List
		var jsonPropList = new Ninemsn.Global.ContentManager.JsonProp (10000, -1, null, "JSONResult", Ninemsn.Global.ContentManager.Json.ResponseFunction.Unique);	
		// Triggers the JSON call
		Ninemsn.Global.ContentManager.GetContent(fetchUrl, _oSwap, jsonPropList);
	}
	catch(e)
	{	
	}
}

function ratingHover(id,objectId)
{
	var _maxRating = eval("_maxRating_" + objectId);
	var _rated = eval("_rated_" + objectId);
	var _arrImages = eval("_arrImages_" + objectId);
	if(_arrImages  && _arrImages == "")
	{
		_arrImages = _AltImagePath.split(",");
	}
		
	if(!_rated)
	{
		var _arrId = id.split("_");
		var _arrIdx = _arrId[2];				
		for(var cnt=0;cnt<_maxRating;cnt++)
		{
			document.getElementById("img_" + objectId + "_" + parseInt(cnt)).src = _arrImages[1];
		}
		
		if(document.getElementById("img_" + objectId + "_" + parseInt(_arrIdx)))
		{
			for(var cnt = 0 ; cnt <= parseInt(_arrIdx) ; cnt++)
			{
				document.getElementById("img_" + objectId + "_" + parseInt(cnt)).src = _arrImages[2];
			}
		}
	}
}

function ratingCheck(id,objectId)
{
	var _maxRating = eval("_maxRating_" + objectId);
	var _rated = eval("_rated_" + objectId);
	var _arrImages = eval("_arrImages_" + objectId);
	var _currentRating = eval("_currentRating_" + objectId);
	if(_arrImages  && _arrImages == "")
	{
		_arrImages = _AltImagePath.split(",");
	}
	
	if(!_rated)
	{
		var _arrId = id.split("_");
		var _arrIdx = _arrId[2];				
		for(var cnt=0;cnt<_maxRating;cnt++)
		{
			document.getElementById("img_" + objectId + "_" + parseInt(cnt)).src = _arrImages[1];
		}
	}
}

function submitRating(id,objectId,contentTypeId)
{
	var _rated = eval("_rated_" + objectId);
	var _arrImages = eval("_arrImages_" + objectId);
	var _strResponseText;
	if(_arrImages  && _arrImages == "")
	{
		_arrImages = _AltImagePath.split(",");
	}
	
	if(!_rated)
	{
		var _arrId = id.split("_");
		var _arrIdx = parseInt(_arrId[2]) + 1;				
		for(var cnt=0;cnt<parseInt(_arrIdx);cnt++)
		{
			document.getElementById("img_" + objectId + "_" + parseInt(cnt)).src = _arrImages[0];
		}
		eval("_rated_" + objectId + " = true");
		try
		{
			var _securityKey = eval("_securityKey_" + objectId);	
			var _securityMsg = eval("_securityMsg_" + objectId);	

			var fetchUrl = getLocationUrl("/UGCPortal.aspx?tool=1&action=1&objectId=" + objectId + "&RatingValue=" + _arrIdx + "&key=" + _securityKey + "&msg=" + _securityMsg);

			var _oSwap = function (responseElement) { 
				if(responseElement && responseElement != null && responseElement.UGCPortalResult && responseElement.UGCPortalResult.SubmitRating)
				{
					_strResponseText = ((JS_RATING_YOURATEDTHISITEM==undefined || JS_RATING_YOURATEDTHISITEM=='') ? _ExistingRatingText : JS_RATING_YOURATEDTHISITEM);

					if(_EnforceSecurity == true)
					{
						setRatingCookie(objectId,_arrIdx);				
					}
				}
				else
				{
					_strResponseText = ((JS_RATING_YOURATEDTHISITEM==undefined || JS_RATING_YOURATEDTHISITEM=='') ? _ExistingRatingText : JS_RATING_YOURATEDTHISITEM);
				}
				document.getElementById("UserRateMsg_" + objectId).innerHTML = _strResponseText;
			} 	
			// Setup Property List
			var jsonPropList = new Ninemsn.Global.ContentManager.JsonProp (10000, -1, null, "JSONResult", Ninemsn.Global.ContentManager.Json.ResponseFunction.Unique);	
			// Triggers the JSON call
			Ninemsn.Global.ContentManager.GetContent(fetchUrl, _oSwap, jsonPropList);			
		}
		catch(e)
		{
		}		
	}				
}

function getLocationUrl(url)
{
	var _siteName;
	var _blnIsProd = false;
	var _blnIsDev = false;
	var _hostName = window.location.hostname;
	var _transferUrl = "";
	var _intRnd = Math.random();
	try
	{
		if(_hostName.indexOf("syd") != -1 || _hostName.indexOf("sbkprod") != -1 || _hostName.indexOf("paddington") != -1)
		{
			_transferUrl = "http://data.syd.ninemsn.com.au/ugc" + url + "&mode=preview&reset=" + _intRnd;
		}
		else if(_hostName.indexOf("devprev01") != -1)
		{
			_transferUrl = "http://data.devprev01.ninemsn.com.au/ugc" + url + "&mode=preview&reset=" + _intRnd;
		}
		else if(_hostName.indexOf("prev01") != -1)
		{
			_transferUrl = "http://data.prev01.ninemsn.com.au/ugc" + url + "&mode=preview&reset=" + _intRnd;
		}
		else if(_hostName.indexOf("sbkdev") != -1)
		{
			_transferUrl = "http://data.sbkdev.ninemsn.com.au/ugc" + url + "&mode=preview&reset=" + _intRnd;
		}
		else if(_hostName.indexOf("devsbk03") != -1)
		{
			_transferUrl = "http://data.devsbk03.ninemsn.com.au/ugc" + url + "&mode=preview&reset=" + _intRnd;
		}
		else if(_hostName.indexOf("devsbk06") != -1)
		{
			_transferUrl = "http://data.devsbk06.ninemsn.com.au/ugc" + url + "&mode=preview&reset=" + _intRnd;
		}
		else if(_hostName.indexOf("devgen02") != -1)
		{
			_transferUrl = "http://data.devgen02.ninemsn.com.au/ugc" + url + "&mode=preview&reset=" + _intRnd;
		}
		else if(_hostName.indexOf("sandeepkd") != -1)
		{
			_transferUrl = "http://data.sandeepkd.ninemsn.com.au/ugc" + url + "&mode=preview&reset=" + _intRnd;
		}
		else
		{
			_transferUrl = "http://data.ninemsn.com.au/ugc" + url + "&reset=" + _intRnd;
		}
	}
	catch(e)
	{
	}
	return _transferUrl;
}

function setRatingCookie(objectId,ratedValue)
{
	try
	{
		var date = new Date();
		date.setTime(date.getTime()+(parseInt(_CookieExpiry)*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
		document.cookie = "Rated_" + objectId + "=" + escape(ratedValue) + expires + "; path=/";
	}
	catch(e)
	{
	}
}

function checkRatingCookie(objectId)
{
	var ratedValue = null;
	try
	{
		var nameEQ = "Rated_" + objectId + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i < ca.length; i++)
		{
			var c = ca[i];
			while (c.charAt(0) == ' ')
			{
				c = c.substring(1, c.length);										
			}
			if(c.indexOf(nameEQ) == 0)
			{
				blnCookieCheck = true;
				ratedValue = unescape(c.substring(nameEQ.length,c.length));
				break;
			}
		}				
	}
	catch(e)
	{
	}
	return ratedValue;
}	