$(document).ready(function() {

    generateShuffledList();
    // Change the image immediately
    changePlug();
    // Set timer to change picture every x milliseconds
    var imageSwapperTimer = setInterval("changePlug()", 3000);

    $("#featureCarousel").featureCarousel({
        // put all options here
        largeFeatureWidth:880,
        largeFeatureHeight:288,
        smallFeatureWidth:680,
        smallFeatureHeight:230,
        topPadding:10,
        smallFeatureOffset:50,
        autoPlay:8000,
        preload:false,
        counterStyle:1,
        startingFeature:1,
        displayCutoff:8,
        carouselSpeed: 350,
        animationEasing: 'swing'
    });

    // When someone clicks a link on the top navigation bar, toggle the div down
    // and prevent the link from firing (if JS is disabled, it will follow the link
    // as it should)
    $("#navigation a").click(function (event) {
        event.preventDefault();
        var div_id = $(this).attr('id').substring(5);
        toggleDiv(div_id);
        return false;
    });

    // When the close link is clicked within the navigation div's. It will toggle the div
    // But if JS is disabled it will just follow the link as it should
    $(".close a").live("click", function (event) {
        event.preventDefault();
        var div_id = $(this).attr('id').substring(6);
        toggleDiv(div_id);
        return false;
    });

});

var $shuffledList;
var shuffleCounter = 0;
function generateShuffledList() {
    var $plugs = $('#allPlugs').children('a');

    // Shuffle the list of all plugs and save in
    for (var n = 0; n < $plugs.length; n++) {
        // pick random item to move to end of array
        var random = Math.floor(Math.random() * $plugs.length);
        // Swap the two items
        var $temp = $plugs[n];
        $plugs[n] = $plugs[random];
        $plugs[random] = $temp;
    }

    $shuffledList = $plugs;
}

function changePlug() {
    // Get the new plug info
    var $newPlug = $shuffledList.eq(shuffleCounter);
    var newLink = $newPlug.attr('href');
    var newImage = $newPlug.find('img').attr('src');

    // Replace current plug info
    var $currentPlug = $('#currentPlug').children('a');
    $currentPlug.attr('href',newLink);
    $currentPlug.find('img').attr('src',newImage);

    // Wrap around plug index if reached end
    if (shuffleCounter == $shuffledList.length - 1) {
        shuffleCounter = -1;
    }

    shuffleCounter++;
}

function toggleDiv(div_id) {
    var exturl = 'home/categories/'+div_id+'.html';
    var $theDivToDisplay = $('#'+div_id);
    var $otherDivs = $('#categories').children('div').not($theDivToDisplay);

    if ($theDivToDisplay.css('display') == 'none') {
        $otherDivs.slideUp(function () {
            $theDivToDisplay.load(exturl, function () {
                $(this).slideDown();
            });
        });
    } else {
        $theDivToDisplay.slideUp();
    }
}

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
            anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}
window.onload = externalLinks;


