/*
  Misc utilities for webcast pages
 */
function suffixNumber(number) 
{
	if ( number > 10 && number < 20 ) {
		return number+"th";
	}

  switch (number%10) {
  case 1: 
    return number+"st";
  case 2:
    return number+"nd";
  case 3:
    return number+"rd";
  default:
    return number+"th"
      }
  return number;
} 

function leftPadNumber(number)
{
	if ( number < 10 ) {
		return "0"+number;
	}
	return number;
}

webcastClass = function() {};

webcastClass.prototype.dowStrArr = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
webcastClass.prototype.moStrArr = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

webcastClass.prototype.setWebcastTime = function(year,month,day,hour,min,startBefore,endAfter) {
  this.webcastTime = new Date();
  this.webcastTime.setUTCDate(day);
  this.webcastTime.setUTCMonth(month-1);
  this.webcastTime.setUTCFullYear(year);
  this.webcastTime.setUTCHours(hour);
  this.webcastTime.setUTCMinutes(min);
  this.webcastTime.setUTCSeconds(0);
  this.webcastTimeStr = this.getWebcastTimeStr();
	this.startBefore = startBefore;
	this.endAfter = endAfter;
	this.start = this.webcastTime.getTime() - ( this.startBefore * 60 * 1000 );
	this.end =  this.webcastTime.getTime() + ( this.endAfter * 60 * 1000 );
}

webcastClass.prototype.getWebcastTimeStr = function() {
  var dow = this.webcastTime.getDay();
  var dowStr = this.dowStrArr[dow];
  //alert(webcastTime.toLocaleString());
  var lhour = this.webcastTime.getHours();
  if ( lhour == 12 ) {
		timeStr = '12:'+leftPadNumber(this.webcastTime.getMinutes())+' PM ';
  } else if ( lhour > 12 ) {
    timeStr = (lhour - 12)+':'+leftPadNumber(this.webcastTime.getMinutes())+' PM ';
  } else {
    timeStr = lhour +':'+leftPadNumber(this.webcastTime.getMinutes())+' AM ' ;
  }
	var localdatestr = this.moStrArr[this.webcastTime.getMonth()] + " " + suffixNumber(this.webcastTime.getDate());
	
  this.webcastDateStr = localdatestr + ", " + this.webcastTime.getFullYear();
  return dowStr + ", " + localdatestr + ", " + timeStr + " in your local time zone, "+this.webcastTime.getUTCHours()+":"+leftPadNumber(this.webcastTime.getMinutes())+" GMT ";
}

webcastClass.prototype.showWebcastTime = function() {
	$('#localdate').text(this.webcastTimeStr);
	$('#localdate-after').text(this.webcastDateStr);
}

webcastClass.prototype.setWebcastTZlink = function() {
	var lnk = 'http://www.timeanddate.com/worldclock/fixedtime.html?';
	lnk = lnk + 'month='+ ( this.webcastTime.getUTCMonth() + 1 );
	lnk = lnk + '&day=' + this.webcastTime.getUTCDate();
	lnk = lnk + '&year=' + this.webcastTime.getUTCFullYear();
	lnk = lnk + '&hour=' + this.webcastTime.getUTCHours();
	lnk = lnk + '&min=' + this.webcastTime.getUTCMinutes();
	lnk = lnk + '&sec=' + this.webcastTime.getUTCSeconds();
	$('#tzlnk').attr('href',encodeURI(lnk));
}

webcastClass.prototype.runstatus = function() {
	var now = new Date();
	var nowtime = now.getTime();
	if ( nowtime < this.start ) {
		return -1;
	} else if ( nowtime > this.end ) {
		return 1;
	}
	return 0;
}

webcastClass.prototype.showPopup = function() {
	var i = this.runstatus();
	
	//var winoptions = 'width=338,height=350,resizable=no,location=no';
	var winoptions = 'width=500,height=340,resizable=no,location=no';

	//if ( window.location.pathname.match("test") || 0 == i ) {
	if ( 0 == i ) {
		window.open('live/webwin.html','webcast',winoptions).focus(); 
  } else if ( i < 0 ) {
		window.open('live/popup_notyet.html','webcast',winoptions).focus();
	} else if ( i > 0 ) {
		window.open('live/webcast_finished.html','webcast',winoptions).focus();
	}
}

webcastClass.prototype.showPage = function() {
			now = new Date();
		// give a week to show the arch message
		archend = webcast.end + ( 1000 * 60 * 60  * 24 * 7 );
		if ( now < webcast.start ) {
		 $('.webcast-sched').show(); 
		 $('.webcast-avail').show();
		 $('.webcast-before').show();
		 $('.webcast-now').hide();
		 $('.webcast-after').hide();
		 $('.webcast-arch').hide();
		 $('.webcast-notsched').hide();
		 webcast.showWebcastTime();
		 webcast.setWebcastTZlink();
		} else if ( webcast.start <= now && webcast.end >= now ) {
		 $('.webcast-sched').show();
		 $('.webcast-avail').show();
		 $('.webcast-before').hide();
		 $('.webcast-now').show();
		 $('.webcast-after').hide();
		 $('.webcast-arch').hide();
		 $('.webcast-notsched').hide();
		 webcast.showWebcastTime();
		 webcast.setWebcastTZlink();
		} else if ( webcast.end < now && archend > now ) {
		 $('.webcast-sched').show();
		 $('.webcast-avail').hide();
		 $('.webcast-before').hide();
		 $('.webcast-now').hide();
		 $('.webcast-after').show();
		 $('.webcast-arch').show();
		 $('.webcast-notsched').hide();
		 webcast.showWebcastTime();
		 webcast.setWebcastTZlink();
		} else {
		 $('.webcast-sched').hide();
		 $('.webcast-avail').hide();
		 $('.webcast-arch').hide();
		 $('.webcast-notsched').show();		 
		}

}

webcast = new webcastClass();


