
	function genreChange(baseUrl)
	{
		if(typeof(baseUrl) == 'undefined') baseUrl = '';
		var ajaxUrl = baseUrl + 'showSubgenre.php';
		new Ajax.Updater('subGenreContent', ajaxUrl, { evalScripts:true, postBody: 'baseUrl=' + baseUrl + '&genreId=' + $F('genre'), onComplete:function(){$('subGenreContainer').show();}});
	}


	function updateKeywords(baseUrl)
	{
		if(typeof(baseUrl) == 'undefined') baseUrl = '';
		var ajaxUrl = baseUrl + 'updateKeywords.php';

		new Ajax.Updater('keywordsContainer', ajaxUrl, {postBody:$('searchForm').serialize()});
	}
	
	
	// function addToFavories(trackId, reloadAccountPage)
	// {
	// 	if(typeof(reloadAccountPage) == 'undefined') reloadAccountPage = false;
	// 
	// 	var ajaxUrl = sysUrl + 'catalog/addToFavories.php';
	// 	new Ajax.Request(ajaxUrl, { evalScripts:true, postBody: 'trackId=' + trackId, onComplete:function(request) {
	// 		if(request.responseText == 'true')
	// 		{
	// 			if(reloadAccountPage)
	// 				rebuildAccountsPage();
	// 			else
	// 				$('favoriteLink-'+trackId).replace('Added to your favorites.');
	// 		}
	// 		else
	// 			alert('Sorry but there was a problem adding this track to your favorites. Please reload this page and try again.');
	// 	}});
	// }
	
	function removePlaylist(playlistId)
	{
		if(confirm('Are you sure?'))
		{
			location.href = sysUrl+'account/removePlaylist.php?playlistId='+playlistId;
		}
	}
	
	function renamePlaylist()
	{
		$('playlistName').toggle();
		$('renameForm').toggle();
		if($('renameForm').visible())
			$('renameForm').focusFirstElement();
	}
	
	function renamePlaylistSave()
	{
		var ajaxUrl = sysUrl + 'account/renamePlaylistSave.php';
		new Ajax.Request(ajaxUrl, { evalScripts:true, postBody: $('renameForm').serialize(), onComplete:function(request) {
			if(request.responseText == 'true')
			{
				$('playlistName').down('span').update($F('newPlaylistName'));
				renamePlaylist();
			}
			else
				alert('Sorry but there was a problem renaming this playlist. Please reload this page and try again.');
		}});
	}
	
	function addToPlaylistSelectorUpdate(selectEl, trackId, reloadAccountPage)
	{
		if(selectEl.selectedIndex > 0)
		{
			var formEl = $(selectEl).up('form');
			addToFavorites(formEl, trackId, reloadAccountPage);
		}
	}
	
	function addToFavorites(formEl, trackId, reloadAccountPage)
	{
		if(typeof(reloadAccountPage) == 'undefined') reloadAccountPage = false;
	
		var ajaxUrl = sysUrl + 'catalog/addToFavorites.php';
		new Ajax.Request(ajaxUrl, { evalScripts:true, postBody: formEl.serialize(), onComplete:function(request) {

			if(request.responseText == 'false')
				alert('Sorry but there was a problem adding this track to your playlist. Please reload this page and try again.');
			else
			{
				var response = request.responseText.evalJSON();
				if(!response)
					alert('Sorry but there was a problem adding this track to your playlist. Please reload this page and try again.');
				else
				{
					if(reloadAccountPage)
						rebuildAccountsPage();
					else
					{
						formEl.replace('Added to your playlist.');
						$$('select.playlistSelector').each(function(e){
							e.replace(response.playlistSelector);
						});
					}
				}
			}
		}});
	}

	
	function addToPlaylistShow(el, trackId, reloadAccountPage)
	{
		if(typeof(reloadAccountPage) == 'undefined') reloadAccountPage = 0;
		if(reloadAccountPage) reloadAccountPage = 1;
		// console.log(reloadAccountPage);

		var ajaxUrl = sysUrl + 'catalog/addToPlaylistShow.php';
		new Ajax.Request(ajaxUrl, { evalScripts:true, postBody: 'trackId=' + trackId+'&reloadAccountPage='+reloadAccountPage, onComplete:function(request) {
			if(request.responseText != 'false')
			{
				// if(reloadAccountPage)
				// 	rebuildAccountsPage();
				// else
					$(el).replace(request.responseText);
					// $(el).next('.trackPlaylistContainer').update(request.responseText);
			}
			else
				alert('Sorry but there was a problem adding this track to your favorites. Please reload this page and try again.');
		}});
	}
	
	
	
	var Ratings = Class.create();

	Ratings.prototype = {
		initialize: function() {
			this.originalRates = {};
			// this.originalRates['trackrating123'] = 'asdf';
			// this.originalRates['trackrating456'] = 'asdffff';
			// console.log(this.originalRates['trackrating456']);
		},
		
		setup: function(ratingsContainer) {
			this.originalRates[ratingsContainer] = $(ratingsContainer).className;
			// this.originalRates[ratingsContainer] = 'asdf';
			Event.observe(ratingsContainer, 'mouseout', ratings.out.bindAsEventListener(ratings,ratingsContainer));
			
			$$('#'+ratingsContainer+' a').each(function(linkEl){
				var currentRate = linkEl.className.split('rate')[1];
				Event.observe(linkEl,'mouseover',ratings.over.bindAsEventListener(ratings, currentRate, ratingsContainer));
			});
		},

		over: function(ev, rate, ratingsContainer) {
			$(ratingsContainer).className = 'trackRating trackingRatingIs'+rate;
		},
		
		out: function(ev, ratingsContainer) {
			// console.log(this.originalRates[ratingsContainer]);
			if(typeof(this.originalRates[ratingsContainer]) != 'undefined')
				$(ratingsContainer).className = this.originalRates[ratingsContainer];
		},
		
		send: function(trackId, rate, reloadAccountPage) {
			if(typeof(reloadAccountPage) == 'undefined') reloadAccountPage = false;
			var ajaxUrl = sysUrl + 'catalog/rateTrack.php';
			new Ajax.Request(ajaxUrl, { evalScripts:true, postBody: 'trackId=' + trackId + '&rate='+rate, onComplete:function(request) {
				if(request.responseText == 'true')
				{
					if(reloadAccountPage)
						rebuildAccountsPage();
					else
					{
						$('trackRating-'+trackId).className = 'trackRating trackingRatingIs'+rate;
	 					ratings.originalRates['trackRating-'+trackId] = $('trackRating-'+trackId).className;						
					}
				}
				else
					alert('Sorry but there was a problem rating this track. Please reload this page and try again.');
			}});
		},
		
		remove: function(trackId) {
			if(confirm('Are you sure that you want to remove this track from your rated tracks list?'))
			{
				if(typeof(baseUrl) == 'undefined') baseUrl = '';
				var ajaxUrl = baseUrl + 'removeFromRatings.php';
				new Ajax.Request(ajaxUrl, { evalScripts:true, postBody: 'trackId=' + trackId, onComplete:function(request) {
					if(request.responseText == 'true')
						rebuildAccountsPage();
					else
						alert('Sorry but there was a problem removing this track. Please reload this page and try again.');
				}});
			}
		}
	};

	var ratings = new Ratings();


	function rebuildAccountsPage() {
		if(location.href.indexOf('?pid') != -1)
			var currentPlaylistID = location.href.split('?pid=')[1].replace('#','');
		else
			var currentPlaylistID = '';
		
		var viewportDimensions    = document.viewport.getDimensions();
		var bodyDimensions        = $$('body')[0].getDimensions();
		$('overlay').style.height = bodyDimensions.height+'px';
		// alert($('overlay').style.height);
		$('loadingIndicator').style.left = ((viewportDimensions.width-48)/2)+'px';
		$('loadingIndicator').style.top  = ((bodyDimensions.height-48)/2)+'px';
		
		$('overlay').show();
		$('loadingIndicator').show();
		
		var ajaxUrl = sysUrl + 'account/refreshAccountIndex.php?pid='+currentPlaylistID;
		new Ajax.Request(ajaxUrl, { evalScripts:true, onComplete:function(request) {
			$('overlay').hide();
			$('loadingIndicator').hide();
			if(request.responseText == 'false')
				alert('Sorry but there was a problem removing this track. Please reload this page and try again.');
			else
				$('accountIndexContent').replace(request.responseText);
		}});
	}

	function removeFromFavorites(trackId, playlistId) {
		if(confirm('Are you sure that you want to remove this track from your favorites list?'))
		{
			if(typeof(baseUrl) == 'undefined') baseUrl = '';
			var ajaxUrl = baseUrl + 'removeFromFavorites.php';
			new Ajax.Request(ajaxUrl, { evalScripts:true, postBody: 'trackId=' + trackId + '&playlistId='+playlistId, onComplete:function(request) {
				if(request.responseText == 'true')
					rebuildAccountsPage();
				else
					alert('Sorry but there was a problem removing this track. Please reload this page and try again.');
			}});
		}
	}

	function showBrowseCatalogBox() {
		new Effect.BlindUp('browseCatalogButton', {duration:0.2, afterFinish:function(){
			new Effect.BlindDown('browseCatalogBox', {duration:0.4});
		}});
	}
	
	function closeBrowseCatalogBox() {
		new Effect.BlindUp('browseCatalogBox', {duration:0.4, afterFinish:function(){
			new Effect.BlindDown('browseCatalogButton', {duration:0.2});
		}});
	}
	
	function doDetailedSearch() {
		new Effect.BlindUp('searchOurCatalogInputArea', {duration:0.4});
		new Effect.BlindDown('detailedSearchOptions', {duration:0.4});
	}
	
	function doSimpleSearch() {
		new Effect.BlindUp('detailedSearchOptions', {duration:0.4});
		new Effect.BlindDown('searchOurCatalogInputArea', {duration:0.4});
		$('searchForm').reset();
	}
	
