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
    $('#tools ul li.share a.top_a').click(function() {
        if ($('#tools .share_wrapper').css('left') == '-8888px') {
            $('#tools .share_wrapper').css('left', 0);
        } else {
            $('#tools .share_wrapper').css('left', '-8888px');
        }
        return false;
    });

	// Dynamically Resize Dropdowns

	var maxWidth = 202;
	$('#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/News/Blog Selector


	if ($('body.is_home').length > 0 ) {
		$('#feedselect .feedcontainer').feedSelect({
		        getNews: $('#select_news').attr('checked'),
		        getBlog: $('#select_blog').attr('checked'),
		        getTwitter: $('#select_twitter').attr('checked'),
		        newsUrl: '/ssi/select_news/index.html',
		        blogUrl: '/ssi/select_blog/index.html',
		        twitterUrl: '/ssi/select_twitter/index.html',
		        newsBlogTemplate : '<div class="listeditem feeditem newsblogitem twocol_left clearfix"><div class="img"><a href="#{story_url}"><img src="#{teaser_image_url}" height="#{teaser_image_height}" width="#{teaser_image_width}" alt="#{title}" title="#{title}" /></a></div><div class="txt"><p class="type">#{feedType}</p><h3 class="title"><a href="#{story_url}">#{title}</a></h3><div class="teaser">#{alt_teaser} <a href="#{story_url}">Read more &#187;</a></div><p class="date">#{cover_date}</p></div></div>',
		        count: 5
		    });
		    $('#select_news, #select_blog, #select_twitter').click(function() {
		        $('#feedselect .feedcontainer').feedSelect({
		            getNews: $('#select_news').attr('checked'),
		            getBlog: $('#select_blog').attr('checked'),
		            getTwitter: $('#select_twitter').attr('checked'),
		            newsUrl: '/ssi/select_news/index.html',
		            blogUrl: '/ssi/select_blog/index.html',
		            twitterUrl: '/ssi/select_twitter/index.html',
		            newsBlogTemplate : '<div class="listeditem feeditem newsblogitem twocol_left clearfix"><div class="img"><a href="#{story_url}"><img src="#{teaser_image_url}" height="#{teaser_image_height}" width="#{teaser_image_width}" alt="#{title}" title="#{title}" /></a></div><div class="txt"><p class="type">#{feedType}</p><h3 class="title"><a href="#{story_url}">#{title}</a></h3><div class="teaser">#{alt_teaser} <a href="#{story_url}">Read more &#187;</a></div><p class="date">#{cover_date}</p></div></div>',
		            count: 5
		        });
		    });
		}


// Homepage Slideshow

$('#features .listeditem').wrapAll('<div id="fade" />');
$('#fade').css('background', '#fff');

var featureCt = $('#fade .listeditem').length;
  $('#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);
      }
    	controlDiv.append('<a href="#" class="ir selector" id="stop">Pause</a>');
      controlDiv.append('<a href="#" class="ir selector" id="start" style="display: none;">Start</a>');
      $(this).append(controlDiv);
  });

 $("#fade").innerfade({
        speed: 2000,
        timeout: 6000,
        containerheight: 303,
        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: 6000,
            containerheight: 303,
            tracker: "switcher",
            trackerclass: "selected"
        });
        var id = $("#fade").data("next");
        $("#s" + id).addClass("selected");
        return false;
    });


	// Sponsors Slideshow

	$('#sponsors .listeditem').wrapAll('<div id="fade_sponsors" />');
	$('#fade_sponsors').css('background', 'transparent');

	var featureCt = $('.home_movement #fade_sponsors .listeditem').length;
	  $('#sponsors').each(function() {
	      var controlDiv = $('<div />').attr('id', 'switcher');
	      for (var i = 1; i <= featureCt; i++) {
	          var control = $('<a href="#" class="selector_sp" id="s' + i + '">' + i + '</a>');
	          if (i == featureCt) control.addClass('last');
	          controlDiv.append(control);
	      }
	      $(this).append(controlDiv);
	  });

		$("#fade_sponsors").innerfade({
	      speed: 2000,
	      timeout: 6000,
	      containerheight: 97,
	      tracker: "switcher",
	      repeat: 1
	  });

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

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



