 
if(typeof TARGET == "undefined") TARGET = {};

if(typeof log == "undefined") {
	function log(str) { try { console.log(str); } catch(e) {} }
}

TARGET.VideoPlayer = function(containerId, width, height, playerUrl) {
	if(typeof TARGET.VideoPlayer.instanceCount == "undefined")
		TARGET.VideoPlayer.instanceCount = 0;
		
	this.videoPlayerId = ++TARGET.VideoPlayer.instanceCount; 
	this.containerId_ = containerId;
	this.flashId_ = "flashVideoPlayer" + this.videoPlayerId;
	
	this.flashPlayer_ = null;
	
	this.playerUrl_ = playerUrl || null;
	this.expressInstallUrl_ = null;
	
	this.flashParams_ = [
		{"name": "allowScriptAccess", "value": "always"},
		{"name": "allowFullScreen",   "value": "true"},
		{"name": "wmode",             "value": "window"}
	];
	this.flashVars_ = [];
	
	this.width_ = width || 558;
	this.height_ = height || 322;
	this.version_ = "9.0.45";
	
	this.movieLoadTimeout_ = 2000;
	this.retryInterval_ = 50;
	this.flashHasLoaded_ = false;
};

TARGET.VideoPlayer.prototype.setFlashId = function(flashId) {
	this.flashId_ = flashId;
};

TARGET.VideoPlayer.prototype.setPlayerUrl = function(playerUrl) {
	this.playerUrl_ = playerUrl;
};

TARGET.VideoPlayer.prototype.setVersion = function(version) {
	this.version_ = version;
};

TARGET.VideoPlayer.prototype.setExpressInstallUrl = function(expressInstallUrl) {
	this.expressInstallUrl_ = expressInstallUrl;
};

TARGET.VideoPlayer.prototype.addParameter = function(name, value) {
	var found = false;
	for(var i = 0; !found && i < this.flashParams_.length; ++i) {
		if(name == this.flashParams_[i].name) {
			this.flashParams_[i].value = value;
			found = true;
		}
	}
	if(!found) 
		this.flashParams_.push({"name": name, "value": value});
};

TARGET.VideoPlayer.prototype.addFlashVar = function(name, value) {
	this.flashVars_.push({"name": name, "value": value});
};

TARGET.VideoPlayer.prototype.movieIsLoaded = function() {
	if (this.flashHasLoaded_)
		return true;
	
	try {
		// First make sure the movie's defined. Browsers treat this 
		// differently, so return false on error.
		if (this.flashPlayer_ && this.flashPlayer_.PercentLoaded()) {
			// If it is, check how much of it is loaded.
			if (this.flashPlayer_.PercentLoaded() == 100) {
				this.flashHasLoaded_ = true;
				return true;
			} 
			else {
				return false;
			}
		} 
		else {
			// If the movie isn't defined, it's not loaded.
			return false;
		}
	}
	catch(err){
		log("movieIsLoaded: error" + err);
		return false;
	}
}		


TARGET.VideoPlayer.prototype.playVideo = function(url) {
	this.flashPlayer_ = this.getFlashPlayer();
	if(this.flashPlayer_ == null) {
		this.createFlashPlayer();
		this.flashPlayer_ = this.getFlashPlayer();
		log("success");	
	}
	this.playVideo_helper(url, new Date());
};

TARGET.VideoPlayer.prototype.pause = function() {
	this.flashPlayer_ = this.getFlashPlayer();
	if(this.flashPlayer_ != null) this.flashPlayer_.pauseVideo();
}

TARGET.VideoPlayer.prototype.playVideo_helper = function(url, startTime) {
	if(this.movieIsLoaded()) {
		try { 
			this.flashPlayer_.playVideo(url); 
			log("success");	
		}			
		catch(e) {
			log("error " + e);
		}
	}
	else if(new Date() - startTime < this.movieLoadTimeout_) {
		var self = this;
		setTimeout(function() { self.playVideo_helper(url, startTime); }, this.retryInterval_);
	}
	else log("error Timed-out");
};

TARGET.VideoPlayer.prototype.getFlashPlayer = function() {
	var r = null;
	var o = document.getElementById(this.flashId_);
	if (o) {
		var n = o.getElementsByTagName("object")[0];
		
		if(!n || (n && typeof(o.SetVariable) != "undefined")) {
	    	r = o;
		} else if (typeof(n.SetVariable) != "undefined") {
			r = n;
		}
	}
	return r;
};

TARGET.VideoPlayer.prototype.createFlashPlayer = function(defaultVideo) {	
	if(typeof swfobject != "undefined") {
		var fv = new Object(); var fp = new Object();
		
		for(var i = 0; i < this.flashVars_.length; ++i) 
			fv[this.flashVars_[i].name] = this.flashVars_[i].value;
		
		if(typeof defaultVideo != "undefined")
			fv["defaultVideo"] = escape(defaultVideo);
		
		if(this.width_ >= 0) fv["width"] = this.width_;
		if(this.height_ >= 0) fv["height"] = this.height_;
		
		for(var i = 0; i < this.flashParams_.length; ++i)
			fp[this.flashParams_[i].name] = this.flashParams_[i].value;
		
		try {
			swfobject.embedSWF(this.playerUrl_, this.containerId_, this.width_, this.height_, this.version_, this.expressInstallUrl_, 
				/* FlashVars */ fv, /* Parameters */ fp, 
				{id:this.flashId_, name:this.flashId_});
			log("player created");
			return true;
		}
		catch(e) {
			log("error " + e);
			return false;
		}
	}
	else if(typeof SWFObject != "undefined") {
		var so = new SWFObject(this.playerUrl_, this.flashId_, this.width_, this.height_, this.version_);
		for(var i = 0; i < this.flashParams_.length; ++i)
			so.addParam(this.flashParams_[i].name, this.flashParams_[i].value);
		
		if(typeof defaultVideo != "undefined") 
			so.addVariable("defaultVideo", escape(defaultVideo));
		
		for(var i = 0; i < this.flashVars_.length; ++i)
			so.addVariable(this.flashVars_[i].name, this.flashVars_[i].value);
			
		if(this.width_ >= 0) so.addVariable("width", this.width_);
		if(this.height_ >= 0) so.addVariable("height", this.height_);
		
		try {
			if(so.write(this.containerId_)) {
				log("player created");
				return true;
			}
			else {
				return false;
			}
		
		}
		catch(e) {
			log("error " + e);
			return false;
		} 
	}
};
