/*
 * Rework of Antfarm streaming code for Stonewall Redbull BWA
 * @author			brett@stonewall.co.za
 * @created			15 July 2008
 */

/*
 * StreamPlayer
 * A rework of the javascript supplied by Antfarm for streaming.
 * This script detects the users browser and serves flash as
 * a preference. If flash is not detected, it will serve the
 * appropriate object embed code for the browser detected.
 *
 * @param string	containerId		#id of the container we are load player into
 * @param integer	w
 * @param integer	h
 * @param array		streamUrls		Array of stream url objects
 * @param bool		hasFlash		Does the user have flash plugin?
 * @param string	browserName		eg. "IE", "Netscape"
 * @param mixed		wmpMajorVer		Windows media player major version
 * @param mixed		wmpMinorVer		Windows media player minor version
 * @param bool		hasNetscapePlugin Is Netscape 6.4 plugin installed
 * @param bool		debug			Basic debugging
 */
var StreamPlayer = Class.create({
  initialize: function( 
  		containerId,
  		w,
  		h,
  		streamUrls,
  		hasFlash,
  		browserName,
  		wmpMajorVer,
  		wmpMinorVer,
  		hasNetscapePlugin,
  		debug
  		) {
  	
 	this.w = w;
	this.h = h;
	this.containerId = containerId;
	this.streamUrls = streamUrls;
  	this.hasFlash = hasFlash;
	this.browserName = browserName;
	this.wmpMajorVer = wmpMajorVer;
	this.wmpMinorVer = wmpMinorVer;
	this.hasNetscapePlugin = hasNetscapePlugin;
	this.debug = debug;
	this.currentStreamIndex = 0;
	
	if ( !$(containerId) ) alert("container '" + containerId + "' does not exist!");
	if ( streamUrls.length == 0 ) alert("you must supply at least one stream object.");
	
	if ( this.debug ) alert("w: " + this.w + " h: " + this.h + " #stream urls: " + this.streamUrls.length + " wmpMajor: " + this.wmpMajorVer + "\nwmpMinor: " + this.wmpMinorVer + " hasNSplugin: " + this.hasNetscapePlugin + " browser: " + this.browserName );
	
  },
  /* Fetches the right code for the browser and slots it into #containerId
   * @param 	containerId		The #id of the element to replace html of (optional)
   */
  write: function() {
	
  	var containerId = (arguments[0]) ? arguments[0] : this.containerId;
  	
  	if ( this.streamUrls[this.currentStreamIndex] ) {
  		
   		var flashEmbedUrl = this.streamUrls[this.currentStreamIndex].flashUrl;
  		var standardUrl = this.streamUrls[this.currentStreamIndex].standardUrl;
  		
  		var html = ( this.hasFlash && flashEmbedUrl != '' ) ? this.getFlashEmbedCode(flashEmbedUrl, this.w, this.h) : this.getStandardEmbedCode(standardUrl, this.w, this.h);
  		$(containerId).innerHTML = html;
		
  		if ( this.debug ) alert("Inserting:\n" + html + "\ninto container\n#" + containerId);
  		
  	} else {
  		alert('There are no streams setup, please configure');
  	}
  	
  },
  /* Returns the next stream index in the array
   * @param 	none
   */
  getNextStreamIndex: function() {
  	if ( this.streamUrls[this.currentStreamIndex + 1] ) {
  		return this.currentStreamIndex + 1;
  	} else {
  		return 0;
  	}
  	return this.currentStreamIndex;
  },
  /* Will iterate through 'streamUrls'
   * @param 	streamIndex		array index within streamUrls (optional)
   */
  changeStream: function() {
  	var changeToIndex = (arguments[0]) ? arguments[0] : this.getNextStreamIndex();
  	if ( this.streamUrls[changeToIndex] ) {
  		this.currentStreamIndex = changeToIndex;
  	} else {
  		alert('Stream not found');
  	}
  	this.write();
  },
  /* Spits out the streaming links
   * @param 	instanceName	the name you gave to the object we are working with now
   * @param 	containerId		The #id of the element to replace html of
   */
  writeStreamOptions: function(instanceName, containerId) {
  	
  	var html = '';
  	var nextStreamIndex = this.getNextStreamIndex();
  	
  	html += '<p>Currently playing: ' + this.streamUrls[this.currentStreamIndex].title + '</p>';
  	
  	if ( this.streamUrls[nextStreamIndex] && nextStreamIndex != this.currentStreamIndex  )
  		html += '<p><a href="javascript:;" onclick="javascript:' + instanceName + '.changeStream(' + nextStreamIndex + ');' + instanceName + '.writeStreamOptions(\'' + instanceName + '\',\'' + containerId + '\');">Click to change to: ' + this.streamUrls[nextStreamIndex].title + '</a></p>';
  	
  	$(containerId).innerHTML = html;
  	
  },
  /* Returns html for standard embedded object (based on browser)
   * @param 	streamUrl
   * @param 	w
   * @param		h
   */
  getStandardEmbedCode: function(streamUrl, w, h) {
  	
  	var embedCode = '';
  	
  	if ( this.wmpMajorVer == "unknown" && this.browserName == "Netscape" && parseInt(navigator.appVersion) < 5 && this.hasNetscapePlugin ) {
  		
  		embedCode = '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="' + streamUrl + '"';
		embedCode += 'Name="AntVidPlayer"';
		embedCode += 'AutoStart=1';
		embedCode += 'ShowStatusBar="1"';
		embedCode += 'ShowControls="1"';
		embedCode += 'ShowTracker="1"';
		embedCode += 'EnableTracker="1"';
		embedCode += 'ShowPositionControls="1"';
		embedCode += 'EnablePositionControls="1"';
		embedCode += 'width="' + w + '"';
		embedCode += 'height="' + h + '"';
		embedCode += '</embed>\n';
  		
  	} else if ( ( this.browserName == "IE" && this.wmpMajorVer == 6 && this.wmpMinorVer == 4 ) || ( this.browserName=="IE" && this.wmpMajorVer >= 7 ) ) {
  	
		embedCode = '<object id="AntVidPlayer" width="' + w + '" height="' + h + '" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,5,0803"  standby="Loading Windows Media Player components..."  type="application/x-oleobject">\n';
		embedCode += '<param name="FileName" value="' + streamUrl + '">\n';
		embedCode += '<param name="AutoStart" value="1">\n';
		embedCode += '<param name="AutoSize" value="0">\n';
		embedCode += '<param name="AnimationAtStart" value="0">\n';
		embedCode += '<param name="AllowScan" value="1">\n';
		embedCode += '<param name="AllowChangeDisplaySize" value="-1">\n';
		embedCode += '<param name="AutoRewind" value="0">\n';
		embedCode += '<param name="Balance" value="0">\n';
		embedCode += '<param name="BufferingTime" value="05">\n';
		embedCode += '<param name="ClickToPlay" value="0">\n';
		embedCode += '<param name="CursorType" value="0">\n';
		embedCode += '<param name="CurrentPosition" value="-1">\n';
		embedCode += '<param name="CurrentMarker" value="0">\n';
		embedCode += '<param name="DisplayBackColor" value="0">\n';
		embedCode += '<param name="DisplayForeColor" value="16776015">\n';
		embedCode += '<param name="DisplayMode" value="0">\n';
		embedCode += '<param name="DisplaySize" value="5">\n';
		embedCode += '<param name="Enabled" value="1">\n';
		embedCode += '<param name="EnableContextMenu" value="1">\n';
		embedCode += '<param name="EnablePositionControls" value="1">\n';
		embedCode += '<param name="EnableFullScreenControls" value="1">\n';
		embedCode += '<param name="EnableTracker" value="1">\n';
		embedCode += '<param name="InvokeURLs" value="-1">\n';
		embedCode += '<param name="Language" value="-1">\n';
		embedCode += '<param name="Mute" value="0">\n';
		embedCode += '<param name="PlayCount" value="1">\n';
		embedCode += '<param name="PreviewMode" value="0">\n';
		embedCode += '<param name="Rate" value="1">\n';
		embedCode += '<param name="SelectionStart" value="-1">\n';
		embedCode += '<param name="SelectionEnd" value="-1">\n';
		embedCode += '<param name="SendOpenStateChangeEvents" value="true">\n';
		embedCode += '<param name="SendWarningEvents" value="true">\n';
		embedCode += '<param name="SendErrorEvents" value="true">\n';
		embedCode += '<param name="SendKeyboardEvents" value="0">\n';
		embedCode += '<param name="SendMouseClickEvents" value="0">\n';
		embedCode += '<param name="SendMouseMoveEvents" value="0">\n';
		embedCode += '<param name="SendPlayStateChangeEvents" value="true">\n';
		embedCode += '<param name="ShowCaptioning" value="0">\n';
		embedCode += '<param name="ShowControls" value="1">\n';
		embedCode += '<param name="ShowAudioControls" value="1">\n';
		embedCode += '<param name="ShowDisplay" value="0">\n';
		embedCode += '<param name="ShowGotoBar" value="0">\n';
		embedCode += '<param name="ShowPositionControls" value="1">\n';
		embedCode += '<param name="ShowStatusBar" value="1">\n';
		embedCode += '<param name="ShowTracker" value="1">\n';
		embedCode += '<param name="TransparentAtStart" value="0">\n';
		embedCode += '<param name="VideoBorderWidth" value="0">\n';
		embedCode += '<param name="VideoBorderColor" value="0">\n';
		embedCode += '<param name="VideoBorder3D" value="0">\n';
		embedCode += '<param name="Volume" value="80">\n';
		embedCode += '<param name="WindowlessVideo" value="0">\n';
		embedCode += '</object>\n';
  			
  	} else if ( ( this.browserName == "pera" ) || ( this.browserName == "Netscape" ) ) {
  		
  		embedCode = '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="' + streamUrl + '" ';
		embedCode += 'Name="AntVidPlayer" ';
		embedCode += 'AutoStart="1" ';
		embedCode += 'AutoSize="0" ';
		embedCode += 'StretchToFit="false" ';
		embedCode += 'ShowStatusBar="1" ';
		embedCode += 'ShowControls="1" ';
		embedCode += 'ShowTracker="1" ';
		embedCode += 'EnableTracker="1" ';
		embedCode += 'ShowPositionControls="1" ';
		embedCode += 'EnablePositionControls="1" ';
		embedCode += 'width="' + w + '" ';
		embedCode += 'height="' + h + '">\n';
		embedCode += '</embed>\n';
  		
  	} else if ( window.GeckoActiveXObject ) {
  		
		embedCode = '<applet name="AntVidPlayer" code="WMPNS.WMP" width="' + w + '" height="' + h + '" mayscript viewastext>';
		embedCode += '<param name="autoStart" value="1">\n';
		embedCode += '<param name="enabled" value="true">\n';
		embedCode += '<param name="balance" value="50">\n';
		embedCode += '<param name="volume" value="80">\n';
		embedCode += '<param name="uiMode" value="mini">\n';
		embedCode += '<param name="URL" value="' + streamUrl +  '">\n';
		embedCode += '<param name="ShowTracker" value="0">\n';
		embedCode += '<param name="ShowStatusBar" value="1">\n';
		embedCode += '<param name="ShowControls" value="0\">\n';
		embedCode += '</applet>\n';
  		
  	} else if ( this.browserName == "Netscape" ) {
  		
		embedCode = '<applet name="AntVidPlayer" code="WMPNS.WMP" width="' + w + '" height="' + h + '" mayscript viewastext>\n';
		embedCode += '<param name="AutoStart" value="true">\n';
		embedCode += '<param name="enabled" value="true">\n';
		embedCode += '<param name="balance" value="50">\n';
		embedCode += '<param name="ShowStatusBar" value="1">\n';
		embedCode += '<param name="ShowControls" value="0">\n';
		embedCode += '<param name="ShowTracker" value="False">\n';
		embedCode += '<param name="ShowDisplay" value="1">\n';
		embedCode += '<param name="ShowPositionControls" value="False">\n';
		embedCode += '<param name="volume" value="80">\n';
		embedCode += '<param name="URL" value="' + streamUrl + '">\n';
		embedCode += '<param name="FileName" value="' + streamUrl + '">\n';
		embedCode += '</applet>\n';
  		
  	} else if ( this.browserName == "MacIE" ) {
  		
		embedCode = '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="' + streamUrl + '" ';
		embedCode += 'Name="AntVidPlayer" ';
		embedCode += 'AutoStart="1" ';
		embedCode += 'AutoSize="0" ';
		embedCode += 'StretchToFit="false" ';
		embedCode += 'ShowStatusBar="1" ';
		embedCode += 'ShowControls="1" ';
		embedCode += 'ShowTracker="1" ';
		embedCode += 'EnableTracker="1" ';
		embedCode += 'ShowPositionControls="1" ';
		embedCode += 'EnablePositionControls="1" ';
		embedCode += 'width="' + w + '" ';
		embedCode += 'height="' + h + '">';
		embedCode += '</embed>\n';
  		
  	} else {
  		
 		if ( window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1 ) {
			alert("Please check your browser security permissions to allow or at least prompt for use of ActiveX components!!");
			embedCode = '<span style="background:url(http://www.antfarm.co.za/images/antplayerback.gif);font-size:10pt;color:black;padding-top:1px;font-family:Arial;width:' + w + ';height:' + h + ';"><img src="http://www.antfarm.co.za/images/securityWarningIcon.gif" border="0" align="left" valign="top"><img src="http://www.antfarm.co.za/images/ant_player-droopy.gif" border="0" align="left" valign="top">Your browser security settings don\'t allow the ActiveX Control!</span>';
		} else {
			embedCode = 'Unable to detect browser player. <a href="http://www.antfarm.co.za/needplayer.asp" target="_blank">Get player here</a>';
		}
  		
  	}
  	
  	return embedCode;
  	
  },
  /* Returns adobe standard flash object code
   * @param 	streamUrl
   * @param 	w
   * @param		h
   */
  getFlashEmbedCode: function(streamUrl, w, h) {
  		
 	 	embedCode = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + w + '" height="' + h + '" id="Untitled-1" align="middle">\n';
		embedCode += '<param name="allowScriptAccess" value="sameDomain" />\n';
		embedCode += '<param name="movie" value="player.swf" />\n';
		embedCode += '<param name="flashvars" value="&file=' + streamUrl + '&skin=Snel.swf&controlbar=over&autostart=true&repeat=false" />\n';
		embedCode += '<param name="quality" value="high" />\n';
		embedCode += '<param name="bgcolor" value="#ffffff" />\n';
		embedCode += '<embed src="player.swf" quality="high" bgcolor="#ffffff" flashvars="&file=' + streamUrl + '&skin=Snel.swf&controlbar=over&autostart=true&repeat=false" width="' + w + '" height="' + h + '" name="mymovie" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />\n';
		embedCode += '</object>';
		
		return embedCode;
  }
});


/*
 * BrowserSniffer
 * Custom browser sniffer to detect applets, wmp plugins etc
 *
 * @param none
 */
var BrowserSniffer = Class.create({
	initialize: function() {
		
	},
	getName: function() {
		
		var agt = navigator.userAgent.toLowerCase();
		
		if( ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("mac") == -1) )  )
			return "IE";
		else if( ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
					&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
					&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)) )
			return "Netscape";
		else if (agt.indexOf("pera") == 1)
			return "pera";
		else if (agt.indexOf("safar") == 1)
			return "Netscape";
		if( ((agt.indexOf("msie") != -1) && (agt.indexOf("mac") != -1))  )
			return "MacIE";
		else
			return "unknown";
		
	},
	hasNetscapePlugin: function() {		// See if 6.4 pluggin installed
	    return (navigator.mimeTypes && navigator.mimeTypes["application/x-mplayer2"] && navigator.mimeTypes["application/x-mplayer2"].enabledPlugin) ? true : false;
	},
	checkForApplet: function() {
		
		try {
			navigator.plugins.refresh();  // Refresh pluggins/applets}
		} catch(e){;}
		
		// If java runtime not enabled, give error message box
		if (!navigator.javaEnabled())
			alert("Java is not enabled.  Windows Media Player applet for Netscape requires a Java runtime.");
		
		// If applet not loaded redirect to download page
		if ( !( document && document['MediaPlayer'] && document['MediaPlayer'].isActive ) )
			alert("APPLET ISSUE");
	},
  	getWMPVersion: function(WMPVer, type) {
	  	var strVer = new String(WMPVer);
		var s = strVer.split(".");
		switch(type) {
			case 'major' :	return (s[0]) ? s[0] : "unknown"; break;
			case 'minor' :	return (s[1]) ? s[1] : "unknown"; break;	
		}
		return "unknown";
  	},
	wmpObjectTest: function() {
	  	document.write('<object classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="../../#Version=6,4,5,715" height="1" id="WMP64" width="1" viewastext></object>');
	  	document.write('<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="../../#Version=7,0,0,1954" height="1" id="WMP7" width="1" viewastext></object>');
	},
  	wmpTestVersion: function() {
  		
  		document.write( ' <scr' + 'ipt language=VBScript>' + '\n' +
  								' On error resume next' + '\n' +
  								' fHasWMP64 = (WMP64.FileName="")'  + '\n' +
  								' fHasWMP7 = (WMP7.URL = "")' + '\n' +
  								'<\/sc' + 'ript>');
  		
  	}
});


