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

    //open external urls in new window
    $('a[href^=http]').each(function () {
        if (this.href.indexOf('cfed.org') == -1) {
            $(this).attr('target', '_blank');
        }
        if (this.href.indexOf('scorecard.cfed.org') > 0) {
             $(this).attr('target', '_blank');
        }
    });

    // 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: ["facebook", "twitthis"]
    });
    $(".thankyoubookmarks").prepend('<p><strong>Share this with your friends:</strong></p>');

    // share bookmarks in general
    $(".bookmarks").bookmark({
        icons: "/images/bookmarks.png",
        sites: ["facebook", "twitthis"]
    });
    $(".bookmarks ul").prepend('<li class="share">Share &#160;</li>');


    // handlers for fields in search and signup stub form
    $("#email_address, #zip_code, #search_keywords").focus(function () {
        vanilla.swapValue(this, false);
    });
    $("#email_address, #zip_code, #search_keywords").blur(function () {
        vanilla.swapValue(this, true);
    });

   // hide preview bars
    $('.preview_notice a.hide_preview').click(function() {
    $('.preview_notice').hide();
    });



    //precheck subscription list boxes on certain subscribe form
    $("body.signup.subscribe_form input:checkbox[name^='subscription_list']").each(function () {
        if ($('#messages').length == 0) {
            this.checked = true;
        }
    });

    $("body.join_the_assets_opportunity_campaign.subscribe_form input:checkbox[name^='subscription_list']").each(function () {
        if ($('#messages').length == 0) {
            this.checked = true;
        }
    });

    // tabs for scorecard state data
    $("#statedata ul#tabs").tabs("#statedata #tabcontent > div");

    // sorting for scorecard data tables
    $("table.sortable").tablesorter({
        cssAsc: 'asc',
        cssDesc: 'desc',
        sortList: [
            [0, 0]
        ]
    });



    // rotate thru a set of divs -- this ties into innerfade.js
    // edit to change default options
    $('#fade').innerfade({
        speed: 2000,
        timeout: 8000,
        containerheight: 303,
        tracker: 'switcher',
        trackerclass: 'selected'
    });

    $('#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: 8000,
            containerheight: 303,
            tracker: 'switcher',
            trackerclass: 'selected'
        });
        var id = $('#fade').data('next');
        $('#' + id).addClass('selected');
        return false;
    });

    $('.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);
        $('.selector').removeClass('selected');
        $(this).addClass('selected');
        $('#fade li:visible').fadeOut(2000);
        $('#feature_' + id).fadeIn(2000);
        var num = $('#fade').data('num_elements');
        if (id < num) {
            $('#fade').data('next', id);
        } else {
            $('#fade').data('next', 0);
        }
        return false;
    });

/* =========================================================

			innerfade.js

			Date: 2008-04-29
			Author: Plus Three LLC (http://plusthree.com/)

			Based heavily on jquery.innerfade.js by Torsten Baldes 
			(http://medienfreunde.com), which in turn is based on the work of 
			Matt Oakes (http://portfolio.gizone.co.uk/applications/slideshow/) 
			and Ralf S. Engelschall (http://trainofthoughts.org/).

			Changes include limiting rotation to sequential/fade, and enabling 
			a control menu.

			// ========================================================= */

    (function ($) {

        $.fn.innerfade = function (options) {
            return this.each(function () {
                $.innerfade(this, options);
            });
        };

        $.innerfade = function (container, options) {
            var settings = {
                'speed': 2000,
                'timeout': 2000,
                'containerheight': 'auto',
                'tracker': null,
                'trackerclass': null
            };
            if (options) {
                $.extend(settings, options);
            }

            var elements = $(container).children();
            $(container).data('num_elements', elements.length);

            if (elements.length > 1) {

                // set css on parent and elements
                $(container).css('position', 'relative').css('height', settings.containerheight);
                for (var i = 0; i < elements.length; i++) {
                    $(elements[i]).css('z-index', String(elements.length - i)).css('position', 'absolute').hide();
                };

                var next, current;
                if ($(container).data('next') == null) { // first run
                    next = 1;
                    current = 0;
                    $(elements[0]).show();

                    var timer = setTimeout(function () {
                        $.innerfade.next(container, elements, settings, next, current);
                    }, settings.timeout);
                    $(container).data('timer', timer);
                    $(container).data('next', next);
                } else {
                    next = $(container).data('next');
                    if (next == 0) {
                        current = elements.length - 1;
                    } else {
                        current = next - 1;
                    }
                    $.innerfade.next(container, elements, settings, next, current);
                }
            }
        };

        $.innerfade.next = function (container, elements, settings, next, current) {
            $(elements[current]).fadeOut(settings.speed);
            $(elements[next]).fadeIn(settings.speed, function () {
                if (settings.trackerclass) {
                    $('#' + settings.tracker + ' a').removeClass(settings.trackerclass);
                    $('#' + (current + 1)).addClass(settings.trackerclass);
                }
            });

            // caluculate for next run
            if ((next + 1) < elements.length) {
                next = next + 1;
                current = next - 1;
            } else {
                next = 0;
                current = elements.length - 1;
            }

            var timer = setTimeout((function () {
                $.innerfade.next(container, elements, settings, next, current);
            }), settings.timeout);

            $(container).data('timer', timer);
            $(container).data('next', next);
        };

        $("#switcher a#1").append($("#fade li#feature_1").attr("title"));
        $("#switcher a#2").append($("#fade li#feature_2").attr("title"));
        $("#switcher a#3").append($("#fade li#feature_3").attr("title"));
        $("#switcher a#4").append($("#fade li#feature_4").attr("title"));

    })(jQuery);

    var AUTH_COOKIE_NAME = 'auth_tkt';
    var PERSISTENT_COOKIE_NAME = 'ARCOS_PERSISTENT_LOGIN';


    // zebra stripes
    $("table.sortable tr:nth-child(even)").addClass("striped");
    
    // precheck checkboxes
    $('body.admf_itemized_form .formrow_admf_support_legislation input').each(function() {
        if ($('ul#messages').length == 0) {
            $(this).attr('checked', true);
        }
    });

});
