// All javascript copyright SteadyMade™ 2008.


window.addEvent('domready', function() {

    if ($('mask')) {

        var projects = $$('#works li');
        var counter = $('project');
        var total = $('total');
        var currentIndex = projects.startItem || 0;
        var oldIndex = null;

        var scroll = new Fx.Scroll('mask', { wait: false, duration: 433, fps: 300, offset: {'x': -130, 'y': 0}, transition: 'quint:out' });

        projects.set('morph', { duration: 777, transition: 'quint:out' });
        var nextEffect = new Fx.Morph('next', { wait: false, duration: 333, transition: 'quint:out' });
        var prevEffect = new Fx.Morph('previous', { wait: false, duration: 333, transition: 'quint:out' });

        total.set('text', projects.length);

        function activate(element, oldElement) {
            scroll.toElement(projects[element]);
            counter.set('text', currentIndex + 1);
            projects[element].morph('.active');
            projects[oldElement].morph('.normal');
        }

        activate(0, projects.length-1);

        function next() {
            currentIndex += currentIndex < projects.length - 1 ? 1 : 1 - projects.length;
            oldIndex = currentIndex + (currentIndex > 0 ? -1 : projects.length - 1);
            activate(currentIndex, oldIndex);
            nextEffect.start('.blink').chain(function(){ nextEffect.start('.back') });
        }

        function previous() {
            currentIndex += currentIndex > 0 ? - 1 : projects.length - 1;
            oldIndex = currentIndex + (currentIndex < projects.length - 1 ? 1 : 1 - projects.length);
            activate(currentIndex, oldIndex);
            prevEffect.start('.blink').chain(function(){ prevEffect.start('.back') });
        }

        $('next').addEvent('click', function() { next() });
        $('previous').addEvent('click', function() { previous() });


        document.addEvent('keydown', function(event) {
            if (event.key == 'right'){ next(); }
            if (event.key == 'left'){ previous(); }
        });

    }

    if ($('myForm')) {
        var myFormValidation = new Validate('myForm',{
            errorClass: 'error'
        });
    }

});