// JavaScript Document
var positionSave = new Object();
var isLoad = new Object();
var cupos = "";
var lld = 1;

function ldingDiv(){
	lld++;
	lld%=4;
	var msg = "";
	for(var i =0; i < lld; i++){
		msg += '.';		
	}
	$(".loadingSpan").text("Loading Page "+msg);
	setTimeout("ldingDiv",1000);
}

function loadPage(i){	
	i = i.replace("#","");
	if(isLoad[i] > 0) return;
	//console.log("LOAD PAGE "+i);	
	isLoad[i] = 1;	
	$('#'+i).load(i+'.php', function(response, status, xhr) {	  
	  if (xhr.statusText == "") {
		   //console.log("LOAD FAILED ");
		   isLoad[$(this).attr("id")] = 0;
			var msg = "Sorry but there was an error: ";
			$(this).text(msg + xhr.status + " " + xhr.statusText);
			//$(this).append('<br /><a href="javascript:void(0)" class="reload" >Please Click Here to Reload Page</a>');
	  } else {		  
		  //console.log("LOAD COMPLETE "+$(this).attr("id"));
  		  isLoad[$(this).attr("id")] = 2;
		  if(response == ""){
			  //console.log("BLANK RESPONSE");
		  }
	  }
	});		
}

function checkPos(){
	var x = $(window).scrollLeft()+1366;
	var y = $(window).scrollTop()+650;		
	for( var i in positionSave){
		if(x > positionSave[i].left && y > positionSave[i].top){
			if(isLoad[i] > 0){
				////console.log("PAGE HAS LOAD "+i);
				continue;
			} else {
				//console.log("LOAD PAGE L: "+positionSave[i].left+", T:"+positionSave[i].top);									
				loadPage(i);	
			}
		}
	}
}

$(function() {
	
	///create scroller link
	$('a.nav').live('click',function(event){
		var $anchor = $(this);
		/*
		if you want to use one of the easing effects:
		$('html, body').stop().animate({
			scrollLeft: $($anchor.attr('href')).offset().left
		}, 1500,'easeInOutExpo');*/		 
		//$('html, body').stop().animate({
//			scrollLeft: $($anchor.attr('href')).offset().left
//		}, 1000);
		$.scrollTo($($anchor.attr('href')), 800, {queue:true});	
		////console.log("GOTO "+$anchor.attr('href'));
		cupos = $anchor.attr('href');
		////console.log("MOGO "+cupos);
		loadPage(cupos);
		event.preventDefault();
	});
	
	$('a.reload').live('click',function(event){
		//console.log("RELOAD CLICKED");
		checkPos();			
	});
	
	
	///get body element location
	$(".body div").each(
		function(){
			if($(this).attr("id").length < 1) return;			
			positionSave[$(this).attr("id")] = $(this).position();
			isLoad[$(this).attr("id")] = 0;
			//console.log("positionSave["+$(this).attr("id")+"] = "+positionSave[$(this).attr("id")].top+" | "+positionSave[$(this).attr("id")].left);			
			
		}
	);
	
	//console.log(positionSave);	
	
	///add event listener to scroll
	$(window).scroll(function () { 
		////console.log($(window).scrollTop());
		////console.log($(window).scrollLeft());
		checkPos();
	});
	
	///cek onload
	var parts = location.href.split('#');
	if(parts.length > 1)
	{
		loadPage(parts[1]);		
	}
	
	//$("#footer").ajaxError(function(event, request, settings){
//	  $(this).append("<li>Error requesting page " + settings.url + "</li>");
//	});	
	ldingDiv();
});


