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
    if (location.href.indexOf('?') == -1) {
        vanilla.baseUrl = location.href;
    } else {
        vanilla.baseUrl = location.href.substring(0, location.href.indexOf('?'));
    }
    $(".thankyoubookmarks").bookmark({
        icons: "/images/bookmarks.png",
        url: vanilla.baseUrl,
        sites: ["delicious", "digg", "fark", "facebook", "google", "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", "fark", "google", "mixx", "propeller", "reddit", "stumbleupon", "technorati", "yahoobuzz"]
    });
    
    // share bookmarks popup
    $('.pt-wrapper li.pt-share a.pt-top-a').click(function() {
        if ($('.pt-wrapper .pt-share-wrapper').css('left') == '-8888px') {
            $('.pt-wrapper .pt-share-wrapper').css('left', 0);
        } else {
            $('.pt-wrapper .pt-share-wrapper').css('left', '-8888px');
        }
        return false;
    });

    // 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);
        }
    );
    
    // sidebar twitter feed
    $('#sidebar .feed').twitterFeed({
        queryType: 'user',
        query: 'CharlotteIn2012',
        template: '<div class="txt"><span class="msg">#{text}</span><br /><span class="footer"><span class="date">#{createdAt}</span> (<a  href="http://twitter.com/intent/tweet?in_reply_to=#{id}">Reply</a>) (<a  href="http://twitter.com/intent/retweet?tweet_id=#{id}">RT</a>)</span></div>',
        refresh: 0,
        count: 3,
        fadeSpeed: 0,
        timeFormat: 'local'
    });
    
    // homepage slideshow
    $('#home_features .listeditem').wrapAll('<div id="fade" />');
    $('#fade').css('background', '#000');
    
    var featureCt = $('#fade .listeditem').length;
    if (featureCt > 1) {
        $('#home_features').each(function() {
            var controlDiv = $('<div />').attr('id', 'switcher');
            for (var i = 1; i <= featureCt; i++) {
                var control = $('<a href="#" class="selector" id="s' + i + '">' + i + '</a>');
                if (i == 1) control.addClass('selected first');
                if (i == featureCt) control.addClass('last');
                controlDiv.append(control);
            }
            $(this).append(controlDiv);
            controlDiv.append('<a href="#" class="selector" id="stop">||</a>');
            controlDiv.append('<a href="#" class="selector" id="start" style="display: none;">&#9654;</a>');
        });
    }
    $("#fade").innerfade({
        speed: 2000,
        timeout: 10000,
        containerheight: 304,
        tracker: "switcher",
        trackerclass: "selected",
        repeat: 1
    });    
    $(".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.substr(1));
        $(".selector").removeClass("selected");
        $(this).addClass("selected");
        $("#fade>div:visible").fadeOut(2000);
        $("#fade>div.item" + id).fadeIn(2000);
        var num = $("#fade").data("num_elements");
        if (id < num) {
            $("#fade").data("next", id);
        } else {
            $("#fade").data("next", 0);
        }
        return false;
    });
    $("#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: 10000,
            containerheight: 304,
            tracker: "switcher",
            trackerclass: "selected"
        });
        var id = $("#fade").data("next");
        $("#s" + id).addClass("selected");
        return false;
    });

    /* feed selector */
    
    if ($('#feedselect').length > 0 ) {
        $('#feedselect .feedcontainer').feedSelect({
            getNews: $('#select_news').prop('checked'),
            getBlog: $('#select_blog').prop('checked'),
            getTwitter: $('#select_twitter').prop('checked'),
            newsUrl: '/ssi/select_news/index.html',
            blogUrl: '/ssi/select_blog/index.html',
            twitterUrl: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=charlottein2012',
            twitterAvatar: '/images/fs_twitter_default.jpg',
            newsAvatar: '/images/fs_news_default.jpg',
            blogAvatar: '/images/fs_blog_default.jpg',
            avatarWidth: 60,
            avatarHeight: 60,
            count: 3
        });
        $('#select_news, #select_blog, #select_twitter').click(function() {
            $('#feedselect .feedcontainer').feedSelect({
                getNews: $('#select_news').prop('checked'),
                getBlog: $('#select_blog').prop('checked'),
                getTwitter: $('#select_twitter').prop('checked'),
                newsUrl: '/ssi/select_news/index.html',
                blogUrl: '/ssi/select_blog/index.html',
                twitterUrl: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=charlottein2012',
                twitterAvatar: '/images/fs_twitter_default.jpg',
                newsAvatar: '/images/fs_news_default.jpg',
                blogAvatar: '/images/fs_blog_default.jpg',
                avatarWidth: 60,
                avatarHeight: 60,
                count: 3
            });
        });
    }
    
    // home tabs need some extra magic
    $('#home_bottom #home_tabs ul.tabs li.tab1').click(function() {
        $(this).parent().css('background-position', '0 0');
    });
    $('#home_bottom #home_tabs ul.tabs li.tab2').click(function() {
        $(this).parent().css('background-position', '0 -38px');
    });
    $('#home_bottom #home_tabs ul.tabs li.tab3').click(function() {
        $(this).parent().css('background-position', '0 -76px');
    });
    
    // home tabs need some extra magic
    $('#home_bottom #home_tabs ul.tabs-1 li.tab1').click(function() {
        return false;
    });
    $('#home_bottom #home_tabs ul.tabs-2 li.tab2').click(function() {
        $(this).parent().css('background-position', '0 0');
    });
    $('#home_bottom #home_tabs ul.tabs-2 li.tab3').click(function() {
        $(this).parent().css('background-position', '0 -38px');
    });
    
    // countdown
    var dncStart = new Date(Date.parse('9/3/2012'));
    var now = new Date();
    var daysUntil = Math.round((dncStart.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
    $('#days-until').text(daysUntil);
        

});

// 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'));
        if (vanilla.cf_data.count == 0) {
            jQuery('#cf_progress #average').html('$0');
        } else {
            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);
});



