/**
 * General site javascript for UpUp.com
 *
 * @author Matt Golus
 * @author Dan Dietz
 * @copyright UpUp LLC
 * @since 3/17/2011
 */

/*
 * Simple tooltip plugin. Use on images. Alt text is used for tooltip
 * Could be moved to separate file
 */
(function ($) {
    $.fn.simpletip = function () {
        this.mouseover(function (e) {
            var tip = $(this).attr('alt');
            $(this).attr('alt', '');
            $('body').append('<div id="tooltip"><div class="tipBody">' + tip + '</div></div>');
            $('#tooltip').css('top', e.pageY + 10);
            $('#tooltip').css('left', e.pageX + 20);
            $('#tooltip').fadeIn('500');
            $('#tooltip').fadeTo(0.8);
        }).mousemove(function (e) {
            $('#tooltip').css('top', e.pageY + 10);
            $('#tooltip').css('left', e.pageX + 20);
        }).mouseout(function () {
            $(this).attr('alt', $('.tipBody').html());
            $('#tooltip').remove();
        });
    }
})(jQuery);

/*
 * Simple dialog
 */
(function ($) {
    $.upupDialog = function (oConfig) {
    	if(oConfig === 'close'){
    		$('#dialog_overlay, #dialog_box').hide();
    		return;
    	}
    	
        var sContents	= '<div>' + oConfig.message + '</div>'
        				+ '<div style="text-align:center;padding-top:8px">';
        
        $.each(oConfig.buttons, function(index, value){
        	sContents += '<button id="dialog_button_' + index + '" value="' + index + '">' + index + '</button>';
        	$('#dialog_button_' + index).live('click', value);
        });
        
        sContents += '</div>';
        
        $('#dialog_box div.message').html(sContents);
        $('#dialog_overlay, #dialog_box').show();
	}
})(jQuery);

/*
 * Site Navigation
 */
$(function () {
	var sCurrentPage = window.location.pathname.replace('/', '');
	
	//Main Nav Sign In Link Event Handler
	$('#topsignin').click(function () {
        $('#login').toggle();
        $('#modal_username').focus();
        $(this).parent('li').toggleClass('active');
        return false;
    });
    
    //Feedback Tab - Open It
    $('#feedbackTab.closed').live('click', function() {
    	$('#feedbackWrapper').animate({'height':'+=100'}); //+=280
    	$(this).removeClass('closed').addClass('open');
    	$(this).text('Close');
    });
    
    //Feedback Tab - Close It
    $('#feedbackTab.open').live('click', function() {
    	$('#feedbackWrapper').animate({'height':'-=100'});
    	$(this).removeClass('open').addClass('closed');
    	$(this).text('Feedback');
    });
    
    //Secondary Nav Event Handler
    $("#nav a").click(function (event) {
    	var $oLink = $(this);
    	
    	//Bail if this isn't an internal link - Follow link
    	if( $(this).attr('href').indexOf('#') !== 0 ){ return true; }
    	
    	//Don't follow link and toggle display
    	event.preventDefault();
    	
        $oLi = $(this).parent();
        
        $oLi.addClass('active')
        	.siblings()
        		.removeClass('active');

        $('#content>div').hide();
        $($(this).attr('href')).show();

        $('#heading h2').html($(this).html());
    });
  	
  	//Load Secondary Nav Display
	if(sCurrentPage){
		jQuery('#nav .menu li a[href="#' + sCurrentPage + '"]').trigger('click');
		jQuery('#nav .menu li a[href="/' + sCurrentPage + '"]').parent().addClass('active');
	}else{
		jQuery("#nav a:eq(0)").trigger('click');
	}
});

/*
 * User/Error Messages
 */
$(function () {
	//Modal login attempt failed, show sign in
	if( $('#modal_username').attr('rel') === '1' ){
		$('#topsignin').click();
		$('#modal_password').focus();
	
	//Regular login attempt failed
	}else if( $('#signin_username').length > 0 && $('#signin_username').val().length > 0 ){
		$('#signin_password').focus();
	}
	
	//Show error
	if( $('#errMessage').length > 0 ){
		$('#errMessage').fadeIn();
	};
	
	//Hide error
	$('#errMessage').click(function(){
		$('#errMessage').fadeOut();
	});
});
