﻿$(document).ready(function() {
	setTimeout(LoadChart, 500);
	setTimeout(LoadComponents, 1000);
	setInterval("UpdatePage()", 300000);
//	var ScrollY = GetCookie("ScrollY");
//    if (ScrollY !== "" && !isNaN(ScrollY))
//         window.scrollTo(0, ScrollY); 
});

var winPopups = new Array();
//window.onunload = function() {
//    var ScrollY = window.pageYOffset || document.documentElement.scrollTop || 0;  
//    document.cookie = "ScrollY=" + ScrollY + ";path=/;domain=.morningstar.com;"; 
//	for (var i = 0; i < winPopups.length; i++)
//		winPopups[i].close();
//}

function GetCookie(name) {
       if (document.cookie.length <= 0)
              return "";
       name += "=";
       var start = document.cookie.indexOf(name);
       if (start < 0)
              return "";
       start += name.length;
       var end = document.cookie.indexOf(";", start);
       if (end < 0)
              end = document.cookie.length;
       return document.cookie.substring(start, end);
}

function QPageOnLoad() {
  var ScrollY = GetCookie("ScrollY");
    if (ScrollY !== "" && !isNaN(ScrollY))
         window.scrollTo(0, ScrollY);  

}

function QPageOnUnLoad() {
	var ScrollY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;  
    document.cookie = "ScrollY=" + ScrollY + ";path=/;domain=.morningstar.com;"; 
    for (var i = 0; i < winPopups.length; i++)
		winPopups[i].close();
}

//unload and load functions
if (typeof('addOnloadEvent') == 'function') {
	    addOnloadEvent(QPageOnLoad);
} 
else 
{
var oldload = window.onload;
if (typeof(oldload) != 'function') {
    window.onload = QPageOnLoad;
} else {
    window.onload = function() {
        oldload();
        QPageOnLoad();
    };
}
}
if (typeof('addUnloadEvent') == 'function') {
    addUnloadEvent(QPageOnUnLoad);
} 
else 
{
var oldUnload = window.onunload;
if (typeof(oldUnload) != 'function') {
    window.onunload = QPageOnUnLoad;
} else {
    window.onunload = function() {
        oldUnload();
        QPageOnUnLoad();
    };
}
}
//end of unload and load functions


function UpdatePage() {
	var params = GetPara();
	document.location = "s.aspx?" + params + "&byrefresh=yes";
}

function LoadComponentsTop() {
	var params = GetPara();
	LoadAComponent("MstarTake.ashx", params, true, 30000, new Array("idPremiumFeatures", "idMstarTake"), null, "idMstarTakeContent", null);
	LoadAComponent("MstarRecommendation.ashx", params, true, 30000, new Array("idPremiumFeatures"), null, "idMstarRecommendationContent", null);
	LoadAComponent("MstarGrades.ashx", params, true, 30000, new Array("idPremiumFeatures", "idMstarGradeContent"), null, "idMstarGradeContent", null);
	LoadAComponent("Headlines.ashx", params, true, 30000, new Array("idNews"), null, "idNewsContent", new Array("MstarCommentary.ashx", "PressRelease.ashx"));
}

function LoadComponents() {
	var params = GetPara();
	LoadAComponent("Profile3.ashx", params, true, 30000, new Array("idProfile"), null, "idProfileContent", null);
	LoadAComponent("Dividends.ashx", params, true, 30000, new Array("idDividends"), null, "idDividendsContent", null);
	LoadAComponent("KeyStats.ashx", params, true, 30000, new Array("idKeyStats"), null, "idKeyStatsContent", null);
	LoadAComponent("Video.ashx", params, true, 30000, new Array("idVideo"), null, "idVideoContent", null);
	LoadAComponent("Competitors.ashx", params, true, 30000, new Array("idCompetitors"), null, "idCompetitorsContent", null);
	LoadAComponent("/Financials/Financials.ashx", params, true, 60000, new Array("idFinancials"), null, "idFinancialsContent", null);
	LoadAComponent("FundOwners.ashx", params, true, 30000, new Array("idOwnership"), null, "idOwnershipContent", new Array("/Financials/Insiders.ashx"));
	LoadAComponent("WSRecommendation.ashx", params, true, 30000, new Array("idWSRecommendation"), null, "idWSRecommendationContent", null);
//	LoadAComponent("Management.ashx", params, true, 30000, new Array("idManagement"), null, "idManagementContent", null);
}

var start_p = null, end_p = null, current_p = 2, preset_p = 1;
function LoadChart() {
	try {
		if (typeof MStar == "undefined")
			return;
		MStar.Retail.Global.Mgr.CreateObj("Graph", "idChart", 565, 500, "");
		var CtrlObj = MStar.Retail.Global.Ctrl.Ctrl;
		CtrlObj.initCustomTimeRangeBox = function(startDate, endDate) {
			try {
				var st = DTFormat.ToLongDate(startDate);
				var ed = DTFormat.ToLongDate(endDate);
				var str = st + " – " + ed;
				$("#ms_rt_gl_timestamp").html(str);
					if (preset_p == 0) {
						var o_p = '#idChartPeriod_' + current_p;
						$(o_p).removeClass('gr_zoomoption_on');
						current_p = 0;
					}
					else {
						preset_p = 0;
					}
					start_p = startDate;
					end_p = endDate;
			} catch (e) { }
		};
	} catch (ee) { }
}

function PeriodChg(p) {
	if (p != current_p) {
		preset_p = 1;
		try { MStar.Retail.Global.Ctrl.Ctrl.timeRangeClicked(p); }
		catch (e) { }
		var o_p = "#idChartPeriod_" + current_p;
		var n_p = '#idChartPeriod_' + p;
		try {
			$(o_p).removeClass('gr_zoomoption_on');
			$(n_p).addClass('gr_zoomoption_on');
		} catch (e) { }
		current_p = p;
	}
}

function GetPara() {
	var paras = "t=" + exchTicker;
	if (culture != "")
		paras += "&culture=" + culture;
	if (region != "")
		paras += "&region=" + region;
	paras += "&r=" + Math.floor(Math.random() * 1000000);
	return paras;
}

function LoadAComponent(url, params, bCache, timeout, showItems, hideItems, idContent, callIfFail)
{
	$.ajax({
		cache: bCache,
		timeout: timeout,
		url: url,
		data: params,
		success: function(data, status) {
			if (status != "success" || data == "" || data.indexOf("<title> Morningstar.com Error Page </title>") > 0) {
				if (callIfFail != null) {
					for (var i = 0; i < callIfFail.length; i++)
						LoadAComponent(callIfFail[i], params, bCache, timeout, showItems, hideItems, null, null);
				}
				return;
			}
			if (showItems != null) {
				for (var i = 0; i < showItems.length; i++)
					$("#" + showItems[i]).show();
			}
			if (hideItems != null) {
				for (var i = 0; i < hideItems.length; i++)
					$("#" + hideItems[i]).hide();
			}
			if (idContent != null)
				$("#" + idContent).html(data);
		}
	});
}

function LoadAComponent2(url, params, bCache, timeout, idContent) {
	$.ajax({
		cache: bCache,
		timeout: timeout,
		url: url,
		data: params,
		success: function(data, status) {
			if (status != "success")
				return;
			if (data.indexOf("<title> Morningstar.com Error Page </title>") > 0)
				data = "";
			if (idContent != null)
				$("#" + idContent).html(data);
		}
	});
}

function GetNews(id) {
	// Return if tab is on
	var obj = $("#" + id);
	if (obj[0].className == "gr_section_tabon")
		return;

	// Reset all tabs off
	$("#idNewsTabs a").removeClass("gr_section_tabon");
	$("#idNewsTabs a").addClass("gr_section_taboff");

	// Set tab on
	obj.removeClass("gr_section_taboff");
	obj.addClass("gr_section_tabon");

	// Find the service
	var service = "";
	if (id == "idNewsHeadlines")
		service = "Headlines.ashx";
	else if (id == "idNewsMstarCommentary")
		service = "MstarCommentary.ashx";
	else if (id == "idNewsPressRelease")
		service = "PressRelease.ashx";
	if (service == "") {
		$("#idNewsContent").html("");
		return;
	}

	LoadAComponent2(service, GetPara(), false, 10000, "idNewsContent");
}

// Open News link
function OpenNews(url) {
	var win;
	if (url.indexOf("/news/tpx/extnews.asp?rdurl=") > 0) {
		win = window.open(url, 'TopixNews', 'toolbar=yes,titlebar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=750,height=650');
		if (win != null) {
			winPopups.push(win);
			win.focus();
		}
	}
	else if (url.indexOf(".morningstar.com/") > 0) {
		document.location = url;
	}
	else {
		win = window.open(url, 'News', 'toolbar=yes,titlebar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=750,height=650');
		if (win != null) {
			winPopups.push(win);
			win.focus();
		}
	}
}

var fullManagement = true;
function GetManagement() {
	var params = GetPara();
	if (fullManagement == true) {
		params += "&f=";
		fullManagement = false;
	}
	else {
		fullManagement = true;
	}
	LoadAComponent2("Management.ashx", params, false, 10000, "idManagementContent");
}

function openWin(url, windowName, windowFeatures) {
	if (windowName == "")
		windowName = "newWindow";
	if (windowFeatures == "")
		windowFeatures = "width=750,height=600,top=30,left=30,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes"
	var win = window.open(url, windowName, windowFeatures);
	if (win != null) {
		winPopups.push(win);
		win.focus();
	}
}

function GetFundOwners() {
	// Return if tab is on
	if ($("#idFundOwners")[0].className == "gr_section_tabon")
		return;

	// Set insiders tab off
	$("#idInsiders").removeClass("gr_section_tabon");
	$("#idInsiders").addClass("gr_section_taboff");

	// Set fund owners tab on
	$("#idFundOwners").removeClass("gr_section_taboff");
	$("#idFundOwners").addClass("gr_section_tabon");

	LoadAComponent2("FundOwners.ashx", GetPara(), false, 10000, "idOwnershipContent");
}

function GetInsiders() {
	// Return if tab is on
	if ($("#idInsiders")[0].className == "gr_section_tabon")
		return;

	// Set fund owners tab off
	$("#idFundOwners").removeClass("gr_section_tabon");
	$("#idFundOwners").addClass("gr_section_taboff");

	// Set insiders tab on
	$("#idInsiders").removeClass("gr_section_taboff");
	$("#idInsiders").addClass("gr_section_tabon");

	LoadAComponent2("/Financials/Insiders.ashx", GetPara(), false, 10000, "idOwnershipContent");
}

function OpenPrintReport(server, type, countryid, symbol) {
	var url = server + "/" + type + "/PrintReport.aspx?Symbol=" + symbol + "&Country=" + countryid;
	var win = window.open(url, 'PrintReport', 'width=500,height=255,top=30,left=30,menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes');
	if (win != null) {
		winPopups.push(win);
		win.focus();
	}
}



