/*
How to use this script:

In this order in the html: 
1. Include 
	<script src="http://fs.net211.com/jwplayer/swfobject.js" type="text/javascript"></script>
	<script type="text/javascript" src="http://fs.net211.com/jwplayer/player2.js" ></script>
2. Define an empty div tag with id= mediaarea + number used for mediaDiv 
	e.g. <div id="mediaarea2433"></div>
3. Enter your text inside a div with id=text + number used for textDiv
	e.g. <div id="text2433"> Text here </div>
4. Within a script tag
   <script type="text/javascript">	
   a. Instantiate the player object. To assure no collision between multiple items on a feed, give the variable a unique name. 
      var p2433 = new n211Player();
	  If you are using the same div number suffix for both the media div and the text div, you can pass the number as a parameter to the object
	  instatiation call as such:
	  var p2433 = new n211Player(2433);
	  if you do this you don't need to set the mediaDivNumber or textDivNumber variables.
   b.  Set your variables as needed. See the full list of variables below:
      p2433.videoFile = 'http://fs.net211.com/vod/media/Video/GCCService/2010-11-14/2010-11-14-Service-rtmp-rss.xml';
	  p2433audioFile = 'http://fs.net211.com/vod/media/Video/GCCService/2010-11-14/2010-11-14-Service-rtmp-rss-a.xml';
	  p2433.podcastFile = 'http://feeds.feedburner.com/GCCService?format=xml';
	  p2433.mediaDivNumber = 100;
	  p2433.textDivNumber = 100;
	c. Load the player
      p2433.player();	
	</script>
You must include at the very minimum the mediaDiv and textDiv tags. 
Other parameters default to what shows below.

=================================================
Parameters (defaults in parenthesis)
mediaDivNumber: number (required)
textDivNumber: number  (required)
videoFile: link (if absent, no video tab will be shown)
audioFile: link (if absent, no audio tab will be shown)
podcastFile: link (if absent, no podcast tab will be shown)
podcastSubscribe: link to subscription link for this podcast (if absent, no subscribe tab will be shown)
autoStart: true/false  (true)
language: 2 letter code (en)
videoPlayList: position (bottom)
audioPlayList: position (bottom)
podcastPlayList: position (bottom)
audioWidth: number (540)
audioHeight: number (24)
videoWidth: number (540)
barWidth: number (540)   - allows you to set the width of the containing table if needed
videoHeight: number (540)
podcastWidth: number (540)
podcastHeight: number (204)
*/
   
function n211player(divNumber){
	this.autoStart =  false;
	this.language =  'en';
	this.videoPlayList =  'bottom';
	this.audioPlayList =  'bottom';
	this.podcastPlayList = 'bottom';
	this.audioWidth =  540;
	this.audioHeight =24;
	this.videoHeight = 540;
	this.videoWidth = 540;
	this.podHeight =  204;
	this.podWidth =  540;
	this.audioFile =  false;
	this.videoFile =  false;
	this.podcastFile =  false;
	this.podcastSubscribe = false;
	this.barWidth = 540;
     this.mediaDivNumber = 100;
	this.textDivNumber = 100;
	if (typeof divNumber !== 'undefined' && parseInt(divNumber) !== 'NaN'){
		this.mediaDivNumber = divNumber.toString();
		this.textDivNumber = divNumber.toString();
	}
	
	this.player = function(){
    // check to see whether the object variables mediaDivNumber and textDivNumber have been properly defined
	if (typeof this.mediaDivNumber === 'undefined') {alert ('You must define the mediaDivNumber variable');return;} else { this.mediaDivNumber = this.mediaDivNumber;}
	if (typeof this.textDivNumber === 'undefined') {alert ('You must define the textDivNumber variable');return;}else { this.textDivNumber = this.textDivNumber;}
	if (this.mediaDivNumber === 'NaN') {alert('mediaDivNumber must be a number');return;}
	if (this.textDivNumber === 'NaN') {alert('textDivNumber must be a number');return;}

	// check to see if the divs are actually present in the document object
	var mediaArea = document.getElementById('mediaarea' + this.mediaDivNumber);
	var textElement = document.getElementById('text' + this.textDivNumber);
	if (mediaArea === null ){
		alert('You must have defined a div tag with id mediaarea' + this.mediaDivNumber );
		return;
	}
	if (textElement === null){
		alert('You must have defined div tag with id text' + this.textDivNumber);
		return;
	}

	// this are computed vars
	var playerDiv = 'mediaspace' + this.mediaDivNumber;
	var textDiv = 'text' + this.textDivNumber;
	var playType = getCookie();
	var videoBar = 'http://fs.net211.com/graphics/barvideo_' + this.language + '.gif';
	var audioBar = 'http://fs.net211.com/graphics/baraudio_' + this.language + '.gif';
	var textBar = 'http://fs.net211.com/graphics/bartext_' + this.language + '.gif';
	var podcastBar = 'http://fs.net211.com/graphics/barpodcast_' + this.language + '.gif';
	var subscribeBar = 'http://fs.net211.com/graphics/barsubscribe_' + this.language + '.gif';
	var barBack = 'http://fs.net211.com/graphics/menuback.gif';
	var firstAuto = true;
	var initPlay;
	var audioPlayer =  "'"+playerDiv +"','" +  this.audioFile + "', '" + firstAuto + "', '" + this.audioWidth+ "', '" + this.audioHeight +"','" + this.audioPlayList +"'";
	var videoPlayer =  "'"+playerDiv +"','" +  this.videoFile + "', '" + firstAuto + "', '" + this.videoWidth+ "', '" + this.videoHeight +"','" + this.videoPlayList+ "'";
	var podPlayer = "'" + playerDiv + "', '" + this.podcastFile + "','" + firstAuto + "', '" + this.podWidth + "', '" + this.podHeight + "','" + this.podcastPlayList + "'";
	var bFirst = true;
	var bAuto = false;
	var bar = "<div align=\"center\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" align=\"center\" style=\"width: " + this.barWidth + "px; height:24px;\"><tbody>";
	bar = bar + "<tr style=\"background-image: url('" + barBack + "');\">";
	if (this.videoFile){
		bar = bar + "<td><a href=\"javascript:setCookie('video');bFirst=false;showtext('"+textDiv+"',false);createPlayer(" + videoPlayer +");\">";
		bar = bar + "<img  height=\"24\" src=\"" + videoBar + "\" alt=\"Watch the Video\" style=\"border-color: rgb(0, 0, 0); border-width: 0px;\"></a></td>";
	}
	if (this.audioFile){
		bar = bar + "<td><a href=\"javascript:setCookie('audio');bFirst=false;showtext('"+textDiv+"',false);createPlayer(" +audioPlayer + ");\">";
		bar = bar + "<img  height=\"24\" src=\"" + audioBar + "\" alt=\"Listen to Audio Only\" style=\"border-color: rgb(0, 0, 0); border-width: 0px;\"></a></td>";
	}
	bar = bar + "<td><a href=\"javascript:setCookie('text');hideSWF('"  + playerDiv + "');showtext('"+textDiv+"',true);;\"> ";
	bar = bar + "<img  height=\"24\" src=\"" + textBar + "\" alt=\"Read the Text\" style=\"border-color: rgb(0, 0, 0); border-width: 0px;\"></a></td>";
	if (this.podcastFile){
	  bar = bar + "<td><a href=\"javascript:setCookie('podcast');bFirst=false;showtext('"+textDiv+"',false);createPlayer(" + podPlayer + ");\">";
	  bar = bar + "<img  height=\"24\" src=\"" + podcastBar + "\"  alt=\"Listen to the Podcast\" style=\"border-color: rgb(0, 0, 0); border-width: 0px;\"></a></td>";
	}
	if (this.podcastSubscribe){
	  bar = bar + '<td><a style="padding:0;background:none" target="_blank" href="' + this.podcastSubscribe + '">';
	  bar = bar + "<img height=\"24\" src = \"" + subscribeBar + "\" + alt=\"Subscribe to this podcast\" style=\"border-color: rgb(0, 0, 0); border-width: 0px;\"></a></td>";
	}
	bar = bar + "<td width=\"90%\" style=\"border-right: thin solid rgb(161, 161, 161);\"></td></tr></tbody></table></div>";

	var divs = "<div style=\"text-align:center\" id=\"" + playerDiv + "\"></div>";

	mediaArea.innerHTML = bar + divs;
	
	if (playType !==""){
		// assumes it finds a cookie
		bAuto = this.autoStart;
		audioPlayer =  "'"+playerDiv +"','" +  this.audioFile + "', "+ bAuto + ", '" + this.audioWidth+ "', '" + this.audioHeight +"','" + this.audioPlayList +"'";
		videoPlayer =  "'"+playerDiv +"','" +  this.videoFile + "', " + bAuto +", '" + this.videoWidth+ "', '" + this.videoHeight +"','" + this.videoPlayList+ "'";
		podPlayer = "'" + playerDiv + "', '" + this.podcastFile + "'," + bAuto +", '" + this.podWidth + "', '" + this.podHeight + "','" + this.podcastPlayList + "'";
		 
	  switch(playType){
		case 'video':
		  if (this.videoFile){
				showtext(textDiv,false);
				eval( "createPlayer(" + videoPlayer+");");
			}else{
				showtext(textDiv,true);	  
			}
			break;
		case 'audio':
			if (this.audioFile){
				showtext(textDiv,false);
				eval("createPlayer(" + audioPlayer + ");");
			}else{
			  showtext(textDiv,true);	  
			}
			break;
		case 'text':
		  showtext(textDiv,true);
		  break;
		case 'podcast':
			if (this.podcastFile){
				showtext(textDiv,false);
				eval("createPlayer(" + podPlayer + ");");
			}else{
				showtext(textDiv,false);	   
			}
		  break;
		default:
			showtext(textDiv,false);	   
		}
	} else {
	   // first time through or if no cookie is set
	   if  (this.videoFile || this.audioFile || this.podcastFile) {
			showtext(textDiv,false);
			if (this.videoFile){
				initialPlay =  "'"+playerDiv +"','" +  this.videoFile + "', '" + this.autoStart +"', '" + this.videoWidth+ "', '" + this.videoHeight +"','" + this.videoPlayList+ "'";
			  } else if (this.audioFile) {
				initialPlay =  "'"+playerDiv +"','" +  this.audioFile + "', '" + this.autoStart +"', '" + this.audioWidth+ "', '" + this.audioHeight +"','" + this.audioPlayList+ "'";        
			} else {
				initialPlay =  "'"+playerDiv +"','" +  this.podcastLink + "', '" + this.autoStart +"', '" + this.podWidth+ "', '" + this.podHeight +"','" + this.podcastPlayList+ "'";			  
			  } 
		  eval("createPlayer(" + initialPlay + ");"); 
	   }else{
			showtext(textDiv,true);
		}
	}
}
}
function _setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays===null) ? "" : ";expires="+exdate.toUTCString());
}
function _getCookie(c_name)
{
	var c_start;
	var c_end;
	if (document.cookie.length>0){
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!==-1){
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end===-1){c_end=document.cookie.length;}
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}
function getCookie(){
   return _getCookie('Network211PlayType');
}  
function setCookie(value){
  _setCookie('Network211PlayType',value,30);
}

function createPlayer(theContainer, theFile, go, theWidth, theHeight,playList) {
  var so = new SWFObject('http://fs.net211.com/jwplayer/5-8-licensed/player.swf','mpl', theWidth, theHeight,'9');
  so.addParam('allowfullscreen','true');
  so.addParam('allowscriptaccess','always');
  so.addParam('wmode','opaque');
  so.addVariable('file', theFile);
  so.addVariable('repeat','list');
  so.addVariable('playlist',playList);
  so.addVariable('subscribe', 'true');
  so.addVariable('backcolor','EEEEEE');
  so.addVariable('frontcolor','000000');
  so.addVariable('lightcolor','AAAAAA');
  so.addVariable('bufferlength','15');
  so.addVariable('plugins','googlytics-1');
  so.addVariable('autostart', go);
  so.write(theContainer);
  }
function hideSWF(mediaDiv){
   var ele = document.getElementById(mediaDiv);
   ele.innerHTML="";
   } 
function showtext(divToHide,bState){
	var ele = document.getElementById(divToHide);
	if(bState === false) {
		ele.style.display = "none";
	}
	else {
		ele.style.display = "block";
	}
} 

