$(document).ready(function(){
		/**
		 * The following line adjusts the css selectors available
		 * in our stylesheets, we'll use a 'degrade' for elements that should be hidden when ajax is available
		 * example:
		 * 
		 * 		.jsEnabled .degrade {
		 * 			display: none;
		 * 		}

		 * For ajax type interfaces, we can do:
		 * 		.jsDisabled .fancy {
		 * 			display: none;
		 * 		}
		 */
		
		
		$('body.jsDisabled').removeClass('jsDisabled').addClass('jsEnabled');
		
		$('ul#messages.show').append('<li><button class="close"><span>close</span></button></li>').find('button.close:first').click(
			function(e){
				e.preventDefault();
				$('ul#messages').fadeOut(500, 
					function(){
						$(this).removeClass('show');
					}
				);
			}
		);
		
		$('#sendSuggestion-dialog').dialog({
			autoOpen: false,
			width: 700,
			height: 270,
			modal: true,
			overlay: {
				opacity: 0.5,
				background: 'black'
			} 
		});	
		
		var suggestionListener = function() {
			$('#sendSuggestion').click(function(event){
				event.preventDefault();
				event.stopPropagation();
				$('#sendSuggestion-dialog').dialog('close');
				
				var data = {
						from: $('#suggestionEmail').attr('value'),
						subject: $('#suggestionSubject').attr('value'),
						suggestion: $('#suggestionComment').attr('value') 
				};

				$.post(baseUrl+'index/post-suggestion', data);
								
				alert('Thank you for the suggestion.');		
				window.location = 'http://www.flyjba.com';
			})
		};
				
		suggestionListener();
		
		$("#sendSuggestionLink").click(function(event) {
			event.preventDefault();
			event.stopPropagation();
			$('#sendSuggestion-dialog').dialog('open','height', 270);
		});
		
		$(".collapseShow").live("click", function(event){
			event.preventDefault();
			event.stopPropagation();
			$(this).parent().parent().nextAll(".featuredContainer:first").show('slow');			
			if($(this).hasClass('feedUpdates')) {
				$.post(baseUrl+'my-account/index/feed-updates', {'hide':false}, function(resp){},'json');
				$(this).parent().html('<a href="" class="collapseHide feedUpdates"><img src="'+baseUrl+'images/icons/minus.png" class="actionIcon" /></a>');
			} else {
				$(this).parent().html('<a href="" class="collapseHide"><img src="'+baseUrl+'images/icons/minus.png" class="actionIcon" /></a>');
			}
		});
		
		$(".collapseHide").live("click", function(event){
			event.preventDefault();
			event.stopPropagation();
			$(this).parent().parent().nextAll(".featuredContainer:first").hide('slow');			
			if($(this).hasClass('feedUpdates')) {
				$.post(baseUrl+'my-account/index/feed-updates', {'hide':true}, function(resp){},'json');
				$(this).parent().html('<a href="" class="collapseShow feedUpdates"><img src="'+baseUrl+'images/icons/plus.png" class="actionIcon" /></a>');
			} else {
				$(this).parent().html('<a href="" class="collapseShow"><img src="'+baseUrl+'images/icons/plus.png" class="actionIcon" /></a>');
			}
		});

		$('.addToFeeds').click(function(event){
			event.preventDefault();
			event.stopPropagation();
			var href = $(this).attr('href');
			addToFeeds = function(href) {				
				$.post(href,{},function(data, textStatus){
					if(data.feed == "added") {
						alert('This has been added to your favorites.');
					} else if (data.feed == "already_added") {
						alert('This was already in your favorites.');
					} else {
						alert('Error adding this to your favorites.');
					}
				}, "json");		
			};
			loginCheck(function(){addToFeeds(href);});
		});

		$('.removeFromFeeds').click(function(event){
			event.preventDefault();
			event.stopPropagation();
			var href = $(this).attr('href');
			$.post(href,{},function(data, textStatus){
				if(data.feed == "removed") {
					alert('This has been removed from your favorites.');
				} else if (data.feed == "already_removed") {
					alert('This was already femoved from your favorites.');
				} else {
					alert('Error removing this from your favorites.');
				}
			}, "json");		
		});

		$('.emailFeedUpdates').click(function(event){
			event.preventDefault();
			event.stopPropagation();
			var href = $(this).attr('href');
			$.post(href,{},function(data, textStatus){
				var link = href.split('/');
				if(link[link.length - 1] == 0) {
					var newLink = '<img src="'+baseUrl+'images/icons/email_add.png" title="Email me updates" />';
					$(this).html(newLink);
				} else {
					var newLink = '<img src="'+baseUrl+'images/icons/email_delete.png" title="Do not email me updates" />';
					$(this).html(newLink);
				}
				if(data.feed == "removed") {
					alert('This has been removed from your favorites.');
				} else if (data.feed == "already_removed") {
					alert('This was already femoved from your favorites.');
				} else {
					alert('Error removing this from your favorites.');
				}
			}, "json");		
		});
});
