	jQuery(document).ready(function() {
		playAudioFlash = function ( which, value, e ) {
						// change href so page does not change
			if ( which == 'mp3' ) {
				e.href = '#';
			}
			var mode = 'html5';
			if ( EP_get('ep_player1') != undefined ) {
				mode = 'flash';
			}
			if ( gvLinkType == 'mobile' ) {
				showHidePlayer();
			}

			jQuery('#playlist').load( gvSiteDir + 'foggyplayer.php?' + which + '=' + value + '&mode=' + mode, function() {
				var daplaylist = jQuery('#playlist').html();

				if ( mode == 'flash' ) {
					daplaylist = str_replace( '[', '<', daplaylist );
					daplaylist = str_replace( ']', '>', daplaylist );
					daplaylist = str_replace( '%26', '&', daplaylist );
					EP_clearPlaylist('ep_player1');
					EP_addTracks('ep_player1', daplaylist, 2); 
					EP_play('ep_player1');
				}
				else {
					jQuery('#ep_container1').hide();
					jQuery('#playlist').html( daplaylist );
				}
			});
			jQuery('#fogbody').keydown(function( e ) {
				if (  e.keyCode == 32 ) {
					EP_playPause('ep_player1');
				}
			});
		}
	});

	function ajaxSub( which ) {
		var stringToPass = '';
		var div = 'index';
		switch ( which ) {
			case 'song_index':
				stringToPass = 'mode=' + gvLinkType + '&cat=songs';
				break;
			case 'track_index':
				stringToPass = 'mode=' + gvLinkType + '&cat=mp3s';
				break;
			case 'artist_index':
				stringToPass = 'mode=' + gvLinkType + '&cat=artists';
				break;
			case 'people_index':
				stringToPass = 'mode=' + gvLinkType + '&cat=people';
				break;
			case 'album_index':
				stringToPass = 'mode=' + gvLinkType + '&cat=albums';
				break;
			case 'cd_index':
				stringToPass = 'mode=' + gvLinkType + '&cat=cds';
				break;
			case 'chamber_index':
				stringToPass = 'mode=' + gvLinkType + '&cat=chamber';
				break;
		}
		ajaxLoad( 'cats', div, stringToPass );
	}

	function ajaxPage( cat, id ) {
		var page = '';
		var stringToPass = '';
		var div = 'details';
		switch ( cat ) {
			case 'person':
			case 'song':
			case 'artist':
			case 'album':
			case 'cd':
			case 'chamber':
				page = 'details';
				stringToPass = 'mode=' + gvLinkType + '&cat=' + cat + '&id=' + id;
				break;
		}
		ajaxLoad( page, div, stringToPass );
	}

	function ajaxLoad( page, div, stringToPass ) {
		if ( page != '' ) {
			jQuery('#loading_' + page ).show();
						// fade out page, then...
			jQuery('#' + div + '_body' ).fadeOut('fast', function() {
							// load page, then...
				jQuery('#main_' + div + '_contents' ).load( gvSiteDir + page + '.php?' + stringToPass, function(response, status, xhr) {
					if ( status == "error") {
						var msg = "Sorry but there was an error: " + xhr.status + " " + xhr.statusText;
						if ( xhr.status == "404" ) {
							msg += " (" + page + ")";
						}
//						jQuery("#error").html(msg + xhr.status + " " + xhr.statusText);
						alert ( msg );
					}
								// fade in page, then..
					jQuery('#' + div + '_body' ).fadeIn('fast', function() {
						jQuery('#loading_' + page ).hide();
					});
				});
			});
		}
	}

	function loginButton() {
		if ( getCookie( 'username' ) == undefined ) {
			logIn( 'loginForm' );
		}
		else {
			setTimeout(function() {
				setCookie( 'user_id', '', -1 );
				setCookie( 'username', '', -1 );
				jQuery('#log_button').html( 'Log In' );
			}, 100 );
		}
	}

	function logIn( which ) {
//alert ( which );
		var emailAddress = jQuery('#login_email input')[0].value;
		var username = jQuery('#login_username input')[0].value;
		var emailname = jQuery('#login_emailname input')[0].value;
		var password = jQuery('#login_password input')[0].value;
		var confirmPassword = jQuery('#login_confirm_password input')[0].value;
		var recaptcha_response_field = '';
		var recaptcha_challenge_field = '';
		var bonus = jQuery('#loginBonus')[0].value;
//alert ( which + ' = \n' + emailAddress + '*\n' + username + '*\n' + emailname + '*\n' + password + '*\n' + confirmPassword + '*' );
		var sendToServer = false;
		var instructions = '';
		var header = '';
		var focusField = '';

//		if ( which == 'loginForm' && jQuery('#loginFormOverlay').css('display') != 'none' ) {
	//		which = 'hideForm';
//		}

		if ( which == 'loginForm' || which == 'forgotForm' || which == 'joinForm' || which == 'resetPwdForm' ) {
			jQuery('#loginForm')[0].reset();
			jQuery('#loginFormEmail')[0].disabled = false;
						// show the appropriate header, input fields and button text/commands
			jQuery('.loginFormTR').hide();
			jQuery('.login_other').hide();
			jQuery('#recaptchaDiv').hide();
			jQuery('.loginTabs li').removeClass('selected');

			if ( which == 'loginForm' ) {
				header = 'Please enter your username (or the email address used when joining foggyrealm.com) and your password.';
				jQuery('#login_emailname').show();
				jQuery('#login_password').show();
				jQuery('#loginFormSubmit').html( '<span class=\"button\" onclick=\"logIn( \'login\' );\">Submit</span>' );
				focusField = 'loginFormEmailName';
			}
			else if ( which == 'forgotForm' ) {
				header = 'Please enter the email address used when joining foggyrealm.com and a temporary password will be emailed to you.';
				jQuery('#login_email').show();
				jQuery('#loginFormSubmit').html( '<span class=\"button\" onclick=\"logIn( \'forgot\' );\">Email temporary Password</span>' );
				focusField = 'loginFormEmail';
			}
			else if ( which == 'resetPwdForm' ) {
				header = 'Please enter a new password.';
				jQuery('#loginFormEmailName')[0].value = emailname;
				jQuery('#loginFormEmailName')[0].disabled = true;
				jQuery('#login_emailname').show();
				jQuery('#login_password').show();
				jQuery('#login_confirm_password').show();
				jQuery('#loginFormSubmit').html( '<span class=\"button\" onclick=\"logIn( \'newPwd\' );\">Save Password</span>' );
				jQuery('#loginBonus')[0].value= password;
				focusField = 'loginFormPassword';
			}
			else if ( which == 'joinForm' ) {
					header = 'Please enter a password (and optional username). A confirmation email will be sent to the email address that you provide, which will allow you to complete registration.';
					jQuery('#login_email').show();
					jQuery('#login_username').show();
					jQuery('#login_password').show();
					jQuery('#login_confirm_password').show();
					jQuery('#recaptchaDiv').show();
					jQuery('#loginFormSubmit').html( '<span class=\"button\" onclick=\"logIn( \'join\' );\">Join</span>' );
					focusField = 'loginFormEmail';
//				jQuery('#recaptchaTD').load( gvSiteDir + 'helpers/ajax_login.php?type=recaptcha', function() {
//alert ( jQuery('#recaptchaTD').html() );
//				});
					showRecaptcha();
			}

			jQuery('#loginHeader').html( header );
			jQuery('.' + which ).addClass('selected');
		}
					// show/hide the login div; if hiding, also reset the form; change button text, depending on context
		if ( which == 'hideForm' ) {
			jQuery('#loginFormDiv').fadeOut('slow',function() {});
			jQuery('#loginFormOverlay').fadeOut();
			jQuery('#loginForm')[0].reset();
		}
		else {
			jQuery('#loginFormOverlay').fadeIn();
			jQuery('#loginFormDiv').fadeIn('slow');
			if ( focusField != '' ) {
				jQuery('#'+ focusField).focus();
			}
		}

		if ( which == 'login' ) {
			if ( emailname == '' ) {
				instructions = 'Username (or email address) is required';
				jQuery('#loginFormEmailName').focus();
			}
			else if ( password == '' ) {
				instructions = 'Password is required';
				jQuery('#loginFormPassword').focus();
			}
			else if ( substr_count ( password, ' ' ) > 0  ) {
				instructions = 'Password cannot have space';
				jQuery('#loginFormPassword').focus();
			}
			else {
				sendToServer = true;
			}
		}
		else if ( which == 'join' || which == 'newPwd' ) {
			if ( substr_count ( emailAddress, '@' ) != 1 && which != 'newPwd' ) {
				instructions = 'Please enter email address';
				jQuery('#loginFormEmail').focus();
			}
			else if ( password == '' ) {
				instructions = 'Password is required';
				jQuery('#loginFormPassword').focus();
			}
			else if ( confirmPassword == '' ) {
				instructions = 'Please confirm the password';
				jQuery('#loginFormConfirmPassword').focus();
			}
			else if ( password != confirmPassword ) {
				instructions = 'Passwords do not match';
				jQuery('#loginFormPassword').focus();
			}
			else {
				sendToServer = true;
			}
		}
		else if ( which == 'forgot' ) {
			if ( substr_count ( emailAddress, '@' ) != 1 ) {
				instructions = 'Please enter email address';
				jQuery('#loginFormEmail').focus();
			}
			else {
				sendToServer = true;
			}
		}

		jQuery('#loginMsg').html( instructions );
		if ( sendToServer == true ) {
			if ( which == 'join' ) {
				recaptcha_response_field = jQuery('#recaptcha_response_field')[0].value;
				recaptcha_challenge_field = jQuery('#recaptcha_challenge_field')[0].value;
			}
			jQuery("#loginStatus").html( '<img src="' + gvImagesDir + 'ajax-loader.gif" />' );
			setTimeout(function() {
				jQuery.post( gvSiteDir + "helpers/ajax_login.php", { 
					type: which,
					email: emailAddress,
					username: username,
					emailname: emailname,
					password: password,
					recaptcha_challenge_field: recaptcha_challenge_field,
					recaptcha_response_field: recaptcha_response_field,
					bonus: bonus,
				}, function( response ) {
					response = jQuery.trim( response );
					jQuery("#loginStatus").html( '' );
					if ( response == 'newpwd' ) {
						logIn( 'resetPwdForm' );
					}
					else if ( response == 'pwdReset' ) {
						logIn('hideForm');
					}
					else if ( substr_count ( response, 'successID' ) > 0 ) {
						var userID = str_replace ( 'successID', '', response );

						jQuery.post( gvSiteDir + "helpers/ajax_login.php", { 
							type: 'getName',
							emailname: emailname,
							password: password,
						}, function( response ) {
							setCookie( 'user_id', userID, -1 );
							setCookie( 'username', '', -1 );
							setCookie( 'user_id', userID, 365 );
							setCookie( 'username', response, 365 );
							jQuery('#log_button').html( 'Log Out ' + response );
							jQuery('#loginMsg').html( 'Login successful' );
							setTimeout( function() {
								logIn('hideForm');
							}, 1000 );
						});
					}
					else if ( response == 'success' && which == 'forgot' ) {
						jQuery('#loginMsg').html( 'Password reset request sent. Please check your email for instructions.' );
							setTimeout( function() {
								logIn('loginForm');
							}, 10000 );
					}
					else if ( response != '' ) {
						jQuery('#loginMsg').html( response );
					}
				});
			}, 200);
		}
	}


	function setDimensions() {
		var mainHeight = window.innerHeight 
		if ( jQuery('#loginFormDiv').css('display') != 'none' ) {
//			mainHeight -= jQuery('#loginFormDiv').height();
		}
		var mainWidth = window.innerWidth;

		jQuery('#main_content').height( mainHeight );
		jQuery('#navbar').height( mainHeight );
		jQuery('#main_content').width( mainWidth - 270 );
		jQuery('.mainColContent').height( mainHeight - 70 );
		jQuery('.scroll-pane .viewport ').css( 'height', ( mainHeight - 72 ) + 'px' );

		jQuery('div.jp-type-playlist div.jp-playlist').css( 'maxHeight', ( mainHeight - 340 ) + 'px' );

		var width = ( jQuery('#main_content').width() - 10 ) / 2;
		jQuery('#main_index').width( width );
		jQuery('#main_details').width( width );

	}

	jQuery(window).resize(function(e ) {
		setDimensions();
	});

	jQuery(document).ready(function() {
		setDimensions();
		jQuery('#fogbody').keydown( function(e) {
			if ( e.keyCode == 13 && jQuery('#loginFormOverlay').css('display') != 'none' ) {
				if ( jQuery('.joinForm').hasClass('selected') == true ) {
					logIn( 'join' );
				}
				else if ( jQuery('.loginForm').hasClass('selected') == true ) {
					logIn( 'login' );
				}
				else if ( jQuery('.forgotForm').hasClass('selected') == true ) {
					logIn( 'forgot' );
				}
			}
// spacebar = 32
		});

					// check if username cookie exists; if so, change login button text
		var username = getCookie( 'username' );
		if ( username != undefined ) {
			jQuery('#log_button').html( 'Log Out ' + username );
		}
	});

	function showAllMemos() {
		if ( jQuery('#all_memos').css('display') == 'none' ) {
			jQuery('#all_memos').show();
			jQuery('#showHideMemos').html ( 'HIDE MEMOS' );
		}
		else {
			jQuery('#all_memos').hide();
			jQuery('#showHideMemos').html ( 'SHOW MEMOS' );
		}
	}

	function setCookie( c_name, value, exdays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
	}

	function getCookie( c_name) {
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++) {
			x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
			if (x==c_name) {
				return unescape(y);
			}
		}
	}

	function updateUser( userID ) {
		if ( userID == '' ) {
			setTimeout( function() {
				setCookie( 'user_id', userID, -1 );
				setCookie( 'username', userID, -1 );
				jQuery('#nav_login').html( '<span id="log_button" class="simpleLink" onclick="logIn( \'loginForm\' );">Log In</span>' );
			}, 100 );
		}
		else if ( userID == 'cookie' ) {
			var username = getCookie( 'username' );
			jQuery('#nav_login').html( username + '<span id="log_button" class="simpleLink" onclick="updateUser( \'\' );">Log Out</span>' );
		}
		else {
			setCookie( 'user_id', userID, 365 );
			jQuery('#nav_login').html( '<span id="log_button" class="simpleLink" onclick="updateUser( \'\' );">Log Out</span>' );
		}
	}

	function showRecaptcha() {
		Recaptcha.create("6LeNX8MSAAAAAHHFcwFj3XpwDB7bjcE9zhPChPPI", 'recaptchaTD', {
			theme: "red",
			callback: Recaptcha.focus_response_field
		});
	}

