
function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
	ft = gy[i].split("=");
	if (ft[0] == ji) {
		return ft[1];
		}
	}
}

function calcTime(city, timezoneStr, offset) {
    // create Date object for current location
    d = new Date();

    // convert to msec, add local time zone offset, get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
   
    // create new Date object for different city using supplied offset
    nd = new Date(utc + (3600000*offset));
   
    // return time as a string
    //return "The local time in " + city + " is " + nd.toLocaleString();

	if (querySt("override") == true || querySt("override") == "true" ) {
		overRideStr = " <a href='#' onclick='loadSWFObject()'>Show it anyway</a>."
	} else {
		overRideStr = "";
	}
	
	if ( nd.getTime() < start.getTime()) {
   		return "<strong>This event has not started yet.</strong><br /><br />Live streaming will begin on " + start.toLocaleString() + " " + timezoneStr + ". " + "Refresh your browser at that time to see the live player in this space." + overRideStr;
	}
	
	else if ( nd.getTime() > end.getTime()) {
		return "<strong>Sorry, streaming coverage of this event is over.</strong> The event ended on " + end.toLocaleString() + " " + timezoneStr + "." + overRideStr;
	}
	
	else {
		return 0;
	}	
	
}

function loadSWFObject() {
	var so = new SWFObject('http://www.publicradio.org/forum/lib/swf/mediaplayer.swf','mpl','320','260','8');
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
	so.addVariable('height','260');
	so.addVariable('width','320');
	so.addVariable('type', 'rtmp');
	
	/* publicradio flashvars */
	so.addVariable('file','rtmp://205.234.238.67/live/forum/');
	//so.addVariable('logo','../lib/img/apm_logo_horiz_reverse.png');
	so.addVariable('id','livestream');
	so.write('player');
}

function instantiatePlayer() {

    statusStr = calcTime('St. Paul', 'CDT', '-5');
	
	if (statusStr!=0) { $("#player").prepend("<p class='alert'>" + statusStr + "</p>"); }
	
	if (statusStr==0) { 
		loadSWFObject();
	} else {
		$("p.accessible").replaceWith("");
	}
	
}
