document.observe('dom:loaded',function(){
	$$("#stripNav a").each(function(s) {
		Event.observe(s,"mousedown",function(){
			s.addClassName("background");
		});
		Event.observe(s,"mouseup",function(){
			s.removeClassName("background");
		});
	});

	// email validation on the signup form
	if ($('signup')) {
		Event.observe('signup','submit',function(e){
			if (isEmpty($('emailAddress'),'You haven\'t entered an email address.')) {
				Event.stop(e);
			} else if (!emailValidator($('emailAddress'),'That email address isn\'t valid')) {
				Event.stop(e);
			}
		});
	}

	// populate hidden fields if adding a new song type (in admin)
	if ($('addNewSongType')) {
		Event.observe('submitNewType', 'click', function(e){
			var newType = prompt("Enter the new song type");
			if (newType != null && newType != '') {
				$('addNewSongType').value = 1;
				$('newSongType').value = newType;
			} else {
				Event.stop(e);
			}
		});
	}

	// add and edit albums using AJAX
	if ($('editAlbums')) {
		var c = 0;
		// attach the onclick event to the edit album links
		$$('#editAlbums a[id^="editAlbum"]').each(function(s){
			var albumId = s.id.substring(10);
			Event.observe(s, 'click', function(e){
				var newName = prompt('Enter the new name for the album', $('album-' + albumId).innerHTML);
				if (newName != null && newName != '') {
					var url='/editAlbumName.php?id=' + albumId + '&newName=' + newName;
					new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(transport){
							if (transport.responseText == 'updated') {
								$('album-' + albumId).innerHTML = newName;
							} else {
								alert('Problem updating the album name');
							}
						},
						onFailure: function(transport){
							alert('failed update');
						}
					});
				}
				Event.stop(e);
			});
		});

		// set up the add new album
		if ($('addNewAlbum')) {
			Event.observe('addNewAlbum', 'click', function(e){
				Event.stop(e); // don't follow the link
				var newName = prompt('Enter the name of the new album');
				if (newName != null && newName != '') {
					var url = '/addAlbum.php?newName=' + encodeURIComponent(newName);
					new Ajax.Request(url, {
						method: 'get',
						onSuccess: function(transport){
							if (transport.responseText.substring(0, 5) == 'added') {
								var newAlbumP = $(document.createElement("p"));
								var newAlbumA = $(document.createElement("a"));
								newAlbumP.appendChild(document.createTextNode(newName + ' ('));
								newAlbumA.href = '';
								newAlbumA.id = 'editAlbum-' + transport.responseText.substring(6);
								newAlbumA.appendChild(document.createTextNode('Edit'));
								$('addNewAlbumP').insert({'before':newAlbumP});
								newAlbumP.insert({'bottom':newAlbumA});
								newAlbumP.appendChild(document.createTextNode(')'));
							} else {
								alert('Problem adding the new album - ' + transport.responseText);
							}
						},
						onFailure: function(transport){
							alert('failed insert');
						}
					});
				}
			});
		}
	}

	// modal dialog box for editing songs
	if ($('editSongs')) {
		$$('#editSongs a[id^="editSong"]').each(function(s){
			// store the database id of this song
			var songId = s.id.substring(9);
			// on click event for each of the 'Edit' links beside songs
			Event.observe(s, 'click', function(e){
				Event.stop(e);// don't follow the link
				// get the song information to fill in as defaults
				var url = '/getSongDetails.php?id=' + songId;
				newDialog.init;
				newDialog.sm('addSong', 400, 350, 'addSongForm');
				document.body.style.cursor = "wait";
				new Ajax.Request(url, {
					method: 'get',
					onSuccess: function(transport){
						var details = transport.responseText.split("|*|");
						$('title').value = details[1];
						$('mp3FileLocation').value = details[2];
						$('flacFileLocation').value = details[3];
						$('songType').value = details[4];
						$('album').value = details[5];
						$('update').value = 1;
						$('updateId').value = songId;
					},
					onFailure: function(transport){
					}
				});
				document.body.style.cursor = "default";
			});
		});
	}

	if ($('addNewSong')) {
		newDialog.init();
		Event.observe('addNewSong', 'click', function(e){
			Event.stop(e);
			newDialog.sm('addSong', 400, 350);
		});
	}

	// subnav tabs on front end songs list
	if ($('subNav')) {
		$$('#subNav li a').each(function(s){
			Event.observe(s, 'click', songTypeTabClick.bindAsEventListener(s));
		});
	}

	// hide all songs within albums until the album name is clicked
	if ($('allSongs')) {
		var i = 1;
		$$('#allSongs a.open-album-link').each(function(s){
			Event.observe(s, 'click', function(e){
				Event.stop(e);
				// get the dt element which is the parent of the clicked link
				var ancestors = s.ancestors();
				var parentDT = ancestors[0];

				// now show all of the dd elements
				parentDT.siblings().each(function(d){
					d.removeClassName('hide');
				});
			});
		});
		/*$$('#allSongs dd').each(function(s){
			s.hide();
		});*/
	}

	// display the login form
	if ($('loginForm')) {
		Event.observe('showLoginForm','click',function(e){
			Event.stop(e);
			$('loginForm').removeClassName('hide');
			if (!$('registrationForm').hasClassName('hide')) {
				$('registrationForm').addClassName('hide');
			}
		});
	}

	// display the registration form
	if ($('registrationForm')) {
		Event.observe('showRegisterForm','click',function(e){
			Event.stop(e);
			$('registrationForm').removeClassName('hide');
			if (!$('loginForm').hasClassName('hide')) {
				$('loginForm').addClassName('hide');
			}
		});
	}

	// show the change password form
	if ($('changePassword')) {
		Event.observe('editPasswordPopup','click',function(e){
			Event.stop(e);
			if ($('changePassword').hasClassName('hide')) {
				$('changePassword').removeClassName('hide');
				$('editPasswordPopup').addClassName('hide');
			}
		});
	}
});

function songTypeTabClick(e) {
	Event.stop(e);// stop the default action
	var lId = this.id.substring(4);
	var tabContent = 'tabContents-' + lId;
	// update the tab links
	$$('#subNav li').each(function(u){
		if (u.id == ('tabLi-' + lId)) {
			var liText = u.firstDescendant().innerHTML;
			if (!u.hasClassName('active')) {
				// give the clicked tab the 'active' class
				u.addClassName('active');
			}
			$('tab-' + lId).remove();
			u.appendChild(document.createTextNode(liText));
		} else {
			if (u.hasClassName('active')) {
				var liText = u.innerHTML;
				var thisId = u.id.substring(6);
				// remove the 'active' class from the old tab
				u.removeClassName('active');
				u.innerHTML = '';
				// change the text to a link
				var newLink = document.createElement('a');
				newLink.id = 'tab-' + thisId;
				newLink.href = '/index.php/songs/listAll/' + thisId + '/';
				newLink.appendChild(document.createTextNode(liText));
				u.insert({'top':newLink});
				Event.observe(newLink, 'click', songTypeTabClick.bindAsEventListener(newLink));
			}
		}
	});
	$$('#allSongs dl[id^="tabContents"]').each(function(t){
		if (t.id == tabContent) {
			if (t.hasClassName('hide')) {
				t.removeClassName('hide');
			}
		} else {
			if (!t.hasClassName('hide')) {
				t.addClassName('hide');
			}
		}
	});
}

