// =======================================	
// Mega Menu / Client login / Index drop down hover inititalization
// =======================================
    var config = {    
         sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
         interval: 50,  // number = milliseconds for onMouseOver polling interval    
         over: doOpen,   // function = onMouseOver callback (REQUIRED)    
         timeout: 750,   // number = milliseconds delay before onMouseOut    
         out: doClose    // function = onMouseOut callback (REQUIRED)    
    };
	// HACK: really only used with IE6 to hide select boxes which appear on top thanks to faulty stacking order
	var body = $('body');
	
    function doOpen() {
        $(this).addClass("hover");
        $('div:first',this).css('display', 'block');
		body.addClass('megaNavActive');
    }

    function doClose() {
        $(this).removeClass("hover");
		$('div:first',this).css('display', 'none');
		body.removeClass('megaNavActive');
    }
		


function loadExternalFragments() {

	// =======================================	
	// Initialize Drop down input objects
	// <dl class="dropDown collapsed"><dt><a href="#">Open</a></dt><dd><ul><li><a href="#">example</a></li></ul></dd></dl>
	// =======================================
	var dropDown = $('dl.dropDown');

	if (dropDown.length > 0) {
		dropDown.click(function click_dropDown(e) {
			var t = $(this);
			t.toggleClass('openLocked');
		});
	}
	
	$("dl.dropDown").hoverIntent(config);


	// Dynamically load various page elements. Note: load() is async so any dependent code must go into a callback function
	
	if ($('#mainmenu').length > 0)
	{
		$('#mainmenu').load('/ssi/main_menu.html', function() 
		{
			$("ul#nav li").hoverIntent(config);
		});
	}
	else
	{
		// china blog uses static html'
		$("ul#nav li").hoverIntent(config);
	}
	
	if ($('#clientmenu').length > 0)
	{
		$('#clientmenu').load('/ssi/client_menu.html', function() 
			{
				$("ul#clientLogin li").hoverIntent(config);
			});
	}
	else
	{
		// china blog uses static html'
		$("ul#clientLogin li").hoverIntent(config);
	}
  
  // counter to keep track how many XHRs have completed
  // works because JavaScript is not multithreaded (so far)
  window.loadCounter = 0;

	$('#resourcesmenu').load('/ssi/resources_menu.html', function() {
  	window.loadCounter += 1;
  	if (window.loadCounter == 5)
  		applyEqualHeights();
	});

	$('#insightsmenu').load('/ssi/insights_menu.html', function() {
  	window.loadCounter += 1;
  	if (window.loadCounter == 5)
  		applyEqualHeights();
	});

	$('#copyrightInfo').load('/ssi/copyright_info.html', function() {
  	window.loadCounter += 1;
  	if (window.loadCounter == 5)
  		applyEqualHeights();
	});

        if (frmNavRHSTopics && frmNavRHSTopics.length > 0) {
                if( $("#resourcesRHS").length > 0) {
                        $.ajax({
                                type: "GET",                             url: "/resources/rss.topics.xml",
                                dataType: "xml",
                                success: frmRhsNavResourcesParseXml
                        });
                }

                if( $("#insightsRHS").length > 0) {
                        $.ajax({
                                type: "GET",
                                url: "/insights/rss.topics.xml",
                                dataType: "xml",
                                success: frmRhsNavInsightsParseXml
                        });
                }
        }


		// figure out which menu to switch on based on URL
		var loc = location.href;
		if ( loc.indexOf("/products/indices/") >= 0 ) {
			$("#nav li.indices").addClass("nav-on");
		} else if ( loc.indexOf("/products/portfolio_management_analytics/") >= 0 ) {
			$("#nav li.portfolio").addClass("nav-on");

		} else if ( loc.indexOf("/products/risk_management_analytics/") >= 0 ) {
			$("#nav li.risk").addClass("nav-on");

		} else if ( loc.indexOf("/products/esg/") >= 0 ) {
			$("#nav li.environment").addClass("nav-on");
		} else if ( loc.indexOf("/about/") >= 0 ) {
			$("#nav li.about").addClass("nav-on");
		}

		$("iframe").attr("allowTransparency", "true");
};

var frmRhsMAX_ITEMS = 3;
function frmRhsNavInsightsParseXml (xml) {
        var buffer = '';
        for (var i in frmNavRHSTopics) {
                var category = frmNavRHSTopics[i];
                var category_xml = $(xml).find("channel > link:contains('/" + category + "')").parent();
                if (category_xml.length > 0) {
                        buffer += '<p><a href="' + category_xml.find("link").eq(0).text() + '">' + category_xml.find("title").eq(0).text() + '</a></p><ul>';
                        category_xml.find("item").each(function() {
                                buffer += '<li><a href="' + $(this).attr("link") + '">' + $(this).attr("title") + '</a></li>';
                        });
                        buffer += '</ul>';
                }
        }
        if (buffer)
                $("#insightsRHS .insights-content").html(buffer);

		  	window.loadCounter += 1;
       	if (window.loadCounter == 5)
		  		applyEqualHeights();

}

function frmRhsNavResourcesParseXml (xml) {
        var items = [];
        for (var i in  frmNavRHSTopics) {
                var category = frmNavRHSTopics[i];
                $(xml).find("category:contains('" + category + "')").each(function() {
                        if ($(this).text() == category) {
                                items[items.length] = $(this).parent();
                        }
                });
        }
        if (items.length) {
                items.sort(function(a,b) {
                        var a_sort = $(a).attr("order_by");
                        var b_sort = $(b).attr("order_by");
                        if (a_sort) {
                                if (b_sort) {
                                        return b_sort - a_sort;
                                } else {
                                        return 1;
                                }
                        } else if (b_sort) {
                                return -1;
                        }
                        return 0;
                });
                // unique array
                var unique = [items[0]];
                for (var i = 1 ; i < items.length ; i++) {
                        var item = items[i];
                        var prev = items[i - 1];
                        if ($(item).attr("link") != $(prev).attr("link")) {
                                unique[unique.length] = item;
                        }
                }
                // unique array
                var unique = [items[0]];
                for (var i = 1 ; i < items.length ; i++) {
                        var item = items[i];
                        var prev = items[i - 1];
                        if ($(item).attr("link") != $(prev).attr("link")) {
                                unique[unique.length] = item;
                        }
                }
                var itemsByType = {};
                for (var i = 0 ; i < unique.length && i < frmRhsMAX_ITEMS; i++) {
                        var item = unique[i];
                        var type = $(item).parent().find("title").eq(0).text();
                        if (! itemsByType[type])
                                itemsByType[type] = [];
                        itemsByType[type][itemsByType[type].length] = '<li><a href="' + $(item).attr("link") + '">' + $(item).attr("title") + '</a></li>';

                }
                buffer = '';
                for (var key in itemsByType) {
                        buffer += '<p>' + key + '</p><ul>';
                        for (var i in itemsByType[key]) {
                                buffer += itemsByType[key][i];
                        }
                        buffer += '</ul>';
                }
                if ($("#resourcesRHS .resources-content a").length > 0) {
                        $("#resourcesRHS .resources-content").append(buffer);
                } else {
                        $("#resourcesRHS .resources-content").html(buffer);
                }
        }
        
       	window.loadCounter += 1;
       	if (window.loadCounter == 5)
		  		applyEqualHeights();

}

/* For Index Performance pages */
function resizeGuts() {
	if(document.getElementById && !(document.all)) {
		h = document.getElementById('guts').contentDocument.body.scrollHeight;
		w = document.getElementById('guts').contentDocument.body.scrollWidth;
		document.getElementById('guts').style.height = h + 50;
		//document.getElementById('guts').style.width = w + 20;
	}
	else if(document.all) {
		h = document.frames('guts').document.body.scrollHeight;
		w = document.frames('guts').document.body.scrollWidth;
		document.all.guts.style.height = h + 50;
		//document.all.guts.style.width = w + 20;
		}
}


/* For Pop up demo pages */
var popUpWindowHandle = false;
function popUpWindow( url, width, height) {
        popUpWindowHandle = window.open(url,'popUpWindow','menubar=0,status=0,location=0,height=' + height + ',width=' + width + ',directories=0');
        popUpWindowHandle.focus();
        return 0;
}

// start loading the fragments as soon as the DOM is loaded and constructed
$(document).ready(loadExternalFragments);
