
function cbsPlayer() {
	this.format;
	this.default_format;
	this.state = 0;
	this.display_level = "low";
	this.volume = -1;
	this.width = 320;
	this.height = 240;
	this._events = new Array();
	this.root = "http://stage.cbs.cbsig.net/video_demo";
	this.frame_id = "testcbsplayer";
	this.real_iframe = "/real_iframe.html";
	this.wmp_iframe = "/wmp_iframe.html";
	this.real_ns_iframe = "/real_ns_iframe.html";
	this.wmp_ns_iframe = "/wmp_ns_iframe.html";
	this.vp_iframe = "/vp_iframe.html";
	this.nosupport = "/nosupport.html";
	this.noplugin = "/noplugin.html";
	this.wmp_url = "http://video.cgi.cbsnews.com/vplayer/play.pl?type=wmv&id=";
	this.rp_url = "http://video.cgi.cbsnews.com/vplayer/play.pl?type=wmv&id=";
	this.image_url = "http://video.cgi.cbsnews.com/vplayer/image.pl?size=320x240&id=";
	this.ad_playing = false;
	this.dclips = new Array();
	this.clips = new Array();
	this.cur_clip = null;
	this.list_pos = 0;
	this.dlist_pos = 0;
	this.player_iframe = "";
	this.iframe = null;
	this.image_id = null;
}

//--------------------------------------------------------
// Debug functions
function log(message) {
	var d = document.getElementById("logDiv");
	if (d)
		d.innerHTML += message + "<br>";
}
cbsPlayer.prototype.log = function(message) {
	log(message);
}

//--------------------------------------------------------
// Set and get functions
// Ad playing
cbsPlayer.prototype.setAdPlaying = function(playing) {
	this.ad_playing = playing;
}
// Ad playing
cbsPlayer.prototype.isAdPlaying = function() {
	return this.ad_playing;
}
// Level to display player controls
cbsPlayer.prototype.setDisplayLevel = function(level) {
	this.display_level = level;
}
// Level to display player controls
cbsPlayer.prototype.getDisplayLevel = function() {
	return this.display_level;
}
// URL to winmed player
cbsPlayer.prototype.setWmpURL = function(url) {
	this.wmp_url = url;
}
// URL to real player
cbsPlayer.prototype.setRpURL = function(url) {
	this.rp_url = url;
}
// URL to image 
cbsPlayer.prototype.setImageURL = function(url) {
	this.image_url = url;
}
// Root of video player directory
cbsPlayer.prototype.setRoot = function(url) {
	this.root = url;
}


//--------------------------------------------------------
// Disables scripting
cbsPlayer.prototype.disableScripting = function(type) {
	this.log("disableScripting " + type);
	if (type == "real") {
		this.hasRealScripting = false;
		if (this.player_iframe == this.root + this.real_iframe)
			this.player_iframe = this.root + this.real_ns_iframe;
	} else if (type == "wmv") {
		this.hasWmvScripting = false;
		if (this.player_iframe == this.root + this.wmp_iframe)
			this.player_iframe = this.root + this.wmp_ns_iframe;
	}
	if (this.format == "real" && !this.hasRealScripting) {
		this._fireEvent("onDisableScripting", this.format, this.hasRealScripting, this.hasWmvScripting);
	} else if (this.format == "wmp" && !this.hasWmvScripting) {
		this._fireEvent("onDisableScripting", this.format, this.hasRealScripting, this.hasWmvScripting);
	}
}

//--------------------------------------------------------
// Play Video Clip
cbsPlayer.prototype.Play = function(clip, format, stopVideo) {
	if (typeof(clip) == "object") log("Play " + clip.id);
	else log("Play " + clip);
	this.state = 1;
	var clipid = null;
	var clip_format = null;
	if (clip == "done") {
		this.Stop();
		return null;
	}
	 
	// If user hasn't chosen format, force them to choose
	if (((!format) || (format == 'all')) && ((!this.format) || (this.format == 'vp'))) {
		this.getVFrame().forms.previewForm.id.value = ((typeof(clip) == "object") ? clip.id : clip);
		this.showChoose();
		return;
	}

	if (clip) {
		if (typeof clip == 'object') {
			clipid = clip.id;
			clip_format = clip.format;
			this.cur_clip = clip;
		} else {
			clipid = clip;
			clip_format = format || 'all';
			this.cur_clip = new cbsVideo(clip, clip_format);
		}
		
		// Check if clip exists in playlist, and reset list position if so
		if (this.clips["i"+clipid]) {
				this.list_pos = this.clips["i"+clipid];
		}
	
		// Change format if necessary			
		if ((clip_format != 'all') && (clip_format != this.format)) {
			if (clip_format == "real") {
				this.changePlayer("real", this.rp_url + clipid, this.image_url + clipid);
			} else if (clip_format == "wmp") {
				this.changePlayer("wmp", this.wmp_url + clipid, this.image_url + clipid);
			}
			return;
		} else if (this.format != this.default_format) {
			if (this.default_format == "real") {
				this.changePlayer("real", this.rp_url + clipid, this.image_url + clipid);
			} else if (this.default_format == "wmp") {
				this.changePlayer("wmp", this.wmp_url + clipid, this.image_url + clipid);
			} else {
				this.choosePlayer();
			}
			return;
		}
	}
	
	// Play
	var result = false;
	var v = this.getVFrame();
	if (clipid) {
		this.setImage(clipid);
		this.showBuffering();
	}
	if (this.format == "real") {
		result = this._RPPlay(v, clipid);
	} else if (this.format == "wmp") {
		result = this._WMPPlay(v, clipid);
	}

	return result;
}

//--------------------------------------------------------
// Play URL with a given format
cbsPlayer.prototype.PlayURL = function(clipurl, format) {
	this.state = 1;
	log("PlayURL: " + clipurl + " format " +format);
	clip = new cbsVideo(clipurl, format);
	this.cur_clip = clip;
	
	if (clipurl == "done") {
		this.Stop();
		return null;
	}
	 
	// If user hasn't chosen format, force them to choose
	if (((!format) || (format == 'all')) && ((!this.format) || (this.format == 'vp'))) {
		this.getVFrame().forms.previewForm.id.value = clipurl;
		this.showChoose();
		return;
	}
	log("Detect Format");
		// Change format if necessary			

	if ((format == "wmp") && (!this.hasWmvScripting)) {
		this.changePlayer("noplugin");
		return;
	}
	if ((format != 'all') && (format != this.format)) {
		if (format == "real") {
			this.changePlayer("real", clipurl, this.image_url + clipurl);
		} else if (format == "wmp") {
			this.changePlayer("wmp", clipurl, this.image_url + clipurl);
		}
//		return;
	} else if (this.format != this.default_format) {
		if (this.default_format == "real") {
			this.changePlayer("real", clipurl, this.image_url + clipurl);
		} else if (this.default_format == "wmp") {
			this.changePlayer("wmp", clipurl, this.image_url + clipurl);
		} else {
			this.choosePlayer();
		}
//		return;
	}
	
	log("Start Playing");
	// Play
	var result = false;
	var v = this.getVFrame();
	if (clipurl) {
//		this.setImage(clipurl);
		this.showBuffering();
	}
	if (this.format == "real") {
		result = this._RPPlay(v, clipurl, 1);
	} else if (this.format == "wmp") {
		result = this._WMPPlay(v, clipurl, 1);
	}

}

//--------------------------------------------------------
// Play Next Video Clip in Playlist
cbsPlayer.prototype.PlayNextClip = function() {
	this.Play(this.getNextClip(), null, true);
}
//--------------------------------------------------------
// Stop
cbsPlayer.prototype.Stop = function() {
	var result = false;
	this.state = 0;
	this.hideBuffering();
	var vframe = this.getVFrame();
	if (this.format == "real") {
		result = this._RPStop(vframe);
	} else if (this.format == "wmp") {
		result = this._WMPStop(vframe);
	}
	vframe = null;	
}

//--------------------------------------------------------
// Pause
cbsPlayer.prototype.Pause = function() {
	var result = false;
	this.hideBuffering();
	var vframe = this.getVFrame();
	if (this.format == "real") {
		result = this._RPPause(vframe);
	} else if (this.format == "wmp") {
		result = this._WMPPause(vframe);
	}
	vframe = null;
	return result;
}

//--------------------------------------------------------
// Volume
cbsPlayer.prototype.setVolume = function(vol) {
	var vframe = this.getVFrame();
	if (this.format == "real") {
		this._RPsetVolume(vframe, vol);
	} else if (this.format == "wmp") {
		this._WMPsetVolume(vframe, vol);
	}
	vframe = null;
	return true;
}
cbsPlayer.prototype.getVolume = function() {
	var vframe = this.getVFrame();
	var vol;
	if (this.format == "real") {
		vol = this._RPgetVolume(vframe);
	} else if (this.format == "wmp") {
		vol = this._WMPgetVolume(vframe);
	}
	vframe = null;
	return vol;
}
// Fullscreen
cbsPlayer.prototype.setFullScreen = function() {
	var result = false;
	var vframe = this.getVFrame();
	if (this.format == "real") {
		result = this._RPsetFullScreen(vframe);
	} else if (this.format == "wmp") {
		result = this._WMPsetFullScreen(vframe);
	}
	vframe = null;
	return result;
}

//--------------------------------------------------------
// Real functions
// Play
cbsPlayer.prototype._RPPlay = function(vframe, clip, url) {
	var clipurl;
	var vd;
	if (this.hasRealScripting) {
		if (clip) {
			// 	If Clip passed as an argument, set it as the source clip
			if (vframe.vplayer.CanStop()) {
				vframe.vplayer.DoStop();
			}
                        if(url) {clipurl = clip;} else {clipurl = this.rp_url + clip;};
						clipurl = clipurl + "&ord="+ (Math.random()*100000000);
                        this.log(clipurl);
                        vd = vframe.vplayer.SetSource(clipurl);
                        this.log("source set: " + vd);
 		}
		this.log("real playing ");
		vframe.vplayer.DoPlay();
		vd = vframe.vplayer.GetSource();
		this.log("real source: " + vd);
	} else {
		if (this.getVFrame()) {
                        if(url) { clipurl = clip; } else {clipurl = this.rp_url + (clip ? clip : this.cur_clip.id);};
                        var imageurl = this.image_url + (clip ? clip : this.cur_clip.id);
                        this.setVFrame(clipurl + "&ord="+ (Math.random()*100000000), imageurl);
 		}
	}
}
// Stop
cbsPlayer.prototype._RPStop = function(vframe) {
	if (this.hasRealScripting) {
		vframe.vplayer.DoStop();
	} else {
		if (this.getVFrame()) {
			this.setVFrame();
		}
	}
}
// Pause
cbsPlayer.prototype._RPPause = function(vframe) {
	if (this.hasRealScripting) {
		if (vframe.vplayer.CanPause()) {
			vframe.vplayer.DoPause();
		}
	} else {
		// ?
	}
}
// Set volume
cbsPlayer.prototype._RPsetVolume = function(vframe, vol) {
	if ((vol >= 0) && (vol <= 100)) {
		if (this.hasRealScripting)
			vframe.vplayer.SetVolume(vol);
		this.volume = vol;
	}

}
// Get volume
cbsPlayer.prototype._RPgetVolume = function(vframe) {
	if (this.hasRealScripting)
		return vframe.vplayer.GetVolume();
	return this.volume;
}
// Fullscreen
cbsPlayer.prototype._RPsetFullScreen = function(vframe) {
	if (this.hasRealScripting) {
		vframe.vplayer.SetFullScreen();
		return true;
	}
	return false;
}


//------------------------------------------------------------
// WinMed functions
// Play
cbsPlayer.prototype._WMPPlay = function(vframe, clip, url) {
	var clipurl;
	/*if (this.hasWmvScripting) {
		if (clip) {
	// 	If Clip passed as an argument, set it as the source clip
                        vframe.vplayer.controls.stop();
                        this.log(this.wmp_url + clip);
                        vframe.vplayer.URL = this.wmp_url + clip + "&ord="+ (Math.random()*100000000);
 		}
		vframe.vplayer.controls.play();
	} else {*/
		if (this.getVFrame()) {
                        if (url) { clipurl = clip;} else {clipurl = this.wmp_url + (clip ? clip : this.cur_clip.id);};
                        var imageurl = this.image_url + (clip ? clip : this.cur_clip.id);
                        this.setVFrame(clipurl + "&ord="+ (Math.random()*100000000), imageurl);
 		}
	//}
}
// Stop
cbsPlayer.prototype._WMPStop = function(vframe) {
	if (this.hasWmvScripting) {
		vframe.vplayer.controls.stop();
	} else {
		if (this.getVFrame()) {
			this.setVFrame();
		}
	}
}
// Pause
cbsPlayer.prototype._WMPPause = function(vframe) {
	if (this.hasWmvScripting) {
		vframe.vplayer.controls.pause();
	} else {
		this.player_iframe = this.root + this.wmp_ns_iframe + "?display=" + this.display_level;
		if (this.volume != -1) this.player_iframe += "&volume=" + this.volume;
		
		if (this.getVFrame()) {this.setVFrame();}
	}
}
// Set volume
cbsPlayer.prototype._WMPsetVolume = function(vframe, vol) {
	if ((vol >= 0) && (vol <= 100)) {
		if (this.hasWmvScripting)
			vframe.vplayer.settings.volume = vol;
		this.volume = vol;
	}
}
// Get volume
cbsPlayer.prototype._WMPgetVolume = function(vframe) {
	if (this.hasWmvScripting);
		return vframe.vplayer.settings.volume;
	return this.volume;
}
// Fullscreen
cbsPlayer.prototype._WMPsetFullScreen = function(vframe) {
	if (this.hasRealScripting) {
		vframe.vplayer.fullScreen = true;
		return true;
	}
	return false;
}


//---------------------------------------------------------
// Clip list functions
// Add a clip to playlist
cbsPlayer.prototype.addClip = function(clipid, format) {
		var f = format || 'all';
		if (!(this.clips["i"+clipid])) {
			var clip = new cbsVideo(clipid, f);
			this.clips.push(clip);
			this.clips["i"+clip.id] = this.clips.length;
			this.list_pos = 0;
		}
}
// Add a clip to default list
cbsPlayer.prototype.addDefaultClip = function(clipid, format) {
		var f = format || 'all';
		if (!this.dclips["i"+clipid]) {
			var clip = new cbsVideo(clipid, f);
			this.dclips.push(clip);
			this.dclips["i"+clip.id] = this.dclips[this.dclips.length-1];
			this.dlist_pos = 0;
		}
}
// Add list of default clips
cbsPlayer.prototype.addDefaultClips = function(clips) {
	var i;
	for (i = 0;i<clips.length;i++){
		this.addDefaultClip(clips[i]);
	}
}
// Removes a clip
cbsPlayer.prototype.removeClip = function(clipid) {
	var i;
	var clip_idx = null;
	for (i = 0;i < this.clips.length; i++) {
		if (this.clips[i].id == clipid) {
			clip_idx = i;
		}
	}
	if (clip_idx != null) {
		this.clips.splice(clip_idx, 1);
		delete this.clips["i"+clipid];
	}
	this._fireEvent("removeClip");
}

// Removes a default clip
cbsPlayer.prototype.removeDefaultClip = function(clipid) {
	var i;
	var clip_idx = null;
	for (i = 0;i < this.dclips.length; i++) {
		if (this.dclips[i].id == clipid) {
			clip_idx = i;
		}
	}
	if (clip_idx != null) {
		this.dclips.splice(clip_idx, 1);
		delete this.dclips["i"+clipid];
	}
	this._fireEvent("removeDefaultClip");
}


// Returns the play list
cbsPlayer.prototype.getPlaylist = function() {
	return this.clips;
}
// Returns default play list
cbsPlayer.prototype.getDefaultPlaylist = function() {
	return this.dclips;
}
// Returns the current clip
cbsPlayer.prototype.getCurrentClip = function() {
	return this.cur_clip;
}
// Returns the next cip
cbsPlayer.prototype.getNextClip = function() {
	var cl = "done";
	if (this.list_pos < this.clips.length) {
		cl = this.clips[this.list_pos];
		this.list_pos++;
	} else if (this.dlist_pos < this.dclips.length) {
		cl = this.dclips[this.dlist_pos];
		this.dlist_pos++;
	}
	this._fireEvent("getNextClip");
	return cl;
}



//-----------------------------------------------------------------
cbsPlayer.prototype.getNextClipID = function() {
	var cl = "done";
	if (this.list_pos < this.clips.length) {
		cl = this.clips[this.list_pos];
		
	} else if (this.dlist_pos < this.dclips.length) {
		cl = this.dclips[this.dlist_pos];
		
	}
}
// Clip info
// Length
cbsPlayer.prototype.getClipLength = function() {
	var vframe = this.getVFrame();
	if (this.format = "real") {
		return Math.round(vframe.vplayer.getLength() / 1000);
	} else if (this.format = "wmp") {
		return vframe.vplayer.currentMedia.duration;
	}
}
// Position
cbsPlayer.prototype.getClipPos = function() {
	var vframe = this.getVFrame();
	if (this.format = "real") {
		return Math.round(vframe.vplayer.getPosition() / 1000);
	} else if (this.format = "wmp") {
		return vframe.vplayer.controls.currentPosition;
	}
}

cbsPlayer.prototype.isPlaying = function() {
	var vframe = this.getVFrame();
	var isplaying = false;
	if (this.format == "real") {
		isplaying = vframe.vplayer.CanStop();
	} else if (this.format == "wmp") {
		var st = vframe.vplayer.playState;
		isplaying = ((st != 10) && (st != 8) && (st != 1)  && (st != 0));
	}	
	return isplaying;
}


//-----------------------------------------------------------------
// Frame functions
// Returns the vframe object
cbsPlayer.prototype.getVFrame = function() {
	if (document.getElementById(this.frame_id)) {
		return document.getElementById(this.frame_id).contentWindow.document;
	} else {
		return null;
	}
}
// Creates the vframe object
cbsPlayer.prototype.createVFrame = function(doc, width, height, clipurl, imageurl) {
	if (width) this.width = width;
	if (height) this.height = height;

	doc.write('<iframe id="' + this.frame_id + '" height="' + height + '" width="' + width + '" scrolling="no" frameborder="0"></iframe>');
	this.setVFrame(clipurl, imageurl);
}
// Sets the frame object 
cbsPlayer.prototype.setVFrame = function(clipurl, imageurl) {
	this.log("setVFrame: " + clipurl + " imageurl " + imageurl);
	var vframe = this.getVFrame();

	var href = this.player_iframe;
	if (href.indexOf('?') == -1 ) {
		href += "?display=" + this.display_level;
	} else {
		href += "&display=" + this.display_level;
	}
	href += "&width=" + this.width + "&height=" + this.height;
	if (this.volume != -1) href += "&volume=" + this.volume;
	if (clipurl) href += '&url=' + escape(clipurl);
	if (imageurl) href += '&image=' + escape(imageurl);
	else if (this.image_id) href += '&image=' + escape(this.image_url + this.image_id);
	log("href: " + href);
	vframe.src = href;
	vframe.location.href = href;
}





//--------------------------------------------------------------------
// Detect installed players
var detectResult = false; 
function detectIEPlugin(ClassID) { 
	document.write('<SCRIPT LANGUAGE=VBScript>\n on error resume next \n detectResult = IsObject(CreateObject("' + ClassID + '"))</SCRIPT>\n'); 
	return detectResult; 
}
function detectNSPlugin(ClassID) { 
	return (navigator.mimeTypes[ClassID].enabledPlugin != null);
}
cbsPlayer.prototype.detectNSPlugins = function() {
	var i;
	for (i=0;i<navigator.plugins.length;i++) {
		var p = navigator.plugins[i];
		if ((p.name.indexOf("RealPlayer") != -1) || (p.description.indexOf("RealPlayer") != -1)) {
			this.hasReal = this.hasRealScripting = true;
		} else if ((p.name.indexOf("ActiveX") != -1) || (p.description.indexOf("ActiveX") != -1)) {
			this.hasWMV = this.hasWmvScripting = true;
		}
	}
}

cbsPlayer.prototype.detectPlayer = function() {
	this.hasReal = false;
	this.hasWMV = false;
	this.hasRealScripting = false;
	this.hasWmvScripting = false;
	this.ieVer = -1;
	var ua = navigator.userAgent.toLowerCase();
	this.isIE  = (ua.indexOf("msie") != -1);
	this.isNS  = (navigator.appName.indexOf("Netscape") != -1);
	this.isWin = ((ua.indexOf("win")!=-1) || (ua.indexOf("32bit")!=-1));
	this.isMac = (ua.indexOf("mac")!=-1);
	this.isSafari = (ua.indexOf("safari") != -1);
	if (this.isIE) {
		this.ieVer = parseFloat(ua.substring(ua.indexOf("MSIE ") + 5, ua.indexOf(";", ua.indexOf("MSIE "))))
	}
	
	if (this.isIE) {
		this.hasReal = this.hasRealScripting = detectIEPlugin("rmocx.RealPlayer G2 Control.1");
		this.hasWMV = this.hasWmvScripting = detectIEPlugin("MediaPlayer.MediaPlayer.1");
		if (this.isMac || this.ieVer < 5) {
			this.disableScripting("wmv");
		} else if (!this.hasRealScripting) {
			this.disableScripting("real");
		} else if (!this.hasWmvScripting) {
			this.disableScripting("wmv");
		}
	} else {
//		this.detectNSPlugins();
		this.hasReal = this.hasRealScripting = detectNSPlugin("audio/x-pn-realaudio-plugin");
		this.hasWMV = detectNSPlugin("application/x-mplayer2");
		this.disableScripting("wmv");
		if (this.isSafari) {
			this.hasRealScripting = false;
		}
		if (!this.hasRealScripting) {
			this.disableScripting("real");
		}
	}
	var format = this.getCookie("cbs_vp");
	if (format) {
		return format;
	} else {
		if (this.hasRealScripting && this.hasWmvScripting) {
			return "wmp";
		} else if (this.hasWmvScripting) {
			return "wmp";
		} else if (this.hasRealScripting) {
			return "real";
		} else if (this.hasReal && this.hasWMV) {
			return "vp";
		} else if (this.hasWMV) {
			return "wmp";
		} else if (this.hasReal) {
			return "real";
		} else {
			return "noplugin";
		}
	}
}

// Choose a player
cbsPlayer.prototype.choosePlayer = function() {
	this.changePlayer("vp", "choose");
}

cbsPlayer.prototype.setDefaultPlayer = function(format) {
	this.default_format = format;
}


// Change to a different player
cbsPlayer.prototype.changePlayer = function(format, clipurl, imageurl) {
	this.format = format;
	if (format == "real") {
		if (this.hasRealScripting) {
			this.player_iframe = this.root + this.real_iframe;
		} else {
			this.player_iframe = this.root + this.real_ns_iframe;
			this._fireEvent("onDisableScripting");
		}
		this._fireEvent("onChangeFormat", "real");
	} else if (format == "wmp") {
		if (this.hasWmvScripting) {
			this.player_iframe = this.root + this.wmp_iframe;
		} else {
			this.player_iframe = this.root + this.wmp_ns_iframe;
			this._fireEvent("onDisableScripting");
		}
		this._fireEvent("onChangeFormat", "wmv");
	} else if (format == "vp") {
		this.player_iframe = this.root + this.vp_iframe;
	} else if (format == "noplugin") {
		this.player_iframe = this.root + this.noplugin;
	} else {
		this.player_iframe = this.root + this.nosupport;
	}
	if (this.getVFrame()) {
		this.setVFrame(clipurl, imageurl);
	}
}



//---------------------------------------------------------------
// Callbacks
// Clip opened
cbsPlayer.prototype.onClipOpened = function(clip) {
	log("onClipOpened " + clip.id);
	if (!this.isBufferingShowing()) {
		this.showBuffering();
	}
	this._fireEvent("onClipOpened", clip);
}
// Clip started
cbsPlayer.prototype.onClipStarted = function(clip) {
	log("onClipStarted " + clip.id);
	if (!this.isVideoShowing()) {
		this.showVideo();
	}
	this._fireEvent("onClipStarted", clip);
}
// Clip params set
cbsPlayer.prototype.onClipSet = function(clip) {
	log("onClipSet");
	this._fireEvent("onClipSet", clip);
}


// Clip closed
cbsPlayer.prototype.onClipClosed = function(clip) {
	log("onClipClosed " + clip.id);
	if ((this.state == 1) && (this.cur_clip.id == clip.id)) {
		this.Play(this.getNextClip());
	}
	this._fireEvent("onClipClosed", clip);
}
// Bit opened
cbsPlayer.prototype.onBitOpened = function(bit) {
	log("onBitOpened " + bit.clip.id);
	this._fireEvent("onBitOpened", bit);
}
// Bit started
cbsPlayer.prototype.onBitStarted = function(bit) {
	log("onBitStarted " + bit.clip.id);
	/*if (bit.hb) {
		var img = new Image();
		if (bit.id == 'ad')
			bit.id = 'ad+-+' + bit.title;
		img.src = "http://ehg-cbs.hitbox.com/HG?hc=we71&cd=1&hv=6&ce=u&hb=" + bit.hb + "&n=" + bit.id + "+-+" + escape(bit.title) + "&vcon=" + escape(bit.dir) + "&c1=" + escape(bit.id) + "|" + escape(bit.title) + "&vpc=090000rn";
		log(img.src);
	}*/
	this._fireEvent("onBitStarted", bit);
	log("done");
}
// Bit closed
cbsPlayer.prototype.onBitClosed = function(bit) {
	log("onBitClosed " + bit.clip.id);
	/*if (bit.hb) {
		var img = new Image();
		if (bit.id == 'ad')
			bit.id = 'ad+-+' + bit.title;
		img.src = "http://ehg-cbs.hitbox.com/HG?hc=we71&cd=1&hv=6&ce=u&hb=" + bit.hb + "&n=" + bit.id + "+-+" + escape(bit.title) + "&vcon=" + escape(bit.dir) + "&c2=" + escape(bit.id) + "|" + bit.position + "/" + bit.duration + "&vpc=090000rn";
		log(img.src);
	}*/
	this._fireEvent("onBitClosed", bit);
}
// Bit set
cbsPlayer.prototype.onBitSet = function(bit) {
	log("onBitSet");
	this._fireEvent("onBitSet", bit);
}

// State changed
cbsPlayer.prototype.onStateChanged = function(state) {
	//log("onStateChange " + state);
	/*if ((state == "buffering") && (this.getClipPos() == 0)) {
		if (!this.isBufferingShowing()) {
			this.showBuffering();
		}
	} else if (state == "playing") {
		if (!this.isVideoShowing()) {
			this.showVideo();
		}
	}*/
	this._fireEvent("onStateChanged", state);
}
// Position changed
cbsPlayer.prototype.onPositionChanged = function(pos, len) {
	//log("onPositionChange " + pos + ", " + len);
	this._fireEvent("onPositionChanged", pos, len);
}


//---------------------------------------------------------------
// Image
cbsPlayer.prototype.setImage = function(clipid) {
	log("setImage " + clipid);
	var vframe = this.getVFrame();
	log('ok');
	var videoImageTD = vframe.getElementById("videoImageTD");
	if (!videoImageTD) return;
	videoImageTD.background = this.image_url + clipid;
	videoImageTD.style.backgroundImage = "url(" + this.image_url + clipid + ")";
	this.showImage();
	this.image_id = clipid;
}

cbsPlayer.prototype.setImageUrl = function(url, clipid) {
	log("setImage " + url);
	var vframe = this.getVFrame();
	log('ok');
	var videoImageTD = vframe.getElementById("videoImageTD");
	if (!videoImageTD) return;
	videoImageTD.background = url;
	videoImageTD.style.backgroundImage = "url(" + url + ")";
	this.showImage();
	this.image_id = clipid;
}

cbsPlayer.prototype.showImage = function() {
	log("showImage");
	var vframe = this.getVFrame();
	var imageDiv = vframe.getElementById("imageDiv");
	if (!imageDiv) return;
	imageDiv.style.visibility = "visible";
	var chooseDiv = vframe.getElementById("chooseDiv");
	if (chooseDiv) chooseDiv.style.visibility = "hidden";
	var videoDiv = vframe.getElementById("videoDiv");
	if (videoDiv) videoDiv.style.visibility = "hidden";
}
cbsPlayer.prototype.isImageShowing = function() {
	var vframe = this.getVFrame();
	var imageDiv = vframe.getElementById("imageDiv");
	if (!imageDiv) return false;
	return (imageDiv.style.visibility == "visible");
}

// Choose
cbsPlayer.prototype.showChoose = function() {
	log("showChoose");
	var vframe = this.getVFrame();
	var imageDiv = vframe.getElementById("imageDiv");
	if (!imageDiv) return;
	var chooseDiv = vframe.getElementById("chooseDiv");
	chooseDiv.style.visibility = "visible";
	imageDiv.style.visibility = "hidden";
}
cbsPlayer.prototype.isChooseShowing = function() {
	var vframe = this.getVFrame();
	var chooseDiv = vframe.getElementById("chooseDiv");
	if (!chooseDiv) return true;
	return (chooseDiv.style.visibility == "visible");
}

// Buffering
cbsPlayer.prototype.isBufferingShowing = function() {
	var vframe = this.getVFrame();
	var bufferingImage = vframe.getElementById("bufferingImage");
	if (!bufferingImage) return;
	return (bufferingImage.src == "http://www.cbs.com/staff/spearson/vplayer/images/video_loader_2.gif");
}
cbsPlayer.prototype.showBuffering = function() {
	log("showBuffering");
	var vframe = this.getVFrame();
	var bufferingImage = vframe.getElementById("bufferingImage");
	if (!bufferingImage) return;
	bufferingImage.src = "http://www.cbs.com/staff/spearson/vplayer/images/video_loader_2.gif";
}
cbsPlayer.prototype.hideBuffering = function() {
	log("hideBuffering");
	var vframe = this.getVFrame();
	var bufferingImage = vframe.getElementById("bufferingImage");
	if (!bufferingImage) return;
	bufferingImage.src = "http://www.cbs.com/Common/images/spacer.gif";
}

// Video
cbsPlayer.prototype.showVideo = function() {
	log("showVideo");
	var vframe = this.getVFrame();
	var imageDiv = vframe.getElementById("imageDiv");
	if (!imageDiv) return;
	var videoDiv = vframe.getElementById("videoDiv");
	videoDiv.style.visibility = "visible";
	imageDiv.style.visibility = "hidden";
}
cbsPlayer.prototype.isVideoShowing = function() {
	var vframe = this.getVFrame();
	var videoDiv = vframe.getElementById("videoDiv");
	if (!videoDiv) return true;
	return (videoDiv.style.visibility == "visible");
}

//---------------------------------------------------------------
// Attached code to an event of an object
cbsPlayer.prototype.attachEvent = function(e, c) {
  if (this._events[e] == null)
    this._events[e] = new Array();
  this._events[e][this._events[e].length] = c;
}

//---------------------------------------------------------------
// Fire all code attached to the given event
cbsPlayer.prototype._fireEvent = function(e) {
  if (this._events[e] != null) {
   	for (var i=0; i<this._events[e].length; i++) {
      switch (typeof(this._events[e][i])) {
   	    case 'function':
       	  this._events[e][i].apply(this, arguments);
          break;
        case 'string':
   	      eval(this._events[e][i]);
       	  break;
        default:
   	      break;
      }
   	}
  }
}

//-------------------------------------------------------------
// Cookies
cbsPlayer.prototype.getCookies = function() {
	var cookies = new Array();
	var args = document.cookie.split('; ');
	for (var i=0; i<args.length; i++) {
		var arg = args[i].split('=');
		cookies[arg[0]] = unescape(arg[1]);
	}
	return cookies;
}
cbsPlayer.prototype.getCookie = function(name) {
	var cookies = this.getCookies();
	return cookies[name];
}
cbsPlayer.prototype.setCookie = function(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
cbsPlayer.prototype.deleteCookie = function(name,path,domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}




function cbsVideo(id, format) {
	this.id = id || null;
	this.format = format || null;
}
