$(document).ready(function(){
	
	$("#387, #388, #389, #390, #391, #392").hide();
	
	$("#continent a").click(function(e) {
		var href = $(this).attr("href");
		ajload(callhref(href));
		e.preventDefault();
	});
	
	$('#country a').live("click", function() {
		var href = $(this).attr("href");
		var bodylist = $("#country").data("bodylist");
		$("#partner_margin").html("<h3>"+bodylist[callhref(href)][0]+"</h3>"+bodylist[callhref(href)][1]);
	});
	
	function callhref(string) {
		var newref = string.replace(/#/g,"");
		return newref;
	}
	
	function ajload(spath) {
		$.ajax({
			type: "GET",
			url: "http://www.connexchain.com/index.php?spath="+spath+"&aj=1&sta=29",
			data: "name=John&location=Boston",
			dataType: "html",
			success: function(html){
				ajhandle(html);
			}
		});
	}
	
	function ajhandle(html) {
		var body = $(html);
		var bodylist = new Array();
		body.find("script").remove();
		body.find(".country_bit").each( function(i) {
			bodylist[i] = new Array();
			bodylist[i][0] = $(this).find(".country_name").html();
			bodylist[i][1] = "";
			$(this).find(".country_post").each( function() {
				if($(this).html() != "") {
					bodylist[i][1] = bodylist[i][1]+"<br /><p>"+$(this).html()+"</p>";
				}
			});
		});
		// $("body").append(bodylist.join("<hr />"));
		$("#country").data("bodylist",bodylist);
		fillcountry(bodylist);
	}
	
	function fillcountry(bodylist) {
		$("#country").html("");
		for(var bit = 0; bit < bodylist.length; bit++) {
			$("#country").append('<a href="#'+bit+'">'+bodylist[bit][0].replace(/:/g,"")+'</a>');
		}
		$("#partner_margin").html("<h3>"+bodylist[0][0]+"</h3>"+bodylist[0][1]);
		$('#country').jScrollPane({scrollbarWidth:8, scrollbarMargin:0, animateTo:true});
	}

	ajload(389);
	
});