/*
**	JW/Longtail Player Javascript class
**
**	(c) 2010 Mikoon Webservices
*/

/*
**	Usage:
**
**	myplayer = new JWPlayer('element-id', {options});
*/

var JWPlayer = new Class({
	
	Implements: Options,
	
	options: {
		debug:false		
    },
	
	playlist: {},
	config: {},
	
	initialize: function(id, options){
		
		//	setup options
		this.setOptions(options);
		
		// get base elements
		this.player = this.get_flash_object(id);
		this.config = this.get_config();
		this.playlist = this.get_playlist();
		
	},

	get_playlist: function(){
		return this.player.getPlaylist();
	},
	
	get_config: function(){
		return this.player.getConfig();
	},

	item: function(item_nr){
		var config = this.get_config();
		if(config.item != item_nr || config.state != 'PLAYING'){
			this.player.sendEvent("ITEM", item_nr);
		}
	},

	play: function(){
		var config = this.get_config();
		if(config.state == 'PAUSED'){
			this.player.sendEvent("PLAY", true);
		} else {
			this.player.sendEvent("PLAY", false);	
		}
	},
	
	load: function(in_url){
		this.player.sendEvent("LOAD", in_url);
	},
	
	pause: function(){
		this.player.sendEvent("PLAY", false);
	},
	
	stop: function(){
		this.player.sendEvent("STOP", true);
	},
	
	seek: function(in_seconds){
		var config = this.get_config();
		if(config.state != 'IDLE'){
			this.player.sendEvent("SEEK", in_seconds);
			if(config.state == 'PAUSED'){
				this.player.sendEvent("PLAY", false);
			}
		}
	},
	
	prev: function(){
		this.player.sendEvent("PREV");
	},
	
	next: function(){
		this.player.sendEvent("NEXT");
	},

	mute: function(){
		var config = this.get_config();
		if(config.mute){
			this.player.sendEvent("MUTE", false);
		} else {
			this.player.sendEvent("MUTE", true);	
		}	
	},
	
	unmute: function(){
		this.player.sendEvent("MUTE", false);
	},
	
	volume: function(in_volume){
		this.player.sendEvent("VOLUME", in_volume);
	},

	ondone: function(in_function){
		this.player.addModelListener("STATE", in_function);
	},
	
	ontime: function(in_function){
		this.player.addModelListener("TIME", in_function);
	},
	
	removelistener: function(in_event, in_function){
		this.player.removeModelListener(in_event, in_function);
	},

	get_flash_object: function(in_id){
		if (window.document[in_id]){
			return window.document[in_id];
		}
		if (navigator.appName.indexOf("Microsoft Internet")==-1){
		    if (document.embeds && document.embeds[in_id]){
      			return document.embeds[in_id]; 
		  	} else {
		    	return document.getElementById(in_id);
		  	}
		}
	}

});
