var MyPlayer = new Class({
	
	Extends: Wimpy_Player,
	
	_analyticsTracker: null,
	_clipLength: null,
	_clipTimer: null,
	_clipMode: 'fadeBreak',
	_volume: 100,
	_volumeIsIncreasing: false,
	_volumeTimer: null,
	
	
	initialize: function(playlist, analyticsTracker, clipLength){
		this.parent(playlist);
		this._analyticsTracker = analyticsTracker;
		this._clipLength = ($defined(clipLength)) ? clipLength : null;
	},
	
	_onNotReady: function(){
		// display only if user hits play before player is loaded
		$('playerLoadStatus').set('display', 'block').set('text', 'Player is loading...');
	},
	
	_onReady: function(){
		wimpy_clearPlaylist();
		wimpy_appendPlaylist(this._playlist.get());
		// player is now ready - clear loading indicator
		$('playerLoadStatus').setStyle('display', 'none');
		
		// start player immediately the track specified in query
		var params = this.getQueryParams(location.href);
		if(params.has('trackId')){ this.toggle(params.get('trackId')); }
	},
	
	_onTrackStarted: function(){
		$('title_' + this.getCurrentId()).set('class', 'playing');
		$('toggle_' + this.getCurrentId()).set('class', 'trackLink playing');
		/*$('toggle_' + this.getCurrentId()).set('text', 'Stop');*/
		
		var duration = wimpy_getPlayerState().duration;
		startTurntable();
		
		if(this._clipLength !== null){
			this._startClip();
		}
	},
	
	_onPlaying: function(){
		var l = (wimpy_getLoadState().percent * 100).floor();
		var sec = wimpy_getPlayheadSeconds();
		var t = (isNaN(sec)) ? 0 : sec;
		var t = this.makeNiceTimeFromSeconds(t);
		$('loaded_' + this.getCurrentId()).set('text', ((l < 100) ? l +'% // ' : ''));
		//$('elapsed_' + this.getCurrentId()).set('text',t+' OF ' );
		$('elapsed_' + this.getCurrentId()).set('text',' NOW PLAYING' );
		
		/*ANSI*/
		moveArm((wimpy_getPlayerState().percent * 100).floor(),l,sec);
	},
	
	_onTrackStop: function(){
		this._handleStops();
		stopTurntable();
	},
	
	_onTrackDone: function(){
		this._handleStops();
		stopTurntable();
		/*
		var myRequest = new Request({
			url: '/_Tests/wimpy/ajax.php',
			method: 'post',
			onSuccess: function(responseText, responseXML) {
				//alert(responseText);
			}
		});
		myRequest.post({
			uid: '6hz23hz563',
			trackId: this.getCurrentId()
		});
		*/
	},
	
	
	_stopClip: function()
	{
		$clear(this._clipTimer);
		$clear(this._volumeTimer);
	},
	
	
	_startClip: function()
	{
		$clear(this._clipTimer);
		$clear(this._volumeTimer);
		this._volume = 100;
		wimpy_setVolume(this._volume);
		//this._clipTimer = this._startClipTimer.periodical(this._clipLength, this);
	},
	
	
	_startClipTimer: function()
	{
		//$('clipTester').set('text', this._clipCount++);
		if(this._clipMode == 'fade')
		{
			this._dbg(this._volume);
			$clear(this._volumeTimer);
			this._volume = 100;
			this._volumeTimer = this._startFadeGap.periodical(10, this);
		}
		else if(this._clipMode == 'break')
		{
			$clear(this._volumeTimer);
			wimpy_setVolume(0);
			this._volumeTimer = this._startBreakGap.periodical(1000, this);
		}
		else if(this._clipMode == 'fadeBreak')
		{
			this._dbg(this._volume);
			$clear(this._volumeTimer);
			this._volume = 100;
			this._volumeTimer = this._startFadeBreakeDecrease.periodical(10, this);
		}
	},
	
	
	_startFadeGap: function()
	{
		this._dbg(this._volume);
		wimpy_setVolume(this._volume);
		
		if(this._volume > 0 && !this._volumeIsIncreasing)
		{
			this._volume--;
		}
		else if(this._volume < 100)
		{
			this._volumeIsIncreasing = true;
			this._volume++;
		}
		else if(this._volume == 100)
		{
			this._volumeIsIncreasing = false;
			$clear(this._volumeTimer);
		}
	},
	
	
	_startBreakGap: function(length)
	{
		wimpy_setVolume(100);
		$clear(this._volumeTimer);
	},
	
	
	_startFadeBreakeDecrease: function()
	{
		wimpy_setVolume(this._volume);
		
		if(this._volume > 0)
		{
			//this._dbg(this._volume);
			this._volume--;
		}
		else if(this._volume == 0)
		{
			$clear(this._volumeTimer);
			this._volumeTimer = this._startFadeBreakWait.delay(500, this);
		}
	},
	
	
	_startFadeBreakWait: function()
	{
		wimpy_setVolume(0);
		$clear(this._volumeTimer);
		this._volumeTimer = this._startFadeBreakIncrease.periodical(10, this);
	},
	
	
	_startFadeBreakIncrease: function()
	{
		wimpy_setVolume(this._volume);
		
		if(this._volume < 100)
		{
			//this._dbg(this._volume);
			this._volume++;
		}
		else if(this._volume == 100)
		{
			$clear(this._volumeTimer);
		}
	},
	
	
	/*
	_startClipTimer: function()
	{
		$('clipTester').set('text', this._clipCount++);
		this._dbg(this._volume);
		$clear(this._volumeTimer);
		this._volume = 100;
		this._volumeTimer = this._startGap.periodical(10, this);
	},
	
	
	_startGap: function()
	{
		this._dbg(this._volume);
		wimpy_setVolume(this._volume);
		
		if(this._volume > 0 && !this._volumeIsIncreasing)
		{
			this._volume--;
		}
		else if(this._volume < 100)
		{
			this._volumeIsIncreasing = true;
			this._volume++;
		}
		else if(this._volume == 100)
		{
			this._volumeIsIncreasing = false;
			$clear(this._volumeTimer);
		}
	},
	*/
	
	
	_dbg: function(s){
		//$('volumeTester').set('text', s);
	},
	
	
	/*ANSI*/
	skipToPercent: function(newPosition)
	{
		var loaded  = wimpy_getLoadPercent();
		
		if(loaded > newPosition){
		  	wimpy_setPlayheadPercent(newPosition);
		}
		else if(loaded < newPosition) {
		   wimpy_setPlayheadPercent(loaded);
		}
		
		if(this._clipLength !== null){
			this._startClip();
		}
	},
	
	detach: function(url, width, height, startPlayingOnload)
	{
		url += '?detached=1';
		
		if(this.getCurrentId() !== null)
		{
			// currently playing - add trackId so detached player starts playing this track
			url += '&trackId=' + this.getCurrentId();
			this.stop();
		}
		var winName = "a" + randomNumber(1, 1000);
		this._win = window.open(url, winName, 'width=' + width + ',height=' + height + ',scrollbars=no');
	},
	
	
	_handleStops: function(){
		this._stopClip();
		if(this._analyticsTracker !== null){
			var artist = this._playlist.getTrackArtist(this.getCurrentId());
			var title = this._playlist.getTrackTitle(this.getCurrentId());
			this._analyticsTracker._trackPageview('/' + artist + '/' + title);
			//alert('/' + artist + '/' + title);
		}
		$('title_' + this.getCurrentId()).erase('class', 'playing');
		$('loaded_' + this.getCurrentId()).set('text', '');
		$('elapsed_' + this.getCurrentId()).set('text', '');
		$('toggle_' + this.getCurrentId()).set('class', 'trackLink stopped');
		/*$('toggle_' + this.getCurrentId()).set('text', 'Listen');*/
	}
});