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

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


    // share bookmarks on thankyou pages
    $(".thankyoubookmarks").bookmark({
        icons: "/images/bookmarks.png", 
        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>');
    
    // share bookmarks in general
    $(".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>');
    
    // opacity effect for bookmarks
    $(".hasBookmark ul a").css("opacity", ".6");
    $(".hasBookmark ul a").hover(
        function() {
            $(this).css("opacity", "1");
        },
        function() {
            $(this).css("opacity", ".6");
        }
    );

    // handlers for fields in signup stub form
    $("#signup_email, #signup_zip, #search_keywords, #search_keywords2").focus(function() {
        vanilla.swapValue(this, false);
    });
    $("#signup_email, #signup_zip, #search_keywords, #search_keywords2").blur(function() {
        vanilla.swapValue(this, true);
    });

	// rounded corners 
    $('#home_bottom').addClass('boxc boxc5').append(vanilla.roundStr);
	$('#home_bottom .leftcell').addClass('boxc boxc5').append(vanilla.roundStr);
	$('#home_bottom .firstitem').addClass('boxc boxc5').append(vanilla.roundStr);
	$('#home_bottom .lastitem').addClass('boxc boxc5').append(vanilla.roundStr);


  //Homepage Slideshow

	$('#home_features .listeditem').wrapAll('<div id="fade" />');
	$('#fade').css('background', '#000');
	
	var featureCt = $('#fade .listeditem').length;
    $('#home_features').each(function() {
        var controlDiv = $('<div />').attr('id', 'switcher');
        var controlsTable = $('<table cellspacing="0"></table>');
        var controlsTr = $('<tr />');
        for (var i = 1; i <= featureCt; i++) {
            var controlTd = $('<td />');
            var title = $('#fade .listeditem').eq(i - 1).find('img').attr('alt');
            var control = $('<a href="#" class="selector" id="s' + i + '">' + title + '</a>');
            if (i == 1) control.addClass('selected first');
            if (i == featureCt) control.addClass('last');
            controlTd.append(control);
            controlsTr.append(controlTd);
        }
        controlsTable.append(controlsTr);
        controlDiv.append(controlsTable);
        $(this).append(controlDiv);
    });

	$("#fade").innerfade({
        speed: 2000,
        timeout: 10000,
        containerheight: 259,
        tracker: "switcher",
        trackerclass: "selected",
        repeat: 1
    });
    $(".selector").click(function() {
        if($("#fade").data("timer")) {
            clearTimeout($("#fade").data("timer"));
            $("#fade").removeData("timer");
        }

        var id = Number(this.id.substr(1));
        $(".selector").removeClass("selected");
        $(this).addClass("selected");
        $("#fade>div:visible").fadeOut(2000);
        $("#fade>div").eq(id - 1).fadeIn(2000);
        return false;
    });
  
	// Sidebar slideshow

	$('#features .listeditem').wrapAll('<div id="sidebar_features" />');

  $("#sidebar_features").innerfade({
        speed: 2000,
        timeout: 10000,
        containerheight: 199,
        tracker: "switcher",
        trackerclass: "selected",
        repeat: 1
    });
	

});

// 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);
});


