(function ( $ ){
	
	var local = {};
	
	local.html = '<span class="ui-icon pb-absolute"></span><h3 class="pb-label"></h3><div class="box-burnchar pb-radius4 pb-ofhidden"><div class="co-progress"></div><div class="co-load"></div></div><div class="box-time-display pb-absolute">00:00</div><div class="box-time-display-break pb-absolute">/</div><div class="box-time-display2 pb-absolute">00:00</div>';
	
		PB.Player.Skin.register('pbsite', {
		
		/**
		 *
		 */
		init: function ( renderTo ) {
			
			var player = this.player;
			
			renderTo.html( local.html  );
			
			this._playtime = renderTo.find('.box-time-display')[0];
			this._playprogress = renderTo.find('.co-progress')[0];
			this._progresshandler = renderTo.find('.co-load')[0];
			this._duration = renderTo.find('.box-time-display2')[0];
			this._bufferbar = renderTo.find('.co-load')[0];
			this._playerbar = renderTo.find('.box-burnchar')[0];
			this._playPause = renderTo.find('.ui-icon')[0];
			//this._handler = renderTo.find('.pbplayer-control-button')[0
			this._songTitle = renderTo.find('.pb-label')[0];
			//this._playlist = renderTo.find('.pbplayer-playlist-skin')[0];
			
			// Events
			this.addEvents();
			
			this.setTitle( player.playlist.files[0][0].title );
						
			this._playerEvent = this.playerEvent.bind(this);
			
			this.player
				.on('loadProgress', this._playerEvent)
				.on('timeupdate', this._playerEvent)
				.on('totalTime', this._playerEvent)
				.on('error', this._playerEvent)
				.on('pause', this._playerEvent)
				.on('play', this._playerEvent)
				.on('stop', this._playerEvent)
			//	.on('playing', this._playerEvent)
			//	.on('volumechange', this._playerEvent)
				.on('load', this._playerEvent)
				.on('ended', this._playerEvent)
				.on('duration', this._playerEvent);
		
			// iPad cant adjust volume, guess iPhone either
			if( /Mobile/.test(navigator.userAgent) ) {

				// Hide volume
			}
		},
		
		/**
		 *
		 */
		addEvents: function () {
			
			// Play/pause toggle
			this._playPause.on('click', function ( e ){
				
				e.stop();
				
				if ( this._playPause.hasClass('pb-pauze') === true ) {
					
					this.player.pause();
				} else {
					
					this.player.play();
				}
			}.bind(this));
			
			// Progress events
			this._bufferbar.on('click', this.progressPoint.bind(this));
			this._playprogress.on('click', this.progressPoint.bind(this));
			
			this.progressDragUpdateWrapper = this.progressDragUpdate.bind(this);
			this.progressDragDestroyWrapper = this.progressDragDestroy.bind(this);
			
		},
	
		/**
		 * Delegate player events
		 */
		playerEvent: function ( type, args ) {
			
			switch( type ) {
				
				case 'play':
					this._playPause.addClass('pb-pauze');
					break;
				
				case 'stop':
				case 'pause':	
					this._playPause.removeClass('pb-pauze');
					break;
				
				case 'volumechange':
					this._volumeSliderHandler.setStyle('bottom', args[0]+'%');
					this._volumeSlider.setStyle('height', args[0]+'%');
					break;
					
				case 'timeupdate':
					var time = new Date();
					time.setTime( parseFloat(args[0])*1000 );
					this._playtime.html( time.format('i:s') );
					
					var percent = parseFloat(args[0]) / (this.duration/100);
					
					this._playprogress.width( percent+"%" );
					this._progresshandler.setStyle( "left", percent+"%" );
					break;
				
				case 'duration':
					var time = new Date();
					time.setTime( parseFloat(args[0])*1000 );
					
					if( isNaN(time) === false ) {
						this._duration.html( time.format('i:s') );
					}
					
					this.duration = parseFloat(args[0]);
					break;
			
				case 'loadProgress':
					this._bufferbar.width( args[0]+'%' );
					break;
				
				case 'ended':
					// Stop or next in playlist :)
					this.player.pause();
					break;
			}
		},
		
		progressDragInit: function ( e ) {
			
			e.stop();
			
			// prevent text selection in IE 
			document.onselectstart = function () { return false; };
			
			$(document).on('mousemove', this.progressDragUpdateWrapper);
			$(document).on('mouseup', this.progressDragDestroyWrapper);
		},
		
		progressDragUpdate: function ( e ) {
			
			if( e.button !== 1 && e.button !== 0 ) {
				
				this.progressDragDestroy();
				return;
			}
			
			this.progressPoint( e );
		},
		
		progressDragDestroy: function () {
			
			document.onselectstart = null;
			
			$(document).stop('mousemove', this.progressDragUpdateWrapper);
			$(document).stop('mouseup', this.progressDragDestroyWrapper);
		},
		
		progressPoint: function ( e ) {
			
			var position = this._playerbar.position(),
				width = this._playerbar.width(),
				x = e.pageX - position.left,
				percent = x / (width / 100);
				
			// Min / max
			percent = ( percent < 0 ) ? 0 : ( percent > 100 ) ? 100 : percent;
			
			this.player.playAt( this.duration * (percent/100) );
		},
		
		/**
		 * Set song title
		 */
		setTitle: function ( title ) {
			if( typeof title === 'string' ) {
				
				this._songTitle.show().html( title );
			} else {

				this._songTitle.hide();
			}
		}
	});
	
	$.ready(function (){
		
		$(document).find('a').forEach(function ( element ){
			
			var url = element.attr('data-href') || element.attr('href') || '';

			if( url.substr(0, 2) === 'm*' ) {
	
				element.attr('data-href', url.replace('?', '@').replace(':', '.').replace('m*', 'mailto:'));
				element.html( element.html().replace('?', '@').replace(':', '.') );
			}
		});
	});
	
})( PB );


