


var SliderMenus = new Class({
    menus : new Array(),
    hideAll : function(){
        this.menus.forEach(function(m){
            var results = m.element.getElementsBySelector('div.results').getLast();
            if(results) {
                $(results).setStyle('overflow', 'hidden');
            }
            if(m.options.handleSearch){
                m.menu.empty();
            }
            m.hide();
        })
    }
});


var SlideMenu = new Class({
    options : {
      handleSearch : Class.empty()
    },
    initialize : function(button, menu, options)  {
        this.setOptions($merge(this.options, options));

        var slideOpts = { duration : 500};
        if(this.options.onStart){
            slideOpts.onStart = this.options.onStart;
        }

        slideOpts.onComplete = function(m){
            var results = m.getElementsBySelector('div.results').getLast();
            if(results) results.setStyle('overflow', 'auto');
        };


        this.menu = new Fx.Slide(menu, slideOpts);
        $(menu).setStyle('display', 'block');
        //this.shim = new IframeShim({element : $(menu)});
        this.menu.hide();

        $(button).addEvent('togg', function(){
            this.menu.slideIn();
        }.bind(this));

        if($(button).hasClass('search')){
            $(button).addEvent("keydown", function(e){
                var event = new Event(e);
                
                if(event.key == "enter") {
                    $(button).fireEvent("togg");
                    if(this.options.handleSearch){
                        this.options.handleSearch($(button), $(menu), this.menu);
                    }
                }
            }.bind(this));
        } else {
            $(button).addEvent("click", function(){
                $(button).fireEvent("togg");
            })
        }
    }
});
SlideMenu.implement(new Options);


var LawrenceWelk = new Class({
    options : {
      keepClosed : true
    },
    containers : Class.empty,
    closeAll : function(){
        this.containers.each(function(c){
            c.hide();
        });
    },
    setUpAccordians : function(){
        this.sets.each(function(s){
            var container = s.getElementsBySelector(this.containerSelector).getLast();
            this.containers.push(container);
            var handle = s.getElementsBySelector(this.handleSelector).getLast();
            handle.addEvent("click", function(){
                if(container.isVisible()){
                    container.hide();
                } else {
                    if(this.options.keepClosed){
                        this.closeAll();
                    }
                    container.show();
                }
            }.bind(this))
        }.bind(this));
    },
    initialize : function(sets, handle, container, options){
        this.setOptions($merge(this.options, options));

        this.sets = sets;
        this.handleSelector = handle;
        this.containerSelector = container;
        this.containers = new Array();
        this.setUpAccordians();
    }
});
LawrenceWelk.implement(new Options);
                                //dbug.enable();

var NotesHandler = new Class({
    showing : false,
    initialize: function(el, nf){
        this.activator = nf;
         $(el).addEvent("click", function(){
            this.toggle();
         }.bind(this));
    },
    toggle : function(){
        if(!this.showing){
            this.activate();

            $('hidenotes').addEvent("click", function(){
                $('notes').fireEvent("click");
            });
            
        } else {
            $$('div.note').each(function(n){
                n.toggle();
            });
            $('noteWriter').toggle();
        }
    },
    activate : function(){
        this.activator();
        this.showing = true;
    }
});

var setsWelk;
var photoViewer;
window.addEvent('domready', function(){
    //dbug.enable();
    var sliders = new SliderMenus();
    sliders.menus.push(new SlideMenu('moreButton', 'moreMenu').menu);
    sliders.menus.push(new SlideMenu('search', 'searchMenu', {
        handleSearch : function(input, menu, slider){
            menu.empty();
            var showQuery = input.value;
            if(showQuery.length > 20){
                showQuery = showQuery.substring(0,20) + " ... ";
            }

            new Element("div").addClass('searchTitle').setHTML(showQuery + ": ").injectTop(menu);
            var heck = new Element("div");
            new Ajax("/search.php?q="+input.value, {
                method: 'get',
                update: heck,
                onComplete: function(){
                    var res = heck.getElementsBySelector("div.results").getLast();
                    menu.adopt(res);
                }
               }).request();
        },
        onStart : function(m){
            m.empty();
        }
	}).menu);
    
    
    document.addEvent('click', function() {
            sliders.hideAll();
	});
    


   // new Accordion($$("#blog fieldset div.blogTitle"), $$("#blog fieldset div.blogContent"));
    new LawrenceWelk($$("#music div.musicset"), "div.setName", "div.setContainer");
    $$("#music div.musicset div.setContainer").each(function(c){
        new LawrenceWelk(c.getElementsBySelector("fieldset dl"), "dt", "dd");
    });
    new LawrenceWelk($$("#music div.musicgenre"), "div.setName", "div.setContainer");
    $$("#music div.musicgenre div.setContainer").each(function(c){
        new LawrenceWelk(c.getElementsBySelector("fieldset dl"), "dt", "dd");
    });


    var subNavs = $$("#subnavigation ul li");
    var subNavHandler = new SubNavHandler("#subnavigation ul li", "div.subNavDetails");

    if($('photos')) {
        photoViewer = new NewPhotoViewer("#photos #photosHold fieldset img", "fullSizeImage");
    }

    new NotesHandler("notes", notes);

 // $$("img.reflect").each(function(i){
  //   i.reflect();
 // });

});

var NewPhotoViewer = new Class({
    initialize : function(thumbs, stickyDiv){
        this.thumbnails = $$(thumbs);
        this.showing = false;
        this.stick = $(stickyDiv);
        this.stick.makeDraggable({handle: $("photoButtonHolder")});
        this.next = this.stick.getElementsBySelector("span.next").getLast();
        this.previous = this.stick.getElementsBySelector("span.previous").getLast();
        this.close = this.stick.getElementsBySelector("span.close").getLast();
        this.close.addEvent("click", function(){
            this.stick.smoothHide();
            this.showing = false;
        }.bind(this));
        this.save = this.stick.getElement("span.save");
        this.getSuperSize = this.stick.getElement("span.supersize");

        this.initStickys();
        this.initNext();
        this.initPrevious();
    },
    initStickys : function(){
        var c = 0;
        this.thumbnails.forEach(function(t){
            t.setAttribute("count", c);
            t.addEvent("click", function(e){
                this.next.setAttribute("count", parseInt(t.getAttribute("count")) + 1);
                this.previous.setAttribute("count", parseInt(t.getAttribute("count")) - 1);

                var holder = this.stick.getElementsBySelector('div.photoHolder img').getLast();
                var caption = this.stick.getElementsBySelector("div.photoCaption").getLast();


                caption.adopt(new Element("img", { src : '/art/ajax-loader.gif'}));

                var tempPhoto = new Element("img", {src : "/photos/"+ t.getAttribute('directory') + "/"+t.getAttribute('file')}).setStyle("display", "none");
                $("photos").adopt(tempPhoto);
                tempPhoto.addEvent("load", function(){
                    holder.setAttribute("src", tempPhoto.getAttribute("src"));
                    this.viewing = tempPhoto.getAttribute("src");
                    this.superViewing = this.viewing.replace(t.getAttribute('directory'), t.getAttribute('directory')+"/superweb");
                }.bind(this));

                holder.addEvent("load", function(){

                    caption.setHTML(t.getParent().getParent().getParent().getElementsBySelector("div.caption").getLast().innerHTML);
                    caption.setStyles({width : holder.getStyle("width"), height: "auto", "padding": "5px"});

                    if(!this.showing){
                        this.stick.setStyle("height", holder.getStyle("height"));
                        this.stick.setPosition({edge : 'center', position: 'center'});
                    }
                    this.showing = true;
                    this.stick.smoothShow();

                }.bind(this));
            }.bind(this));
            c++;
        }.bind(this));

        this.save.addEvent("click", function(){
         window.open(this.viewing);

        }.bind(this))

        this.getSuperSize.addEvent("click", function(){
         window.open(this.superViewing);

        }.bind(this))

    },
    initNext : function(){
        this.next.addEvent('click', function(){
            var n = this.next.getAttribute("count");
            if(this.thumbnails[n]) {
                this.thumbnails[n].fireEvent("click");
            }
        }.bind(this));
    },
    initPrevious : function(){
        this.previous.addEvent('click', function(){
            var n = this.previous.getAttribute("count");
            if(this.thumbnails[n]) {
                this.thumbnails[n].fireEvent("click");
            }
        }.bind(this));
    }
});


var SubNavHandler = new Class({
    initialize : function(handles, detailsSel){
        this.hands = $$(handles);
        this.detailsSel = detailsSel;
        this.showers = new Array();
        this.addBehavior();
    },
    addBehavior : function(){
        this.hands.forEach(function(h){
            var details = h.getElementsBySelector(this.detailsSel).getLast();

            if(details) {
                details.setPosition({relativeTo: h, position: "topRight"});

                var anchor = h.getElementsBySelector("a").getLast();
                anchor.addEvent("mouseover", function(){
                    details.toggle();
                }.bind(this));
                anchor.addEvent("mouseout", function(){
                    details.hide();
                });
            }
        }.bind(this));
    },
    hideAll : function(){
        this.showers.forEach(function(s){
            s.hide();
        });
    }

});