$(function(){
	var i=0;
	$("#menu a[rel]").each(function() {
		$(this).data("pos",i++);
		
		if (ajaxLoad) {
			$(this).click(function() {			
				var $pageContainer=$("#content .section");	
				
				var href=$(this).attr("href");	
				var name=$(this).attr("rel");
				var pos=$(this).data("pos");
				
				loadSection(href,$pageContainer,name,pos);
				
				return false;
			})
		}				
	})
	
	
	
	
})



function loadSection(href,pageContainer,idName,pos) {
	moveImages(pos);			
	
	$("#menu li a").removeClass("current");
	$("#menu li a[rel="+idName+"]").addClass("current");
	
	
	$("#loader").show();
	$(pageContainer).fadeOut("fast",function() {	
		$.ajax({
			type: "GET",
			url: href,
			data: {json: 1},
			timeout: 25000,
			dataType: 'json',
			success: function(res,status) {	
	
				/* LOAD CSS */
				$(res.css).each(function() {
					
					if ($("head link[href='"+this+"']").size()==0) {					
						$("head").append('<link rel="stylesheet" href="'+BASE+"/"+this+'" type="text/css" />');
					}
				})
				
										
				/* LOAD JS */
				$(res.js).each(function() {
					if ($("head script[src='"+BASE+"/"+this+"']").size()==0) {
						$.getScript(this);
					}
				})
				
				
				/* .section DIV id repacement */
				pageContainer.attr("id",idName);
				
				/* ASSIGN CONTENT */			
				pageContainer.html(res.html);

				fancybox();
				$(pageContainer).fadeIn("fast");
				$("#loader").hide();	
		
			},
			error: function(t,s) {
				alert(s);	
				$("#loader").hide();		
			}
		})
	});
	
}

