/*
Given a start time of an event, waits until it's 20 minutes away and then starts checking
the event to see if 1) the event state changes or 2) we are past the start time and an asset is available.
When it the necessary conditions are hit, it activates a modal display for a couple seconds, and makes the changes.
It takes a couple parameters:
start - when the event is due to start in timestamp format
dom - the id of the dom object that's gonna have it's contents reloaded
asset - an object that contains the ref_type and optionally 'data' for the asset that should trigger the change. eg { 'ref_type' : 401, 'data' : 'archive'}
url - the url to the php that will generate the new content
*/

function event_state_checking(text_id, start, end, curr_state, dom, asset, url) {

	//set up the timer - if start is in the future
	//find out how long until 20 min before the event
	var dateObj = new Date();
	
	var now_ts = dateObj.getTime() / 1000;
	
	//we're past the need to check when the event is closed for 1 hour
	var check_until =  (end*1)+60*60;

	if( now_ts >= check_until ){
		return;
	}
	
	if (curr_state == false) {
	   curr_state = 0;
	}

	var start_checking_in = Math.max(( ( start - 20 * 60 ) - now_ts ) * 1000, 0);

	//set a timer to wait until 20 min until the event, and then check onece a minute
	window.setTimeout(function(){ event_state_action( text_id, start, check_until, curr_state, dom, asset, url ); }, start_checking_in );

}


function event_state_action( text_id, start, check_until, curr_state, dom, asset, url ) {

	//set a timer to check the state every minute
	var state_check = window.setInterval(function(){
	
		//cleanup if we don't need it anymore
		var dateObj = new Date();
	
		var now_ts = dateObj.getTime() / 1000;

		var end_ts = check_until*1 - 60*60;

		if( now_ts >= check_until || curr_state == 0 && now_ts > end_ts ){
		
			clearInterval(state_check);
			return;
		
		}
	
		//check the stuff
		new Ajax('/pages/event/common/event_state_check.php?event=' + text_id + '&rand=' + now_ts, 
				{
					method: 'get',
					onComplete: function(data){
					 //parse the data
	
					var dataObj = Json.evaluate(data, true);

						if ( typeof( dataObj ) == 'object' && dataObj['state'] != curr_state ) { //there's a state change
						
							event_state_dom_process( dom, url );
							
							curr_state = dataObj['state'];
							
						} else if ( typeof( asset ) == 'object' ) {
						
							for ( assoc in dataObj['assets'] ) {
							
								if( assoc['ref_type'] == asset['ref_type'] && assoc['data'] == asset['data'] ) {
								
									event_state_dom_process( dom, url );
									
								}
							
							}
																
						}
						
					}
					
				}).request();
								
	} , 55*1000);
}

function event_state_dom_process( dom, url ) {

	//display a modal?  too disruptive? no for now.
	//load the new content
	var dateObj = new Date();
	
	var now_ts = dateObj.getTime() 
	
	new Ajax( url + '&rand=' + now_ts, { method: 'get', evalScripts: true, update: $(dom) }).request();

}