/* Run function only when main lib is loaded */
Event.observe(document, 'core:loaded', function()
{
    // run only if rot13 is defined
    if (typeof rot13 == "function")
    {
        // process scrobled emails
        $$('a.email-scrambled').each(function(el)
        {
            if (el.readAttribute('rel'))
            {
                el.innerHTML = rot13(el.readAttribute('rel'));
                el.writeAttribute('href', 'mailto:' + el.innerHTML);
            }
        });

        $$('a.image-scrambled').each(function(el)
        {
            if (el.readAttribute('rel'))
            {
                el.writeAttribute('href', 'mailto:' + rot13(el.readAttribute('rel')));
            }
        });
    }

    // disable links with action
    $$('a.nolink').invoke('observe', 'click', function(e)
    {
        Event.stop(e);
    });

    // {{{ do dropdown menu
    if (Prototype.Browser.IE)
    {
        // get menu button
        var button = $('menubar').down('ul.ia_dropdown li.about a');

        // get menu boundries
        var buttonBoundries =
            {
                x1: button.cumulativeOffset()[0],
                y1: button.cumulativeOffset()[1],
                x2: button.cumulativeOffset()[0]*1 + button.getDimensions().width*1, 
                y2: button.cumulativeOffset()[1]*1 + button.getDimensions().height*1
            }

        // get submenu
        var submenu = button.up().down('ul');

        // get submenu boundries
        var submenuBoundries =
            {
                x1: submenu.cumulativeOffset()[0],
                y1: submenu.cumulativeOffset()[1],
                x2: submenu.cumulativeOffset()[0]*1 + submenu.getDimensions().width*1, 
                y2: submenu.cumulativeOffset()[1]*1 + submenu.getDimensions().height*1
            }

        // make sub-menu invisible
        submenu.hide();

        // Ugly IE hack
        document.observe('mouseover', function(e)
        {

            // get pointer position
            var pointer =
                {
                    x: e.pointerX(),
                    y: e.pointerY()
                };

            // show sub menu
            if (pointer.x > buttonBoundries.x1
                && pointer.x < buttonBoundries.x2
                && pointer.y > buttonBoundries.y1
                && pointer.y < buttonBoundries.y2
                )
            {
                button.addClassName('active');
                submenu.show();
            }
            else if (
                (pointer.x > submenuBoundries.x1
                    && pointer.x < submenuBoundries.x2
                    && pointer.y > submenuBoundries.y1
                    && pointer.y < submenuBoundries.y2
                    )
                ||
                (pointer.x > buttonBoundries.x1
                    && pointer.x < buttonBoundries.x2
                    && pointer.y > buttonBoundries.y1
                    && pointer.y < submenuBoundries.y2
                    )
                )
            {
                // do nothing
            }
            else
            {
                button.removeClassName('active');
                submenu.hide();
            }
        });

    }
    else // normal browsers
    {
        $$('ul.ia_dropdown').each(function(el)
        {
            // should be LI elememnts
            el.childElements().each(function(child)
            {
                // if there is a submenu
                var submenu = child.down('ul');
    
                if(submenu)
                {
                    // make sub-menu invisible
                    submenu.hide();
    
                    child.observe('mouseover', function(e)
                    {
                        this.addClassName('active');
                        submenu.show();
    
                    }.bind(child));
    
                    child.observe('mouseout', function(e)
                    {
                        this.removeClassName('active');
                        submenu.hide();
                    }.bind(child));
    
                }
            });
        });
    }
    // }}}

    // add some live stuff to the gallery images
    $$('ul.gallery li, ul.posters li').each(function(el)
    {
        el.insert({bottom: new Element('span',
                    {
                        'class': 'overlay'
                    })
            });

        el.down('a').writeAttribute('rel', 'lightbox[gallery]');

        el.observe('mouseover', function()
        {
            this.addClassName('hover');
        }.bind(el));
        el.observe('mouseout', function()
        {
            this.removeClassName('hover');
        }.bind(el));
    });


    // {{{ accordion
    if (!NO_FEATURES && $('accordion_container'))
    {
        var Accordion = new accordion('accordion_container');

        // open first by default
        Accordion.activate($$('#accordion_container .accordion_toggle')[0]);
    }
    // }}}

    // {{{ show time - Wed Oct 21 5:00:11 pm
    if ($('clock'))
    {
        updateClock = function()
        {
            $('clock').update(dateFormat(new Date(), 'ddd, mmm dS h:MM:ss TT'));

            updateClock.delay(0.5);
        }

        updateClock();
    }
    // }}}

    // {{{ process links' rel attributes - replacement for deprecated A[target]
    $$('a[rel]').each(function(el)
    {
        var rel = el.readAttribute('rel');

        switch (rel)
        {
            case 'pdf': // we have no backend support for forced download, so open pdfs in new window
                el.observe('click', function(e)
                {
                    Event.stop(e);

                    var href = this.readAttribute('href');

                    if (href)
                    {
                        window.open(href, '_blank');
                    }
                }.bind(el));
                break;

            default: // something unsupported
                // do nothing yet
        }
    });

    // }}}
    

    // {{{ add some cosmetics to HTML

    // popin descriptions
    $$('.accordion_content>ul li').each(function(el)
    {
        el.observe('mouseover', function(e)
        {
            this.addClassName('hover');
        }.bind(el));

        el.observe('mouseout', function(e)
        {
            this.removeClassName('hover');
        }.bind(el));
    });

    // }}}

    // {{{ Add some beauty to the form fields
    $$('input.text, textarea.textarea').each(function(el)
    {
        if (el.isEmpty())
        {
            el.setText(el.title);
            el.empty(true);
        }

        el.observe('focus', function(e)
        {
            var el = Event.element(e);

            if (el.empty())
            {
                el.setText('');
            }

            el.active(true);
            el.empty(false);
        });

        el.observe('blur', function(e)
        {
            var el = Event.element(e);

            if (el.isEmpty())
            {
                el.setText(el.title);
                el.empty(true);
            }

            el.active(false);
        });
    });
    // }}}

    // {{{ Add interactivity to the map
    var panel;

    if ($('contacts_map_panel'))
    {
        panel = $('contacts_map_panel');
    }

    // add some control logic
    if ($('contacts_map_address'))
    {

        $('contacts_map_address').up('form').observe('submit', function(e)
        {
            e.stop();
        });

        $('contacts_map_address').observe('keyup', function(e)
        {
            if (e.keyCode == Event.KEY_RETURN || e.keyCode == Event.KEY_TAB)
            {
                googleMapShowRoute(contactMap, this.value, panel);
            }
        });
    }

    $$('a.map.controller').each(function(el)
    {
        el.observe('click', function(e)
        {
            Event.stop(e);

            var el = Event.element(e);

            if (el.readAttribute('rel'))
            {
                googleMapShowRoute(contactMap, el.readAttribute('rel'), panel);
            }

        });
    });
    // }}}


/* gift cards */


    if ($('gift_form_delivery') && $('checkbox_wrapped'))
    {
        // custom event
        $('gift_form_delivery').observe('ia:change', function()
        {
            if ($F(this) == 'Mail')
            {
                $('checkbox_wrapped').up().show();
                $$('.recpt_addr').each(function($x) { $x.show()});
            }
            else
            {
                $('checkbox_wrapped').checked = false;
                $('checkbox_wrapped').up().hide();
                $$('.recpt_addr').each(function($x) { $x.hide()});

            }
        }.bind($('gift_form_delivery'))
        );

        // built-in event as wrapper
        $('gift_form_delivery').observe('change', function()
        {
            this.fire('ia:change');

        }.bind($('gift_form_delivery'))
        );
        
        $('gift_form_delivery').fire('ia:change');
    }

    if ($('gift_form_amount') && $('gift_form_delivery'))
    {
        $('gift_form_amount').observe('change', function()
        {
            if ($F(this) >= 150)
            {
                $('gift_form_delivery').selectedIndex = 0;
                $('gift_form_delivery').options[0].removeAttribute('disabled')

                $('gift_form_delivery').fire('ia:change');
            }
            else
            {
                $('gift_form_delivery').selectedIndex = 1;
                $('gift_form_delivery').options[0].writeAttribute('disabled', 'disabled')

                $('gift_form_delivery').fire('ia:change');
            }

        }.bind($('gift_form_amount'))
        );
    }

// done with main load
// let others to do their job

    Event.fire(document, 'core:main');


});

