var EmailJS = new JS.Class({
    initialize: function() {
        Event.observe(window, 'load', this._initialize.bind(this));
    },
    _initialize: function() {
        // Get all the email links
        var emails = $$('a[class="secure_email"]');
        emails.each(function(email) {
            if(email.readAttribute('email_set') != 'true') {
                // Get the spans
                var spans = email.select('span[class!=hide_me]');
                if(spans.length == 2) {
                    email.writeAttribute('href', 'mailto:' + spans[0].innerHTML + '@' + spans[1].innerHTML);
                    email.writeAttribute('email_set', 'true');
                }
            }
        });
    }
});

var NavigationJS = new JS.Class({
    timeout: null,
    wrapper: null,
    
    initialize: function(wrapper) {
        Event.observe(window, 'load', this._initialize.bind(this, wrapper));
    },
    _initialize: function(wrapper) {
        this.wrapper = $(wrapper);
        
        // Monitor all of the links
        this.wrapper.select('a').each(function(anchor) {
            Event.observe(anchor, 'mouseover', this.ShowSubNavAnchor.bind(this, anchor));
            Event.observe(anchor, 'mouseout', this.HideSubNavAnchor.bind(this, anchor));
        }.bind(this));
        
        // Monitor all the sub navs
        this.wrapper.select('ul').each(function(list) {
            Event.observe(list, 'mouseout', this.HideSubNavList.bind(this, list));
        }.bind(this));
    },
    
    HideSubNavAnchor: function(anchor) {
        // Get the sub nav associated with this anchor
        var sub_nav = $('sub_navigation_' + anchor.readAttribute('alias'));
        if(sub_nav) {
            if(this.timeout != null) {
                window.clearTimeout(this.timeout);
            }
            
            this.timeout = this._HideSubNavList.bind(this, sub_nav).delay(0.5);
        }
    },
    
    HideSubNavList: function(list) {
        if(this.timeout != null) {
            window.clearTimeout(this.timeout);
        }
        
        this.timeout = this._HideSubNavList.bind(this, list).delay(0.5);
    },
    _HideSubNavList: function(sub_nav) {
        // Hide all sub navs of this sub nav
        var depth = sub_nav.readAttribute('depth')/1 + 1;
        for(; depth < 10; ++depth) {
            var sub_navs = this.wrapper.select('ul[depth=' + depth + ']');
            sub_navs.each(function(sub_nav) {
                sub_nav.hide();
            });
        }

        sub_nav.hide();
        this.timeout = null;
    },
    
    ShowSubNavAnchor: function(anchor) {
        if(this.timeout != null) {
            window.clearTimeout(this.timeout);
            this.timeout = null;
        }

        // Get the sub nav associated with this anchor
        var sub_nav = $('sub_navigation_' + anchor.readAttribute('alias'));
        
        if(sub_nav) {
            this.ShowSubNavList(sub_nav);
        } else {
            // Hide all sub navs at the same depth
            var depth = anchor.readAttribute('depth');
            for(; depth < 10; ++depth) {
                var sub_navs = this.wrapper.select('ul[depth=' + depth + ']');
                sub_navs.each(function(item) {
                    item.hide();
                });
            }
        }
    },
    
    ShowSubNavList: function(sub_nav) {
        if(this.timeout != null) {
            window.clearTimeout(this.timeout);
            this.timeout = null;
        }
        
        // Get all sub navs in the same level
        var sub_navs = this.wrapper.select('ul[depth=' + sub_nav.readAttribute('depth') + ']');
        sub_navs.each(function(to_show, sub_nav) {
            if(to_show.identify() == sub_nav.identify()) {
                if(to_show.readAttribute('depth') < 2) {
                    var parent = this.wrapper.select('a[alias=' + to_show.identify().sub('sub_navigation_', '') + ']');
                    var offset = parent[0].cumulativeOffset();
                    to_show.setStyle({
                        left: offset.left + 'px',
                        top: offset.top + 15 + 'px'
                    });
                } else if(!Prototype.Browser.IE) {
                    to_show.setStyle({
                        marginLeft: '125px',
                        marginTop: '-20px'
                    });
                } else {
                    to_show.setStyle({
                        left: 0,
                        marginLeft: '125px'
                    });                
                }
                
                to_show.setStyle({display: 'block'});
            } else {
                this._HideSubNavList(sub_nav);
            }
        }.bind(this, sub_nav));
    }
});

var SlideShowJS = new JS.Class({
    images: [],

    initialize: function(wrapper) {
        Event.observe(window, 'load', this._initialize.bind(this, wrapper));
    },
    _initialize: function(wrapper) {
        this.images = $(wrapper).select('img').sort(function() {
            return Math.random() <= 0.5;
        });
        
        this.ChangeImage(0);
    },
    
    ChangeImage: function(counter) {
        counter = counter >= this.images.length ? 0 : counter;
        for(var i = 0; i < this.images.length; ++i) {
            if(i == counter) {
                this.images[i].setStyle({display: 'block'});
            } else {
                this.images[i].hide();
            }
        }
        
        this.ChangeImage.bind(this).delay(5, counter + 1);
    }
});

new NavigationJS('navigation');
new SlideShowJS('church_images');
new EmailJS();

