
var country_array;	// global variable
var inst_array;		// global variable
var enable_rollover;    // global variable
enable_rollover = 1;
readAndEvalJsonData();

function readAndEvalJsonData() {
var country_data_url 	= "inc/country.json";  
var inst_data_url 	= "inc/institution.json";  
new Ajax.Request ( country_data_url, 
    {
	    method: "GET", 
	    onSuccess: function (oXHR, oJson) {
		country_array=oXHR.responseText.evalJSON()
		for (var i=0; i<country_array.length; i++) {
		    country_array[country_array[i].country_code] = country_array[i]; 
		}
	    },
	    onFailure: function (oXHR, oJson) {
	    }
    }
)
new Ajax.Request ( inst_data_url, 
    {
	method: "GET", 
	onSuccess: function (oXHR, oJson) {
	    inst_array=oXHR.responseText.evalJSON()
	    for (var i=0; i<inst_array.length; i++) {
		inst_array[inst_array[i].inst_code] = inst_array[i]; 
	    }
	},
	onFailure: function (oXHR, oJson) {
	}
    }
)
}

function $id(id) {
    return document.getElementById(id);
}

function processCountryClick(countryCode) {
	
    replaceMap(countryCode);
    showInstitutions(countryCode);
}

function showInstitutions(countryCode) {
	
    if(enable_rollover == 1) {
      var html = "";  
      var instCode;
      var inst_list;
      var the_city_name;
      html = "<h3>" + country_array[countryCode].country_name + "</h3>"
      inst_list = country_array[countryCode].country_institutions
      for(i = 0 ; i < inst_list.length ; i++) {
	    instCode = inst_list[i]
	    the_city_name = inst_array[instCode].city_name
	  html += '<div id="' + instCode + '" class="inst_short">'
	    + '<b>' + makeInstLink(instCode) + '</b><br />'
	    + the_city_name + '</div>'
      }
      $id('country_listing_pane').innerHTML = html;
    }
}

function showOneInstitution(instCode) {
    var html;
    html = '<div class="inst_name">' + makeInstLink(instCode) + '</div>' + imageTag(inst_array[instCode].image_url) ;
    $id('one_inst_listing_pane').innerHTML = html;
    $id('one_inst_listing_pane').style.display = "block"
}

function makeInstLink(instCode) {	// make a link to show institution details
    return "<a href='" + inst_array[instCode].url + "'>" + 
	    inst_array[instCode].inst_name + "</a>";    
}

function imageTag(image_url) {
    return image_url ? '<img src="' + image_url + '" />' : ''
}

function lightupCity(city_code) {
    for(var i = 0; i < inst_array.length; i++) {
	instit = inst_array[i]
	if($id(instit.inst_code))
	    $id(instit.inst_code).style.backgroundColor = "#fcfeee"
	if(city_code == instit.city_code)
	    $id(instit.inst_code).style.backgroundColor = "#ffff77"
    }
}

function showWorldMap(countryCodeToHide) {
    enable_rollover = 1;
    $id('portal-breadcrumbs').innerHTML = makeBreadCrumbs('','');
    if (countryCodeToHide != '')
	Effect.Fade(countryCodeToHide, { queue: 'front' })
    Effect.Appear('worldmap', { queue: 'end', duration: 0.5 })
    $id('welcom-message-top').style.display = "block"
    $id('welcom-message-country').style.display = "none"
    clearOneInstListingPane();
    clearCountryListingPane();
}

function clearCountryListingPane() {
// clear the country listing when returning to world map
    $id('country_listing_pane').innerHTML = ""
}

function clearOneInstListingPane() {
// clear the last indiv institution listing when returning to world map
    $id('one_inst_listing_pane').innerHTML = ""
    $id('one_inst_listing_pane').style.display = "none"
}

function replaceMap(countryCode) {
	
	//alert($id('world-return').innerHTML);
    enable_rollover = 0;
    $id('portal-breadcrumbs').innerHTML = makeBreadCrumbs(countryCode,'');
    $id('world-return').innerHTML = makeLogoLink(countryCode);
	
	//alert($id('world-return').innerHTML);
	
	
    Effect.Fade('worldmap', { queue: 'front', duration: 0.5 })
    Effect.Appear(countryCode, { queue: 'end', duration: 0.5 })
	
    $id('welcom-message-top').style.display = "none"
    $id('welcom-message-country').style.display = "block"
}

function makeLogoLink(countryCode) {
    var logolink;
    logolink = "<a href='#' onclick=\"showWorldMap('" +
               countryCode + 
	       "')\"><img src=\"inc/world_return.gif\" />"
    return logolink;
}

function makeBreadCrumbs(countryCode, instCode) {
    var bread;
    var country_name;
    if (countryCode=="") {
	    //bread = '<a href="../../index.php">Home</a> &gt; About AC21 ';
		bread = '<a href="../../index.php">Home</a> &gt; <a href="../who/about.php">About AC21</a>  &gt; <a href="../members/members.php">Members &amp; Partners</a> &gt; Where we are';
    }
    else if (instCode=="") {
	    /*bread = "<a href='#' onclick=\"showWorldMap('" +
		    countryCode + "')\">Home</a> &gt; About AC21 &gt; " +
		    country_array[countryCode].country_name;*/
		bread = "<a href='../../index.php'>Home</a> &gt; <a href='../who/about.php'>About AC21</a>  &gt; <a href='../members/members.php'>Members &amp; Partners</a> &gt;<a href='#' onclick=\"showWorldMap('" +
		    countryCode + "')\">Where we are</a> &gt;  " +
		    country_array[countryCode].country_name;	
    }
    else {
	    /*bread = "<a href='#' onclick=\"showWorldMap('" +
		    countryCode + "')\">Home3</a> &gt; About AC21 &gt; " +
		    "<a href='#' onclick=\"replaceMap('" + countryCode + "')\">" + 
		    country_array[countryCode].country_name + "</a> &gt; " +
		    inst_array[instCode].inst_name;*/
			bread = "<a href='../../index.php'>Home</a> &gt; <a href='../who/about.php'>About AC21</a>  &gt; <a href='../members/members.php'>Members &amp; Partners</a> &gt;<a href='#' onclick=\"showWorldMap('" +
		    countryCode + "')\">Where we are</a> &gt; " +
		    "<a href='#' onclick=\"replaceMap('" + countryCode + "')\">" + 
		    country_array[countryCode].country_name + "</a> &gt; " +
		    inst_array[instCode].inst_name;
    }
    return bread;
}

function showCurrentTimeOffset() {
    var d = new Date()
    var utc = new Date(d.getTime() + (d.getTimezoneOffset() * 60000))
    return utc.getHours()
}

// from: http://simon.incutio.com/archive/2004/05/26/addLoadEvent
// This is last to ensure that this file has been completely parsed before
// it is used by other files.
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
	window.onload = func;
    else
	window.onload = function() {
	    func();
	    oldonload();
	}
}

