
window.main = function() {};
main.page = function() {};


(function(m, $) {

    
    $.fn.deco_new_window = function(options) {
        return this.each(function() {
           $(this).click(function() {
                window.open($(this).attr('href'));
                return false;
            });
        });
    };

    /**
     * Inline gallery plugin.
     *
     * Expected HTML markup:
     * <outer_element>
     *  <inner_element> [collection of photos (any structure)] </inner_element>
     * </outer_element>
     * 
     * Usage: $('outer_element').deco_gallery({ ... })
     */
    $.fn.deco_gallery = function(options) {
        var opts = $.extend({}, $.fn.deco_gallery.defaults, options);
        var $thumbs = opts.thumbs;
        
        return this.each(function() {
            var $outer = $(this);
            var $photos = opts.photos ? $outer.children(':first').find(opts.photos)
                                      : $outer.children(':first').children();
            init();

            function init() {
                $thumbs.click(function() {
                    var index = $thumbs.index($(this));
                    show_photo(index);
                    return false;
                });

                if (opts.prev_next) setup_prevnext();
            }

            function setup_prevnext() {
                $('<div class="nav prev"><span></span></div>')
                    .appendTo($outer)
                    .hide()
                    .click(function() { show_photo(current_photo() - 1); });
                $('<div class="nav next"><span></span></div>')
                    .appendTo($outer)
                    .click(function() { show_photo(current_photo() + 1); });
            }

            function show_photo(index) {
                var $nav = $outer.children('.nav');
                
                if (index < 0 || index >= $photos.size())
                    return;
                if (index == current_photo())
                    return;
                
                var $next_photo = $photos.eq(index);

                if (opts.prev_next) {
                    if (index == 0)
                        $nav.filter('.prev').hide()
                    else
                        $nav.filter('.prev:not(:visible)').show();
                    if (index == $photos.size()-1)
                        $nav.filter('.next').hide()
                    else
                        $nav.filter('.next:not(:visible)').show();
                }

                $photos.filter(':visible').fadeOut('normal', function() {
                    $photos.eq(index).fadeIn('normal');
                });
                opts.after_change($next_photo);
            }

            function current_photo() {
                return $photos.index($photos.filter(':visible'))
            }
        });
    };
    $.fn.deco_gallery.defaults = {
        photos: '',         // selector for photos if should be something else than all children
        thumbs: $(),        // jQuery collection containing thumbnails
        prev_next: true,    // prev_next functionality disabled/enabled
        after_change: function($nphoto) {} // photo after change callback
    };
    
    /**
     * Dropdown menu plugin
     */
    $.fn.deco_dropmenu = function(options) {
        var opts = $.extend({}, $.fn.deco_dropmenu.defaults, options);

        return this.each(function() {
            var closetimer = 0;
            var menuitem = null;
            var $menu = $(this);

            
            function init() {
                var $action_items = $menu.children('li');
                if (opts.active_stay_expanded)
                    $action_items = $action_items.not('.active');

                $menu.find('a ~ ul').prevAll('a').click(function() { return false; });
                $menu.children('li').hoverIntent({
                    over: jsddm_open,
                    out: jsddm_timer,
                    interval: opts.show_timeout
                });
                
                $(document).click(jsddm_close);

                $menu.find('li').mouseenter(function() {
                    $(this).addClass('hover');
                    opts.item_enter.call(this);
                }).mouseleave(function() {
                    $(this).removeClass('hover');
                    opts.item_leave.call(this);
                });
            }

            function jsddm_open() {
                jsddm_canceltimer();
                jsddm_close(true);

                menuitem = $(this).find('ul');
                if (menuitem.size()) {
                    $menu.trigger('opt_dropmenu.before-show', [menuitem.get(0)]);
                    menuitem.fadeIn('fast');
                }
            }

            function jsddm_close(fadeout) {
                if (menuitem) {
                    if (fadeout) {
                        menuitem.fadeOut('normal', function() {
                            $menu.trigger('opt_dropmenu.after-hide', [menuitem.get(0)]);
                        });
                    } else {
                        menuitem.hide();
                        $menu.trigger('opt_dropmenu.after-hide', [menuitem.get(0)]);
                    }
                }
            }

            function jsddm_timer() {
                closetimer = window.setTimeout(function() { jsddm_close(true); }, opts.hide_timeout);
            }

            function jsddm_canceltimer() {
                if (closetimer) {
                    window.clearTimeout(closetimer);
                    closetimer = null;
                }
            }

            init();
        });
    };
    $.fn.deco_dropmenu.defaults = {
        hide_timeout: 200,
        show_timeout: 40,
        active_stay_expanded: false, // menu mode in which active submenus are permanently expanded
        item_enter: function() {},
        item_leave: function() {}
    };

    

    
    m.$pover = $('#photo_over');
    m.slides_first_run = true;

    m.change_slide_title = function(new_title) {
        var $next = $(this);
        var $invisible = m.$pover.children(':not(:visible)');
        var $visible = m.$pover.children(':visible')

        if (!m.slides_first_run) {
            $invisible.eq(0).text(new_title);
            Cufon.replace($invisible);

            $visible.fadeOut(1000);
            if ($invisible.eq(0).text())
                $invisible.eq(0).fadeIn(1000);
        } else {
            m.slides_first_run = false;
        }
    }

    m.Base = function() {

        $('#menu').deco_dropmenu({
            hide_timeout: 200,
            item_enter: function() { Cufon.replace($(this).children('a'), { letterSpacing: '0.7px' }); },
            item_leave: function() { Cufon.replace($(this).children('a'), { letterSpacing: '0.7px' }); }
        });
        $('#slides').cycle({
            timeout: 5000,
            speed: 1000,
            before: function() { m.change_slide_title($(this).find('em').text()); }
        });

        if ($.browser.webkit) {
            $('body').addClass('webkit');
        }

        $('#footer a.fbook').deco_new_window();
    };

    

    m.Frontpage = function() {
        if (!main.fpage_visited) {
            $('#logo').fadeIn(1000);
            $('#menu').delay(800).fadeIn(900);
            $('#cnt, #footer').delay(1700).fadeIn(1300);
        }
        
    };

    m.Page = function() {
        $('#p > div.content > div').jScrollPane({
            verticalDragMinHeight: 3,
            verticalDragMaxHeight: 3
        });
    };

    m.Gallery = function() {
        m.slides_first_run = false;

        $('#view').deco_gallery({
            thumbs: $('div.content a'),
            after_change: function ($nphoto) { m.change_slide_title($nphoto.find('em').text()); }
        });

        $('#view > .nav').mouseenter(function() {
            $('span', this).stop(true, true).animate({ opacity: 1 }, 250);
        })
        .find('span').css('opacity', '0.3');
        
        $('#view > .nav').mouseleave(function() {
            $('span', this).stop(true, true).animate({ opacity: 0.3 }, 250);
        });

        $('div.content a').mouseenter(function() {
            $('span', this).fadeOut();
        })
        .mouseleave(function() {
            $('span', this).fadeIn();
        });
    };

    m.GalleryOnLoad = function() {
        m.Page();
    };

})(main.page, jQuery);

