var _exploreGovtContainerId = "mygovernment ul"; <!-- id of the container Explore Government <div> -->
var _exploreGovtULId = "explore"; <!-- id given to the <ul> tag which holds the itemized ministry links -->
var _lang = "en";  <!--values can be "fr" or "en" -->

/**
 * General config vars, to send to server
 */

var ERROR = "FILE NOT FOUND";
var serviceUrl;
var exploreGovtContainerId = "exploreGovt";
var exploreGovtULId = "explore";
var lang = "en";

$(function(){
	if(location.protocol === 'https:'){
		serviceUrl = "https://www.plugins.gov.on.ca/exploregovernment/exploregovt.php";
	}
	else{
        serviceUrl = "http://www.plugins.gov.on.ca/exploregovernment/exploregovt.php";
	}
	
	if(typeof _exploreGovtContainerId!='undefined'){
		exploreGovtContainerId = _exploreGovtContainerId;
	}
	if(typeof _exploreGovtULId!='undefined'){
		exploreGovtULId = _exploreGovtULId;
	}
	if(typeof _lang!='undefined'){
	 lang = _lang;
	}
	getData();
});

function getData(){
	serviceUrl += "?format=json&lang="+lang;
	$.ajax({
		'url': serviceUrl,
		'type': 'GET',
		'dataType': 'jsonp',
		'cache': true,
		'success': function(data) { formatData(data); }
	});
}

function formatData(jsonResponse){
	if(jsonResponse==ERROR || jsonResponse==null){
		return;
	}
	eval(jsonResponse);
	var itemArray = jsonResponse.ministry;
	var ul = $("<ul></ul>");
	
	$(ul).attr("class","collapsible menu");
	$(ul).attr("id",exploreGovtULId);
	
	$.each(itemArray, function(index,value){
		$("<li><a href='"+value["@attributes"].link+"'>"+value.name+"</a></li>").appendTo(ul);
	});
	$("#"+exploreGovtContainerId).replaceWith(ul);
}
