/* The Current Rich Playlist jQuery functions.  Developed by Ari Koinuma, Sept-Nov, 2008. 
Dependency: jQuery 1.2.6, facebox 1.2, dimscreen_modded 1.0.1
Also, BE SURE to load this after loading CSS, as it rewrites some of the CSS by printing some styles in the head section.

This file deals with all the custom behaviors for the Current rich play list.  

Table of content:
1. Show/hide/Highlight utilities
2. Form-related scripts
3. star rating system
4. Ajax window system
5. Deployment

I'm going go use the abbreaviation "aw" at the beginning of function names. 
*/


/*Hide, show and highlight scripts _____________________________________________________________________ */


//function to toggle each songUtilities div
function toggleEachUtilities(ele,direction,page) {
	//ele is a jQuery object
	
	//setting up what the containing element is, depending on the page
	if (page == "playlist") {
		var container = ele.parent('.songBlock');
	} else {
		var buttonParentTR = ele.parents('.songTRMain');
		var container = buttonParentTR.next('.songTRSub');
	}

	//executing the toggle
	if (direction == "down") {
		if (page == "playlist") {
			//on the playlist page, if the stars are not deployed yet (meaning, .rateDiv is not replaced by .starDiv, then deploy the stars.
			if (container.find('.rateDiv').attr('title') != '') {
				var rateDiv = container.find('.rateDiv');
				var index = rateDiv.attr('title');
				constructStars(rateDiv, index);						
			}
		} else {
		//It's not the playlist page
			buttonParentTR.find('.sortColumn').removeClass('sortColumn').addClass('exSortColumn');
			container.find('td.songTDUtilities').removeClass('collapsed');
			
			//if it's IE, then remove the bottomLine class I inserted
			if ($.browser.msie == true) {
				buttonParentTR.removeClass('bottomLine');
			}
		}
		
		//for all pages
		container.find('.songUtilities').slideDown('fast');
		ele.removeClass('expandButton').addClass('collapseButton').attr('src','images/icon_minus.gif').attr('alt','collapse');

	} else {
		//sliding up - collapse the songUtilities
		container.find('.songUtilities').slideUp('fast');
		ele.removeClass('collapseButton').addClass('expandButton').attr('src','images/icon_plus.gif').attr('alt','expand');
		if (page != "playlist") {
			buttonParentTR.find('.exSortColumn').removeClass('exSortColumn').addClass('sortColumn');
			container.find('td.songTDUtilities').addClass('collapsed');
			if ($.browser.msie == true) {
				function addLine() {
					buttonParentTR.addClass("bottomLine");
				}
				setTimeout(addLine, 200);
			}
		}
	}
}


//function to toggle all songUtilities in that hour.  It assumes that the toggleHour's ID is the same as the class of all songBlock divs within. 
function toggleUtilitiesByHour(ele,direction) {
	//ele is a jQuery object
	var targetClass = '.'+ele.attr('id');
	if (direction == "down") {
		ele.removeClass('expandHour').addClass('collapseHour').html('Collapse this hour <img src="images/icon_minus.gif" alt="" />');
			$(targetClass+' img.toggleButton').each(function(){
					toggleEachUtilities($(this), 'down', 'playlist');
			});
	} else {
		ele.removeClass('collapseHour').addClass('expandHour').html('Expand this hour <img src="images/icon_plus.gif" alt="" />');
		$(targetClass+' img.toggleButton').each(function(){
				toggleEachUtilities($(this), 'up', 'playlist');
		});
	}
}




//the function to hide and show the song rating and links at the page load.  Call this from individual pages, not the head tags include.  
function toggleSongUtilities(page) {
	//hacky way to hide the songUtilities div 
	document.write('<style type="text/css">.songUtilities {display: none;}</style>');
	
	$(document).ready(function(){
		//first, hide the songUtilities div.  This is the correct way to do it, but since it's slower and uglier, I'm using the hacky way above.
		//$('.songUtilities').hide();
	
		//if it's the playlist page
		if (page == 'playlist') {
			
			//hide the toggles that are hidden by default
			$('.toggleHour').css('visibility','visible');
			
			//insert the plus/minus images in each songBlock div
			$('div.songBlock').append('<img src="images/icon_plus.gif" class="toggleButton expandButton" alt="Expand" />');
			
			//then make those images clickable
			$('img.toggleButton').click(function(){
					if ($(this).hasClass('expandButton')) {
						toggleEachUtilities($(this), 'down', page);
					} else {
						toggleEachUtilities($(this), 'up', page);
					} 
			});
			
			//apply the behavior to the toggleHour
			$('.toggleHour').click(function(){
				if ($(this).hasClass('expandHour')) {
					toggleUtilitiesByHour($(this),'down');
				} else {
					toggleUtilitiesByHour($(this),'up');
				}
			});
			
			//at the page load, go ahead and "click" on the first toggleHour
			var firstHourToggle = $('.toggleHour').get(0);
			$(firstHourToggle).click();
			
			//apply behavior to the toggleDay.  Saved in case we want to implement this functionality.
			/* $('.toggleDay').click(function(){
					if ($(this).hasClass('expandDay')) {
						$(this).removeClass('expandDay').addClass('collapseDay').html('Collapse this day <img src="images/icon_minus.gif" alt="" />');
						$('.toggleHour').each(function(){
								toggleUtilitiesByHour($(this),'down');
						});
					} else {
						$(this).removeClass('collapseDay').addClass('expandDay').html('Expand this day <img src="images/icon_plus.gif" alt="" />');
						$('.toggleHour').each(function(){
								toggleUtilitiesByHour($(this),'up');
						});
					}
		
			});  */
			
		} else { //it's not the playlist page
			
			//show the hidden toggle.  Saved in case we want to implement this functionality.
			$('.toggleAll').css('visibility','visible');
			
			$('td.toggleColumn').append('<img src="images/icon_plus.gif" class="toggleButton expandButton" alt="Expand" />');
			$('tr.songTRSub td').addClass('collapsed');
			
			//IE work-around for a CSS display anomaly.  
			if ($.browser.msie == true) {
				$('tr.songTRMain').addClass('bottomLine');
			}
		
			//then make those images clickable
			$('img.toggleButton').click(function(){
					if ($(this).hasClass('expandButton')) {
						toggleEachUtilities($(this), 'down', page);
					} else {
						toggleEachUtilities($(this), 'up', page);
					} 
			});
			
			//apply behavior to the toggleAll Saved in case we want to implement this functionality.
			$('.toggleAll').click(function(){
					if ($(this).hasClass('expandAll')) {
						$(this).removeClass('expandAll').addClass('collapseAll').html('Collapse all <img src="images/icon_minus.gif" alt="" />');
						$('.toggleButton').each(function(){
								toggleEachUtilities($(this),'down', page);
						});
					} else {
						$(this).removeClass('collapseAll').addClass('expandAll').html('Expand all <img src="images/icon_plus.gif" alt="" />');
						$('.toggleButton').each(function(){
								toggleEachUtilities($(this),'up', page);
						});
					}	
			});
		}
	});
}

//The row highlighting 
function highlightRow(page) {

	$(document).ready(function(){
			switch(page) {
				case 'catalog':
				case 'ratings':
				case 'requests':
				$('tr.songTRMain, tr.songTRSub').hover(function(){
						var songTitle = $(this).attr('rel');
						var targetTRs = $('tr[rel='+songTitle+']');
						targetTRs.addClass('highlight'); 
					}, function(){
						var songTitle = $(this).attr('rel');
						var targetTRs = $('tr[rel='+songTitle+']');
						targetTRs.removeClass('highlight');
					}
				); 
				break;
				
			case 'playlist':
				$('div.songBlock').hover(function(){
						$(this).addClass('highlight');
				}, function(){
						$(this).removeClass('highlight');
				});
				break;
				
			case 'profile':
				$('table.songBlock tr').hover(function(){
						$(this).addClass('highlight');
				}, function(){
						$(this).removeClass('highlight');
				});
				break;
			}
	});
}



/* Form-Related Scripts _______________________________________________________ */


function displayError(text) {
	$('.errorMsg').html(text).show('fast');
}


//disable the submit Button on submit.  But don't forget to enable it back!
function disableSubmit() {
	$('#searchProfileBar .submitButton, #facebox .submitButton').attr('disabled','disabled').addClass('disabledSubmit');
	$('.faceboxLoginPassword').remove();
	$('#facebox .close').hide();
	return true;
}

//enable it back
function enableSubmit() {
	$('#searchProfileBar .submitButton, #facebox .submitButton').removeAttr('disabled').removeClass('disabledSubmit');
	$('#facebox .close').show();
}

/*The field-related scripts*/


//search
function theCurrentFields(identifier, content) {
	var field = $(identifier);
	if (field.val() == '') {
		field.val(content);
	}
	field.focus(function(){
			if (field.val() == content) {
				field.val('');
				field.select();
			}
	}).blur(function(){
		if (field.val() == '') {
			field.val(content);
		}
	});
}

//Insert default text into textareas.  It adds a class so that the instruction text is gray but the comment user types in is black.
function theCurrentTextarea(identifier, text) {
	var field = $(identifier);
	
	if (field.text() == '') {
		field.text(text).addClass('commentInstruction');
	}
	field.focus(function(){
			if (field.text() == text) {
				field.text('').select().removeClass('commentInstruction');
			}
	}).blur(function(){
		if (field.text() == '') {
			field.text(text).addClass('commentInstruction');
		}
	});
}
	

//function to replace password fields in register and other forms except the login form.
function replacePasswordFieldsById(id, value) {
	var fieldClasses = $("input#"+id).attr('class');
	$("input#"+id).after('<input type="text" value="'+value+'"  id="'+id+'Replaced" class="'+fieldClasses+'"/>').hide().blur(function(){
		if ($(this).val() == '') {
			$(this).hide();
			$('input#'+id+'Replaced').show();
		}
	});;
	$("input#"+id+"Replaced").focus(function(){
			$(this).hide()
			$('input#'+id).show().focus();
	});
}


//used to replace password fields in the login form.  This, I can't assign ID because it'll appear twice within a page, once in header and once in facebox. 
function replacePasswordFieldsByClass(selector,className) {
	//show the replacement field, and bind a behavior
	var passwordFields = '#searchProfileBar input.'+className+', #facebox input.'+className;
	
	$(passwordFields).show().focus(function(){
			$(this).hide()
			$(selector).show().focus();
	});;
	
	//hide the real password field, and bind a behavior
	$(selector).hide().blur(function(){
		if ($(this).val() == '') {
			$(this).hide();
			$(passwordFields).show();
		}
	});

}

function processSearch(){
	$('form#plSearch').submit(function(){
			if ($('form#plSearch input#formKeywords').val() == 'search catalog') {
				$('form#plSearch input#formKeywords').removeAttr('value');
			}
			return true;
	});
}
                                                

//master executor
function theCurrentTextFields() {
	theCurrentFields('#formKeywords', 'search catalog');
	theCurrentFields('#searchProfileBar input[name=user_email], #facebox input[name=user_email]', 'e-mail address');
	replacePasswordFieldsByClass('#searchProfileBar input[name=user_password]','passwordReplacementField');
	processSearch();
};


/*Star rating system ______________________________________________ */


//Global variables (constants)

//An array to store all the song rating information at the page load.  This gets modified as user rates songs on that page.
var allSongs = new Array();	

//A variable used to disable all the stars' behaviors when a rating is submitted and user is waiting for the response from the server.  
var starDisabled = false; 

	
//Minor utility functions

//a trick to hide the default form when JS is on. 
document.write('<style type="text/css">.htmlRater { display: none; } .songDetail .songRater { display: none; } table.songBlock td.ratingTD {width: 90px;} #searchProfileBar #plSearchLink { display: block; } td.sortColumn {background-color: #eef8fd;}</style>');

// if it's IE (6) hide the select fields first and then display it when the page is finished loading
if ($.browser.msie == true) {
	document.write('<style type="text/css">select { visibility: hidden }</style>');
}

$(document).ready(function(){
		$('select').css('visibility','visible');
});


function round(num) {
	var rounded = Math.round(num);
	return rounded;
}


//apply appropriate rating class to a div
function applyRating(div, rating) {
	if (rating == 0) {
		for (var i = 1; i<=5; i++ ){
			var rateClass = 'rated'+i;
			$(div).removeClass(rateClass);
		}
	} else {
		//figure out the rating
		var rateClass = 'rated'+rating;
		//I'm assuming that targetDiv is a jQuery object
		$(div).addClass(rateClass);
	}
}


//Star behaviors
//this is what happens at starDivs onmouseover event.  Note that it gets applied to each individual star div.
function mouseEnterStar(div) {
	if (starDisabled == false) {
		var parentDiv = $(div).parent();
		var rating = $(div).attr('title'); //this finds which rating should be applied
		var index = parentDiv.attr('title');
		var thisSong = allSongs[index];
		var targetContainerDiv = $('.'+thisSong.id+'Div');
		targetContainerDiv.removeClass().addClass(thisSong.id+'Div').addClass('starDiv').addClass('userRateStars');
		applyRating(targetContainerDiv, rating); 
	}
}

//What happens on mouseleave
function mouseLeaveStars(div) {
	if (starDisabled == false) {
		var index = $(div).attr('title');//index for figuring out where in the allSongs array is this song
		var thisSong = allSongs[index]; //find this song in the array
		function mouseLeave() {
			$(div).removeClass().addClass(thisSong.id+'Div').addClass('starDiv')
			if (thisSong.userRateStatus == true) {
				$(div).addClass('userRateStars');
			}	
			applyRating(div, thisSong.rateToUse);
		}
		setTimeout(mouseLeave,150);
	}
}

//What happens on mouseleave when it's the user stars on detail page
function mouseLeaveUserStars(div) {
	if (starDisabled == false) {
		var index = $(div).attr('title');//index for figuring out where in the allSongs array is this song
		var thisSong = allSongs[index]; //find this song in the array
		function mouseLeave() {
			$(div).removeClass().addClass(thisSong.id+'Div').addClass('starDiv')
			if (thisSong.userRateStatus == true) {
				$(div).addClass('userRateStars');
			}	
			
			applyRating(div, thisSong.userRate);
		}
		setTimeout(mouseLeave,150);
	}
}


//What happens when a start is cliked. 
function mouseClickStar(div) {
	//Collect the information
	var parentDiv = $(div).parent();
	var rating = $(div).attr('title');
	var index = parentDiv.attr('title');
	var thisSong = allSongs[index];
	var thisSongID = thisSong.id;
	var songIDString = 'usra_song_id='+thisSongID;
	var ratingString = '&usra_rating='+rating;
	
	//function to reset the stars to where they were in case of errors
	function resetStar(parentDiv, thisSong) {
		//parentDiv.find('.saveMsg').remove();
		parentDiv.find('div').show();
		if (thisSong.userRateStatus != true) {
			parentDiv.removeClass('userRateStars');
			} 
		applyRating(parentDiv, thisSong.rateToUse);
		starDisabled = false;
	}
	
	//what happens when the user is logged on.
	var trueFunc = function() {
		//the actual ajax post function.
		$.post('/radio/services/the_current/playlist/song_rating.php',
			songIDString+ratingString+'&format=json',
			function(data,status) {
				if (status == "success") { //connection to server was successfully established.
					if (data.status == "OK"){//successfully rated
						parentDiv.find('div').show();
						applyRating(parentDiv,data.request.usra_rating);
						allSongs[index].userRateStatus = true;
						allSongs[index].userRate = data.request.usra_rating;
						allSongs[index].rateToUse = allSongs[index].userRate;
						starDisabled = false;
					} else { // connected, but the rating transaction failed.  Returning everything to pre-click status.
						var title="Error";
						var message=data.errors.general;
						messageAW(title, message, 'error');
						resetStar(parentDiv, thisSong);
					}
				} else {//There was error connecting to the server
					var title="Error";
					var message="We are sorry, but there was a problem connecting to the server.  Please refresh the page and try again.";
					messageAW(title, message, 'error');
					resetStar(parentDiv, thisSong);
				}
			},
			'json');
		
	} 
	
	//what happens if the user is not logged in.
	var falseFunc = function() {
		resetStar(parentDiv, thisSong);
		ajaxWindow('loginRating', thisSongID, rating);
	}
	
	if (starDisabled == false) {  //stars are not disabled -- meaning, there are no other ajax star ratiing process in progress.
			starDisabled = true; //disable the stars while the function is working.
			
			//hiding the internal start divs so that they are no longer clickable.
			parentDiv.find('div').hide();

			//check to see if the user is logged on.
			checkLogin(trueFunc, falseFunc);
	}
}


/*Star Rating object */
function StarRatings(id, userRate, avgRate) {
	
	//given properties
	this.id = id;
	this.userRate = userRate;
	this.avgRate = round(avgRate);//average rate can return a decimal number
	//this.rateTotal = rateTotal;
	this.clickStatus = false;
	
	//a status that sets to see if the star rating system for that particular song is deployed or not.  By default it's true, but when it's on the playlist page, then it gets set to false.
	this.starsDeployed = true;
	
	//calculated properties
	if (this.userRate == 0 && this.avgRate > 0) {
		this.userRateStatus = false;
		this.rateToUse = this.avgRate;
	} else {
		this.userRateStatus = true;
		this.rateToUse = this.userRate;
	}
	
	this.containerClass = '.'+this.id+'Div';
	this.starDivClasses = new Array();
	for (var i=1; i<=5; i++) {
		this.starDivClasses.push('.'+this.id+'Star'+i);
	}
	
}



//method to build the stars.  It basically constructs a parent div with five small divs inside, and assign appropriate classes. 
StarRatings.prototype.buildStars = function(index) {
	var starDiv = $('<div class="'+this.id+'Div starDiv" title="'+index+'"></div>');
	
	//figure out which star to use
	if (this.userRateStatus == true) {
		starDiv.addClass('userRateStars');
	} 
	
	//create the stars	
	var starImgs = new Array();
	for (var h=1; h<=5; h++) {
		var starImg = $('<div class="'+this.id+'Star'+h+'" title="'+h+'"></div>');
		starDiv.append(starImg);
	}
	
	if (this.rateToUse > 0) {
		$(starDiv).addClass('rated'+this.rateToUse);
	}
	
	return starDiv;
	
}

StarRatings.prototype.buildUserStars = function(index) {
	var starDiv = $('<div class="'+this.id+'Div starDiv" title="'+index+'"></div>').addClass('userRateStars');;
	
	//create the stars	
	var starImgs = new Array();
	for (var h=1; h<=5; h++) {
		
		var starImg = $('<div class="'+this.id+'Star'+h+'" title="'+h+'"></div>');
		starDiv.append(starImg);
	}
	
	applyRating(starDiv, this.userRate);
	return starDiv;
	
}

StarRatings.prototype.buildAvgStars = function(index) {
	var avgStarDiv = $('<div class="starDiv" id="'+this.id+'_avgStars" title="'+index+'"></div>');
	applyRating(avgStarDiv, this.avgRate);
	return avgStarDiv;
}

//function to gather data and deploy stars.  Figure out the div that contains the rating info and then run it through this.  Index is used to store the rating information in the allSongs array, so you can extract and manipulate the information when the stars are clicked
function constructStars(rateDiv, index) {
	//extract information
	var userRate, avgRate;
	var id = rateDiv.attr('rel');
	
	//setting up the userRate
	 if (typeof (rateDiv.find('.userRate')).attr('title') == 'undefined') {
		 userRate = 0;
	} else {
		userRate = rateDiv.find('.userRate').attr('title');
	}
	
	//setting up the avgRate
	if (typeof rateDiv.find('.avgRate').attr('title') == 'undefined') {
		avgRate = 0;
	} else {
		avgRate = rateDiv.find('.avgRate').attr('title');
	}
	

	//build the StarRating object
	var thisSongRating = new StarRatings(id, userRate, avgRate);

	//add it to the master array of songs
	allSongs[index] = thisSongRating;
	
	//build the stars and replace the HTML form
	var starDiv = allSongs[index].buildStars(index);
	rateDiv.replaceWith(starDiv);
	
	//apply the behaviors
	starDiv.bind("mouseleave", function(){
			mouseLeaveStars(this);
	});
	starDiv.find('div').bind("mouseenter", function(){
		mouseEnterStar(this);	
	}).click(function(){
		mouseClickStar(this);
	}); 
	
	return allSongs;
	
}
	
//Deploy functions
function deployStarRatingSystem(page) {
	$(document).ready(function() {
			
			switch(page) {
			
			case 'songDetail':
				$('.rateInfo').each(function(i) {
						var userRate, avgRate;
						
						//extract information
						var id = $(this).attr('rel');
						
						//setting up the userRate
						 if (typeof ($('.userRate')).attr('title') == 'undefined') {
							 userRate = 0;
							
						} else {
							userRate = $('.userRate').attr('title');
						}
						
						//setting up the avgRate
						if (typeof $('.avgRate').attr('title') == 'undefined') {
							avgRate = 0;
						} else {
							avgRate = $('.avgRate').attr('title');
						}
						
						//build the StarRating object
						var thisSongRating = new StarRatings(id, userRate, avgRate);
						
						//deploy the stars
						var userStarDiv = thisSongRating.buildUserStars(i);
						$(this).find('.userRate').replaceWith(userStarDiv);
						var avgStarDiv = thisSongRating.buildAvgStars(i);
						$(this).find('.avgRate').replaceWith(avgStarDiv);
						
						//apply the behaviors
						userStarDiv.bind("mouseleave", function(){
								mouseLeaveUserStars(this);
						});
						
						userStarDiv.find('div').bind("mouseenter", function(){
							mouseEnterStar(this);	
						}).click(function(){
							mouseClickStar(this);
						});
						
						//store the information in one master array for later manipulation
						allSongs.push(thisSongRating);
				});
							
				return allSongs;
				break;
				
			case 'playlist':
				$('.rateDiv').each( function(i) {
						$(this).attr('title',i);
				});
				break;
				
			default:
				$('.rateDiv').each( function(i) {
						constructStars($(this), i);
				});
			}	
	});
}


/*Ajax Window System ___________________________________________________ */

//utility functions


//open Ajax window
function openAW(url) {
	//hiding all drop-down menus because of the IE6 bug in which it ignores the z-index and shows all <select> fields even when there's a higher z-index div on top of it. 
	if ($.browser.msie == true) {
		$('select').css('visibility','hidden');
	}
	
	$.dimScreen(1000, 0.3);
	
	//call up the facebox
	
	$.facebox({ajax: url});
	
	//remove the class that was added when the facebox was used to display the loading message
	$('#facebox .loadMsg').removeClass('loadMsg');
}

//Close the ajax window
function closeAW() {
	if ($.browser.msie == true) {//show all the dropdown menu, slowly (to accomodate the fading effect of facebox)
		$('select').css('visibility','visible'); 
	}
	$(document).unbind('reveal.facebox'); //resets the facebox reveal event
	$.facebox.close();
	$.dimScreenStop(); //undim the background
}

//display messages in the facebox
function messageAW(title, message, transactType, songID, rating) {
	

	$(document).unbind('reveal.facebox').bind('reveal.facebox', function(){
		$('#facebox #plMessageTitle').html(title);
		$('#facebox #plMessage').html(message);
		
		$('#facebox .close').click(function(){
				closeAW();
		});	
		
		switch(transactType){
			
		case 'alreadyLoggedIn':
			$('#facebox form#confirm').submit(function(){
					disableSubmit()
					//$('#facebox .submitButton').val('Reloading...');
					window.location.reload(true);
					return false;
			});
			$('#facebox .close').hide();
			break;
			
		case 'loading':
			$('#facebox .close, #facebox form#confirm').hide();
			$('#__dimScreen').css('height','26000px');
			$('#facebox .body ').addClass('loadMsg');
			$('#facebox').css('left',(($(window).width() / 2) - 112))
			break;
			
		//case 'ontoNextActionError':
		case 'passwordResetRequestSpecific':
			$('#facebox form#confirm').submit(function(){
					ajaxWindow('loginRequestSpecific', songID);
					return false;
			});	
			$('#facebox .close').hide();
			break;
			
		case 'passwordResetRequestGeneral':
			$('#facebox form#confirm').submit(function(){
					ajaxWindow('loginRequestGeneral');
					return false;
			});	
			$('#facebox .close').hide();
			break;
			
		case 'passwordResetRating':
			$('#facebox form#confirm').submit(function(){
					ajaxWindow('loginRating');
					return false;
			});	
			$('#facebox .close').hide();
			break;
			
		case 'passwordReset':
		case 'notLoggedIn':
			$('#facebox form#confirm').submit(function(){
					ajaxWindow('login');
					return false;
			});	
			$('#facebox .close').hide();
			break;
			
		case 'standAloneError':
			$('#facebox form#confirm').submit(function(){
					closeAW();
					return false;
			});	
			break;
			

				
			
		case 'login':
			$('#facebox form#confirm').hide();
			//setTimeout('closeAW();', 2000);
			setTimeout('window.location.reload(true);', 2201);
			break;
			
			
		default:
			$('#facebox form#confirm').hide();
			setTimeout('closeAW();', 2000);
			
		}
		
	});

	openAW('/radio/services/the_current/playlist/include/message.html');
}

//display the loading graphic at the page load of playlist
function loadPlaylist() {
	var title='<img src="http://minnesota.publicradio.org/radio/services/the_current/playlist/images/loading_playlist.gif" alt="Loading Playlist" class="loading" />';
	var message='';
	
	messageAW(title, message, 'loading');
}


//What to do if connection to server fails
function connectFailed() {
	
	//enable the submit buttons again
	enableSubmit();
	
	var message="We are sorry, but there was a problem connecting to the server.  Please refresh the page and try again.";
	$('#facebox .errorMsg').text(message);

}

//What to do if connected but transaction fails
function transactFailed(errors) {
	//enable the submit buttons again
	enableSubmit();
	
	//fill out the fields if it's empty (silently fails if it's not in the form)
	theCurrentFields('#facebox input[name=usr_song_name]', 'song*');
	theCurrentFields('#facebox input[name=usr_artist_name]','artist*');
	theCurrentFields('#facebox input[name=user_email]','e-mail address*');
	theCurrentFields('#facebox input[name=user_first_name]','first name');
	theCurrentFields('#facebox input[name=user_last_name]','last name');
	
	
	//reset previous fields with errors
	$('#facebox input').removeClass('invalidField');
	
	function makeItInvalid(selector) {
		$('#facebox '+selector).addClass('invalidField');
	}
		
	
	//errors is an object but I'm treating it as an associative array
	var errorMsg='';
	// looping through error messages.  I don't know how many properties there are in the errors object.  See the tutorial at the bottom here: http://www.quirksmode.org/js/associative.html
	for (var i in errors) {
		
		switch(i) {
		
		case 'new_user_password_confirm':
			makeItInvalid('input#newPasswordConfirm');
			break;
			
		case 'old_user_password':
			makeItInvalid('input#oldPassword');
			break;
			
		case 'user_password':
			makeItInvalid('input.password');
			break;
			
		case 'user_password_confirm':
			makeItInvalid('input.password');
			makeItInvalid('input.passwordConfirm');
			break;
			
		case 'user_email':
			makeItInvalid('input.email');
			break;
			
		case 'usr_song_name':
			makeItInvalid('input[name=usr_song_name]');
			break;
			
		case 'usr_artist_name':
			makeItInvalid('input[name=usr_artist_name]');
			break;
			
			
		}
		
		errorMsg = errorMsg + errors[i]+'<br />';
		
		//if it's an "e-mail laready exists" error on register, then add the link to reset password
		if (errorMsg == 'Email address already exists<br />') {
			errorMsg = errorMsg + '<a class="highlightedLink" href="#" onclick="javascript:ajaxWindow(\'passwordReset\')">Reset Password</a><br />';
		}
	}
	displayError(errorMsg);
}


/* Canned error message for the unlikely case that somebody tries to do login-related stuff when they are already logged on */
function alreadyLoggedIn(closeAfterThis) {
	
	var title = 'Error';
	var message = "You are already logged in. Please click OK to reload, so the page reflects your logged-in status.";
	
	if (closeAfterThis == true) {
		messageAW(title, message, 'standAloneError');
	} else {
		messageAW(title, message, 'alreadyLoggedIn');
	}
}

/* Canned error message for the unlikely case that they are not logged on when they're doing login-only stuff, like changing password and updating profile.*/
function notLoggedIn() {
	var title = "Error";
	var message = "You are not logged in right now.  Please login first.";
	messageAW(title, message, 'notLoggedIn');
}

// Login success message 
function loginMessage() {
	var title="Welcome";
	var message='You have successfully logged in. <br /><br /> Go to <a class="highlightedLink" href="/radio/services/the_current/playlist/summary.php" title="Your profile">your profile</a> to view the songs you requested and rated.'
	
	messageAW(title, message, 'login')
}	

//Ajax post. 
function ajaxPost(url, form, ajaxData, success, failTrans, failConnet) {
	var postSuccess = false;
	if (ajaxData == null) {
		ajaxData = $(form).serialize()+'&format=json';
	} 
	$.post(
		url,
		ajaxData, 
		function(data,status) {
			if (status == "success") {//connected to the server successfully
				if (data.status == "OK") {//ajax action succeeded
					success();
					enableSubmit();
				} else {//connected, but the transaction failed
					failTrans(data.errors);
				}
			} else {//connection to the server failed
				failConnect();
			}
				//alert(status);
				//alert(data);
		}, 
			'json'
	);
}



/*Non-facebox ajax functions*/

function checkLogin(trueFunc, falseFunc) {
	$.post(
		'/radio/services/the_current/playlist/logged_in_user.php?format=json',
		'format=json', 
		function(data,status) {
			if (status == "success") {//connected to the server successfully
				if (data.status == "OK") {//ajax action succeeded
					if (typeof data.response.user_id == "undefined") {
						falseFunc();
					} else {
						trueFunc(data.response);
					} 
				} else {//connected, but the transaction failed
					transactFailed();
				}
			} else {//connection to the server failed
				connectFailed();
			}
				//alert(status);
				//alert(data);
		}, 
			'json'
	);
}
	
//Check to see if the user is logged in at the page load.  If true, then bind the logout behavior.  If not, then bind the login form behavior in the header.
function deployLoginLogout() {
	var trueFunc = function() {
		$('.plLogout').click(function(){
			var url = '/radio/services/the_current/playlist/logout.php?format=json';
			var ajaxData = 'format=json';
			var form = '';
			var success = function(){
				//redirect to playlist
				window.location.replace('/radio/services/the_current/playlist/playlist.php');
			}
			var failTrans = function(errors){
				transactFailed(errors);
			}
			var failConnect = function() {
				connectFailed();
			}
			ajaxPost(url, form, ajaxData,success,failTrans,failConnect);
			return false;
		});
	}
	
	var falseFunc = function() {
		$('#searchProfileBar form.plLogin').submit(function(){
				//disable the go button.  It also indicates to user that the page responded to the submission.
				disableSubmit();
				
				//When the login form is submitted, it'll check one more time to see if the user is already logged in.
		
				var trueFunc = function() {
					enableSubmit();
					alreadyLoggedIn();
				}
			
				var falseFunc = function() {
					var url = '/radio/services/the_current/playlist/login.php?format=json';
					var ajaxData = null;
					var form = '#searchProfileBar form.plLogin';
					var success = function(){
						enableSubmit();
						loginMessage();
					}
					var failTrans = function(errors){
						enableSubmit();
						//reset previous error fields
						$('#searchProfileBar input').removeClass('invalidField');
						
						//display error message
						var title="Error";
						var errorMsg='';
						for (var i in errors) {
							switch(i){
							case 'user_email':
								$('#searchProfileBar input[name=user_email]').addClass('invalidField');
								break;
								
							case 'user_password':
								$('#searchProfileBar input.password').addClass('invalidField');
								break;
							}
							errorMsg = errorMsg+ errors[i]+'<br />';
							
							if (errorMsg == 'No user found with the given email address<br />') {
								errorMsg = errorMsg + '<a class="highlightedLink" href="#" onclick="javascript:ajaxWindow(\'register\')">Create a new account</a>';
							}
						}
						messageAW(title, errorMsg, 'standAloneError');
					}
					var failConnect = function() {
						enableSubmit();
						var title="Error";
						var message = "We are sorry, but there was a problem connecting to the server.  Please refresh the page and try again.";
						messageAW(title, message, 'standAloneError');
					}
					
					//execute
					ajaxPost(url, form, ajaxData,success,failTrans,failConnect);
					
					return false;
				}
		
			checkLogin(trueFunc, falseFunc);	
			return false;
		});
	}
	
	checkLogin(trueFunc, falseFunc);
}


//constructtor of the ajax window info object to store all the info
function awInfo(transactType, songID, rating) {
	
	//first, set up functions for successes to multiple transactTypes
	
	//login success
	 function loginSuccess(transactType, songID, rating) {
		if ((transactType == "register")) {
			var title="Welcome";
			var message = "Your account was successfully created. Thanks!";
			messageAW(title, message, 'login');
			return;
		} 	
		
		switch(transactType){
		case "loginRequestSpecific":
		case "registerRequestSpecific":
			//open a new facebox with specific request
			ajaxWindow('requestSpecificAfterLogin', songID);
			break;
		
		case "loginRequestGeneral":
		case "registerRequestGeneral":
			//open a new facebox with general request
			ajaxWindow('requestGeneralAfterLogin');	
			break;
		case "loginRating":
		case "registerRating":
			ajaxWindow('ratingAfterLogin', songID, rating);
			break;
			
		default: 
			loginMessage();
	
		}
	} 
	
	
	
	//specific or general request success functions
	function requestSuccess(){
		var title="";
		var message = "Your request has been sent to The Current.  Thanks!";
		messageAW(title, message, 'request');
		
	}
	
	function requestAfterLoginSuccess(){
		var title="";
		var message = "Your request has been sent to The Current.  Thanks!";
		messageAW(title, message, 'login');
	}
	
	
	this.ajaxData = null;
	this.form = null;
	
	//Two failure messages.  The transaction failure has to look into the error messages. 

	this.failConnect = function(){
		connectFailed();
	}
		
	this.failTrans = function(errors) {
		transactFailed(errors);
	}
	
	switch(transactType)
		{
		case 'login':
		case 'loginRequestGeneral':
		case 'loginRequestSpecific':
		case 'loginRating':
			this.url = '/radio/services/the_current/playlist/login.php?format=json';
			this.form = '#facebox form.plLogin';
			this.success = function() {
				loginSuccess(transactType, songID, rating);	
			}
			this.applyAtReveal = function() {
			
				//remove all the previous password replacement fields, in case it's floating out there
				$('.faceboxLoginPassword').remove();
				
				theCurrentFields('#facebox input[name=user_email]', 'e-mail address*');
				replacePasswordFieldsByClass('#facebox input[name=user_password]','passwordReplacementField');
				
				$('#facebox .highlightedLink').click(function() {
						var trueFunc = function(){
							alreadyLoggedIn();
						}
						
						var falseFunc  = function(){
							switch(transactType){
								case "loginRequestSpecific":
									ajaxWindow('registerRequestSpecific', songID);
									break;
								case "loginRequestGeneral":
									ajaxWindow('registerRequestGeneral');
									break;
								case "loginRating":
									ajaxWindow('registerRating', songID, rating);
									break;
								default:
									ajaxWindow('register');
							}
						}
							
						checkLogin(trueFunc, falseFunc);
						return false;
							
				});
			
				//binding the reset password link inside the login screen. 
				$('#facebox .loginResetPassword').click(function(){
						var trueFunc = function() {
							alreadyLoggedIn();
						}
						var falseFunc = function() {
							switch(transactType){
								case "loginRequestSpecific":
									ajaxWindow('passwordResetRequestSpecific', songID);
									break;
								case "loginRequestGeneral":
									ajaxWindow('passwordResetRequestGeneral');
									break;
								case "loginRating":
									ajaxWindow('passwordResetRating', songID, rating);
									break;
								default:
									ajaxWindow('passwordReset');
							}
							
						}
						
						checkLogin(trueFunc, falseFunc);
						return false;
				});
				
				//in case it got hidden before getting to this step, I'm making sure the close button is shown.
				$('#facebox .close').click(function(){
						closeAW();
				}).show();
				
			}
			break;
			
		case 'register':
		case 'registerRequestGeneral': 
		case 'registerRequestSpecific':
		case 'registerRating':
			this.url = '/radio/services/the_current/playlist/register.php?format=json';
			this.form = '#facebox #register_form';
			this.success = function() {
				loginSuccess(transactType, songID, rating);	
			}
			this.applyAtReveal = function() {
				theCurrentFields('#facebox input[name=user_email]','e-mail address*');
				theCurrentFields('#facebox input[name=user_first_name]','first name');
				theCurrentFields('#facebox input[name=user_last_name]','last name');
				replacePasswordFieldsById('registerPassword', 'password* (6-20 char.)');
				replacePasswordFieldsById('registerPasswordConfirm','confirm password*');
				
				$('#facebox .highlightedLink').click(function() {
						var trueFunc = function(){
							alreadyLoggedIn();
						}
						
						var falseFunc  = function(){
							switch(transactType){
								case "registerRequestSpecific":
									ajaxWindow('loginRequestSpecific', songID);
									break;
								case "registerRequestGeneral":
									ajaxWindow('loginRequestGeneral');
									break;
								case "registerRating":
									ajaxWindow('loginRating', songID, rating);
									break;
								default:
									ajaxWindow('login');
							}
						}
							
						checkLogin(trueFunc, falseFunc);
						return false;
							
					});
				
				$('#facebox form').submit(function(){
						if ($('input[name=user_first_name]').val() == 'first name') {
							$('input[name=user_first_name]').val('');
						}
						if ($('input[name=user_last_name]').val() == 'last name') {
							$('input[name=user_last_name]').val('');
						}
						return true;
				});
					
				$('#facebox .close').click(function(){
						closeAW();
				});
			}
			break;
		
		
		case 'requestSpecificAfterLogin':
			this.url = '/radio/services/the_current/playlist/specific_request.php?usr_song_id='+songID+'&format=json';
			this.form = '#facebox #specific_request_form';
			//this.ajaxData = 'usr_song_id='+songID+'&usr_request_msg=test&format=json';
			this.success = function() {
				requestAfterLoginSuccess();
			}
			this.applyAtReveal = function() {
				
				theCurrentTextarea('textarea#usr_request_msg', "Please tell our DJs your name and where you're listening.");
				$('#facebox .close').click(function(){
						loginMessage();
				});
				
				$('#facebox form').submit(function(){
						if ($('textarea[name=usr_request_msg]').val() == "Please tell our DJs your name and where you're listening.") {
							$('textarea[name=usr_request_msg]').val('');
						}
						return true;
				});

			}
			
			break;
		case 'requestSpecific':
			this.url = '/radio/services/the_current/playlist/specific_request.php?usr_song_id='+songID+'&format=json';
			this.form = '#facebox #specific_request_form';
			//this.ajaxData = 'usr_song_id='+songID+'&usr_request_msg=test&format=json';
			this.success = function() {
				requestSuccess();
			}
			this.applyAtReveal = function() {
												
				theCurrentTextarea('textarea#usr_request_msg', "Please tell our DJs your name and where you're listening.");
				$('#facebox .close').click(function(){
						closeAW();
				});

				$('#facebox form').submit(function(){
						if ($('textarea[name=usr_request_msg]').val() == "Please tell our DJs your name and where you're listening.") {
							$('textarea[name=usr_request_msg]').val('');
						}
						return true;
				});
			}
			
			break;
			
		case 'requestGeneralAfterLogin':	
			this.url = '/radio/services/the_current/playlist/general_request.php?format=json';
			this.form = '#facebox #general_request_form';
			this.success = function() {
				requestAfterLoginSuccess();
			}
			this.applyAtReveal = function(){
				theCurrentFields('#facebox input[name=usr_song_name]', 'song*');
				theCurrentFields('#facebox input[name=usr_artist_name]','artist*');
				theCurrentTextarea('textarea#usr_request_msg', "Please tell our DJs your name and where you're listening.");
				
				$('#facebox form').submit(function(){
						if ($('input[name=usr_song_name]').val() == 'song*') {
							$('input[name=usr_song_name]').val('');
						}
						if ($('input[name=usr_artist_name]').val() == 'artist*') {
							$('input[name=usr_artist_name]').val('');
						}
						if ($('textarea[name=usr_request_msg]').val() == "Please tell our DJs your name and where you're listening.") {
							$('textarea[name=usr_request_msg]').val('');
						}
						return true;
				});
				
				$('#facebox .close').click(function(){
						loginMessage();
				});
			}
			break;
			
			
		case 'requestGeneral':
			this.url = '/radio/services/the_current/playlist/general_request.php?format=json';
			this.form = '#facebox #general_request_form';
			this.success = function() {
				requestSuccess();
			}
			this.applyAtReveal = function(){
				theCurrentFields('#facebox input[name=usr_song_name]', 'song*');
				theCurrentFields('#facebox input[name=usr_artist_name]','artist*');
				theCurrentTextarea('textarea#usr_request_msg', "Please tell our DJs your name and where you're listening.");
					
				$('#facebox form').submit(function(){
						if ($('input[name=usr_song_name]').val() == 'song*') {
							$('input[name=usr_song_name]').val('');
						}
						if ($('input[name=usr_artist_name]').val() == 'artist*') {
							$('input[name=usr_artist_name]').val('');
						}
						if ($('textarea[name=usr_request_msg]').val() == "Please tell our DJs your name and where you're listening.") {
							$('textarea[name=usr_request_msg]').val('');
						}
						return true;
				});
				
				$('#facebox .close').click(function(){
						closeAW();
				});
			}
			break;
	
		case 'ratingAfterLogin':
			this.url="/radio/services/the_current/playlist/song_rating.php";
			this.ajaxData = 'usra_song_id='+songID+'&usra_rating='+rating+'&format=json';
			this.success = function() {
				var title="";
				var message = "Your rating was successfully saved.  Thanks!";
				messageAW(title, message, 'login');
			};
			break;
			
		case 'passwordChange':
			this.url= '/radio/services/the_current/playlist/change_password.php?format=json';
			this.form = '#facebox #change_password_form';
			this.success = function() {
						var title = "";
						var message = "Your password was successfully changed.";
						messageAW(title, message, 'password');
			}
			this.applyAtReveal = function() {
					replacePasswordFieldsById('oldPassword','old password*');
					replacePasswordFieldsById('newPassword','new password*');
					replacePasswordFieldsById('newPasswordConfirm','confirm new password*');
					
					$('#facebox .close').click(function(){
						closeAW();
					});
			}
			break;
		
		case 'passwordReset':
		case 'passwordResetRequestSpecific':
		case 'passwordResetRequestGeneral':
		case 'passwordResetRating':
			this.url= '/radio/services/the_current/playlist/reset_password.php?format=json';
			this.form = '#facebox #reset_password_form';
			this.success = function() {
						var title="Reset Password";
						var message = "Your password was reset, and the new random password was sent to your e-mail address.";
						//transactType and song info are passed on
						messageAW(title, message, transactType, songID, rating);
			}
			this.applyAtReveal = function() {
				theCurrentFields('#facebox input[name=user_email]','e-mail address*');
				
				$('#facebox .close').click(function(){
						closeAW();
				});
				
			}
			break;
			
		case 'profileUpdate':
			this.url= '/radio/services/the_current/playlist/update_profile.php?format=json';
			this.form = '#facebox #update_profile_form';
			this.success = function() {
					var title = '';
					var message = "Your profile was successfully updated.  Thanks!";
					messageAW(title, message, 'profile');
			}
			this.applyAtReveal = function() {
				theCurrentFields('#facebox input[name=user_email]','e-mail address*');
				theCurrentFields('#facebox input[name=user_first_name]','first name');
				theCurrentFields('#facebox input[name=user_last_name]','last name');
				
				$('#facebox form').submit(function(){
						if ($('input[name=user_first_name]').val() == 'first name') {
							$('input[name=user_first_name]').val('');
						}
						if ($('input[name=user_last_name]').val() == 'last name') {
							$('input[name=user_last_name]').val('');
						}
						return true;
				});
				
				$('#facebox .close').click(function(){
						closeAW();
				});
				
			}
			break;		
		}	
}





//Work flow and the master executer the ajaxWindow
function ajaxWindow(transactType, songID, rating) {

	var awInstance = new awInfo(transactType, songID, rating);
	if (transactType == 'ratingAfterLogin') {
		//just do the post, since there are no forms to display in facebox
		ajaxPost(awInstance.url, awInstance.form, awInstance.ajaxData, awInstance.success, awInstance.failTrans, awInstance.failConnect);
	} else {
		$(document).unbind('reveal.facebox').bind('reveal.facebox', function(transactType) {
				
			//show any hidden elements from previous times
			$('#facebox .close, #facebox form#confirm').show();
			
			//reset the close button
			$('#facebox .close').unbind('click');
				
			//apply behaviors specific to that window
			awInstance.applyAtReveal();
			
			$(awInstance.form).submit(function(){
					//disable the submit button when clicked
					disableSubmit();
					
					//do the post
					ajaxPost(awInstance.url, awInstance.form, awInstance.ajaxData, awInstance.success, awInstance.failTrans,awInstance.failConnect);
				
					
					return false;
			});		
	
		});
		openAW(awInstance.url);
	}
}
	


function applyRegister(transactType) {
	$('.plRegister').click(function(){
			
			var trueFunc = function() {
				alreadyLoggedIn();
			}
			var falseFunc = function() {
				ajaxWindow('register');
			}
			checkLogin(trueFunc, falseFunc);
			return false;
	});
} 

function applyGeneralRequest() {
	$('.general_request').click(function(){
			var trueFunc = function() {
				ajaxWindow('requestGeneral');
			}
			var falseFunc = function() {
				ajaxWindow('loginRequestGeneral');
			}

			checkLogin(trueFunc, falseFunc);
			return false;
	});
}

function applySpecificRequest() {
	$('.specific_request').click(function(){
		var targetURL = $(this).attr('href');
		var songID = $(this).attr('rel');
		
		var trueFunc = function() {
			ajaxWindow('requestSpecific', songID);
		}
		var falseFunc = function() {
			ajaxWindow('loginRequestSpecific', songID);
		}
					
		checkLogin(trueFunc, falseFunc);
		return false;
	});
}

function applyResetPassword() {
	$('.plResetPassword').click(function(){
			
			var trueFunc = function() {
				alreadyLoggedIn();
			}
			var falseFunc = function() {
				ajaxWindow('passwordReset');
			}
			
			checkLogin(trueFunc, falseFunc);
			return false;
	});
}

function applyChangePassword() {
	$('a.change_password').click(function(){
			var trueFunc = function() {
				ajaxWindow('passwordChange');
			}
			var falseFunc = function() {
				notLoggedIn();
			}
			
			checkLogin(trueFunc, falseFunc);
			
			return false;
	});
}	 

function applyUpdateProfile() {
	$('a.update_profile').click(function(){
			
			var trueFunc = function() {
				ajaxWindow('profileUpdate');
			}
			var falseFunc = function() {
				notLoggedIn();
			}
			
			checkLogin(trueFunc, falseFunc);	
			return false;
	});
}


//This is just for the song detail pagem, as that is the only page that displays a link to login.php when the user is not logged on.
function applyLogin() {
	$(document).ready(function(){
			$('a.plLoginLink').click(function() {
				var trueFunc = function() {
				alreadyLoggedIn();
				}
				var falseFunc = function() {
					ajaxWindow('login');
				}
				
				checkLogin(trueFunc, falseFunc);	
				return false;
			});
		});
}
			

//check to see if the user is logged in before sending them to logged in only pages (requests, ratings, profile, etc.)
function applyLoggedInOnly() {
	$('a.plLoginOnly').click(function() {
			var destinationURL = $(this).attr('href');
			
			var trueFunc = function() {
				window.location.replace(destinationURL);
			}
			
			var falseFunc = function() {
				notLoggedIn();
			}
	
			checkLogin(trueFunc, falseFunc);
			return false;
	});
}
			
//function to deploy all ajax behaviors
function deployAjaxBehaviors() {
	deployLoginLogout();
	applyRegister();
	applyChangePassword();
	applySpecificRequest();
	applyGeneralRequest()
	applyResetPassword();
	applyUpdateProfile();
	applyLoggedInOnly()
}


/* Deployment _________________________________________________________________________________*/


//Master deployment.  These are to be done on every single page within the playlist.
$(document).ready(function(){
			 
	 //advanced search show/hide and related functions
	 $('#plSearchLink').click(function(){
			 $('#advancedSearch').slideToggle('fast');
			 return false;
	 });
	 $('#advancedSearch').append('<div class="closeSearch">close X</div>');
	 $('#advancedSearch > .closeSearch').click(function(){
			 $('#advancedSearch').slideUp('fast');
	 });
	 
 
	 //deploy text field scripts
	 theCurrentTextFields();
	 
	 //deploy the Ajax window
	 deployAjaxBehaviors();
	 
	 //There are additional functions executed at page load, but they are called individually from each page, as needed.

});
