var _HOMEDIR = "http://www.shadybee.com/index.php/home/";
var _IMGDIR = "http://www.shadybee.com/public/images/";
var _MENU_PREFIX = "#menu_"

$(window).load(function(){
	$('#preloader').fadeOut('slow',function(){$(this).remove();});
});

$(document).ready(function() {
	// Execute on Page Load	
	getPage(_HOMEDIR+"profile");
	setMenuImg("home_selected", "home");
	
	// Add menu event listeners	
	var menuItems = ["profile", "portfolio", "projects", "contact"];
	$.each(menuItems, function(intIndex, nm) {
		$(_MENU_PREFIX+nm).click(function () {
			getPage(_HOMEDIR+nm);
			setMenuImg(nm+"_selected", nm);
			resetMenu($(this).attr('id'));
		});		
	});	
});

function setMenuImg(img, nm) {
	var item = $(_MENU_PREFIX+nm);
	item.empty();
	item.append("<img src=\""+_IMGDIR+"btns/"+img+".png\" alt=\""+nm+"\" />");
}

function resetMenu(id) {
	var _PREFIX = "menu_";
	var menuItems = ["profile", "portfolio", "projects", "contact"];
	$.each(menuItems, function(intIndex, nm) {
		if(_PREFIX+nm != id) {
			setMenuImg(nm+"_deselected", nm);
		}
	});
}

function getPage(link) {
	var msg = $("#msg");
	var msgBack = $("#msgBack");
	$.ajax({
		type: "GET",
		async: false,
		url: link,
		beforeSend: function() {
			if (msgBack.is(":hidden")) {
				msgBack.slideDown("medium");
			}
			msg.fadeIn("fast");
			msg.text("Loading..");
		},
		error: function (xhr, desc, thrownError) {
			msg.text("An error has occured, Please try again");
			msg.queue(function(){ 
				setTimeout(function(){ 
				msg.dequeue(); 
				}, 10000 );	
			});
			msg.fadeOut("slow");			
			msgBack.queue(function(){ 
				setTimeout(function(){ 
				msgBack.dequeue(); 
				}, 10000 );
			});
			msgBack.slideUp("slow");
		},
		success: function(html) {
			$("#subContent").empty();
			$("#subContent").append(html);
			msg.text("Loading Complete");
			msg.queue(function(){ 
				setTimeout(function(){ 
				msg.dequeue(); 
				}, 2000 );
			});
			msg.fadeOut("slow");
			msgBack.queue(function(){ 
				setTimeout(function(){ 
				msgBack.dequeue(); 
				}, 2000 );
			});
			msgBack.slideUp("slow");
		}
	});
}

function test() {

}
