//------------------------------------------------------
// The Video Object
Video.prototype = new CBSObject();
Video.prototype.constructor = Video;
Video.prototype.numbers = "1234567890";
Video.prototype.xmlLoader = new XMLLoader();
function Video(videoPlayer, videoId, format, videoUrl, imageUrl, title, subtitle, caption, prop1, prop2) {
  this.superClass = CBSObject.prototype;
  this.superClass.constructor.call(this);
	this.objectType = "Video";
	_objects[this.objectId] = this;

	this.videoPlayer = videoPlayer;
	
	this.videoId = videoId;
	this.format = format;
	if ((this.format) && (this.format.indexOf("wmv") >= 0)) {
		if (this.format.indexOf("flv") < 0) {
			this.format += "|flv";
		}
	}

	if (videoUrl) this.videoUrl = videoUrl;
	else if (videoPlayer) videoUrl = videoPlayer.videoRoot + videoId;
	if (imageUrl) this.imageUrl = imageUrl;
	else if (videoPlayer) imageUrl = videoPlayer.videoImageRoot + videoId;
	
	this.title = title;
	this.subtitle = subtitle;
	this.caption = caption;
	this.mediaId = 0;
	this.prop1 = prop1;
	this.prop2 = prop2;
	this.props = new Array();
	if (prop1) this.props[0] = prop1;
	if (prop2) this.props[1] = prop2;
	this.position = 0;
	this.maxPosition = 0;
	this.duration = 0;
	this.pids = new Array();
	this.currentPIDNum = 0;
	
	this.adSegment = null;
	this.segments = new Array();
	this.currentSegment = null;
	
	this.loading = false;
	this.loadCount = 0;
	this.pidLoaded = false;
	if (this.videoId) {
		this.load();
	}
	//if ((this.videoId) && (!this.title) && (this.videoPlayer)) {
	//	this.load();
	//} else if (this.title) {
	//	this.loaded = true;
	//}
}
Video.prototype.load = function() {
	if (this.loading) return;
	if (this.loadCount > 3) return;
	this.loadCount = this.loadCount + 1;
	//alert("load");
	this.loading = true;
	this.loaded = false;
	this.pidLoaded = false;
	this.log("video", "xmlLoad");
	this.xmlLoader.load("/common/vplayer4/php/xml.php?id=" + this.videoId, this.onLoaded, this);
}

//------------------------------------------------------
// Are these videos the same?
Video.prototype.equals = function(param1, param2, param3) {
	var videoId = null;
	var videoUrl = null;
	if (param1 == null) {
		return false;
	} else if (typeof(param1) == "object") {
		if (param1.videoId) {
			videoId = param1.videoId;
		} else {
			videoUrl = param1.videoUrl;
		}
	} else if (this.numbers.indexOf(param1.substring(0, 1)) == -1) {
		videoUrl = param1;
	} else {
		videoId = param1;
	}
	if (videoId != null) {
		return (this.videoId == videoId);
	} else {
		return (this.videoUrl == videoUrl);
	}
}

//------------------------------------------------------
// Clear a video for re-use
Video.prototype.clear = function() {
	this.removeAllSegments();
	this.startTracked = false;
	this.endTracked = false;
	this.startingTime = 0;
	this.startingSegment = 0;
}

//------------------------------------------------------
// Called when the video is loaded from XML
Video.prototype.onLoaded = function(request) {
	if (request.status == 200) {
		//alert("loaded");
		this.log("video", "onLoaded");
		this.log("xml", request.responseText);
		var video = this.xmlLoader.getElementByTagName(request.responseXML, "video");
		if (video) {
			this.log("video", "onLoaded = " + this.videoId + ", " + this.xmlLoader.getElementTextByTagName(video, "id"));
			if (this.videoId == this.xmlLoader.getElementTextByTagName(video, "id")) {
				this.title = this.xmlLoader.getElementTextByTagName(video, "title");
				this.subtitle = this.xmlLoader.getElementTextByTagName(video, "subtitle");
				this.caption = this.xmlLoader.getElementTextByTagName(video, "caption");
				this.duration = this.xmlLoader.getElementTextByTagName(video, "duration");
				this.sectionId = this.xmlLoader.getElementTextByTagName(video, "sectionId");
				this.mediaId = this.xmlLoader.getElementTextByTagName(video, "mediaId");
				this.sectionName = this.xmlLoader.getElementTextByTagName(video, "sectionName");
				this.watchTracking = this.xmlLoader.getElementTextByTagName(video, "watchTracking");
				this.numSegments = this.xmlLoader.getElementTextByTagName(video, "numSegments");

				this.pids["flv"] = new Array();
				var flvPIDs = video.getElementsByTagName("flvPID");
				if (flvPIDs) {
					for (var i=0; i<flvPIDs.length; i++) {
						var rank = this.xmlLoader.getAttributeValue(flvPIDs[i], "rank");
						if (rank) {
							this.pids["flv"][parseInt(rank)-1] = this.xmlLoader.getElementText(flvPIDs[i]);
						}
					}
				}

				this.pids["wmv"] = new Array();
				var wmvPIDs = video.getElementsByTagName("wmvPID");
				if (wmvPIDs) {
					for (var i=0; i<wmvPIDs.length; i++) {
						var rank = this.xmlLoader.getAttributeValue(wmvPIDs[i], "rank");
						if (rank) {
							this.pids["wmv"][parseInt(rank)-1] = this.xmlLoader.getElementText(wmvPIDs[i]);
						}
					}
				}

				var properties = video.getElementsByTagName("property");
				if (properties) {
					for (var i=0; i<properties.length; i++) {
						var rank = this.xmlLoader.getAttributeValue(properties[i], "rank");
						if (rank) {
							this["prop" + rank] = this.xmlLoader.getElementText(properties[i]);
							this.props[rank-1] = this.xmlLoader.getElementText(properties[i]);
						}
					}
				}
		
				this.loaded = true;
				this.pidLoaded = true;

				this.log("pid", "loaded " + this.numSegments);
				this._fireEvent("onLoaded", this);
				if (this.videoPlayer) {
					this.videoPlayer.onVideoLoaded(this);
				}
			}
		}
		
		this.loading = false;
	} else {
		//alert(request.statusText);
	}
}

Video.prototype.onPlay = function() {
	this.log("onPlay", "img");
	this.img = new Image();
	this.img.src = "http://www.cbs.com/thunder/ad/image.php?len=" + this.pids["flv"].length;
	this.log("onPlay", "img2");
}
//------------------------------------------------------
// URL to the video
Video.prototype.getVideoUrl = function() {
	var url;
	if (!this.videoPlayer) {
		url = this.videoUrl;
	} if (this.videoId) {
		url = this.videoPlayer.getVideoUrl(this.videoId);
	} else {
		url = this.videoPlayer.getVideoUrl(this.videoUrl);
	}
	
	// Starts
	if (this.startingSegment) url += "&sgmt=" + this.startingSegment;
	if (this.startingTime) url += "&time=" + this.startingTime;
	
	return url;
}

//------------------------------------------------------
// URL to the image
Video.prototype.getImageUrl = function() {
	if (!this.videoPlayer) {
		return this.imageUrl;
	} if (this.videoId) {
		return this.videoPlayer.getImageUrl(this.videoId);
	} else {
		return this.videoPlayer.getImageUrl(this.imageUrl);
	}
}

//------------------------------------------------------
// Get position
Video.prototype.getPosition = function() {
	var pos = 0;
	for (var i=0; i<this.segments.length; i++) {
		if (!this.segments[i].ad) {
			pos += parseInt(this.segments[i].position);
		}
	}
	return pos;
}
//------------------------------------------------------
// Get position
Video.prototype.getMaxPosition = function() {
	var pos = 0;
	for (var i=0; i<this.segments.length; i++) {
		if (!this.segments[i].ad) {
			pos += parseInt(this.segments[i].maxPosition);
		}
	}
	return pos;
}

//------------------------------------------------------
// Add a segment
Video.prototype.newSegment = function() {
	// Ad segment used for special non-script tracking
	if (this.adSegment) {
		var segment = this.adSegment;
		this.adSegment = null;
		this.log("status", "newSegment adSegment " + this.segments.length);
		this.currentSegment = segment;
		return segment;
	} else {
		var segment = new Segment(this);
		if (this.segments.length != 0) {
			this.startTime = 0;
		}
		this.segments[this.segments.length] = segment;
		this.log("status", "newSegment " + this.segments.length);
		this.currentSegment = segment;
		return segment;
	}
}
Video.prototype.addSegment = function(segment) {
	this.segments[this.segments.length] = segment;
	this.log("status", "addSegment " + this.segments.length);
}
Video.prototype.removeAllSegments = function() {
	this.adSegment = null;
	this.segments.length = 0;
	this.currentSegment = null;
}
Video.prototype.getCurrentSegment = function() {
	this.log("status", "getCurrentSegment " + this.currentSegment);
	return this.currentSegment;
}
Video.prototype.getCurrentSegmentNum = function() {
	this.log("status", "getCurrentSegmentNum");
	for (var i=0; i<this.segments.length; i++) {
		if (this.segments[i] == this.currentSegment) {
			return i;
		}
	}
	return null;
}
Video.prototype.getCurrentContentSegmentNum = function() {
	this.log("status", "getCurrentContentSegmentNum");
	var j = 0;
	for (var i=0; i<this.segments.length; i++) {
		if (this.segments[i] == this.currentSegment) {
			return j + this.startingSegment;
		}
		if (!this.segments[i].ad) j++;
	}
	return j;
}
Video.prototype.getContentSegment = function() {
	for (var i=0; i<this.segments.length; i++) {
		if (!this.segments[i].ad) {
			return this.segments[i];
		}
	}
	return null;
}
Video.prototype.getNumSegments = function() {
	this.log("status", "getNumSegments " + this.segments.length);
	return this.segments.length;
}
Video.prototype.getNumContentSegments = function() {
	this.log("status", "getNumContentSegments " + this.numSegments);
	return this.numSegments;
}




//------------------------------------------------------
// The Segment Object
Segment.prototype = new CBSObject();
Segment.prototype.constructor = Segment;
function Segment(video) {
	this.adSegment = false;
	this.video = this;
	this.position = 0;
	this.maxPosition = 0;
	this.duration = 0;
	this.video = video;
}
//------------------------------------------------------
// Get position
Video.prototype.getPosition = function() {
	return this.position;
}
//------------------------------------------------------
// Get position
Segment.prototype.getMaxPosition = function() {
	return this.maxPosition;
}


