var GRadioPlayer = function(uniqId, params, listener) 
{
	var self = this;
	this.uniqId = uniqId;
	
	this.params = params;
	this.listener = listener;
		
	this.listener.onInit = function(){
		
		self.player = new GSWFPlayer(uniqId);
		self.containerNode = document.getElementById(uniqId + '_player');
		self.construct();
		
	};
};

GRadioPlayer.prototype = 
{
	construct: function()
	{
		var self = this;

		this.player.setUrl(this.params.url);
		
		this.player.setVolume(this.params.volume);
		this.setVolume(this.params.volume / 5, true);
		
		this.$('.grpplay').click(function(){
			self.player.play();
			$(this).hide();
			self.$('.grppause').show();
		});
		
		this.$('.grppause').click(function(){
			self.player.pause();
			$(this).hide();
			self.$('.grpplay').show();
		});
		
		if ( this.params.autoplay )
		{
			this.$('.grpplay').click();
		}
		
		this.$('.grpstatus').hover(function(){
			$(this).find('marquee').attr('truespeed', 'truespeed');
		}, function(){
			$(this).find('marquee').removeAttr('truespeed');
		});
		
		this.$('.grpsound').click(function(e){
			var x = e.clientX - $(this).offset().left;
			self.setVolume(x + 1);
		});
		
		var mousedown = false; 
		this.$().mousedown(function(e){
			mousedown = true;

			return false;
		});
		
		this.$().mouseup(function(e){
			mousedown = false;

			return false;
		});
		
		this.$('.grpsound').mousemove(function(e){

			if (!mousedown)
			{
				return false;
			}

			var x = (e.clientX + 2) - $(this).offset().left;

			self.setVolume(x);

			return false;
		});
	},
	
	setVolume: function(k, exact)
	{exact = true;

		k = k > 20 ? 20 : k;
		k = k < 3 ? 0 : k;
	
		this.$('.grpsound .slide').width(k);
		v = exact ? k * 5 : k * (k / 4);

		this.player.setVolume(v);
		
		cookieName = this.uniqId + '_volume';
		document.cookie = cookieName + '=' + v;
	},

	$: function( selector )
	{
		if (!selector)
		{
			return $(this.containerNode);
		}

		return $(selector, this.containerNode);
	}
};




var GSWFPlayer = function( id )
{
	this.id = id;
}

GSWFPlayer.prototype = 
{
	getObject: function()
	{
		return document.getElementById(this.id);
	},
		
	setUrl: function( url )
	{
		this.getObject().SetVariable('method:setUrl', url);
	},
		
	play: function()
	{
		this.getObject().SetVariable('method:play', '');
	},
	
	pause: function()
	{
		this.getObject().SetVariable('method:pause', '');
	},
	
	stop: function()
	{
		this.getObject().SetVariable('method:stop', '');
	},
	
	setPosition: function( pos )
	{
		this.getObject().SetVariable('method:setPosition', pos);
	},
	
	setVolume: function( vol )
	{
		this.getObject().SetVariable('method:setVolume', vol);
	}
}


var GPlayerListener = function()
{
	this.onInit = function() {};
	this.onUpdate = function() {};
};

