function checkButtons()
{
    var previous = $('#slide div.active').prev();
    var next = $('#slide div.active').next();

    if(previous.length < 1)
        $('#left').fadeOut();
    else
        $('#left').fadeIn();

    if(next.length < 1)
        $('#right').fadeOut();
    else
        $('#right').fadeIn();


}
function checkHistory()
{
    // Check for a # in the url and activate it:
    var url = window.location.href;
    if(url.lastIndexOf('#') != -1)
    {
        var activeFrame = $('#slide div.active').attr('title');
        var target = url.substring(url.lastIndexOf('#')).substring(1);

        if(target.length > 0 && target != activeFrame && !window.sliding)
        {
            activate($("#slide div[title="+target+"]"));
            return;
        }
    }

    if(firstSlide==true && (typeof target == 'undefined' || target.length == 0 || target == activeFrame)) {
        nextMiniScreen();
        firstSlide = false;
    }

}
function activate(target)
{
    if(target == '')
        return false;

    var target = $(target);

    if(target.length != 0 && !target.hasClass('active'))
    {
        // get it's id for the # string:
        var name = target.attr('title');
        window.sliding = true;

        /* stop any screenshot slideshows */
        $('#slide div .screens ul>li').clearQueue();
        $('#slide div.active .screens ul>li.active').animate({"opacity":dim},1000);

        $('#slide div.active')
            .removeClass('active')
            .addClass('inactive')
            .animate({opacity:dim}, 1000);


        $(target)
            .removeClass('inactive')
            .addClass('active')
            .animate({opacity:active}, 1000);


        checkButtons();

        // Load in the content
        if($('#'+name+' .content').length == 0)
        {
            var page = name=='home' ? 'home' : name;
            $('#'+name).load(page+'.html div.content',function()
            {
                $('#slide div.active').css('background-image','url(img/bg_item.png)');
                $('#slide div.active .content').fadeIn('fast');
            });
        }

        $('#slide').animate({marginLeft: (extraMargin+$(target).prevAll().length*834)*-1},
                          500,
                          function() {
                                      window.location = '#'+name;
                                      window.sliding  = false;

                                      nextMiniScreen();
                          });

        return false;
    }
}


function nextMiniScreen()
{
    if($('#slide div.active .screens ul>li.active').length == 0) {
        $('#slide div.active .screens ul li:first-child').addClass('active');
    }
    if($('#slide div.active .screens ul>li').length <= 1) {
        $('#slide div.active .screens ul>li').animate({"opacity":active},1000);
        return;
    }

    $('#slide div.active .screens ul>li.active').animate({"opacity":active},1000).delay(200).animate({"opacity":0},1000, function() {
                                        var nextScreen = $('#slide div.active .screens ul>li.active').next();
                                        if(nextScreen.length == 0)
                                            nextScreen = $('#slide div.active .screens ul>li:first-child');

                                        $('#slide div.active .screens ul>li').removeClass('active');

                                        nextScreen.animate({"opacity":active},1600,function()
                                        {
                                                           $(this).addClass('active').delay(300).queue(function() {
                                                                                                                      $('#slide div.active .screens ul>li').clearQueue();
                                                                                                                      nextMiniScreen();
                                                                                                                      $(this).dequeue();
                                                                                                                      });
                                        });
                                       });
}


$(document).ready(function()
{
    window.screenPause = 5000;
    dim = 0.2;
    active = 1.0;
    window.sliding  = false;
    firstSlide = true;

    extraMargin = 417;
    if (jQuery.browser.msie && parseInt(jQuery.browser.version)==6) {
        extraMargin = 479;
    }

    $('#slide').children().css('opacity', dim);
    $('.active').css('opacity', active);


    $('#right').click(function(e)
    {
        var target = $('#slide div.active').next();
        if(!window.sliding) activate(target);
        return(false);
    });

    $("a[href^='#']").each(function() {
        var theid = $(this).attr('href').substring(1);

        if ( $("#slide > div[title="+theid+"]").length == 0 ) {
            return;
        }

        $(this).click(function(e)
            {
                var theid = $(this).attr('href').substring(1);

                if(!window.sliding) activate($("#slide > div[title="+theid+"]"));
                return(false);
            });
    });


    $('#left').click(function(e)
    {
        var target = $('#slide div.active').prev();
        if(!window.sliding) activate(target);
        return(false);
    });

    /* hide screenshots */
    $('#slide div .screens ul>li').css({opacity: 0});
    $('#slide div .screens ul>li.active').css({opacity: dim});

    checkHistory();
    setInterval(checkHistory, 200);

    setTimeout(checkButtons,300);
});
