
startPlaying = 1;

function playSong(songnr) {

	if(startPlaying == 1) {

		// Player läuft bereits; Player stoppen bzw. aktualisieren.
		if(Element.visible('currentlyplaying')) {

			// Player spielt denselben Song, der jetzt angeklickt wurde. Also: Song zum Anfang zurück-
			// spulen und erneut wiedergeben
			if(document.getElementById('currentlyplaying').getAttribute('rel') == 'song'+songnr) {

				tmpPlayerObj = document.getElementById('mp3player');
				tmpPlayerHTML = tmpPlayerObj.innerHTML;
				tmpPlayerObj.innerHTML = tmpPlayerHTML;

			}


			// Player soll anderen Song spielen
			else {

				oldSongID = document.getElementById('currentlyplaying').getAttribute('rel');


				if(document.getElementById(oldSongID)) {
					Effect.Fade('currentlyplaying',{duration:0.4});

					Effect.Fade('isplaying',
						{ duration:0.4, afterFinish:function() { Element.remove('isplaying'); document.getElementById('currentlyplaying').removeAttribute('rel'); document.getElementById(oldSongID).childNodes[0].className = '';
						Element.update('playbox_trackname','<span id="edit_filename">no track selected</span>');
						playSong(songnr); } }
					);
				}

				else {
					Effect.Fade('currentlyplaying',{duration:0.4,afterFinish:function() {
						Element.update('playbox_trackname','<span id="edit_filename">no track selected</span>');
						playSong(songnr);
					}});
				}


				// Die Songbox wird schonmal aktiviert, damit der Übergang besser aussieht.
				document.getElementById('song'+songnr).childNodes[0].className = 'playing';

			}

		}


		// Player läuft noch nicht; Player starten.
		else {

			// Mouseover
			document.getElementById('song'+songnr).childNodes[0].className = 'playing';
			document.getElementById('song'+songnr).innerHTML = document.getElementById('song'+songnr).innerHTML + '<img src="./pics/isplaying.png" id="isplaying" alt="" style="display:none;" />';

			// Tracknamen einfügen
			document.getElementById('edit_filename').innerHTML = document.getElementById('song'+songnr).getElementsByClassName('trackname')[0].innerHTML;

			// paste directlink
			document.getElementById('playbox_directlink').innerHTML = '<a href="?play='+songnr+'">direct link</a>';

			// MP3-Player initialisieren
			new Ajax.Request('./inc/ajax.mp3player.php',{ postBody:'songID='+songnr, method:'post',

				onFailure:function() {
					alert('ajax request failure')
				},

				onSuccess:function(t) {
					Element.update('mp3player',t.responseText);
				}

			});


			// Playerbox einblenden
			Effect.Appear('isplaying',{duration:0.4});
			Effect.Appear('currentlyplaying',{duration:0.4});
			document.getElementById('currentlyplaying').setAttribute('rel','song'+songnr);
			new Ajax.InPlaceEditor('edit_filename', './inc/ajax.update_mp3filename.php?fileID='+songnr, {highlightcolor:'#626262',highlightendcolor:'#000000',clickToEditText:'click to edit',formClassName:'edit_songinfo',okText:'save',onComplete:function(transport,element){ if(transport) { updateSongName(transport.responseText,songnr); } }});


		}

	}

	return false;

}






// update song name after renaming
function updateSongName(songName,songnr) {

	if(songName != '') {

		if(obj = document.getElementById('song'+songnr).getElementsByClassName('trackname')[0]) {

			obj.innerHTML = songName;

		}

	}

}




/*
// add all shown songs to playlist
function addAllSongsToPlaylist() {

	// this is only possible when playlist is still empty
	if(document.getElementById('list_playlist').childNodes.length == 0) {

		// get all songs
		obj = document.getElementsByClassName('list_tracklist_song');
		for(i=0; i<obj.length; i++) {

			addSongToPlaylist(obj[i].id.replace(/song/,''));

		}

	}

	return false;

}






// add a song to playlist
function addSongToPlaylist(songnr) {

	addsong = true;


	// only add song if it is not yet in playlist
	if(document.getElementById('playlist'+songnr)) { addsong = false; }


	if(addsong) {

		tmp_trackname = document.getElementById('song'+songnr).getElementsByClassName('trackname')[0].innerHTML;
		newObj = document.createElement('li');
		newObj.setAttribute('id','playlist'+songnr);
		newObj.setAttribute('style','display:none;');
		document.getElementById('list_playlist').appendChild(newObj);
		document.getElementById('playlist'+songnr).innerHTML = '<a href=\"#\" rel=\"'+songnr+'\" onclick=\"return playSong(\''+songnr+'\');\">'+tmp_trackname+'</a>';
		Effect.Appear('playlist'+songnr);

		Sortable.create('list_playlist');

		if(Element.visible('b_addallsongs')) {
			Element.hide('b_addallsongs');
		}

	}

	else {

		showAlert('this song already is in your playlist.');

	}

	return false;

}
*/





// show a little div for alerts
function showAlert(text) {

	newID = 'alertbox';

	if(!document.getElementById(newID)) {

		newObj = document.createElement('div');
		newObj.setAttribute('class','alertbox');
		newObj.setAttribute('id',newID);
		newObj.setAttribute('style','display:none;');
		document.getElementById('body').appendChild(newObj);
		document.getElementById(newID).innerHTML = text;


		xPos = 0; yPos = 0;
		function savePosition(e) {
			xPos = Event.pointerX(e)+15;
			yPos = Event.pointerY(e)+15;
			Element.setStyle(newID,{'top':yPos+'px', 'left':xPos+'px'});
			Event.stopObserving(document, "mouseover", savePosition, false);
		}

		Event.observe(document, "mouseover", savePosition, false);
		new Effect.Appear(newID,{duration:0.6,afterFinish:function() {
			new Effect.Highlight(newID,{startcolor:'#636363',endcolor:'#000000',afterFinish:function() {
				window.setTimeout('Effect.Fade(\''+newID+'\',{afterFinish:function() { Element.remove(\''+newID+'\'); }})',600);
			}});
		}});

	}

}








// search songs
function searchSongs() {

	deactive_orderbuttons();

	searchstring = document.getElementById('input_search').value

	var currentlyplaying;
	currentlyplaying = document.getElementById('currentlyplaying').getAttribute('rel');

	new Effect.Parallel([new Effect.BlindUp('list_tracklist'), new Effect.Fade('list_tracklist')], {afterFinish:function() {

		new Ajax.Request('./inc/ajax.mp3search.php',{ postBody:'searchstring='+escape(searchstring)+'&currentlyplaying='+currentlyplaying, method:'post',

			onFailure:function() {
				alert('ajax request failure')
			},

			onSuccess:function(t) {
				Element.update('list_tracklist',t.responseText);
				new Effect.Parallel([new Effect.BlindDown('list_tracklist'), new Effect.Appear('list_tracklist')]);
				document.getElementById('input_search').select();
			}

		});

	}});




	return false;

}





// reset search
function resetSearch() {

	document.getElementById('input_search').value = '';
	new Effect.Appear('b_resetsearch');

	searchSongs();
	return false;

}









// order songs
function orderSongs(how) {

	// get order mode
	orderMode = document.getElementById('b_'+how).getAttribute('rel');

	// deactivate styles of all order buttons and activate style for current button
	deactive_orderbuttons();
	document.getElementById('b_'+how).className = 'active';

	// reset order mode
	if(orderMode == 'asc') {
		document.getElementById('b_'+how).setAttribute('rel','desc');
	}
	else {
		document.getElementById('b_'+how).setAttribute('rel','asc');
	}


	var currentlyplaying;
	currentlyplaying = document.getElementById('currentlyplaying').getAttribute('rel');

	new Effect.Parallel([new Effect.BlindUp('list_tracklist'), new Effect.Fade('list_tracklist')], {afterFinish:function() {

		new Ajax.Request('./inc/ajax.mp3order.php',{ postBody:'orderType='+how+'&orderMode='+orderMode+'&currentlyplaying='+currentlyplaying, method:'post',

			onFailure:function() {
				alert('ajax request failure')
			},

			onSuccess:function(t) {
				Element.update('list_tracklist',t.responseText);
				new Effect.Parallel([new Effect.BlindDown('list_tracklist'), new Effect.Appear('list_tracklist')]);
			}

		});

	}});


	return false;

}








// deactivate styles of order buttons
function deactive_orderbuttons() {

	myButtons = new Array('alphabet','playtime','counter');

	for(i=0; i<myButtons.length; i++) {
		Element.removeClassName('b_'+myButtons[i], 'active');
		document.getElementById('b_'+myButtons[i]).setAttribute('rel','asc');
	}

	return false;

}

