jQuery.preloadImages = function() { for(var i = 0; i<arguments.length; i++) { jQuery("<img>").attr("src", arguments[i]); } }

$(document).ready(function() {
    setupJumpToPickers();
    configureMasterPicker();

    // flash widget checkbox config
    $('#nolargeflashlink').bind("click", function() {
        if($('.large_widget:hidden').size() > 0) {
            $('.large_widget:hidden').each(function() {
                $(this).flash({ src: "http://www.ronpaul2008.com/features/countdown-to-win/countdown-to-win.swf?v=2",
                              width: 650, height: 300, wmode: 'transparent' }, { version: 8, wmode: 'transparent' });
                $(this).show();
            });
            $.cookie('hide_large_flash', 'false', { expires: 365 });
        } else {
            $('.large_widget:visible').each(function() {
                $(this).hide();
                $(this).html("");
            });
            $.cookie('hide_large_flash', 'true', { expires: 365 });
        }
    });
    if($.cookie('hide_large_flash') == 'false') {
        $('#nolargeflashlink').attr("checked", true);
        $('.large_widget').flash({src: "http://www.ronpaul2008.com/features/countdown-to-win/countdown-to-win.swf?v=2",
                            width: 650, height: 300, wmode: 'transparent' }, { version: 8});
    } else {
        $('.large_widget').hide();
    }

    // internal links
    $('span.internal').bind("mouseover", function() {
        $(this).addClass("internal-over");
        $(this).removeClass("internal");
    });
    $('span.internal').bind("mouseout", function() {
        $(this).addClass("internal");
        $(this).removeClass("internal-over");
    });

    $('span.internal').each(function() {
        if(location.href.indexOf($(this).find('a').attr("href")) > -1 ||
                (location.href.charAt(location.href.length-1) == '/' &&
                 $(this).find('a').attr("href") == 'index.html')) {
            $(this).addClass("internal-selected");
        }
    });

    // image viewer setup
    if ($.ImageBox) {
        $.ImageBox.init({
            loaderSRC: 'images/loading.gif',
            closeHTML: '<img src="images/close.jpg" />'
        });
    }

/*
    $('#fancyimages').bind("click", function() {
        if($(this).is(':checked')) {
            $('a.slot-thumbnail').each(function() {
                var hr = $(this).attr('href');
                if (hr.indexOf(".html") > -1) {
                    $(this).attr('href', hr.substring(0, hr.indexOf(".html")) + ".png");
                }
            });
            if ($.ImageBox) {
                $('a.slot-thumbnail').bind('click', $.ImageBox.start);
            }
            $.cookie('fancy_images', 'true', { expires: 365 });
        } else {
            $('a.slot-thumbnail').each(function() {
                var hr = $(this).attr('href');
                if (hr.indexOf(".png") > -1) {
                    $(this).attr('href', hr.substring(0, hr.indexOf(".png")) + ".html");
                }
            });
            if ($.ImageBox) {
                $('a.slot-thumbnail').unbind('click', $.ImageBox.start);
            }
            $.cookie('fancy_images', 'false', { expires: 365 });
        }
    });
*/

    if (!$.cookie('slot_config')) {
        $('.highlightFeature').css("visibility", "visible");
        $('.highlightFeature').fadeOut(2500);
    }

    $('#slotconfig').bind("click", function() {
        if($(this).is(':checked')) {
            $('.slot').each(function() { configureSlotPicker($(this)); });
        } else {
            $('.slot .graph_select').remove();
        }
    });
});

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function getParameter(queryString, parameterName) {
    parameterName = parameterName + "=";
    if (queryString.length > 0) {
        var begin = queryString.indexOf(parameterName);
        if (begin != -1) {
            begin += parameterName.length;
            var theend = queryString.indexOf("&", begin);
            if (theend == -1) {
                theend = queryString.length
            }
            return decodeURIComponent(queryString.substring(begin, theend));
        }
    }
}
function setupJumpToPickers() {
    var jumpToNode = $("#jump_to");
    if(jumpToNode.length > 0) {
        jumpToNode.load("jump_to.html #jump_to_select > option, #jump_to_select optgroup", function() {
            $("#jump_to option[value='"+$('#shortName').val()+"']").attr("selected", true);
            $('#jump_to').bind("change", function(e) {
                var target = e.currentTarget ? e.currentTarget : e.srcElement;
                var selected = $(':selected', target);
                location.href = selected.val() + ".html"
            });
        });
    }
}

function configureMasterPicker() {
    $('#master_chart_list').unbind("change");
    $('#master_chart_list').bind("change", function(e) {
        var target = e.currentTarget ? e.currentTarget : e.srcElement;
        var selected = $(':selected', target);
        // figure out which slot we are in.
        var slotId = $(this).parents('.slot').attr("id");

        // start the new cookie value off with this new slot setting.
        var newCookie = slotId + "=" + selected.val() + "&";

        // find any old slots that are not this one and add the to the cookie.
        var oldCookie = "";
        if($.cookie('slot_config')) oldCookie = $.cookie('slot_config');
        var pairs = oldCookie.split('&');
        for(var pair in pairs) {
            if (pairs[pair].length > 3) {
                var slotNum = pairs[pair].split("=")[0];
                if (slotId != slotNum) {
                    newCookie = newCookie + slotNum + "=" + pairs[pair].split("=")[1] + "&";
                }
            }
        }

        // set the cookie and do the slot setup.
        $.cookie('slot_config', newCookie, { expires: 365 });
        setupSlots();
    });
}

function configureSlotPicker(slot) {
    // if this slot does not already have a select box, add one.
    if($('.graph_select', slot).size() == 0) {
        // clone the master, including its events.
        var theClone = $('#master_chart_list').clone(true);
        var graph = slot.attr('chart');
        theClone.attr("id", "chart_" + graph);
        theClone.css("display", "inline");
        slot.prepend(theClone);
        $('.graph_select option[value=' + graph + ']', slot).attr("selected", "true");
    }
}
