window.JWPlayer = Class.create();
JWPlayer.players = {};
Object.extend(JWPlayer.prototype, {

	initialize: function(player_id, container_selector, item_selector) {

		this.object_id = 'jwp_' + player_id;
		this.container_selector = container_selector;
		this.item_selector = item_selector;

		window['jwp_' + player_id + '_onplayerready'] = function(obj) {

			this.player = document.getElementById(this.object_id);
			if (this.player && this.player.nodeName.toUpperCase() == 'OBJECT' && typeof this.player.SetVariable == 'undefined') {
				this.player = this.player.getElementsByTagName('object')[0] || null;
			}

			if (this.initial) {
				this.load.apply(this, this.initial);
			}

		}.bind(this);

		JWPlayer.players[player_id] = this;
	},

	setInitial: function(file, image, type, provider) {
		this.initial = arguments;
	},

	load: function(file, image, type, provider) {
		if (this.player) {
			this.stop();
			type = type || 'video';
			var params = {file: file, type: type};
			if (image) {
				params.image = image;
			}
			if (provider) {
				params.provider = provider;
			}
			this.player.sendEvent('LOAD', params);
		}
		return this;
	},

	play: function(elem) {
		if (this.player) {
			this.player.sendEvent('PLAY');
		}
		if (elem) {
			$(elem).up(this.container_selector).select(this.item_selector).invoke('removeClassName', 'selected');
			$(elem).up(this.item_selector).removeClassName('selected');
		}
		return this;
	},

	stop: function() {
		if (this.player) {
			this.player.sendEvent('STOP');
		}
		return this;
	}
});

/*@cc_on window.onerror = function(){ return true; }; @*/
