
/*overstate*/
$(document).ready(function() {

    var window_width = $(window).width();

    var offset = 590 - ((window_width - 512) / 2);

    $('#content-container').scrollTo({ left: (670 * 3) + offset, top: 0 });


    /*opacity*/

    var pages = $("#top-level").children();

    $(pages).css({ opacity: 0.2 });
    $('.current').css({ opacity: 1.0 });

    var current = 3;

    $(window).resize(function() {
        var window_width = $(window).width();
        offset = 590 - ((window_width - 512) / 2);
        $('#content-container').scrollTo({ top: 0, left: (670 * current) + offset }, 1);
    });

    $(".logo img").click(function() {
        $('#content-container').scrollTo({ left: (670 * 3) + offset, top: 0 }, 1000);
        $(pages[current]).animate({ opacity: 0.2 }, 1000);
        $(this).fadeOut(500);
        $(pages[3]).stop().animate({ opacity: 1.0 }, 1000);
        current = 3;
        return false;
    });

    $('.nav').each(function() {

        $(this).find("a").each(function(i) {
            if (i > 2)
                i++;

            $(this).click(function() {
                if (i == current)
                    return false;
                $('#content-container').scrollTo({ top: 0, left: (670 * i) + offset }, 1000);
                $(pages[current]).animate({ opacity: 0.2 }, 1000);
                $(pages[i]).animate({ opacity: 1.0 }, 1000);
                $(".logo img").fadeIn('50');
                current = i;
                return false;
            });

        });
    });

    $("#top-level>div").each(function(i) {
        $(this).click(function() {
            if (i != current) {


                $('#content-container').scrollTo({ top: 0, left: (670 * (i)) + offset }, 1000);
                $(pages[current]).animate({ opacity: 0.2 }, 1000);
                $(pages[i]).animate({ opacity: 1.0 }, 1000);
                if (i != 3)
                    $(".logo img").fadeIn('50');
                else
                    $(".logo img").fadeOut('500');

                current = i;

                setCurrentNavByKeys(current, pages);

                return false;
            }
        });
    });

    $('.worksnav').each(function() {

        $(this).find("a").each(function(i) {

            $(this).click(function() {
                if ((i + 4) == current)
                    return false;
                $('#content-container').scrollTo({ top: 0, left: (670 * (i + 4)) + offset }, 1000);
                $(pages[current]).animate({ opacity: 0.2 }, 1000);
                $(pages[i + 4]).animate({ opacity: 1.0 }, 1000);
                $(".logo img").fadeIn('50');
                current = i + 4;
                return false;
            });

        });
    });
    
    
    

    $(document).keydown(function(event) {
        //37 left, 39 right
        if (event.keyCode == 37 || event.charCode == 37) {
            if (current != 0) {
                $('#content-container').scrollTo({ top: 0, left: (670 * (current - 1)) + offset }, 1000);
                $(pages[current]).animate({ opacity: 0.2 }, 1000);
                $(pages[current]).find(".logo img").fadeOut('50')
                $(pages[current - 1]).animate({ opacity: 1.0 }, 1000);
                $(pages[current - 1]).find(".logo img").fadeIn('50');
                current = current - 1;
            }
            setCurrentNavByKeys(current, pages);
            return false;
        }
        else if (event.keyCode == 39 || event.charCode == 37) {
            if (current != (pages.length - 1)) {
                $('#content-container').scrollTo({ top: 0, left: (670 * (current + 1)) + offset }, 1000);
                $(pages[current]).animate({ opacity: 0.2 }, 1000);
                $(pages[current]).find(".logo img").fadeOut('50')
                $(pages[current + 1]).animate({ opacity: 1.0 }, 1000);
                $(pages[current + 1]).find(".logo img").fadeIn('50');
                current = current + 1;
            }
            setCurrentNavByKeys(current, pages);
            return false;
        }
    });


    function setCurrentNavByKeys(current, pages) {
        $('.nav').each(function() {

            $(this).find("a").each(function(i) {
                $(this).removeClass("current");
                if (current != 3) {
                    $(".logo img").fadeIn(50);
                    if (current > 3) {
                        if (i == current - 1) {
                            $(this).addClass("current");
                        }
                        if (current > 3 && current < (pages.length - 2)) {
                            $("#worksnav").addClass("current");
                        }
                    }
                    else {
                        if (i == current) {
                            $(this).addClass("current");
                        }
                    }
                }
                else {
                    $(".logo img").fadeOut(50);
                }
            });
        });
    }
    
    
    var urlParts = document.URL.split("#");

    if (urlParts.length > 1) {
        var fragment = urlParts[1];
        if (fragment.indexOf('work') != -1) {
            try
            {
                var newpage = parseInt(fragment.replace('work', '')) + 3;
                                                
                 $('#content-container').scrollTo({ top: 0, left: (670 * (newpage)) + offset }, 1000);
                $(pages[3]).animate({ opacity: 0.2 }, 1000);
                $(pages[3]).find(".logo img").fadeOut('50')
                $(pages[newpage]).animate({ opacity: 1.0 }, 1000);
                $(pages[newpage]).find(".logo img").fadeIn('50');
                current = newpage;
            }
            catch (ex) 
            {
                alert('whoops');
             }
        }
    }
    
    $('.mobile-images').cycle({
        fx: 'fade',
        timeout: 4000
    });

});

 




function stopSlideshow(imageID, workID) 
{
    
    var $movieDiv = $('#divMov' + imageID);
    if ($movieDiv.length != 0) {
        $('.slideshow' + workID).cycle('pause');
        $('.slideshow' + workID).hide();
        $movieDiv.show();
    }
    $('#mov' + imageID).show();
}

function startSlideshow(workID) {
    $('.slideshow' + workID).cycle('resume');
    $('.slideshow' + workID).show();
    $("[id^='divMov']").hide();
    $("[id^='mov']").hide();
}

function removeCurrentLink() 
{
    $('.nav a').each(function() {
        $(this).removeClass("current");
    });
}
