/*
 *  flvPlayer.js for JWPlayer
 *  By Glenn Baker (http://www.gbaker.net/)
 */
(function(){

window.playerReady = function(thePlayer) {
	flvPlayerSync.playerReady(thePlayer.id)
}

var flvPlayerSync = window.flvPlayerSync = {
	players:{},
	playerReady:function(id){
		this.players[id].setPlayer(window.document[id]);
	},
	isReady:function(id){
		return this.players[id];
	}	
};

/* Counter */
var _guid = +new Date();
function guid() {return _guid++;}

var flvPlayer = window.flvPlayer = function(id, options, data) {
	flvPlayerSync.players[id] = new flvPlayer.prototype.init(id, options, data)
	return flvPlayerSync.players[id];
}

flvPlayer.prototype = {
	defaults:{
		width:320, 
		height:260,
		showdownload:false,
		showdigits:false,
		linktarget:"_blank",
		aboutlink:""
	},
	init:function(id, opts, data){
		if(flvPlayerSync.isReady(id)){return flvPlayerSync.isReady(id);}
		
		this.player = {};
		this.props = {};
		this.isReady = false;
		this.eventQueue = [];
		self.waitFor = "";
		this.id = id;
		
		//TODO initalise all props
		this.props.currentVolume = 8;
		
		var options = this.defaults;
		for(name in opts){
			options[name] = opts[name];	
		}
		if(data && data['image']) {options["image"] = data["image"];}
		if(data && data['file']) {options["file"] = data["file"];}
		var attributes = {id: id,name: id};
		swfobject.embedSWF(options.player, id, options.width, options.height, "9.0.0", this.wroot + "includes/swf/expressInstall.swf", options, {wmode:"opaque",allowfullscreen:true}, attributes);

		//this.startEventsQueue();
		return this;
	},
	ready:function(fn){
		if(!this.isReady){
			this.readyList = this.readyList || [];
			this.readyList.push(fn)
		} else {
			fn.apply(this, []);
		}		
	},
	createSpecListeners:function(){
		this.addModelListener("STATE", function(obj){
			//IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
			this.props.currentState = obj.newstate; 
			this.props.previousState = obj.oldstate;
		}).addViewListener("LOAD" ,function(obj){
			this.props.loadFile = obj.object
			//Page Track
		});
	},
	setPlayer:function(player){
		this.player = player;
		this.config = player.getConfig();
		this.createSpecListeners();

		this.isReady = true;
		while(this.readyList && this.readyList.length > 0) {
			this.readyList.shift().apply(this, []);
		}
	},
	addControllerListener:function(key, fn){
		this.addListener("addControllerListener", key, fn);
		return this;
	},
	addModelListener:function(key, fn){
		this.addListener("addModelListener", key, fn);
		return this;
	},
	addViewListener:function(key, fn){
		this.addListener("addViewListener", key, fn);
		return this;
	},
	addListener:function(type, key, fn){
		var thisPlayer = "flvPlayerSync.players['"+this.id+"'].";		
		var fnName = "fn"+guid();
		if(typeof fn != "string") {
			var self = this;
			this[fnName] = (function(callback, self){
				return function(){ callback.apply(self, arguments); };
			})(fn, self);
		} else {
			fnName = fn;
		}
		this.player[type](key, thisPlayer + fnName);
	},
	nextEvent:function(){
		try {
			this.player["sendEvent"].apply(this.player, arguments);
		} catch(e) {
			if(arguments.length == 1) {
				this.player.sendEvent(arguments[0])
			} else if(arguments.length == 2) {
				this.player.sendEvent(arguments[0], arguments[1])
			} else if(arguments.length == 3) {
				this.player.sendEvent(arguments[0], arguments[1], arguments[2])
			}
		}
	},
	play:function(){
		this.nextEvent('PLAY', 'true');
		return this;
	},
	pause:function(){
		this.nextEvent('PLAY', 'false');
		return this;
	},
	load:function() { 
		if(arguments.length <= 0) {
			return this.props.loadFile; }
	
		if (typeof arguments[0] == "function") {
			this.addViewListener('LOAD', arguments[0]);
		} else {
			//this.nextEvent("STOP", 'true');
			this.nextEvent("LOAD", arguments[0]);
		} 
		return this;
	},
	previousState:function(){
		return this.props.previousState || "";
	},
	state:function(){
		return this.props.currentState || "";
	}	
}

flvPlayer.prototype.init.prototype = flvPlayer.prototype;

})();

