﻿var nActiveRegionID = 0;
var arRollover = new Array();
var arSelected = new Array();
var arRegions = new Array(); // for sorted Regions-only array
var arCountries = new Array();	// for sorted Countries-only array
var showAlldiv = document.getElementById('ShowAllLink');
var showAll;
var showAllText = "";
var tooltip = document.getElementById("tooltipdiv");
tooltip.style.display = "none";				
if (showAlldiv != null) {
		var showAll = showAlldiv.getElementsByTagName('a')[0];
		if (showAll != null)
		{
			showAllText = showAll.innerHTML
			showAll.href = "javascript:goRegion('ww')";
		}
	}
function makeSortableArrays()
 {
	var nCountry = 0;
	for (var x = 1; x <= arSubs.length; x++) 
	{
		// Put the Region into an array
		if (arSubs[x]) 
		{
			arRegions[x] = [arSubs[x][0][0],x];
			for (var y = 0; y < arSubs[x].length; y++) 
			{
                //Only Countries, not Regions
				if (y >= 1)
					{
					arCountries[nCountry] = [arSubs[x][y][0], arSubs[x][y][1]];
					nCountry++;
					}
			}
		}
	}
	arRegions = arRegions.sort(sortArray);
	arCountries = arCountries.sort(sortArray);
	
}

function dictRegions(regionName)
{
	for (y = 0; y < arSubs.length; y++)
	 {
		if (regionName == arSubs[y][0][1])
		{
			return y;
		}
	 }
}

function sortArray(a1, a2)
 {
	return 	a1[0] < a2[0] ? -1 :
			a1[0] > a2[0] ? 1 : 0;
}

function loadRegionalImages(x) 
{				    				    				    
	arSelected[x] = new Image(415,227);
	arSelected[x].src = '/script/map/images/'+arSubs[x][0][1]+'_selected_.gif';
	arRollover[x] = new Array();
	arRollover[x][0] = new Image(415,227);
	arRollover[x][0].src = '/script/map/images/'+arSubs[x][0][1]+'_rollover_.gif';
	for (y = 0; y < arSubs.length; y++)
	 {
		if (x != y) 
		{ // make sure we're not creating ww_select_ww_roll or similar redundencies
			if (arSubs[x][0][1] != 'cac') 
			{ // don't preload for Central America / Carribean
				arRollover[x][y] = new Image(415,227);
				arRollover[x][y].src = '/script/map/images/'+arSubs[x][0][1]+'_select_'+arSubs[y][0][1]+'_roll_.gif';
			}
		}
	}
}		

function ShowToolTip(object,RegionAbbr)
{
	var nMORegionID = dictRegions(RegionAbbr);
	if (nMORegionID!=0) 
	{
	   tooltip.style.position = "absolute";
	   tooltip.style.top = event.clientY + document.body.scrollTop - 20;
	   tooltip.style.left = event.clientX + document.body.scrollLeft + 10;
	   tooltip.innerHTML = arSubs[nMORegionID][0][0],-1;
	   tooltip.style.display = "block"; 
	   tooltip.style.index = 1;
	}
}
function Swapin(RegionAbbr)
{
	if (document.images) 
	{
		var nMORegionID = dictRegions(RegionAbbr);
		if (nMORegionID != nActiveRegionID) 
		{
			document.getElementById('wwimage').src = arRollover[nActiveRegionID][nMORegionID ].src;
		}
	}
}

function Swapout()
{
	if (document.images) 
	{
		document.getElementById('wwimage').src = arSelected[nActiveRegionID].src;
		tooltip.style.display = "none";						
	}
}				

function goRegion(RegionAbbr) 
{
	var oSelTitle = document.getElementById('selCountryTitle');
	var oSel = document.getElementById('selCountryID');
	var oShowAll = document.getElementById('ShowAll');
	nActiveRegionID = dictRegions(RegionAbbr);
	oSel.innerHTML = '';
	loadRegionalImages(nActiveRegionID);
	document.getElementById('wwimage').src = arSelected[nActiveRegionID].src;
	if (nActiveRegionID==0) 
	{
		oSelTitle.innerHTML = sSelDefault;
		oSelTitle.style.color = "#333333";
		if (showAll != null) {
			showAll.innerHTML = "";
			//Fix. default highlight the "World Wide Partner Portal Home page"
			document.getElementById('selCountryID').focus();
		}
		//  Add all countries sorted by alpha
		addOption(arWW[0],arWW[1]);
		for (var s = 0; s < arCountries.length; s++) 
		{
			if (s == 0)
			{
				addOption(arCountries[s][0],arCountries[s][1]);
			}
			else
			{

			   if (arCountries[s][0] != arCountries[s-1][0])
				{
					addOption(arCountries[s][0],arCountries[s][1]);
				}
			}							
		}
        oShowAll.style.visibility = "hidden";
	} 
	else 
	{
		addCountries(nActiveRegionID);

		// display title when selected
		oSelTitle.innerHTML = arSubs[nActiveRegionID][0][0],-1;
		if (showAll != null) {
			showAll.innerHTML = showAllText;
		}
		oSelTitle.style.color = "#F87311";			
        oShowAll.style.visibility = "visible";
	}
	oSel.selectedIndex = 0;
}

function addRegions(exclude1, exclude2) 
{
	var oSel = document.getElementById('selCountryID');
	for (var r = 0; r < arRegions.length; r++) {
		if (arRegions[r]) 
		{
			if (arRegions[r][1] != exclude1 && arRegions[r][1] != exclude2) 
			{
				addOption(arRegions[r][0],arRegions[r][1]);
			}
		}
	}	
}

function addCountries(RegionID) 
{
	for (var c = 1;c < arSubs[RegionID].length; c++) 
	{
		addOption(arSubs[RegionID][c][0],arSubs[RegionID][c][1]);
	}
}

function addOption(name, value) 
{
	var oSel = document.getElementById('selCountryID');
	if (name == '' && value == 0) 
	{
		oSel.innerHTML = '';
	} 
	else 
	{
		oSel.options[oSel.options.length] = new Option(name, value);
	}
}

function goToSelected() 
{
	var sel = document.getElementById('selCountryID');
	c = sel.options[sel.options.selectedIndex].value;
	document.getElementById('siteID') = c;
	
	if (isNaN(c)) 
	{ 	// it's a URL
		document.location = sel.options[sel.options.selectedIndex].value;
	} 
	else 
	{ 	// it could be a ResourceID (> 100) or a RegionID (< 100)
		// this isn't scalable past 100 Regions ;-)
		if (c < 100) 
		{
			if (c > -1) goRegion(arSubs[c][0][1]);
			return;
		} 
		else 
		{	// if it's more than 100, then it's a ResourceID (e.g. 3923300 = WW Partner Home)
			document.location = c;
		}
	}
}

// sort arrays for alphabetical listing of all countries
makeSortableArrays();
// on page load, load all countries (ww)
goRegion('ww');
