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
    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>');
    
    // 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");
        }
    );
    
    // remove border under last blog post
    $('.blogitem:last').css('border', 'none');
    
    // equalize size of boxes on /technology
    var tbHeight = 0;
    $('.techbox').not('.techbox_right').each(function() {
        if ($(this).height() > tbHeight) tbHeight = $(this).height();
    });
    $('.techbox_right').not('.techbox_double').each(function() {
        var ht = $(this).height() - 59;
        if (ht > tbHeight) tbHeight = ht;
    });
    $('.techbox_double').each(function() {
        var ht = $(this).height() / 2 - 65;
        if (ht > tbHeight) tbHeight = ht;
    });
    $('.techbox').css('height', tbHeight);
    $('.techbox_right').css('height', tbHeight + 59);
    $('.techbox_double').css('height', 2 * tbHeight + 130);
    
    // make whole text box clickable
    $('.techbox').not('.techbox_right').click(function() {
        location.href = $(this).find('p.readmore a').attr('href');
    });
    $('.techbox').not('.techbox_right').css('cursor', 'pointer');
    
    // design feature controls
    var dl = $('#design_features #designlink');
    $('#design_features h3.title').append('<br />' + dl.html());
    dl.remove();
    $('#design_features h3.title a').not('.clientlink').click(function() { return false; });
    $('#design_features #switcher').show();
    $('#design_features').append('<p class="clicktozoom ir">Click the image above to enlarge</p>');
    
    $("#faders").innerfade({
        speed: 2000,
        timeout: 10000,
        containerheight: 302,
        tracker: "switcher",
        trackerclass: "selected",
        repeat: 1
    });
    $(".selector").click(function() {
        if($("#faders").data("timer")) {
            clearTimeout($("#faders").data("timer"));
            $("#faders").removeData("timer");
        }
    
        var id = Number(this.id.substr(1));
        $(".selector").removeClass("selected");
        $(this).addClass("selected");
        $("#faders>div:visible").fadeOut(2000);
        $("#faders>div").eq(id - 1).fadeIn(2000);
        return false;
    });
    
    // fancybox design images
    $('#faders .img a').fancybox({
        overlayColor: '#000',
        overlayOpacity: .8,
        enableEscapeButton: true,
        imageScale: true
    });

    
    // design subfeatures
    $('#design_subfeatures a').attr('href', '#').click(function() {
        clearTimeout($('#faders').data('timer'));
        var url = $(this).closest('.leadin').find('span.jsdata_include').attr('title');
        $('#design_features').load(url, function() {
            $('html, body').animate({scrollTop: 0}, 'fast');
            var dl = $('#design_features #designlink');
            $('#design_features h3.title').append('<br />' + dl.html());
            dl.remove();
            $('#design_features h3.title a').not('.clientlink').click(function() { return false; });
            $('#design_features #switcher').show();
            $('#design_features').append(vanilla.roundStr);
            $('#design_features').append('<p class="clicktozoom ir">Click the image above to enlarge</p>');
            
            $("#faders").innerfade({
                speed: 2000,
                timeout: 10000,
                containerheight: 302,
                tracker: "switcher",
                trackerclass: "selected",
                repeat: 1,
                reset: 1
            });
            $(".selector").click(function() {
                if($("#faders").data("timer")) {
                    clearTimeout($("#faders").data("timer"));
                    $("#faders").removeData("timer");
                }
            
                var id = Number(this.id.substr(1));
                $(".selector").removeClass("selected");
                $(this).addClass("selected");
                $("#faders>div:visible").fadeOut(2000);
                $("#faders>div").eq(id - 1).fadeIn(2000);
                return false;
            });
            
            // fancybox design images
            $('#faders .img a').fancybox({
                overlayColor: '#000',
                overlayOpacity: .8,
                enableEscapeButton: true,
                imageScale: true
            });
            
            // set newwindow links
            $("a.newwindow").css("padding-right", "18px").css("background", "url(/images/newwindow.png) no-repeat right").attr('target', '_blank').attr('title', 'Opens in a new window');

        });
        return false;
    });
    
    // tabs on homepage
    $('#home_tabs_form ul a#strategy_tab').mouseover(function() {
        $('#home_tabs_content>div').hide();
        $('#home_tabs_form').css('background-position', '0 0');
        $('#home_strategy').show();
    });
    $('#home_tabs_form ul a#design_tab').mouseover(function() {
        $('#home_tabs_content>div').hide();
        $('#home_tabs_form').css('background-position', '-206px 0');
        $('#home_design').show();
    });
    $('#home_tabs_form ul a#technology_tab').mouseover(function() {
        $('#home_tabs_content>div').hide();
        $('#home_tabs_form').css('background-position', '-412px 0');
        $('#home_technology').show();
    });
    $('#home_tabs_form ul a').click(function() { return false; });
    
    // make individual homepage tech boxes clickable
    var techHtml = '<ul><li><a class="ir odd" href="/technology/arcos/cms/">Content Management System</a></li><li><a class="ir even" href="/technology/arcos/member_management/">Member Management</a></li><li><a class="ir odd" href="/technology/arcos/advocacy/">Advocacy</a></li><li><a class="ir even" href="/technology/arcos/email_marketing/">Email Marketing</a></li><li><a class="ir odd" href="/technology/arcos/fundraising/">Fundraising and Donations</a></li><li><a class="ir even" href="/technology/arcos/reporting/">Reporting and Analytics</a></li><li><a class="ir odd" href="/technology/arcos/organizing/">Community Organzing Tools</a></li><li><a class="ir even" href="/technology/arcos/web_tools/">Web 2.0 Tools</a></li></ul>';
    var imgSrc = $('#home_technology .img img').attr('src');
    $('#home_technology .img a').hide();
    $('#home_technology .img').css('background', 'url(' + imgSrc + ') no-repeat');
    $('#home_technology .img').append(techHtml);
    
    if ($.userAgent.browserVersionX == 'explorer6') {
        // $('body.is_home #demo_submit').attr('src', '/images/btn_demo_submit.gif');
    }
    
    
    // munge dropdown menus
    var maxWidth = 225;
    $('#topnav ul ul').css('width', 'auto');
    $('#topnav li li a').css('width', 'auto').css('white-space', 'nowrap');
    $('#topnav > ul > li').each(function(i) {
        // if (i > 2) return false;
        $this = $(this);
        var widest = $this.width() - 22; // ul border and a padding
        
        $this.find('ul li a').each(function() {
            var w = $(this).width();
            if (w > widest) widest = w;
        });
        
        if (widest > maxWidth) {
            widest = maxWidth;
            $this.find('ul li a').css('white-space', 'normal');
        }
        $this.find('ul').css('width', widest + 20); // border is the extra +2
        $this.find('ul li a').css('width', widest);
    });

    
});

// 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/vanilla.cf_data.count).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);
});


