soundManager.useConsole = false;
soundManager.debugMode = false;
soundManager.useHighPerformance = true;
soundManager.url = '/img/js/sm2/';
soundManager.flashVersion = 9;
soundManager.onload = function(){
	Draugiem.musicPlayer.setReady( true );
};

Draugiem.musicPlayer = function(){
	Draugiem.musicPlayer.id ++;
	Draugiem.musicPlayer.createdPlayers.push( this );
	var $musicPlayer = this;
	this._ = {
		id:Draugiem.musicPlayer.id,
		playList:[],
		playOrder:[],
		currentSong:0,
		shuffle:false,
		repeat:false,
		volume:( empty( Draugiem.Cookie.read( 'musicPlayerVolume' ) ) ? 50 : Number( Draugiem.Cookie.read( 'musicPlayerVolume' ) ) )
	};
	this._.node = mkE( {
		tag:'div',
		className:'musicPlayer',
		prop:{
			onmousedown:function(){ return false; },
			onmouseup:function(){
				$musicPlayer._.volumeProgressNode.onmouseup();
			},
			onselectstart:function(){ return false; },
			oncontextmenu:function(){ return false; },
			style:{
				MozUserSelect:'none'
			}
		},
		els:[
			mkE( {
				tag:'div',
				className:'border',
				els:[
					mkE( {
						tag:'div',
						className:'track',
						els:[
							this._.positionNode = mkE( {
								tag:'div',
								className:'time',
								text:'--:--'
							} ),
							mkE( {
								tag:'div',
								className:'tracking',
								els:[
									mkE( {
										tag:'div',
										className:'progress',
										els:[
											this._.progress_contentNode = mkE( {
												tag:'div',
												className:'progress_content'
											} )
										]
									} ),
									this._.curPosNode = mkE( {
										tag:'div',
										className:'curPos link',
										prop:{
											onmousedown:function(){
												Draugiem.moveNode.enable( this, function( x, y ){
													if( x < 0 ) return false;
													if( x > $musicPlayer._.progress_contentNode.offsetWidth ) return false;
													this.style.left = x + 'px';
												} );
											},
											onMoveEnd:function( x ){
												var song = $musicPlayer._.playOrder[ $musicPlayer._.currentSong ];
												song.sm.setPosition( Math.floor( x / ( this.parentNode.offsetWidth - this.offsetWidth ) * song.sm.duration ) );
											}
										}
									} )
								]
							} ),
							this._.durationNode = mkE( {	
								tag:'div',
								className:'duration',
								text:'--:--'
							} )
						]
					} ), // div.track
					mkE( {
						tag:'div',
						className:'controls',
						els:[
							mkE( {
								tag:'table',
								els:[
									mkE( {
										tag:'tbody',
										els:[
											mkE( {
												tag:'tr',
												els:[
													mkE( {
														tag:'td',
														className:'sideTd',
														els:[
															mkE( {
																tag:'img',
																className:'shuffleButton link',
																prop:{
																	src:pimg + '/img/music/player/shuffle.gif',
																	onclick:function(){
																		$musicPlayer._.shuffle = ! $musicPlayer._.shuffle;
																		$musicPlayer.shuffle( $musicPlayer._.shuffle );
																		this.src = pimg + '/img/music/player/shuffle' + ( $musicPlayer._.shuffle ? 'on' : '' ) + '.gif';																		
																	}
																}
															} ),
															mkE( {
																tag:'img',
																className:'repeatButton link',
																prop:{
																	src:pimg + '/img/music/player/repeat.gif',
																	onclick:function(){
																		$musicPlayer._.repeat = ! $musicPlayer._.repeat;
																		this.src = pimg + '/img/music/player/repeat' + ( $musicPlayer._.repeat ? 'on' : '' ) + '.gif';
																	}
																}
															} )
														]
													} ), // td
													mkE( {
														tag:'td',
														els:[
															mkE( {
																tag:'img',
																className:'prevButton link',
																prop:{
																	src:pimg + '/img/music/player/prev.gif',
																	onclick:function(){
																		$musicPlayer.prevSong();
																	}
																}
															} ),
															this._.playButton = mkE( {
																tag:'img',
																className:'playButton link',
																prop:{
																	src:pimg + '/img/music/player/play.gif',
																	onclick:function(){										
																		var song = $musicPlayer._.playOrder[ $musicPlayer._.currentSong ];
																		if( ! song ) return;
																		var stat = $musicPlayer._.playList[ song.playListI ].sm.playState;
																		if( stat == 0 || $musicPlayer._.playList[ song.playListI ].sm.paused ){
																			$musicPlayer._.playList[ song.playListI ].sm.play();
																			$musicPlayer._.playButton.src = pimg + '/img/music/player/pause.gif';
																		} else {
																			$musicPlayer._.playList[ song.playListI ].sm.pause();
																			$musicPlayer._.playButton.src = pimg + '/img/music/player/play.gif';
																		}
																	}
																}
															} ),
															mkE( {
																tag:'img',
																className:'nextButton link',
																prop:{
																	src:pimg + '/img/music/player/next.gif',
																	onclick:function(){
																		$musicPlayer.nextSong();
																	}
																}
															} )
														]
													} ), // td
													mkE( {
														tag:'td',
														className:'sideTd',
														attr:{
															align:'right'
														},
														els:[
															mkE( {
																tag:'div',
																className:'volume',
																els:[
																	this._.volumeProgressNode = mkE( {
																		tag:'div',
																		className:'volumeProgress link',
																		prop:{
																			onclick:function( e ){
																				if( ! e ) e = event;
																				if( e && e.layerX ){																					
																					$musicPlayer._.volumeProgressContentNode.style.width = e.layerX + 'px';
																				} else {																					
																					$musicPlayer._.volumeProgressContentNode.style.width = e.x - this.offsetLeft;
																				}
																				$musicPlayer.volume( Math.round( $musicPlayer._.volumeProgressContentNode.offsetWidth / this.offsetWidth * 100 ) );
																			},
																			onmousedown:function(){
																				this.mouseDown = true;
																			},
																			onmouseup:function(){
																				this.mouseDown = false;
																			},
																			onmousemove:function( e ){
																				if( this.mouseDown ) this.onclick( e );
																			}
																		},																		
																		els:[
																			this._.volumeProgressContentNode = mkE( {
																				tag:'div',
																				className:'volumeProgressContent',
																				prop:{
																					style:{
																						width:this._.volume + '%'
																					}
																				}
																			} )
																		]
																	} ) // div
																]
															} ) // div.volume
														]
													} ) // td
												]
											} ) // tr
										]
									} ) // tbody
								]
							} ) // table							
						]
					} ),
					this._.playListNode = mkE( {
						tag:'div',
						className:'playList load'
					} )
				]
			} ) // div.border
		]
	} );
};
Draugiem.musicPlayer.id = 0;
Draugiem.musicPlayer.createdPlayers = [];
Draugiem.musicPlayer.ready = false;
Draugiem.musicPlayer.setReady = function( v ){
	Draugiem.musicPlayer.ready = v;
	for( var k in Draugiem.musicPlayer.createdPlayers ){
		removeClassName( Draugiem.musicPlayer.createdPlayers[ k ]._.playListNode, 'load' );
	}	
};

Draugiem.musicPlayer.prototype.append = function( node ){
	if( typeof node == 'object' ){
		node.appendChild( this._.node );
		return this;
	}
	var newNode;
	if( typeof node == 'string' && ( newNode = document.getElementById( node ) ) ){
		newNode.appendChild( this._.node );
		return this;
	}
	return false;
};

Draugiem.musicPlayer.prototype.addSong = function( par ){
	Draugiem.musicPlayer.prototype.addSong.id ++;
	var $musicPlayer = this;
	var song = {
		rnd:1
	};	
	this._.playList.push( song );
	song.playListI = this._.playList.length - 1;	
	song.node = mkE( {
		tag:'div',
		className:'listItem link ' + ( song.playListI == 0 ? ' act' : '' ),
		prop:{
			onclick:function(){
				for( var k in $musicPlayer._.playOrder ){
					if( $musicPlayer._.playOrder[ k ] === song ){
						$musicPlayer.playSong( k );
						break;
					}
				}				
			},
			onmouseover:function(){
				if( song.titleNode.scrollWidth <= song.titleNode.offsetWidth ) return;
				song.interval = setInterval( function(){
					song.titleNode.scrollLeft = song.titleNode.scrollLeft + 5;
				}, 100 );
			},
			onmouseout:function(){
				if( song.interval ) clearInterval( song.interval );
				song.titleNode.scrollLeft = 0;
			}
		},
		els:[
			song.lengthNode = mkE( {
				tag:'div',
				className:'length',
				text:par.length || '--:--'
			} ),
			mkE( {
				tag:'div',
				className:'padding',
				els:[
					song.titleNode = mkE( {
						tag:'div',
						className:'title',
						text:par.title || 'No title'
					} )
				]
			} )
		]
	} ).append( this._.playListNode );
	var method = 'createVideo';
	if( par.url.substr( par.url.length - 3, 3 ) == 'mp3' ) method = 'createSound';
	song.sm = soundManager[ method ]( {
		id:'musicPlayer_' + String( this.id ) + '_' + String( Draugiem.musicPlayer.prototype.addSong.id ),
		autoLoad:false,
		url:par.url,
		volume:this._.volume,
		whileloading:function(){
			$musicPlayer._.progress_contentNode.style.width = String( Math.round( this.bytesLoaded / this.bytesTotal * 100 ) ) + '%';
		}, // whileloading
		whileplaying:function(){
			if( this.options.volume != $musicPlayer._.volume ) this.setVolume( $musicPlayer._.volume );
			$musicPlayer._.progress_contentNode.style.width = String( Math.round( this.bytesLoaded / this.bytesTotal * 100 ) ) + '%';
			var node = $musicPlayer._.curPosNode;
			if( ! Draugiem.moveNode.nowMoving( node ) ) node.style.left = String( ( node.parentNode.offsetWidth - node.offsetWidth ) * this.position / this.duration ) + 'px';
			$musicPlayer._.positionNode.innerHTML = Draugiem.musicPlayer.ms2O( this.position ).time;
			$musicPlayer._.durationNode.innerHTML = Draugiem.musicPlayer.ms2O( this.duration ).time;
			if( song.lengthNode.innerHTML == '--:--' || song.lengthNode.innerHTML < $musicPlayer._.durationNode.innerHTML ) song.lengthNode.innerHTML = $musicPlayer._.durationNode.innerHTML;			
			if( ! song.sm.paused ) $musicPlayer._.playButton.src = pimg + '/img/music/player/pause.gif';
		}, // whileplaying
		onfinish:function(){
			$musicPlayer.nextSong();
		}
	} );
	this._.playOrder.push( song );
};
Draugiem.musicPlayer.prototype.addSong.id = 0;

Draugiem.musicPlayer.prototype.playSong = function( nr ){
	if( ! this._.playOrder[ nr ] ) return false;
	this._.progress_contentNode.style.width = '1px';
	soundManager.stopAll();
	for( var k in this._.playList )	removeClassName( this._.playList[ k ].node, 'act' );
	var song = this._.playOrder[ nr ];
	this._.currentSong = nr;
	addClassName( this._.playList[ song.playListI ].node, 'act' );
	this._.playList[ song.playListI ].sm.play();
};

Draugiem.musicPlayer.prototype.nextSong = function(){
	if( this._.playOrder[ Number( this._.currentSong ) + 1 ] ){
		this.playSong( Number( this._.currentSong ) + 1 );
	} else if( this._.playOrder[ 0 ] && this._.repeat ){
		this.shuffle( this._.shuffle );
		this.playSong( 0 );
	} else {
		this._.playButton.src = pimg + '/img/music/player/play.gif';
	}
};

Draugiem.musicPlayer.prototype.prevSong = function(){
	if( this._.playOrder[ Number( this._.currentSong ) - 1 ] ){
		this.playSong( Number( this._.currentSong ) - 1 );
	} else if( this._.playOrder[ this._.playOrder.length - 1 ] && this._.repeat ){
		this.shuffle( this._.shuffle );
		this.playSong( this._.playOrder.length - 1 );
	} else {
		this._.playButton.src = pimg + '/img/music/player/play.gif';
	}
};

Draugiem.musicPlayer.prototype.setPlayList = function( list ){
	var $musicPlayer = this;
	for( var k in this._.playList ) this._.playList[ k ].sm.destruct();
	if( ! Draugiem.musicPlayer.ready ){
		setTimeout( function(){
			$musicPlayer.setPlayList( list );
		}, 200 );
		return;
	}
	clearNode( this._.playListNode );
	this._.playList = [];
	this._.playOrder = [];
	for( var k in list ) this.addSong( list[ k ] );
};

Draugiem.musicPlayer.prototype.shuffle = function( v ){
	if( v ){
		for( var k in this._.playOrder ) this._.playOrder[ k ].rnd = Math.random();
		this._.playOrder.sort( function( x, y ){
			if( x.rnd > y.rnd ) return 1;
			if( x.rnd < y.rnd ) return -1;
			return 0;
		} );
	} else {
		this._.playOrder.sort( function( x, y ){
			if( x.playListI > y.playListI ) return 1;
			if( x.playListI < y.playListI ) return -1;
			return 0;
		} );
	}
};

Draugiem.musicPlayer.prototype.volume = function( v ){
	if( v === false || typeof v == 'undefined' ) return this._.volume;
	if( v < 0 || v > 100 ) return this._.volume;
	this._.volume = Math.round( Number( v ) );
	Draugiem.Cookie.create( 'musicPlayerVolume', this._.volume, 7 );
	return this._.volume;
};

Draugiem.musicPlayer.ms2O = function( ms ){
	var re = {};
	re.m = Math.floor( ms / 60000 );
	re.s = String( Math.floor( ( ms - re.m * 60000 ) / 1000 ) );
	re.m = String( re.m );
	if( re.m.length < 2 ) re.m = '0' + re.m;
	if( re.s.length < 2 ) re.s = '0' + re.s;
	re.time = re.m + ':' + re.s;
	return re;
};