/*
 * Javascript events for the Event Player page
 */

function randomString() {
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  var string_length = 8;
  var randomstring = '';
  for (var i=0; i<string_length; i++) {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
  }
  return randomstring;
}

function getSecondsToStart(eventid) {
  var sectostart = 0;
  sectostart = $.ajax({
    url: '/event/secondsToStart/id/'+eventid+'?'+randomString(),
    async: false
  }).responseText;
  return sectostart;
}

function countdown2TZ(sectostart, format, eventid) {
	if ( $("#countdown") ) {
    
		Time_Left = sectostart;
		
		if(Time_Left <= 0)
			Time_Left = 0;
		
		switch(format) {
			case 0:
				//The simplest way to display the time left.
				$("#countdown").html(Time_Left + ' seconds');
				break;
			case 1:
				//More datailed.
				var days = Math.floor(Time_Left / (60 * 60 * 24));
				Time_Left %= (60 * 60 * 24);
				var hours = Math.floor(Time_Left / (60 * 60));
				Time_Left %= (60 * 60);
				var minutes = Math.floor(Time_Left / 60);
				Time_Left %= 60;
				var seconds = Time_Left;
				
				var dps = 's'; hps = 's'; mps = 's'; sps = 's';
				//ps is short for plural suffix.
				if(days == 1) dps ='';
				if(hours == 1) hps ='';
				if(minutes == 1) mps ='';
				if(seconds == 1) sps ='';
				
				$("#countdown").html("");

        var htmlStr = "";
				if ( days > 0 )
          htmlStr += days + ' day' + dps + ' ';
				if ( hours < 10 )
          htmlStr += "0";
				htmlStr += hours + ':';
				if ( minutes < 10 )
					htmlStr += "0";
				htmlStr += minutes + ':';
				if ( seconds < 10 )
					htmlStr += "0";
				htmlStr += seconds;
        $("#countdown").html(htmlStr);
				break;
			default: 
      $("#countdown").html(Time_Left + ' seconds');
		}
		
		//Recursive call, keeps the clock ticking.
		if (days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
                  
                        location.reload();
                        
			$.get('/event/clearCache/' + eventid, function() {
                        //location.reload();
			});
		} else {
			setTimeout('countdown2TZ(' + (sectostart - 1) + ',' + format + ',' + eventid + ');', 1000);
		}
	}
}

/* resize iframe based on content */
function doIframe(){
	o = document.getElementsByTagName('iframe');
	for(i=0;i<o.length;i++){
		if (/\bautoHeight\b/.test(o[i].className)){
			setHeight(o[i]);
			addEvent(o[i],'load', doIframe);
		}
	}
}

function setHeight(e){
	if(e.contentDocument){
		e.height = e.contentDocument.body.offsetHeight + 35;
	} else {
		e.height = e.contentWindow.document.body.scrollHeight;
	}
}

function addEvent(obj, evType, fn){
	if(obj.addEventListener)
	{
	obj.addEventListener(evType, fn,false);
	return true;
	} else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
	return r;
	} else {
	return false;
	}
}

if (document.getElementById && document.createTextNode){
 addEvent(window,'load', doIframe);
}

$(document).ready(function() {

	// Player tabs setup
 /*
	$('.forms-tabs').tabs();
	$('.results-tabs').tabs();

  // jquery media plugin
  $('a.media-player').media({
    width: 170,
    height: 66,
    autoplay: 1,
    flvPlayer:     '/player/player.swf',
    mp3Player:     '/player/player.swf'
  });
	*/
});

