// ensure CVS revision number survives minification
var dummy = '$Id: pre_bir_push.js,v 1.1.2.24 2010-02-05 13:53:46 smansing Exp $';

document.pre_bir_listeners = {};


function pre_bir_listen(ev_id, init_msg_id, redirect, redirect_popup_id, bids_container, bids_inner) {

	document.pre_bir_listeners['redirect,'       + ev_id] = redirect;
	document.pre_bir_listeners['bids_container,' + ev_id] = bids_container;
	document.pre_bir_listeners['bids_inner,'     + ev_id] = bids_inner;
	document.pre_bir_listeners['redirect_popup_id,' + ev_id] = redirect_popup_id;

	var padded_ev_id = ps_connect_lpad_id(ev_id);
	var channels = ["sEVENT" + padded_ev_id];
	ps_connect_register ("pre_bir_listen_" + ev_id, pre_bir_listen_handle_msg, channels, init_msg_id);
}



function pre_bir_listen_handle_msg(msg) {

	var data = {}

	if (msg.subject_level == "sEVENT") {
		eval("data = " + msg.data + ";");
		data.ev_id = msg.subject_id;

		// first the bids status
		bids_status_update(data);

		if (document.pre_bir_listeners['redirect,' + data.ev_id] == true) {
			setup_bir_redirect(data);
		}
	}
}



/* Bids Status handling*/
function bids_status_xl(bids_status) {

	if (document.bids_status_xlations && document.bids_status_xlations[bids_status]) {
		return document.bids_status_xlations[bids_status];
	} else {
		return '';
	}

}


function bids_status_update(data) {

	var ev_id = data.ev_id;
	var container = document.pre_bir_listeners['bids_container,' + ev_id];
	var inner     = document.pre_bir_listeners['bids_inner,'     + ev_id];

	if (inner) {
		inner.innerHTML = bids_status_xl(data.bids_status);
	}

	if (container) {
		if (inner.innerHTML != '') {
			container.style.display = '';
		} else {
			container.style.display = 'none';
		}
	}
}
/* End of Bids Status handling */



/* Check if a bir redirect is needed */
document.showEventStartTimer = -1;

function setup_bir_redirect(ev_msg) {

	if (document.lb_pre_bir_no_check_redirect) {
		// we've been told not to check for redirects
		// this is most likely because we are already viewing the BIR event
		return;
	}

	var ms_before_start = -1;
	var ev_id = ev_msg.ev_id;
	var current_time_ms = Date.parse(Date());
	
	
	/*
	 * Here's the redirect logic:
	 *
	 * - we reach the Suspend_At time
	 * - Suspend_At time is null and Is_Off = Y
	 * - Suspend_At time is null and Is_Off = N/A and we reach the Start Time
	 * - Status = S and Displayed = N and Is_Off = N/A
	 */
	
	if (ev_msg.status == 'S' && ev_msg.displayed == 'N' && ev_msg.is_off == '-') {
		
		// Redirect now!
		ms_before_start = 0;
		
	} else if (typeof ev_msg.suspend_at == 'string' && ev_msg.suspend_at != '') {
		
		// Redirect after the suspend_at time expires
		ms_before_start = _time_in_milliseconds(ev_msg.suspend_at) - current_time_ms;
		
	} else if (ev_msg.is_off == 'Y') {
		
		// Redirect now!
		ms_before_start = 0;
		
	} else if (ev_msg.is_off != 'N' && typeof ev_msg.start_time == 'string' && ev_msg.start_time != '') {
		
		// Redirect after the start time expires
		ms_before_start = _time_in_milliseconds(ev_msg.start_time) - current_time_ms;
		
	}

	clearTimeout(document.showEventStartTimer);
	if (document.show_popup) {
	
		if (ms_before_start == 0 || ms_before_start < -1) {
			showEventStartNotification(ev_id);
	
		} else if (ms_before_start != -1) {
			document.showEventStartTimer = setTimeout('showEventStartNotification(' + ev_id + ')', ms_before_start);
			
		} else {
			/* there is nothing to do here! */
		}
	
	}
}



function showEventStartNotification(ev_id) {

	var redirect_popup = null;
	
	// do we have an ev specific popup defined?
	if (document.pre_bir_listeners['redirect_popup_id,' + ev_id]) {
		redirect_popup = document.getElementById(document.pre_bir_listeners['redirect_popup_id,' + ev_id]);
	}
	
	if (!redirect_popup) {
		redirect_popup = document.getElementById('event_link_popup');
	}

	if (redirect_popup) {
		redirect_popup.style.display = '';
	}

	// perhaps the app is configured to simply show a link
	if (document.pre_bir_redirect_link_only == true) {
		// actually there is nothing more to do here as the popup is already being displayed
	} else {
		// Decide how long to wait.
		// This should be a value between 0 and the value bound to max_wait
		max_wait        = document.pre_bir_redirect_max_wait;
		random_timeout  = Math.floor(Math.random() * max_wait);
		
		// Now we create a new timer, which will take the customer to the race's
		// BIR page.
		setTimeout('goBIREvent()', random_timeout);
	}

}



function _time_in_milliseconds(time_frm) {
	
	return Date.parse(
		new Date (
			parseInt(time_frm.substring(0, 4), 10),
			(parseInt(time_frm.substring(5, 7), 10) - 1),
			parseInt(time_frm.substring(8, 10), 10),
			parseInt(time_frm.substring(11, 13), 10),
			parseInt(time_frm.substring(14, 16), 10),
			parseInt(time_frm.substring(17), 10)
		)
	);
}



function pre_bir_lb_listen(pre_bir_ev_id, event_only, init_msg_id) {

	// Event subscription (note, we have to listen on the entire event, as we need to listen for price changes too)
	var padded_ev_id = ps_connect_lpad_id(pre_bir_ev_id);
	var channels = [];
	if (event_only) {
		var channels = ["sEVENT" + padded_ev_id];
	} else {
		var channels = ["sEVENT" + padded_ev_id, "SEVENT" + padded_ev_id];
	}
	ps_connect_register ("pre_bir_lb_listen_" + pre_bir_ev_id, pre_bir_lb_got_msg, channels, init_msg_id);
}



function pre_bir_lb_got_msg(msg) {

	var data = {};

	if (msg.subject_level == "sEVENT") {
		eval("data = " +  msg.data + ";");
		data.ev_id = msg.subject_id;
		_lb_evt_got_EVENT(data);
		setup_bir_redirect(data);

	} else if (msg.subject_level == "sEVMKT") {
		eval("data = " +  msg.data + ";");

		// we have to lie somewhat, because we have to pretend that the market
		// is actually for the BIR event.
		data.ev_id = _lb_evt_event.ev_id;
		data.ev_mkt_id = msg.subject_id;
		try {
			_lb_evt_got_EVMKT(data);
		} catch (err) {}

	} else if (msg.subject_level == "sPRICE") {
		// we have a price change. We simply call the lb_evt msg handler.
		// Because we are in show_pre_bir mode, the market & selns on the
		// BIR page are actually from the pre-bir event.
		// Thus, we can handle the push message about the pre-bir price change.
		eval("data = " +  msg.data + ";");
		data.ev_oc_id = msg.subject_id;
		try {
			_lb_evt_got_PRICE(data);
		}
		catch (err) {}

	} else if (msg.subject_level == "sSELCN") {
		// see the comments above
		eval("data = " +  msg.data + ";");
		data.ev_oc_id = msg.subject_id;
		try {
			_lb_evt_got_SELCN(data);
		}
		catch (err) {}

	} else {
		// Nothing to do or see here!
	}
	
	return;
}


function pre_bir_lb_set_bids_status(msg) {
	
	new_bids_status = bids_status_xl(msg.bids_status);
	
	if (new_bids_status != '') {
		_lb_hr_evt_event.BIDS_status = new_bids_status;
	}
	else {
		_lb_hr_evt_event.BIDS_status = '';
	}
	
}
