$(document).ready(function () {

    /* Variables */
    var $article = $('article.notVisible');
    var $hideButton = $('article .toggleContent');
    var $shortSpeed = 300;
    var $longSpeed = 800;
    var $slideRel = $('.simpleSlide-window').attr('rel');
    var $slideIntervalFunction = "simpleSlideAction('.right-button', '" + $slideRel + "');";
    var $slideIntervalTime = 8000;
    var $buttonsClicked = false;
    var $slideIntervalID;

    var $times = $('article.drawer aside');

    function startSlider() {
        if ($('.simpleSlide-window article.drawer').length > 1) {
            $slideIntervalID = setInterval($slideIntervalFunction, $slideIntervalTime)
        }
    }

    /* Scroll function */
    $(function () { $('a[rel=page]').click(function () { if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) { var $target = $(this.hash); $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']'); if ($target.length) { var targetOffset = $target.offset().top; $('html,body').animate({ scrollTop: targetOffset }, 500); return false; } } }); });

    /* Useful classes */
    $('ul.root > li').hover(
        function () { $(this).addClass('hover'); },
        function () { $(this).removeClass('hover'); });

    $('li:first-child').addClass('first');
    $('li:last-child').addClass('last');

    //make external sites open up in a new window
    $('a[rel="external"], a[rel="file"]').each(function () {
        var $title = $(this).attr('title');

        $(this).attr({
            target: "_blank",
            title: $title + " (opens in a new window)"
        });
    });

    /* Plot Tabs Setup */
    $(".tabContent").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tabContent:first").show(); //Show first tab content

    /* Plot Tabs On Click */
    $("ul.tabs li a").click(function () {
        var activeTab = $(this).attr("href");

        $("ul.tabs li").removeClass("active");
        $(this).closest("li").addClass("active");
        $(".tabContent").css('display', 'none');

        $(activeTab).css('display', 'block');
        return false;
    });

    /* Slide and Drawer setup */
    $('.simpleSlide-window').append('<div class=\"slider-control\" rel=\"' + $slideRel + '\"><div class=\"left-button bg\" rel=\"' + $slideRel + '\">&lt; Prev</div><div class=\"status-tray\" rel=\"' + $slideRel + '\"><div class=\"status-window\" rel=\"' + $slideRel + '\"></div></div><div class=\"right-button bg\" rel=\"' + $slideRel + '\">Next &gt;</div></div>');
    $('article.drawer').addClass('notVisible').append('<div class=\"toggleContent bg\">Hide</div>');
    $('article.drawer').append('<div class=\"grip bg\"></div>');
    $times.hide(0);

    /* Slide Initialization */
    simpleSlide({
        'set_speed': 900,
        'status_width': 10,
        'status_height': 10, // this has been coded into the plugin - didn't exist;
        'status_color_outside': 'transparent',
        'status_color_inside': 'transparent',
        'fullscreen': 'false',
        'swipe': 'false'
    });

    //Display Slides
    $('.simpleSlide-window').css('display', 'block')

    //Set the slideshow running
    $('.simpleSlide-window').each(function () {
        startSlider();
    });

    // Mouseover Grab
    $article.live('mouseover', (function () {
        clearInterval($slideIntervalID);
        $(this).animate({ right: '-=40' }, $shortSpeed, 'swing', (
            function () {
                $(this).removeClass('notVisible').addClass('visible');
                $(this).children('.grip').remove();
            }
            ));
        $(this).animate({ right: '0' }, $longSpeed, 'swing', (
            function () {
                if ($(this).children('aside').length) {
                    $(this).children('aside').animate({ left: '+=810' }, ($longSpeed) * 2);
                }
            }
        ));
        //console.log('Panel Opened');
    }));

    // Click Grab - for touch devices
    $article.live('click', (function () {
        clearInterval($slideIntervalID);
        $(this).animate({ right: '-=40' }, $shortSpeed, 'swing', (
            function () {
                $(this).removeClass('notVisible').addClass('visible');
                $(this).children('.grip').remove();
                }
            ));
        $(this).animate({ right: '0' }, $longSpeed, 'swing', (
            function () {
                if ($(this).children('aside').length) {
                    $(this).children('aside').animate({ left: '+=810' }, ($longSpeed) * 2);
                }
            }
        ));
        //console.log('Panel Opened');
    }));

    // Click Hide
    $hideButton.live('click', (function () {
        if ($(this).siblings('aside').length) {
            $(this).siblings('aside').animate({ left: '-=810' }, $longSpeed);
        }

        $(this).parent().animate({ right: '-=360' }, $longSpeed, 'swing', (
            function () {
                $(this).removeClass('visible').addClass('notVisible');
                $(this).append('<div class=\"grip bg\"></div>');
                }
            ));
        $(this).parent().animate({ right: '+=40' }, $shortSpeed, 'swing');

        if (!$buttonsClicked) {
            //console.log('this is false');
            startSlider();
        }
    }));

    // Click Buttons
    $('.right-button, .left-button').live('click', function () {
        clearInterval($slideIntervalID);
        $buttonsClicked = true;
        //console.log('Left/Right Clicked');
    });
});


