var msie = false;
var ccPlayStateOn = true;
var ccForwardStateOn = false;
var ccReverseStateOn = false;
var isLiveStream = false;
var ccFlip4mac = false;

function ccSetBrowser() {
	//alert('ccSetBrowser()');
	var agent = navigator.userAgent.toLowerCase();
	if(agent.indexOf('msie')>-1) msie = true;
	//if(window.GeckoActiveXObject) msie = true;

	var wmvPlugins = Plugin.getPluginsForMimeType("video/x-ms-wmv");
	//alert("wmvPlugins " + wmvPlugins);
	if (wmvPlugins) {
		if (wmvPlugins.length >= 0) {
			ccFlip4mac = wmvPlugins[0].indexOf("Flip4Mac") != -1;
		}
	}
}

function ccSetVolume() {
	var volume = parent.ccVideoVolume*6;
	document.getElementById('ccVideoVolumeBottom').style.width=volume + 'px';
	document.ccMediaPlayer.settings.volume = parent.ccVideoVolume*12; 
}

function ccSetDurationBar() {
	var current = Math.floor(document.ccMediaPlayer.controls.CurrentPosition) || 0;
	var total = Math.floor(document.ccMediaPlayer.currentMedia.duration) || 0;
	var time = document.ccMediaPlayer.controls.CurrentPositionString || '';
	if(total != 0 && current != 0) {
		document.getElementById('ccVideoPlayheadBottom').style.width = Math.round( (current/total)*100);
		}
	document.getElementById('ccVideoPlayheadTime').innerHTML = time;
	self.setTimeout('ccSetDurationBar()',1000);
}

function ccAdjustVolume(up) {
	if(up) {
		if(parent.ccVideoVolume<12) parent.ccVideoVolume++;
		
	} else {
		if(parent.ccVideoVolume>0) parent.ccVideoVolume--;
	}
	parent.ccMuteStateOn = false;
	ccSetVolume();
}

function ccLoadVolume() {
	if(parent.ccMuteStateOn) {
		document.getElementById('ccVideoVolumeBottom').style.width='0px';
	} else {
		ccSetVolume();
	}
}

function ccMute() {
	parent.ccMuteStateOn = !parent.ccMuteStateOn;
	document.ccMediaPlayer.settings.mute = parent.ccMuteStateOn;
	ccLoadVolume();
}

function ccPlay() {
	if(ccPlayStateOn && !isLiveStream) {
		document.ccMediaPlayer.controls.Pause();
		ccPlayStateOn = false;
		document.getElementById('ccVideoPlay').className='play';
	} else {
		document.ccMediaPlayer.controls.Play();
		ccPlayStateOn = true;
		if(!isLiveStream) {
		document.getElementById('ccVideoPlay').className='pause';
		}
	}
}

function ccScan(forward) {
	if(!ccPlayStateOn) {
		ccPlay();
	}
	if(forward) {
		if(ccForwardStateOn) {
			document.ccMediaPlayer.controls.play();
		} else {
			document.ccMediaPlayer.controls.FastForward();	
		}
		ccForwardStateOn = !ccForwardStateOn;
	} else {
		if(ccReverseStateOn) {
			document.ccMediaPlayer.controls.play();
		} else {
			document.ccMediaPlayer.controls.FastReverse();	
		}
		ccReverseStateOn = !ccReverseStateOn;
	}
}

function ccStop() {
	document.ccMediaPlayer.controls.Stop();
	ccPlayStateOn = false;
	document.getElementById('ccVideoPlay').className='play';
	//parent.ccLoadVideo('content/video/standby');
}

function ccPlayLiveVideo(url) {
	isLiveStream = true;
	ccLoadVideo(url);
}

function ccPlayVideo(url) {
	isLiveStream = false;
	ccLoadVideo(url);
}

function cc_getCookies() {
	//alert("'document.cookie': " + document.cookie);
        var hash = new Array;
        if ( document.cookie ) {
                var cookies = document.cookie.split( '; ' );
                for ( var i = 0; i < cookies.length; i++ ) {
                        var namevaluePairs = cookies[i].split( '=' );
                        hash[namevaluePairs[0]] = unescape( namevaluePairs[1] ) || null;
                }
        }
        return hash;
}



// _____________________________________________________________ WebMonkey code
/*
WM_setCookie(), WM_readCookie(), WM_killCookie()
A set of functions that eases the pain of using cookies.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Nadav Savio
*/

// This next little bit of code tests whether the user accepts cookies.
function WM_browserAcceptsCookies() {
	var WM_acceptsCookies = false;
	if ( document.cookie == '' ) {
		document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
		if ( document.cookie.indexOf( 'WM_acceptsCookies=yes' ) != -1 ) {
			WM_acceptsCookies = true;
		} // If it succeeds, set variable
	} else { // there was already a cookie
		WM_acceptsCookies = true;
	}
	
	return ( WM_acceptsCookies );
}

function WM_setCookie( name, value, hours, path, domain, secure ) {
	if ( WM_browserAcceptsCookies() ) { // Don't waste your time if the browser doesn't accept cookies.
		var numHours = 0;
		var not_NN2 = ( navigator && navigator.appName
					&& (navigator.appName == 'Netscape')
					&& navigator.appVersion
					&& (parseInt(navigator.appVersion) == 2) ) ? false : true;

		if ( hours && not_NN2 ) { // NN2 cannot handle Dates, so skip this part
			if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
				numHours = hours;
			} else if ( typeof(hours) == 'number' ) { // calculate Date from number of hours
				numHours = ( new Date((new Date()).getTime() + hours*3600000) ).toGMTString();
			}
		}
		
		document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
	}
} // WM_setCookie

function WM_readCookie( name ) {
	if ( document.cookie == '' ) { // there's no cookie, so go no further
	    return false;
	} else { // there is a cookie
	    var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
		var NN2Hack = firstChar + name.length;
		if ( (firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=') ) { // if you found the cookie
			firstChar += name.length + 1; // skip 'name' and '='
			lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
			if (lastChar == -1) lastChar = theBigCookie.length;
			return unescape( theBigCookie.substring(firstChar, lastChar) );
		} else { // If there was no cookie of that name, return false.
			return false;
		}
	}	
} // WM_readCookie

function WM_killCookie( name, path, domain ) {
	var theValue = WM_readCookie( name ); // We need the value to kill the cookie
	if ( theValue ) {
		document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
	}
} // WM_killCookie

function ccLoadVideo(url) {

		ccSetBrowser();
		var videoString = "";
	
	
	
	/*
	videoString +='<OBJECT ID="ccMediaPlayer" WIDTH=480 HEIGHT=360\n';
	
	videoString +='CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95\n"'; 
	videoString +='STANDBY="Loading Windows Media Player components..."\n'; 
	videoString +='TYPE="application/x-oleobject">\n'; 
	videoString +='<param name="filename" value="' + url + '" />\n';
	videoString +='<param name="animationatStart" value="true" />\n';
	videoString +='<param name="transparentStart" value="true" />\n';
	videoString +='<param name="autoStart" value="true" />\n';
	videoString +='<param name="uiMode" value="full" />\n';
	
	videoString +='<param name="ShowStatusBar" value="false" />\n'; 
	videoString +='<param name="DisplaySize" value="4" />\n'; 
	
	videoString +='<EMBED TYPE="application/x-mplayer2"\n';
	
	videoString +='SRC="' + url + '"\n'; 
	videoString +='NAME="ccMediaPlayer"\n';

	videoString +='ShowControls="1"\n';
	videoString +='ShowStatusBar="1"\n';
	videoString +='autosize="false"\n';
	videoString +='enablecontextmenu="0"\n';
	videoString +='autostart="true"\n';
	
	videoString +='displaysize="4"\n'; // this is diff in sitewide media player, probably because our videos in sitewide is encoded to exact size
	  
	// flip4mac only supported on safari (not firefox)	
	if (ccFlip4mac) {
		videoString +='WIDTH=400\n';
		videoString +='HEIGHT=316\n';
	} else {

		if (msie)
		{
			videoString +='WIDTH=480\n';
			videoString +='HEIGHT=360\n';
			
		} else { // firefox, other
			videoString +='WIDTH=480\n';
			videoString +='HEIGHT=360\n';
		}
	}

	videoString +='showstatusbar="1">\n'; 
	videoString +='</EMBED>\n'; 
	videoString +='</OBJECT>\n';
	// }
	*/
	
	videoString +='<EMBED TYPE="application/x-mplayer2"\n';
	
	videoString +='SRC="' + url + '"\n'; 
	videoString +='NAME="ccMediaPlayer"\n';

	videoString +='ShowControls="1"\n';
	videoString +='ShowStatusBar="1"\n';
	videoString +='autosize="false"\n';
	videoString +='enablecontextmenu="0"\n';
	videoString +='autostart="true"\n';
	
	videoString +='displaysize="4"\n'; // this is diff in sitewide media player, probably because our videos in sitewide is encoded to exact size
	  
	// flip4mac only supported on safari (not firefox)	
	if (ccFlip4mac) {
		videoString +='WIDTH=400\n';
		videoString +='HEIGHT=316\n';
	} else {

		if (msie)
		{
			videoString +='WIDTH=480\n';
			videoString +='HEIGHT=430\n';
			
		} else { // firefox, other
			videoString +='WIDTH=480\n';
			videoString +='HEIGHT=430\n';
		}
	}

	videoString +='</EMBED>\n'; 
	// }

	document.write(videoString);
	
	
	document.write("<br /><br /><a href='/help/faq.jhtml#flip4mac' target='new'>For troubleshooting for Flip4Mac users, click <u>here</u></a>.");
	
}

