(function($){
	
	var _options;
	var _container;
	var _holder;
	var _flash;
	var _playerId = 'spoti-player'
	var _playerSource = '/swfs/player.swf';
	var _currentState = 'collapsed'
	
	/**
	 * Spoti object.
	 * 
	 */
	$.spoti = function() {
		
	};
	
	/**
	 * Creates player.
	 * 
	 * @param object options
	 */
	$.fn.createPlayer = function(options) {

		// Save options.

		_options = $.extend(
			true,
			{
				url: '',
				source: '',
				link: '',
				collapsedSound: false,
				bufferTime: 0.1,
				holderCollapsedWidth: 800,
				holderCollapsedHeight: 300,
				holderExpandedHeight: 600,
				flashExpandedWidth: 600,
				flashExpandedHeight: 500,
				collapsedBackground: '',
				expandedBackground: ''  
			},
			options
		);
		
		// Create template.
		
		$(this).append(createTemplate());
		
		// Position.
		
		position();
		
		// Add listeners.
		
		$(window).resize(position);
		
	}
	
	/**
	 * Collapse player.
	 * 
	 */
	$.spoti.collapsePlayer = function() {

		// Resize holder.
			
		$(_holder).css('width', (_options.holderCollapsedWidth +'px'));
		$(_holder).css('height', (_options.holderCollapsedHeight +'px')); 
		$(_holder).css('background', (isUrl(_options.collapsedBackground) ? ('url(' + _options.collapsedBackground + ')') : _options.collapsedBackground));
		
		// Resize flash object.

		var flashObject = getFlashObject();
		flashObject.width = _options.holderCollapsedWidth;
		flashObject.height = _options.holderCollapsedHeight;	

		// Update state.
		
		_currentState = 'collapsed';
		
		// Position.
		
		position();		
			
	}
	
	/**
	 * Expands player.
	 * 
	 */
	$.spoti.expandPlayer = function() {

		// Resize holder.
		
		$(_holder).css('width', (_options.holderCollapsedWidth +'px'));
		$(_holder).css('height', (_options.holderExpandedHeight +'px')); 
		$(_holder).css('background', (isUrl(_options.expandedBackground) ? ('url(' + _options.expandedBackground + ')') : _options.expandedBackground));

		// Resize flash object.

		var flashObject = getFlashObject();
		flashObject.width = _options.flashExpandedWidth;
		flashObject.height = _options.flashExpandedHeight;	
		
		// Update state.
		
		_currentState = 'expanded';
		
		// Position.
		
		position();		
				
	}

	/**
	 * Is url?
	 * 
	 */
	function isUrl(parameter) {
		return (parameter.indexOf('http://') != -1);
	}

	/**
	 * Positions.
	 * 
	 */
	function position() {
		
		// Get position.
		
		var position = $(_container).offset();
		
		// Move.
		
		$(_holder).css('left', ((position.left) +'px')); 
		$(_holder).css('right', ((position.top) +'px'));
		switch(_currentState) {
			case 'collapsed':
				$(_flash).css('left', '0px');
				$(_flash).css('right', '0px');					
			break;
			case 'expanded':
				$(_flash).css('left', (((_options.holderCollapsedWidth - _options.flashExpandedWidth) / 2) +'px')); 
				$(_flash).css('top', (((_options.holderExpandedHeight - _options.flashExpandedHeight) / 2) +'px')); 
			break;			
		}	
			
	}
	
	/**
	 * Creates template.
	 * 
	 */
	function createTemplate() {
		
		// Create container.
		
		_container = $('<div />');			
		$(_container).css('width', (_options.holderCollapsedWidth +'px'));
		$(_container).css('height', (_options.holderCollapsedHeight +'px')); 
		$(_container).css('margin', '0 auto');
		 
		// Create holder.
		
		_holder = $('<div />');
		$(_holder).css('width', (_options.holderCollapsedWidth +'px'));
		$(_holder).css('height', (_options.holderCollapsedHeight +'px')); 
		$(_holder).css('position', 'absolute');
		$(_holder).css('background', (isUrl(_options.collapsedBackground) ? 'url(' + _options.collapsedBackground + ')' : _options.collapsedBackground));
		$(_holder).css('z-index', 10000);			
		$(_container).append(_holder);
		
		// Create flash.
		
		_flash = $(getFlashTemplate(
			_options.holderCollapsedWidth, 
			_options.holderCollapsedHeight,
			(
				'source='+ escape(_options.source) +
				'&linkLabel='+ escape(_options.link.label) +
				'&linkUrl='+ escape(_options.link.url) +
				'&bufferTime='+ escape(_options.bufferTime) +
				'&collapsedSound='+ escape(_options.collapsedSound ? 'true' : 'false') +
				'&url='+ escape(_options.url)
			)
		));
		$(_flash).css('position', 'relative');
		$(_holder).append(_flash);		
			
		// Return container.	
			
		return _container;
		
	}
	
	/**
	 * Gets flash object.
	 * 
	 */
	function getFlashObject() {
		if (window.document[_playerId]) {
			return window.document[_playerId];
		}
		if (navigator.appName.indexOf("Microsoft Internet") == -1) {
			if (document.embeds && document.embeds[_playerId]) {
				return document.embeds[_playerId]; 
			} else {
				return document.getElementById(_playerId);
			}
		}
	}
	
	/**
	 * Gets flash template.
	 * 
	 * @param uint width
	 * @param uint height
	 * @param string flashvars
	 */
	function getFlashTemplate(width, height, flashvars) {
		return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="'+ width +'" height="'+ height +'" id="'+ _playerId +'" align="middle">'+
			'<param name="allowScriptAccess" value="sameDomain" />'+
			'<param name="allowFullScreen" value="false" />'+
			'<param name="movie" value="'+ _playerSource +'" />'+
			'<param name="quality" value="high" />'+
			'<param name="flashvars" value="'+ flashvars +'" />'+
			'<param name="wmode" value="transparent">'+	
			'<embed src="'+ _playerSource +'" wmode="transparent" swliveconnect="true" flashvars="'+ flashvars +'" quality="high" width="'+ width +'" height="'+ height +'" name="'+ _playerId +'" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer_es" />'+
		'</object>';			
	} 
	
})(jQuery);