Emphimy = {
    menu: [],
    currentPage: 0,
    scroll: 0,
    buffer: [],
    semaphore: false,

    init: function (){
        Emphimy.setUpFancybox();
        Emphimy.getMenu();
        Emphimy.setUpMenu();
        Emphimy.setUpScrolls();
        Emphimy.setupContact();
        
        Emphimy.loadMenuItem( Emphimy.menu[0].label );
    },

    initBuffer: function (){
        $.get( Emphimy.menu[0].href.substr(0, Emphimy.menu[0].href.length -3) + "html", {}, function ( data ){
            Emphimy.refreshBuffer( data );
        });
    },

    getMenu: function (){
        $("div#menu ul li a").each(function (index, item){
            Emphimy.menu.push({
                label: $.trim($(item).html()),
                href: $(item).attr("href")
            });
        });
    },

    setUpMenu: function (){
        $("div#menu ul li").click(function ( event ){
            event.preventDefault();
            event.stopPropagation();

            var target = event.target;
            if( event.target.tagName.toLowerCase() == "li" )
                target = event.target.firstChild;

            Emphimy.loadMenuItem( $.trim( $(target).html() ) );
        });
    },

    getMenuItem: function ( m ){
        for( var i=0; i < Emphimy.menu.length; i++ ){
            if( Emphimy.menu[i].label == m )
                return Emphimy.menu[i];
        }
    },

    loadMenuItem: function ( menu ){
        menuItem = Emphimy.getMenuItem( menu );
        Emphimy.currentPage = Emphimy.menu.indexOf(menuItem);
        $.get( menuItem.href.substr(0, menuItem.href.length -3) + "html", {}, function ( data ){
            Emphimy.refreshBuffer( data );
            
            if( $("div#worksContent a").length > 0 ){
                $("div#worksContent a").fadeOut( 700, function (){
                    Emphimy.showContents( Emphimy.scroll );
                    $("div#worksContent a").fadeIn( 700 );
                    Emphimy.setUpFancybox();
                });
            }else{
                Emphimy.showContents( Emphimy.scroll );
                $("div#worksContent a").fadeIn( 700 );
                Emphimy.setUpFancybox();
            }
        });
        $("h2#contentTitle").html( menu );
    },

    showContents: function ( scroll ){
        var tmp = $("<div>").get(0);
        for( var i = scroll; i < Emphimy.buffer.length /* && i < (scroll + 3) */; i++){
            $(tmp).html( $(tmp).html() + Emphimy.buffer[i] );
        }
        $(tmp).children().css("display", "none");
        $("div#worksContent").html( $(tmp).html() );
    },

    refreshBuffer: function ( rawHTML ){
        Emphimy.buffer = [];
        var tmp = $("<div>").html( rawHTML ).get(0);
        $(tmp).children().each( function ( index, item ){
            Emphimy.buffer[index] = $('<div>').append( item ).html();
        });
        Emphimy.scroll = 0;
    },

    setUpScrolls: function (){
        $( "div#verticalControls span" ).each(function (index, item){
            if( index == 0 )
                $(item).click( Emphimy.scrollUp );
            else
                $(item).click( Emphimy.scrollDown );
        });

        $( "div#horizontalControls span" ).each(function (index, item){
            if( index == 0 )
                $(item).click( Emphimy.scrollRight );
            else
                $(item).click( Emphimy.scrollLeft );
        });
    },

    scrollUp: function ( event ){
        event.stopPropagation()

        if( Emphimy.scroll == 0 || Emphimy.semaphore )
            return false;
        
        Emphimy.semaphore = true;
        Emphimy.scroll = Emphimy.scroll - 3 > 0 ? Emphimy.scroll - 3 : 0;
        var current = $("div#worksContent a").css("top").substr(0, $("div#worksContent a").css("top").length - 2);
        var newValue = parseInt(current) + 560;
        $("div#worksContent a").animate({ 
            top: newValue + "px"
        }, 1500, function (){
            Emphimy.semaphore = false;
        });
    },

    scrollDown: function ( event ){
        event.stopPropagation()

        if( Emphimy.scroll + 3 >= Emphimy.buffer.length || Emphimy.semaphore )
            return false;

        Emphimy.semaphore = true;
        Emphimy.scroll += 3;
        var current = $("div#worksContent a").css("top").substr(0, $("div#worksContent a").css("top").length - 2);
        var newValue = parseInt(current) - 560;
        $("div#worksContent a").animate({ 
            top: newValue + "px"
        }, 1500 , function (){
            Emphimy.semaphore = false;
        });
    },

    scrollLeft: function (){
        Emphimy.loadMenuItem( Emphimy.menu[(Emphimy.currentPage + 1 % Emphimy.menu.length)].label );
    },

    scrollRight: function (){
        Emphimy.loadMenuItem( Emphimy.menu[(Emphimy.currentPage + 1 % Emphimy.menu.length)].label );    
    },

    setUpFancybox: function (){
        $("div#worksContent a").fancybox({
            'overlayShow': true,
            'overlayOpacity': 0.6
        });
    },
    
    setupContact: function (){
        $("form#contactForm").submit(function ( event ){
            event.preventDefault();
            event.stopPropagation();
            
            var name = $("#contactName")[0].value;
            var mail = $("#email")[0].value;
            var message = $("#message")[0].value;
            
            $.post( 
                "contact/", 
                { name: name, mail: mail, message: message }, 
                function ( data, textStatus ){
                    $("#contactForm button")[0].parentNode.innerHTML = "thanks, your mail has been sent";
                },
                "text"
            );
        });
    }
}

$(window).bind( 'load', Emphimy.init );