/**
 * Multicall General Javascript. Put any site-wide 'onDocReady' functions here.
 * 
 * @author     Mike Barnlund <mike@chaordix.com>
 */

var isPanelOpen = false;

$(document).ready(function() {
	
	// ======================== Login Panel Control ===========================
	
	// Anything with the 'loginprompt' class opens the panel
	$(".loginprompt").click( function(e) { openPanel(); e.stopImmediatePropagation(); return false; });	
	
	// Collapse Panel with the 'close' link
	$("#close").click( function() { closePanel(); });	
	
	// ======================== Profile Commands ==============================
	
	// Show profile commands when the user hovers over the avatar
	$('#avatarwrapper').hover(
		function() { $(this).children('#profilecommands').stop(false,true).animate({bottom:-10},{duration:700, easing:'easeOutElastic'}); },
		function() { $(this).children('#profilecommands').stop(false,true).animate({bottom:-32},{duration:700, easing:'easeOutQuart'}); }
	);
	
	// ======================== Prize Description =============================
	
	// Show prize description when the user hovers over a prize summary
	$('.prize').hover(
		function() {
			if (!$.browser.msie) { $(this).children('.prizetextwrapper').fadeIn(); }
			else $(this).children('.prizetextwrapper').show();
		},

		function() {
			if (!$.browser.msie) { $(this).children('.prizetextwrapper').fadeOut(); }
			else $(this).children('.prizetextwrapper').hide();
		}
	);
	
	// ========================== Help Text Control ===========================
	
	// Elements (input, textarea, select) inside a .helpcontainer show the help text
	var helpContainers = '.helpcontainer input, .helpcontainer textarea, .helpcontainer select';
	$(helpContainers).focus( function() { $(this).siblings('.help').fadeIn(300); } );
	$(helpContainers).blur( function() { $(this).siblings('.help').fadeOut(300); } );
	
	// ========================== Voting Widget Control =======================

	$( '.activevote' ).click( function() { return onVoteClick(this); } );
	
	// ========================== jGrowl Form Errors ==========================

	if ( $( '.form_errors li' ).length > 0 ) {
		$.jGrowl( '<div class="errors"><label class="error">Submission Error</label>' + $( '.form_errors' ).html() + '</div>', { sticky: true } );
	}
	
	// ========================= Close Notifications ==========================
	
	/* -- One day, when we use AJAX here, re-enable this.
	$('#closenotification').click(
		function () {
			$('#notification').fadeOut(
				function() {
					$('#activecall').removeClass('sbcontent right');
				}
			)
		}
	);
	*/
		
});

function onVoteClick(voteContainer) {
	var clickedThumb = $(voteContainer).children('.hasnotvoted');
	var isNewVote = $(clickedThumb).is(':visible');

	// if they aren't logged in, don't process click.
	var link = $(voteContainer).attr('href')
	if( null != link.match( 'login' ) )
		return true;
	
	var link = $(voteContainer).attr('href')
	if( null != link.match( 'login' ) )
		return true;
	
	//fade back in the white thumbs (skips visible ones)
	if (isNewVote) {
		$(voteContainer).parent().find('.hasnotvoted').fadeIn();
		$(clickedThumb).fadeOut();
	}
	
	$.ajax({
	  url: $(voteContainer).attr('href'),
	  success: function(data) {
		$('#point_value').text( data['new_score'] );
	  },
	  error: function(obj, error, exception) {
		  
	  },
	 dataType: 'json'
	
	});
	
	return false;
}

function openPanel() {
	if (!isPanelOpen) {
		$("div#panel").slideDown("slow", function() {
			$('#log').focus();
		});
		$("#toggle a").toggle();
		isPanelOpen = true;
	}
}

function closePanel() {
	if (isPanelOpen) {
		$("div#panel").slideUp("slow");
		$("#toggle a").toggle();
		isPanelOpen = false;
	}
}
