/* ------------------------------------------- */
/* Tool for JW Flash Player 4.x                */
/* (c) dan sosedov 2008                        */
/* --------------------------------------------*/

var JW_PROGRESSIVE = 0;					
var JW_STREAMING = 1;
var JW_DEFAULT_WIDTH = '440';
var JW_DEFAULT_HEIGHT = '360';
var JW_DEFAULT_TRANSMIT = JW_PROGRESSIVE;

function JWPlayer() {
	var id = 'jwplayer';			// player object name
	var width = '';					// frame width
	var height = '';				// frame height
	var preview = '';				// movie preview
	var swf = '';					// swf object file
	var logo = '';					// movie logo image
	var file = '';					// file path
	var duration = 0;				// duration in seconds
	var transmit; 					// transmit data type (streaming or progressive)
	var object = 0;					// player object
	var skin = 0;					// player skin
	var autostart = 'false';			// player autostart
	var buffer = 5;
				
	// set size of player
	this.size = function(width,height) {
		this.width = width;
		this.height = height;
	}

	// embed player to html container with ID = targetid
	this.embed = function(playerid,targetid) {
		// set defaults if no properties was set
		if (!this.width) this.width = JW_DEFAULT_WIDTH;
		if (!this.height) this.height = JW_DEFAULT_HEIGHT;
		if (!this.transmit) this.transmit = JW_DEFAULT_TRANSMIT;
	
		this.id = playerid;
				
		// flash vars
		var flashVars = "";
		flashVars += "file=" + this.file;
		flashVars += "&type=video";
		flashVars += "&image=" + this.preview;
		flashVars += "&logo=" + this.logo;
		flashVars += "&bufferlength=" + this.buffer;
		flashVars += "&showdigits=true";
		flashVars += "&fullscreen=true";
		flashVars += "&duration=" + this.duration;
		flashVars += "&autostart=" + this.autostart;
		flashVars += "&quality=true";
		flashVars += "&skin=" + this.skin;
		
		if (this.transmit == JW_STREAMING) {
			flashVars += "&streamscript=lighttpd";
		}
		
		
		
		// SWF object creating
		var obj = new SWFObject(this.swf,this.id,this.width,this.height,"9","#FFFFFF");
		obj.addParam("allowfullscreen","true");
		obj.addParam("allowscriptaccess","always");
		obj.addParam("seamlesstabbing","true");
		obj.addParam("flashvars",flashVars);
		obj.addVariable("skin",this.skin);
		
		
					
		// write player object to html container
		if (targetid) {
			obj.write(targetid);
					
			// getting player reference
			if(navigator.appName.indexOf("Microsoft") != -1) this.object = window[this.id];
			else this.object = document[this.id];
		}
	}
	
	// change current playing file to new from href location
	this.openFile = function(href) {
		if (this.object) {
			this.object.sendEvent("LOAD",href);
			this.object.sendEvent("PLAY","true");
			return true;
		}
		return false;
	}
				
	// seek current position to second = value
	this.seekSecond = function(value) {
		if (this.object) {
			if (value >= 0 && value <= this.duration) {
				this.object.sendEvent("SEEK",value);
				return true;
			}
		}
		return false;
	}
								
	// seek current position to percent of duration, [1..100]
	this.seekPercent = function(value) {
		if (!this.duration) return false;
		else {
			position = Math.round((this.duration*value)/100);
			return this.seekSecond(position);
		}
	}
}
