//Sandbox Engine
//PHP AJAX JSON Controller and front-end functions file

//Used for convenient internal variables
var SB_System=new Object();

//If you ever set systemLock to true, then this prevents user interface actions from taking place
SB_System.systemLock=false;

$(document).ready(function(){
	//You can bind events and whatnot here as you would in any other case. 
	//This here is a placeholder for your convenience.
	$('.beforeafter').lightBoxNew({fixedNavigation:true});
	$("#before").beforeAfter2({animateIntro:false,showFullLinks : false,imagePath:'/controllers/'});
	
	  $('#fader').show().innerfade({
	    speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '260px'
	  });
	$(".switch").each(function() {
		$(this).click(function() {
			$(".switched").hide();
			$("#"+$(this).attr("name")).show();
			return false;
		});
	});
	

	$(".check").click(function() {
		var checkboxid = $(this).attr("id");
		if($("."+checkboxid).val()=="true"){
			$("."+checkboxid).val("false");
			$(this).css("background-image","none");
		}else{
			$("."+checkboxid).val("true");
			$(this).css("background-image","url(" + basedir + "/design/checked2.png)");
		}
		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){
		
			//This is a template and shows how the system works
			case 'simpleExample':	
				//Most UI actions are usually simple and don't require AJAX, this simply throws an alert
				alert('Hello world!');
			break;
			
			//This is a template and shows how the system works
			case 'ajaxExample':			
				//this does not have to be the same as action, but usually is
				input['testvariable']='testing';
				executeAjaxAction(action,input);
			break;
			
			
			case 'campainFeedback':
				input['name']=$('input[name="campname"]').val();
				input['email']=$('input[name="campemail"]').val();
				input['phone']=$('input[name="campphone"]').val();
				input['campid']=$('input[name="campid"]').val();
				input['campcsi']=$('input[name="campcsi"]').val();
				input['campid']=$('input[name="campid"]').val();
				executeAjaxAction(action,input);
			break;				
			
			case 'reader':			
				SB_Reader(data);
			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, ajaxurl){
	if(!ajaxurl){ ajaxurl='/controllers/ajax.php'; }
	//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: true,
			data: ({action : action, data : data}),
			beforeSend: function(header){
				// 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']){
		
			//this is used for example, it simply throws an alert
			case 'exampleAction':
				alert(json['message']);
			break;
			
			//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;
			case 'campainFeedback':
				if(json['type']=='1'){
					$('.errortext').html('');
					// alert(json['type']);
					// $('.successtext').html(json['message']);
					document.location.href=json['message'];
				}else{
					// alert(json['type']);
					$('.errortext').html(json['message']);
				}
			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!');
		}
	}
}
