var Page = {

    initialize: function()
    {
        this.navigation();
        this.forms();
    },
    
    
    navigation: function()
    {
        if ($('navigation')) {
            $$('#navigation li').each(
                function(el, i) {
                    var moving = false;
                    var oldStyle = {
                        top: el.getStyle('top'),
                        left: el.getStyle('left') 
                    };

                    Event.observe(el, 'mouseover', function() { 
                        new Effect.MoveBy(el, 0, 10, {duration: 0.1, transition: Effect.Transitions.sinoidal,
                            beforeSetup: function() {
                              el.undoPositioned().setStyle(oldStyle);  
                            }
                        });
                    });

                    Event.observe(el, 'mouseout', function() { 
                        new Effect.MoveBy(el, 0, -10, {duration: 0.03, transition: Effect.Transitions.sinoidal,
                            afterFinish: function(element) {
                                el.undoPositioned().setStyle(oldStyle);
                            }
                        });
                    });
                }
            );
        }   
    },
    
    
    forms: function()
    {

        if ($('search_form')) {         
            Event.observe($('search_form'), 'submit', 
                function(e) {
                    if ($('search_form').elements[0].value == '')
                        Event.stop(e);
                }.bindAsEventListener()
            );
            
            
            /*
            new Insertion.Bottom($('search_form'), '<div id="search_result"></div>');
           
            new Form.Element.Observer('query', 1.0, 
                function(element, value) {
                    new Ajax.Updater('search_result', '/search', {
                        asynchronous:true, 
                        evalScripts:true, 
                        parameters:'query=' + value})
                })
            */
        }
        
        if ($('convert_form')) {
            // preload the spinner
            var preload = new Image(); 
            preload.src = 'images/spinner.gif';
            
            Event.observe($('convert_form'), 'submit', 
                function(e) {
                    if ($('convert_form').elements[0].value == '') {
                        Event.stop(e);
                    } else {
                        Event.stop(e);
                        
                        if (!$('convert_spinner')) {
                            el = $$('#convert_form p');
                            new Insertion.Bottom(el[0], '<img src="images/spinner.gif" width="16" height="16" alt="Loading" id="convert_spinner" />');
                        } else {
                            Element.show('convert_spinner');
                        }
                        
                        var params = Form.serialize($('convert_form'));
                        var myAjax = new Ajax.Request($('convert_form').action, {
                            method: 'post', 
                            parameters: params,
                            onComplete: function() {
                                Element.hide('convert_spinner');
                            }
                        });                            
                    }
                }.bindAsEventListener()
            );
        }
    }
};


Event.observe(window, 'load', function() { Page.initialize(); });
