var prevSong = null;
function onFinishSong()
{	
	if(prevSong) {
		$('#'+prevSong).removeClass('pause');
		$('#'+prevSong).addClass('play');
		var next = 'player-'+(parseInt(prevSong.replace('player-', '')) + 1);
		if($('#'+next).hasClass('play')) {
			$('#'+next).removeClass('play');
			$('#'+next).addClass('pause');
			if($('#'+next+'-flash').get(0)) {			
				$('#'+next+'-flash').get(0).playSong();				
			} else {
				$('#ie-'+next+'-flash').get(0).playSong();
			}
			$.post($listenUrl, {
				id: $('#'+next).attr('song')
			});
			prevSong = next;
		}			
	}
}

$(function(){	
        	
	$('form:not(.custom)').myEngineForm();
     
	$('a[rel="confirm"]').myEngineConfirm();
	$('a[rel="display"]').myEngineDisplay();
	$('a[rel="window"]').myEngineWindow(function(){
		$('textarea.bbcode').markItUp(mySettings);
		$('form:not(.custom)').myEngineForm();		
		$(".autocomplete").myEngineAutocomplete();
	});
	$(".autocomplete").myEngineAutocomplete();
	
	$('.messages tr.data').live('mouseover', function(){
		$(this).addClass('selected');
	});
	$('.messages tr.data').live('mouseout', function(){
		$(this).removeClass('selected');
	});
	
	$('a[rel="messages-select-all"]').live('click', function(){
		$('.messages tr.data input').attr('checked', 1);
	});
	$('a[rel="messages-unselect-all"]').live('click', function(){
		$('.messages tr.data input').attr('checked', 0);
	});
	
	$('.playerButton').live('click', function(e){		
		if(prevSong && prevSong != this.id) {
			$('#'+prevSong).removeClass('pause');
			$('#'+prevSong).addClass('play');
			if($('#'+prevSong+'-flash').get(0)) {			
				$('#'+prevSong+'-flash').get(0).stopSong();
			} else {
				$('#ie-'+prevSong+'-flash').get(0).stopSong();
			}
		}
		if($('#'+this.id).hasClass('play')) {
			$('#'+this.id).removeClass('play');
			$('#'+this.id).addClass('pause');
			if($('#'+this.id+'-flash').get(0)) {
				$('#'+this.id+'-flash').get(0).playSong();				
			} else {
				$('#ie-'+this.id+'-flash').get(0).playSong();
			}
			$.post($listenUrl, {
				id: $(this).attr('song')
			});
			prevSong = this.id;
		} else if($('#'+this.id).hasClass('pause')){
			$('#'+this.id).removeClass('pause');
			$('#'+this.id).addClass('play');			
			if($('#'+this.id+'-flash').get(0)) {			
				$('#'+this.id+'-flash').get(0).pauseSong();
			} else {
				$('#ie-'+this.id+'-flash').get(0).pauseSong();
			}
			prevSong = this.id;
		}
	});
	
				
	$('a[rel="with-selected"]').live('click', function(e){
		if(e.button==0) {
			e.preventDefault();				
			var result = '[', flag = false;
			$('.messages tr.data input').each(function (){
				if(this.checked) {
					if(flag ) {
						result = result + ',' + this.value;
					} else {
						result = result + this.value;
						flag = true;
					}
				}
			});
			result = result + ']';
				if(confirm($(this).attr('msg'))) {
				$.post(this.href, {
					data: result
				}, function () {
					location.reload();
				});
			}
		}
	});	
});