/**
 * 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', // this adds &callback=? by design
		'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","menu");
	$(ul).attr("id",exploreGovtULId);
	$(ul).css('display','none');	
	
	$.each(itemArray, function(index,value){
		$("<li><a href='"+value["@attributes"].link+"'>"+value.name+"</a></li>").appendTo(ul);
	});
	$(ul).appendTo("#"+exploreGovtContainerId);
}


