$(document).ready(function() {
	// tab functionality for user home
	$("ul.tabs").tabs("div.panes > div");
	
	// preventing default action for bit control buttons
	$("ul.bit_controls button").click(
		function(event){
			event.preventDefault();
		}
	);
	
	// Set half opacity for all bit control buttons
	$('button.bit_options').animate({ opacity: ".5" }, 0);
	
	// Hover state changes (the .stop() prevents animation queue buildup)
	$(".firehose .bit").hover(
		function(){
			$(this).stop().animate({ backgroundColor: "#f1f1f1" }, 300);
			$('button.bit_options', this).stop().animate({ opacity: "1" }, 300);
		},
		function(){
			$(this).stop().animate({ backgroundColor: "#fff" }, 300);
			$('button.bit_options', this).stop().animate({ opacity: ".5" }, 300);
		}
	);
	
	$('button.bit_options').click(
		function(event){
			var bitOptionMenu = $(this).parent('li').siblings('.bit_options');
			
			if ( bitOptionMenu.is(':visible') ) {
				$('ul.bit_controls li.bit_options').hide();
			} else {
				// hiding any other open menus
				$('ul.bit_controls li.bit_options').hide();
				bitOptionMenu.css('display','block'); //because show() sets display to list-item insteads of block
			} 
			
			event.stopPropagation();
		}
	);
		
	// So that clicks anywhere outside will close the menu
	$('html').click(
		function(event){
			if(event.target != 'button.bit_options' && $('button.bit_options').is(':visible') ) {
				$('ul.bit_controls li.bit_options').hide();
				event.stopPropagation();
			}
		}
	);

	
	$(".favorite_button").click(function () {
		isFavorite = $(this).attr('class').split(' ').slice(-1);
		if (isFavorite == 'on') {
			$(this).removeClass("on");
			$(this).addClass("off");
		} else {
			$(this).removeClass("off");
			$(this).addClass("on");
		}
		
		$.post(
			"/favorited_wisdom_bits/favorite", 
			{
				"isFavorite": (isFavorite == 'on'),
				"id": $(this).val(),
			}, 
			function(data, textStatus) { // A status message to display whether the post was successful (for testing)
				alert(data);
			}
		);
	});

});