jQuery(document).ready(function($) {

    // fade out error message bg color
    $("#messages, #comment_errors, #comment_msgs").animate({opacity: 1.0}, 3000).animate({backgroundColor: '#ffffff'}, 3000);

    // handlers for fields in stub forms
    $("#signup_box_email, #signup_box_zip, #search_box_keywords").focus(function() {
        vanilla.swapValue(this, false);
    });
    $("#signup_box_email, #signup_box_zip, #search_box_keywords").blur(function() {
        vanilla.swapValue(this, true);
    });
    
    // stub forms in sidebar
    $("#heroes_form #id_subject").focus(function() {
        vanilla.swapValue(this, false, "Your Hero's Name");
    });
    $("#heroes_form #id_subject").blur(function() {
        vanilla.swapValue(this, true, "Your Hero's Name");
    });
    $("#heroes_form #id_message").focus(function() {
        vanilla.swapValue(this, false, "Why This Person Is Your Hero");
    });
    $("#heroes_form #id_message").blur(function() {
        vanilla.swapValue(this, true, "Why This Person Is Your Hero");
    });
    
    $("#myths_form #id_message").focus(function() {
        vanilla.swapValue(this, false, "Your Myth About Gay Marriage");
    });
    $("#myths_form #id_message").blur(function() {
        vanilla.swapValue(this, true, "Your Myth About Gay Marriage");
    });
    
    $("#video_form #id_message").focus(function() {
        vanilla.swapValue(this, false, "Your Video About Gay Marriage");
    });
    $("#video_form #id_message").blur(function() {
        vanilla.swapValue(this, true, "Your Video About Gay Marriage");
    });
    
    $("#events_form #id_message").focus(function() {
        vanilla.swapValue(this, false, "Your Upcoming Community Event");
    });
    $("#events_form #id_message").blur(function() {
        vanilla.swapValue(this, true, "Your Upcoming Community Event");
    });
    
    $("#march_form #id_message").focus(function() {
        vanilla.swapValue(this, false, "Your Opinion on the Next March on Washington");
    });
    $("#march_form #id_message").blur(function() {
        vanilla.swapValue(this, true, "Your Opinion on the Next March on Washington");
    });
    
    $("#quote_form #id_message").focus(function() {
        vanilla.swapValue(this, false, "A Celebrity Quote You Think is Insightful or Inspiring");
    });
    $("#quote_form #id_message").blur(function() {
        vanilla.swapValue(this, true, "A Celebrity Quote You Think is Insightful or Inspiring");
    });
    
    // always the same
    $(".stubform #stub_email").focus(function() {
        vanilla.swapValue(this, false, "Your Email Address");
    });
    $(".stubform #stub_email").blur(function() {
        vanilla.swapValue(this, true, "Your Email Address");
    });
    
    // open rssbox links in new window
    $(".rssboxitem a").each(function() {
        $(this).click(vanilla.openInNewWindow);
    });

    // rotate thru a set of divs -- this ties into innerfade.js
    $('#fade').innerfade({
        speed: 2000,
        timeout: 8000,
        containerheight: 300,
        tracker: 'switcher',
        trackerclass: 'selected'
    });

    $('#stop').click(function() {
        clearTimeout($('#fade').data('timer'));
        $('#fade').removeData('timer');
        $('#stop').hide();
        $('#start').show();
        return false;
    });


    $('#start').click(function() {
        $('#start').hide();
        $('#stop').show();
        $('.selector').removeClass('selected');
        $('#fade').innerfade({
            speed: 2000,
            timeout: 8000,
            containerheight: 300,
            tracker: 'switcher',
            trackerclass: 'selected'
        });
        var id = $('#fade').data('next');
        $('#' + id).addClass('selected');
        return false;
    });

    $('.selector').click(function() {
        if (this.id == 'stop' || this.id == 'start') {
            return;
        }

        if($('#fade').data('timer')) {
            clearTimeout($('#fade').data('timer'));
            $('#fade').removeData('timer');
            $('#stop').hide();
            $('#start').show();
        }

        var id = Number(this.id);
        $('.selector').removeClass('selected');
        $(this).addClass('selected');
        $('#fade li:visible').fadeOut(2000);
        $('#feature_' + id).fadeIn(2000);
        var num = $('#fade').data('num_elements');
        if (id < num) {
            $('#fade').data('next', id);
        } else {
            $('#fade').data('next', 0);
        }
        return false;
    });
    
    // add social bookmarks
    $("#bookmarks").bookmark({icons: '/images/bookmarks.png', sites: ['delicious', 'digg', 'facebook', 'fark', 'google', 'kaboodle', 'mixx', 'propeller', 'reddit', 'stumbleupon', 'technorati', 'twitthis', 'yahoobuzz']});
    $("#bookmarks ul").prepend('<li class="share">Share&#160;</li>');

    $("#bookmarks2").bookmark({icons: '/images/bookmarks26.png', iconSize: 26, sites: ['yahoobuzz', 'delicious', 'digg', 'facebook', 'stumbleupon']});
    $("#bookmarks2 ul").prepend('<li class="share">Share&#160; &#160;</li>');
    
    vanilla.baseUrl = location.href.split('?', 1);
    $(".thankyoubookmarks").bookmark({
        icons: "/images/bookmarks.png",
        url: vanilla.baseUrl,
        sites: ["delicious", "digg", "facebook", "fark", "google", "kaboodle", "mixx", "propeller", "reddit", "stumbleupon", "technorati", "twitthis", "yahoobuzz"]
    });
    $(".thankyoubookmarks").prepend('<p><strong>Share this with your friends:</strong></p>');


    // handler for checkbox on contribute pages
    $("#id_send_tribute").click( function() {
        if ($("#id_send_tribute").attr("checked")) {
            $("#acknowledgement").show();
        } else {
            $("#acknowledgement").hide();
        }
    });
    
    $('#gstabs').tabs('#gspanes > div');
    
    // make the height of the login box constant
    var loginPanesHeight = 0;
    $('#gspanes > div').each(function() {
        var isHidden = $(this).is(':hidden');
        if (isHidden) $(this).show();
        if ($(this).height() > loginPanesHeight) {
            loginPanesHeight = $(this).height();
        }
        if (isHidden) $(this).hide();
    });
    $('#gspanes').each(function() {
        $(this).css('min-height', loginPanesHeight + 4);
        if ($.browser.msie && $.browser.version.substr(0,1) == 6) {
            $(this).css('height', loginPanesHeight + 4);
        }
    });
    
    $('#tstabs').tabs('#tspanes > div');
    
    // make the height of the login box constant
    loginPanesHeight = 0;
    $('#tspanes > div').each(function() {
        var isHidden = $(this).is(':hidden');
        if (isHidden) $(this).show();
        if ($(this).height() > loginPanesHeight) {
            loginPanesHeight = $(this).height();
        }
        if (isHidden) $(this).hide();
    });
    $('#tspanes').each(function() {
        $(this).css('min-height', loginPanesHeight + 4);
        if ($.browser.msie && $.browser.version.substr(0,1) == 6) {
            $(this).css('height', loginPanesHeight + 4);
        }
    });

});

// CF thermometer
vanilla.addLoadEvent(function() {
    if (typeof vanilla.cf_data == 'undefined') {
        return;
    }
    
    // we delay this a bit so the user will see the entire effect
    setTimeout(function() {
        jQuery('#cf_progress #goal').html('$' + vanilla.format_number(vanilla.cf_data.goal));
        jQuery('#cf_progress #count').html(vanilla.cf_data.count + ((vanilla.cf_data.count == 1) ? ' donor' : ' donors'));
        jQuery('#cf_progress #average').html('$' + vanilla.format_number((vanilla.cf_data.raised/20).toFixed(2)));

        jQuery("#cf_progress").progressBar({
            value    : Math.round(vanilla.cf_data.raised * 100 / vanilla.cf_data.goal),
            height   : 190,
            width    : 32,
            callback : function(config) {
                var raised = 0;
                if (config.value > 0) {
                    raised = vanilla.format_number((vanilla.cf_data.raised * (config.runningValue / config.value)).toFixed(2));
                    raised = raised.replace('.00', '');
                }
                jQuery('#cf_progress #raised').html('$' + raised);
                
                if (config.runningValue == config.max) {
                    jQuery('#cf_progress').addClass('goal_reached');
                    jQuery('#cf_progress #scale').hide();
                }
                if (config.runningValue > config.max) {
                    jQuery('#cf_progress').addClass('goal_exceeded');
                    jQuery('#cf_progress #scale').hide();
                }
            }
        });
    }, 500);
});
