//Sandbox Engine
//PHP AJAX JSON Controller and front-end functions file

//Used for convenient internal variables
var SB_System=new Object();
var menuState=0;

//If you ever set systemLock to true, then this prevents user interface actions from taking place
SB_System.systemLock=false;

$(document).ready(function(){
	if(window.location.hash){
		uiAction('loadUrl',window.location.hash);
	}
	var ua = $.browser;
	if ( ua.msie && (ua.version.slice(0,3) == "7.0" || ua.version.slice(0,3) == "6.0")) {
		//IE7 is unable to handle this interface
	} else {
		$('a').live('click',function(){
			var url=$(this).attr('href');
			if($(this).attr('direct')=='yes'){
				return true;
			} else {
				var pattern1=/http\:\/\//i;
				var pattern2=/https\:\/\//i;
				var pattern3=/javascript\:/i;
				if(url.match(pattern1) || url.match(pattern2) || url.match(pattern3)){
					return true;
				} else {
					if($(this).attr('previous')=='yes'){
						uiAction('loadContentPrevious',url);
					} else {
						uiAction('loadContentNext',url);
					}
					return false;
				}
			}
		});
	}
});

//This is front-end input controller. Data can be whatever you want, 
//you can use it in different context depending on action.
function uiAction(action,data){
	//input is an object used to be sent to AJAX backend.
	var input=new Object();
	if(SB_System.systemLock==false){
		switch(action){
			
			case 'loadContentNext':			
				document.location.href='#'+data;
				$('#intro').fadeOut(500);
				$('#slide').fadeOut(500);
				$('#pageturner').css('opacity','0');
				$('#pageturner').css('display','');
				$('#pageturner').animate({
					opacity: 0.30,
					width: '0'
				}, 500, function() {
					input['url']=data;
					executeAjaxAction(action,input);
				});
			break;
			
			case 'loadContentPrevious':		
				document.location.href='#'+data;
				$('#intro').fadeOut(500);
				$('#slide').fadeOut(500);
				$('#pageturner').css('opacity','0');
				$('#pageturner').css('margin-left','0px');
				$('#pageturner').css('display','');
				$('#pageturner').animate({
					opacity: 0.30,
					marginLeft:388,
					width: '0'
				}, 500, function() {
					input['url']=data;
					executeAjaxAction(action,input);
				});
			break;
			
			case 'menuControl':
				if(menuState==1){
					$('[direct]').slideUp(200);
					menuState=0;
				} else {
					$('[direct]').slideDown(200);
					menuState=1;
				}
			break;
			
			case 'loadUrl':
				input['url']=data;
				executeAjaxAction(action,input);
			break;
			
			default:
				alert('UI ERROR: This action ['+action+'] does not exist in user interface!');
		}
	}
}

//This should not be modified, it is standardized function for sending data to backend
//and retreiving the JSON string before forwarding it to frontend callback
function executeAjaxAction(action, rawdata, aSyncSetting){
	if(aSyncSetting!=false){ aSyncSetting=true; }
	ajaxurl='/controllers/ajax.php';
	if(!rawdata){ rawdata=new Object(); }
	//data is an array or an object serialized for use in a URL
	data=$.param(rawdata);
	//Lock is used to prevent action spamming
	//If you don't want to lock AJAX actions, then comment this out or at least display an error
	if(SB_System['lock_'+action]!=1){
		SB_System['lock_'+action]=1;
		$.ajax({
			type: 'POST',
			url: ajaxurl,
			dataType: 'json',
			cache: false,
			async: aSyncSetting,
			data: ({action : action, data : data}),
			beforeSend: function(header){
				//Recommended, however for IE browsers this is an unsupported method, so leave it commented out (for now).
				//header.overrideMimeType('text/plain');
				header.setRequestHeader('From',SB_SandboxKey);
			},
			success: function(json){
				//If successful the JSON returned from backend is forwarded to be parsed by frontend actions
				parseAjaxReturn(json);
				SB_System['lock_'+action]=0;
			},
			error: function(obj,msg,detailedmsg){
				//In case there was any unexpected error in the backend (PHP errors and whatnot), alert everything
				alert('ENGINE ERROR: '+msg+' ('+detailedmsg+')');
				SB_System['lock_'+action]=0;
			}
		});
	} else {
		//You can comment this alert out
		// alert('ENGINE ERROR: This action is already in progress');
	}
}

//This function takes the AJAX returned JSON and executes actions depending on the type
//You can call new user interface actions from here as well
function parseAjaxReturn(json){
	//If backend defines an error and a message for the error, alert this
	if(json['error']==1){
		alert('ERROR: '+json['message']);
	} else {
		switch(json['action']){
			
			//Do not remove this, it is used by backend engine in case session needs to be refreshed
			case 'refresh':
				document.location.href=document.location.href;
			break
			
			//Do not remove this, it is used by backend engine in case session needs to be refreshed
			case 'redirect':
				document.location.href=json['redirect'];
			break
			
			//Do not remove this, it is used by backend engine in case session needs to be refreshed
			case 'loadContentNext':
				document.getElementById('mainmenu').innerHTML=json['menu'];
				document.getElementById('maincontent').innerHTML=json['content'];
				// alert(json['content']);
				$('.intro').fadeIn(500);
				$('.slide').fadeIn(500);
				$('#pageturner').animate({
					opacity: 0,
					marginLeft:0,
					width: '388'
				}, 500, function() {
					$('#pageturner').css('display','none');
					$('#pageturner').css('margin-left','388px');
					// Animation complete.
				});
			break;
			
			//Do not remove this, it is used by backend engine in case session needs to be refreshed
			case 'loadContentPrevious':
				document.getElementById('mainmenu').innerHTML=json['menu'];
				document.getElementById('maincontent').innerHTML=json['content'];
				// alert(json['content']);
				$('.intro').fadeIn(500);
				$('.slide').fadeIn(500);
				$('#pageturner').animate({
					opacity: 0,
					width: '388'
				}, 500, function() {
					$('#pageturner').css('display','none');
					// Animation complete.
				});
			break;
			
			//Backend should always define action to be 'none' if no callback is defined
			case 'none':
			break;
			
			//In case no action was defined, alert an error
			default:
				alert('UI ERROR: Callback action ['+json['action']+'] does not exist in user interface!');
		}
	}
}
