$('document').ready(function()
{
	
	$('.counter strong').each(function(){
		var number = $(this).html();
		var numbers = number.split('');
		var str = '';
		var pad = 8 - numbers.length;
		for(var i = 0; i < pad; i++) numbers.unshift('0');
		for(num in numbers) str += '<span class="number-' + numbers[num] + '"><em>' + numbers[num] + '</em></span>';
		$(this).html(str);
	});
	
	$('input[type=checkbox]').prettyCheckboxes({
		checkboxWidth: 28,
		checkboxHeight: 28,
		display: 'list'
	});


	if (!$.browser.msie())
	{
		$('#register-form,#sign-in,.input-wrap,.alt,.feed,#sort-by,#featured-pick,.active,.info,#top-feed-list > li').corners("5px");
		$('#content-body').corners("5px bottom-left bottom-right");
		$('#footer-bg').corners("5px top-left top-right");
	}
	

	$('#overview').click(function(ev) {
		ev.preventDefault();
		getAjaxIndex();
	});
	
	$('#rss-directory').click(function(ev) {
		ev.preventDefault();
		getAjaxRss();
	});

	$('#help').click(function(ev) {
		ev.preventDefault();
		getAjaxHelp();
	});

	$('#my-account, .get-ajax-account').click(function(ev) {
		ev.preventDefault();
		getAjaxAccount();
	});

	setRegistrationForm();
	setSignInForm();	
});

function showLoadingGif()
{
	$('#content-body, #loading').stop();

	$('#content-body').fadeTo('fast', 0, function(){
		$('#loading').show().fadeTo('fast', 1);
	});
}

function hideLoadingGif()
{
	$('#content-body, #loading').stop();

	$('#loading').fadeTo('fast', 0, function() {
		$('#loading').hide();
		$('#content-body').fadeTo('fast', 1);
	});
}

function setRegistrationForm()
{
	$('#register-form').submit(function() {
		$.post('/register/?ajax', $('#register-form').serialize(), function(data) {
			/*
			 * check the return for error messages.  if none, login and reload account page
			 */
			errors = $(data).find('.registration-errors').html();
			if (errors != null) {
				$('#content-body').html(data);	
				if (!$.browser.msie()){
					$('#register-form,#sign-in,.input-wrap').corners("5px");
				}
			} else {
				window.location = '/account';
				return true;
			}
		});
		return false;
	});	
}

function setSignInForm()
{
	$('#sign-in').submit(function() {
		$.post('/login/?ajax', $('#sign-in').serialize(), function(data) {
			/* 
			 * check the return for error messages.  if none, login and reload account page
			 */
			errors = $(data).find('.login-errors').html();
			if (errors != null) {
				$('#content-body').html(data);
				if (!$.browser.msie()){
					$('#register-form,#sign-in,.input-wrap').corners("5px");
				}
			} else {
				window.location = '/account';
				return true;
			}
		});
		return false;
	});
}

function sortBy(str)
{
	$.ajax({
		url: '/rss/ajax/'+str,
		success: function(msg) {
			$('.rss-content-wrapper').html(msg);
		}
	});
}

function setHideLastFiveTracks(id)
{
	$('#last-five-header'+id).click(function(){
		$('#last-five-header'+id).addClass('last-five-hidden').removeClass('last-five-showing');
		$('#last-five-tracks'+id).hide('normal');
		$('#last-five-header'+id).unbind();
		setShowLastFiveTracks(id);
	});
}

function setShowLastFiveTracks(id)
{
	$('#last-five-header'+id).click(function(){
		$('#last-five-header'+id).addClass('last-five-showing').removeClass('last-five-hidden');
		$('#last-five-tracks'+id).show('normal');
		$('#last-five-header'+id).unbind();
		setHideLastFiveTracks(id);
	});
}

function addFeed(user_id, feed_id)
{
	$.ajax({
			url: '/addfeed/'+user_id+'/'+feed_id,
			success: function(msg){
				$('#'+feed_id).addClass("remove-button").removeClass("add-button");
				$('#'+feed_id).unbind();
				$('#'+feed_id).click( function() {
					removeFeed(user_id, feed_id);
				})
			}
	});			
}

function removeFeed(user_id, feed_id)
{
	$.ajax({
			url: '/removefeed/'+user_id+'/'+feed_id,
			success: function(msg){
				$('#'+feed_id).addClass("add-button").removeClass("remove-button");
				$('#'+feed_id).unbind();
				$('#'+feed_id).click( function() {
					addFeed(user_id, feed_id);
				})
			}
	});
}

function removeFeedFromAccount(user_id, feed_id)
{
	$.ajax({
			url: '/removefeed/'+user_id+'/'+feed_id,
			success: function(msg){
				$('#feed'+feed_id).hide("slow");
			}
	});
}

function getAjaxPage(sort_by_str, page_num)
{
	$.ajax({
		url: '/rss/ajax/'+sort_by_str+'?page='+page_num,
		success: function(msg) {
			$('.rss-content-wrapper').html(msg);
		}
	});
}

function setAjaxPages(sort_by, page) {
	$('.step-links a:last-child').click(function(ev) {
		ev.preventDefault();
		next_page = Number(page) + 1;
		getAjaxPage(sort_by, next_page);
	
	});

	$('.step-links a:first-child').click(function(ev) {
		ev.preventDefault();
		last_page = Number(page) - 1;
		getAjaxPage(sort_by, last_page);
	});
}

function setAjaxSort()
{

	$('#sort-by-popularity').click( function(ev) {
		ev.preventDefault();
		sortBy('popularity');
	});

	$('#sort-by-date-added').click( function(ev) {
		ev.preventDefault();
		sortBy('date-added');
	});

	$('#sort-by-alpha').click( function(ev) {
		ev.preventDefault();
		sortBy('alphabetically');
	});
}

function getAjaxIndex()
{
	$.ajax({
		url: '/?ajax',
		type: 'GET',
		success: function(data){
			$('#content-body').html(data);
			$('#overview').addClass("active");
			$('#rss-directory, #help, #my-account').removeClass('active');
			$('.counter strong').each(function(){
				var number = $(this).html();
				var numbers = number.split('');
				var str = '';
				var pad = 8 - numbers.length;
				for(var i = 0; i < pad; i++) numbers.unshift('0');
				for(num in numbers) str += '<span class="number-' + numbers[num] + '"><em>' + numbers[num] + '</em></span>';
				$(this).html(str);
			});
		},
		beforeSend: function() {
			showLoadingGif();
		},
		complete: function() {
			hideLoadingGif();
		}
	});
}

function getAjaxRss()
{
	$.ajax({
		url: '/rss?ajax',
		type: 'GET',
		success: function(data){
			$('#content-body').html(data);
			$('#rss-directory').addClass("active");
			$('#overview, #help, #my-account').removeClass('active');
			if (!$.browser.msie()) {
				$('#sort-by,#featured-pick,.active,.info,.alt').corners("5px");
			}
		},
		beforeSend: function(){
			showLoadingGif();
		},
		complete: function(){
			hideLoadingGif();
		}
	});
}

function getAjaxHelp()
{
	$.ajax({
		url: '/help?ajax',
		type: 'GET',
		success: function(data){
			$('#content-body').html(data);
			$('#help').addClass('active');
			$('#overview, #rss-directory, #my-account').removeClass('active');
			if (!$.browser.msie()) {
				$('#q-a > div > ul > li.alt').corners('5px');
			}
		},
		beforeSend: function(){
			showLoadingGif();
		},
		complete: function(){
			hideLoadingGif();
		}
	});
}

function getAjaxAccount()
{
	$.ajax({
		url: '/account?ajax',
		type: 'GET',
		success: function(data){
			$('#content-body').html(data);
			$('#my-account').addClass('active');
			$('#overview, #rss-directory, #help').removeClass('active');
			if (!$.browser.msie()){
				$('#register-form,#sign-in,.input-wrap').corners("5px");
			}
			setRegistrationForm();
			setSignInForm();	
		},
		beforeSend: function(){
			showLoadingGif();
		},
		complete: function(){
			hideLoadingGif();
		}
	});
}
