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

    // change the main local bg image
    var bg = vanilla.cookie.get('odpbg');
    if (!bg) {
        bg = Math.floor(Math.random() * 4) + 1;
        vanilla.cookie.set('odpbg', bg, .75, '/', '.ohiodems.org');
    }
    if (($('body.contribution_form').length > 0) || ($('body.pac_contribution_form').length > 0)) {
        $('#localbg').css('background-image', 'url(/images/bg_contrib_localbg' + bg + '.jpg)');
    } else {
        $('#localbg').css('background-image', 'url(/images/bg_localbg' + bg + '.jpg)');
    }

    // 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").animate({opacity: .6}, 0);
    $(".hasBookmark ul a").hover(
        function() {
            $(this).animate({opacity: 1}, 0);
        },
        function() {
            $(this).animate({opacity: .6}, 0);
        }
    );
    
    // open news clipping links in new window    
    $("body.news_clipping p.source a, body.news_clipping p.articlelink a").each(function() {
        $(this).click(vanilla.openInNewWindow);
    });
    
    // rounded corners on temphome
    $("body.temphome #middle").append('<b class="thtop"></b><b class="thbottom"></b>');
    
    // rounded corners on action center
    $('#action_top h2, #action_bottom h2, #home_bottom h2').addClass('boxc boxc5').append(vanilla.roundStr);
    
    // RSS links on hompage
    $('#home_bottom #home_news h2').prepend('<a href="/news/rss/index.xml" class="rss ir">RSS</a>');
    $('#home_bottom #home_blog h2').prepend('<a href="/blog/rss/index.xml" class="rss ir">RSS</a>');

    /* fancybox homepage video removed EAF 1/23/10 
    $('a#playvideo').fancybox({
        hideOnContentClick: false,
        overlayColor: '#000',
        overlayOpacity: .8,
        enableEscapeButton: true,
        callbackOnShow: function() {
            $('div.video object').css('visibility', 'visible');
        }
    });
    */
    
    // homepage feature slideshow
    $('#home_features .listeditem').wrapAll('<div id="fade" />');

    // design feature controls
    var featureCt = $('#fade .listeditem').length;
    if (featureCt > 1) {
        $('#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: 1000,
        timeout: 5000,
        containerheight: 270,
        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;
    });
    
    // 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) {
        $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);
    });
    
    // twitter feed
    $('#sidebar .feed').twitterFeed({
        queryType: 'user',
        query: 'OHDems',
        template: '<div class="txt"><span class="msg">#{text}</span><br /><span class="footer"><span class="date">#{createdAt}</span> (<a  href="http://twitter.com/home?status=%40#{screenName}%20&in_reply_to_status_id=#{id}&in_reply_to=#{screenName}">Reply</a>) (<a  href="http://twitter.com/home?status=RT%20%40#{screenName}%20#{encodedText}&in_reply_to_status_id=#{id}&in_reply_to=#{screenName}">RT</a>)</span></div>',
        refresh: 0,
        count: 2,
        fadeSpeed: 0,
        timeFormat: 'local'
    });

    // change a video link on homepage (5/12/10)
    $('#home_news a').each(function() {
        if ($(this).attr('href').match(/\/news\/video\/portmanshirt\/$/)) {
            $(this).attr('href', '/news/video/?item=item-5DC98226-5C5C-11DF-9596-9FEF647D51AE');
        }
    });

});

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


