// JavaScript Document
/*$(document).ready(function(){
	/*$("#logo").click(function () {
		$("#homePopUp").show();				   
	  })
	.end();
	Lightview.show({ href: 'http://www.google.com', rel: 'iframe', options: { width: 800, height: 500 }});
}*/

//jQuery.noConflict();

//initialize the lightview (popup window)
document.observe('lightview:loaded', lightViewLoaded);

function lightViewLoaded() {
	var lvLoaded = true;	
	if(document.URL.include('index.html') || !(document.URL.include('.html'))) {
		if(!(document.URL.include('.asp'))) {
		 	loadHomepage();
		}
	 }
}

//Show the homepage lightview box
function loadHomepage () {
	Lightview.show({
      href: 'home.html',
      rel: 'iframe',
	  radius: 1,
      options: {
      	topclose: true,
      	width: 712,
	    height: 685
      }
  });
}

function closeHomepage () {
	Lightview.hide();
}

function activateTab (obj) {
	obj.observe('click', respondToClick);
}

function respondToClick(event) {
  var element = Event.element(event);
  element.addClassName('selected');
}

function gotoPage(form){
    var url = form.issues.options[form.issues.selectedIndex].value;
    if (url != "") {
        location.href = url;
    }
}

function showViewedPages() {
	recentPages = pages.get('recentPages');
	if(recentPages.pageTitles) {
		for(var i = 0; i < recentPages.pageTitles.length; i=i+1) {
			var string = "<li><a href='" + recentPages.pageURLs[i] + "'>" + recentPages.pageTitles[i] + "</a></li>";
			$('recentlyViewed').insert(string);	
		}
	}
}

function storePage (txt) {
	pages = new CookieJar({
		expires:3600, // seconds
		path: '/'
	});
	if(pages.get('recentPages')) {
		pTitles = pages.get('recentPages').pageTitles;
		pURLs = pages.get('recentPages').pageURLs;
	}
	else {
		pTitles = [];
		pURLs = [];
	}
		
	if(pTitles.indexOf(txt) == -1) {
		pTitles.push(txt);
		pURLs.push(document.URL);
	}
	data = {pageTitles: pTitles, pageURLs: pURLs};
	pages.put('recentPages', data);
}


// Read a page's URL variables and return them as an associative array.
function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&amp;');
	   
	for(var i = 0; i < hashes.length; i++) {
	  hash = hashes[i].split('=');
	  vars.push(hash[0]);
	  vars[hash[0]] = hash[1];
	}
	return vars;
}

//change the page based on its URL variables 
function switchPage() {
	var urlVars = getUrlVars();
	
	switch(urlVars["issue"]) {
 	case "01":
		location.replace("males.html?iss=01&amp;country=" + urlVars["country"]);
	    break;
	case "02":
		location.replace("growing.html?iss=02&amp;country=" + urlVars["country"]);  
		break;
	default: 
	    //no issue variable
		//alert(vars["issue"]);
	}
	
	if(urlVars["iss"]) {
		var issue = urlVars["iss"];	
	}
	
	if(urlVars["country"]) {  
	  var country = urlVars["country"];
	  
	  mapLoad(issue, country);
	}
	//
}

function mapLoad(issue_str, country_str) {
	//var issue_str = "02";
	//if (issue_str == "") {
	//	alert("No Issue found");
	//} else {
		//fmEngine.SetVariable("_root.outsideIssue", issue_str); 
		
		new PeriodicalExecuter(function(pe) {
		    if(fmEngine.TCallLabel) {
			  pe.stop();
			  fmEngine.SetVariable("_root.outsideIssue", issue_str); 
			  fmEngine.SetVariable("_root.outsideCountry", country_str); 
			  fmEngine.SetVariable("_root.outsideAction", "mapLoad"); 
			  fmEngine.TCallLabel("_root.outside_mc", "outsideAction");
			  
		    }
		    else {
			  alert("function not loaded");
		    }
		  }, 10);
	//}
}

function loadPage(url) {
	if (url != "") {
		location.href = url;
	}
}

/*function mapLoad() {
	id_str = $('map').value;
	alert(id_str);
	fmEngine.SetVariable("_root.outsideMap", id_str); 
	fmEngine.SetVariable("_root.outsideAction", "mapLoad"); 
	fmEngine.TCallLabel("_root.outside_mc", "outsideAction");
}*/