var _image1Path = "/share/com/ugc/Img/y30.gif";
var _image2Path = "/share/com/ugc/Img/y20.gif";
var _groupIds;
var _moneyGuideRatingCatIds = "";
var _numOfGroups = 0;
var _strCode  = ""; 


function displaySuperGroup(supgrpid, catid) {
	c = document.getElementById(supgrpid);
	if (c != null) {
		c.style.display = "block";
		c.style.visibility = "visible";
		_moneyGuideRatingCatIds = (_moneyGuideRatingCatIds == "" ? catid : _moneyGuideRatingCatIds + ";" + catid);
	}
}

function changeMoneyGuideTopic(optVal)
{	
	if (JS_TOP_RATED_ARTICLE_RATINGS == undefined || JS_TOP_RATED_ARTICLE_RATINGS == null || JS_TOP_RATED_ARTICLE_RATINGS == "")
    {
		return false;
    }
    _groupIds = JS_TOP_RATED_ARTICLE_RATINGS.split(",");
	
    if (_groupIds.length < optVal)
    {
		return false;
    }
	//construct the json request url
	
	var requestURL = getLocationUrl("/hl_templatesoa.aspx?siteid=" + JS_SITEID + "&sectionid=" + JS_SECTIONID + "&subsectionid=" + (JS_SUB_SECTION==undefined ? 0 : JS_SUB_SECTION) + "&hlgroup=" + _groupIds[optVal] + "&soamode=templateToJSON&hltid=" + 4231);
	//construct the callback function that will be executed 
	
	var jsonCallback = function (responseData)
	{
		var _categoriesJavaScriptCode = responseData.JsonOutput.highlight.group.value;
		_categoriesJavaScriptCode = _categoriesJavaScriptCode.replace(/<!--\$\$HAS_VERSION\$\$-->/g, "") 
		evalGlobalJavaScript(_categoriesJavaScriptCode);
		consumeMoneyGuideData();
	}
	
	//configure and send the json request
	var configuration = new Ninemsn.Global.ContentManager.JsonProp(10000, -1, null, "JsonOutput", Ninemsn.Global.ContentManager.Json.ResponseFunction.Identical);
	Ninemsn.Global.ContentManager.GetContent(requestURL, jsonCallback, configuration);
}
			
function consumeMoneyGuideData()
{
	if (typeof(_moneyGuideCategories) != 'undefined')
	{   
		var _divRatings = document.getElementById("moneyguideratings");
		_strCode = "";
		_divRatings.innerHTML = "";
		for(var _catIndex = 0; _catIndex < _moneyGuideCategories.length; _catIndex++)
		{
			_id = _moneyGuideCategories[_catIndex].id;
			_title = _moneyGuideCategories[_catIndex].title;	

			_class = (_catIndex % 2 != 0 ? "moneyGuideratingeven" : "moneyGuideratingodd")	
			_strCode = _strCode + "<DIV id=\"cat_hl_" + _id + "\" class=\"" + _class + "\"><DIV class=\"ratingTitle\" id=\"ratingTitle_" + _id + "\">" + _title + "</div>";
			_strCode = _strCode + "<DIV class=rating id=rating_" + _id + ">";
			_intCnt = 0;

			for(var _linkIndex = 0; _linkIndex < _moneyGuideCategories[_catIndex].links.length; _linkIndex++)
			{
				_intMaxRatings = _moneyGuideCategories[_catIndex].links[_linkIndex].ratingstars;
				_intRated = _moneyGuideCategories[_catIndex].links[_linkIndex].rated;
				_intCnt += 1;
				_strCode = _strCode + "<div class=\"ratingitem" + ((_intCnt % 2) != 0 ? "odd\">" : "even\">") + "<span class=\"ratingText\"><a href=" + 
				_moneyGuideCategories[_catIndex].links[_linkIndex].url + ">" + _moneyGuideCategories[_catIndex].links[_linkIndex].title + "</a></span><span class=\"ratingImages\">";
				
				for (var cnt=1; cnt<=_intMaxRatings; cnt++)
				{
					_strCode = _strCode + "<img class=\"averageImg\" src='" + (cnt <= _intRated ? _image1Path : _image2Path) + "' alt='Rating value: " + cnt + " out of " + 							   _intMaxRatings + "' >";
				}
				_strCode = _strCode + "</span></div>";
			}
			_strCode = _strCode + "</DIV></DIV>";
		}
		_divRatings.innerHTML = _strCode;
		_moneyGuideCategories = [];
	}
}

function evalGlobalJavaScript(code)
{
	if (window.execScript)
	{
		//run code in IE global scope
		window.execScript(code);
		return null;
	}
	
	//run code in global scope in other browsers
	var globalScopeRef = this;
	return globalScopeRef.eval ? globalScopeRef.eval(code) : eval(code);
}

function getLocationUrl(url) {
	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/share/com/highlights" + url + "&mode=preview&reset=" + _intRnd;
		} else if(_hostName.indexOf("prev01") != -1) {
			_transferUrl = "http://money.devprev01.ninemsn.com.au/share/com/highlights" + url + "&reset=" + _intRnd;		
		} else if(_hostName.indexOf("sbkdev") != -1) {
			_transferUrl = "http://data.sbkdev.ninemsn.com.au/share/com/highlights" + url + "&mode=preview&reset=" + _intRnd;
		} else if(_hostName.indexOf("devsbk03") != -1) {
			_transferUrl = "http://data.devsbk03.ninemsn.com.au/share/com/highlights" + url + "&mode=preview&reset=" + _intRnd;
		} else if(_hostName.indexOf("devsbk06") != -1) {
			_transferUrl = "http://data.devsbk06.ninemsn.com.au/share/com/highlights" + url + "&mode=preview&reset=" + _intRnd;
		} else if(_hostName.indexOf("devgen02") != -1) {
			_transferUrl = "http://news.devgen02.ninemsn.com.au/share/com/highlights" + url + "&reset=" + _intRnd;
		} else {
			_transferUrl = "http://data.ninemsn.com.au/share/com/highlights" + url + "&reset=" + _intRnd;
		}
	} catch(e) {
	}
	return _transferUrl;
}

function changeCatClassName(divId) {
	var _div = document.getElementById(divId);
	if (_div == null) {
		return false;
	}
	_numOfGroups += 1;

	if (_numOfGroups % 2 != 0){
		_div.className = "moneyguideratingodd";
	} else {
		_div.className = "moneyguideratingeven";
	}
}


