/* -----------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
	
	boe.chicodev.com
	Stylesheet: public/main.js
	Author: Brent White, chicowebdesign.com
	Version: November 01, 2009
    Porter from jQuery: Dan Krieger, navnorth.com
	
--------------------------------------------------------------------------------------
----------------------------------------------------------------------------------- */
Event.observe(document, "dom:loaded", function() {

	/* -----------------------------------------------------------------------------------
	LAST CHILD / FIRST CHILD : ADD FIRST / LAST CLASSES
	----------------------------------------------------------------------------------- */
	$$('li:last-child').invoke('addClassName', 'last');
    $$('li:first-child').invoke('addClassName', 'first');
    
	/* -----------------------------------------------------------------------------------
	ODD CHILD / EVEN CHILD : ADD ODD / EVEN CLASSES
	----------------------------------------------------------------------------------- */
	/* -----------------------------------------------------------------------------------
	BODY -- WRAPPER -- HEADER -- NAVIGATION
	----------------------------------------------------------------------------------- */
	function HeaderNavigation() {
		var headerNavigationObject = this;
        
        $$('#header_navigation .navigation *').invoke('observe', 'mouseover', function()
            {
				clearTimeout(headerNavigationObject.windowTimeout);
			});
              
        
		var mainNavLinks = $$('#header_navigation .navigation > li > a'); 
		
		mainNavLinks.invoke('observe', 'mouseover', function() {
		        var thisObj = $(this);
		        if(thisObj.swapTimeout) { clearTimeout(thisObj.swapTimeout); }
		        thisObj.swapTimeout = window.setTimeout(function() {		        
                    headerNavigationObject.swapList(thisObj.up('li'));
                }, 300);
		});
		
		mainNavLinks.invoke('observe', 'mouseout', function() {
		    if(this.swapTimeout)
		    {
		        clearTimeout(this.swapTimeout);
		        this.swapTimeout = null;
		    }
		});
        
        headerNavigationObject.main_navigation = $$('#header_navigation .navigation > li.active')[0]; 
                
		$$('#header_navigation .navigation').invoke('observe', 'mouseout', function(){
			headerNavigationObject.windowTimeout = window.setTimeout(function(){
				headerNavigationObject.swapList(headerNavigationObject.main_navigation);
			}, 1000)
		});
        
		$$('#header_navigation .navigation ul li').each(function(subli){
			if (!subli.hasClassName('active')) {
                subli.observe('mouseover', function() { subli.addClassName('active'); });
                subli.observe('mouseout', function() { subli.removeClassName('active'); });
			}
		});
	}
	HeaderNavigation.prototype.swapList = function(li){
        $$('#header_navigation .navigation > li').invoke('removeClassName', 'active');
        li && li.addClassName('active');
		return true;
	};
	var hn = new HeaderNavigation();
	
	/* -----------------------------------------------------------------------------------
	SEARCH -- ADVANCED OPTIONS
	----------------------------------------------------------------------------------- */
	/* function SearchAdvanced() {
		jQuery(".search .advanced_options_link").click(function(){
			if ( jQuery(this).hasClass("active") ) {
				jQuery(".search .advanced_options").removeClass("active");
				jQuery(this).removeClass("active");
			} else {
				jQuery(".search .advanced_options").addClass("active");
				jQuery(this).addClass("active");
			}
		});
		jQuery(".search").mouseenter(function(){
			clearTimeout(this.windowTimeout);
		}).bind('mouseleave', function(){
			this.windowTimeout = window.setTimeout(function(){
				jQuery(".search .advanced_options").removeClass("active");
				jQuery(".search .advanced_options_link").removeClass("active");
			}, "500");
		});
	};
	searchAdvanced = new SearchAdvanced(); */
	
	/* -----------------------------------------------------------------------------------
	FILTER -- SUBJECT
	----------------------------------------------------------------------------------- */
	function FilterSubject() {
		var filterSubjectObject = ".module .subject";
        
        var objs = $$(filterSubjectObject);
		objs.invoke('observe', 'mouseover',function(){
			clearTimeout(this.windowTimeout);
			$(this).addClassName("active");
		});
        
        objs.invoke('observe', 'mouseout',
            function(){
                var thisObj = this;
                thisObj.windowTimeout = window.setTimeout(function(){
                $(thisObj).removeClassName("active");
                }, "500");
            });
	};
	var filterSubject = new FilterSubject();
	
	/* -----------------------------------------------------------------------------------
	CUSTOM SCROLLBARS
	----------------------------------------------------------------------------------- */
	/* jQuery('.module .list').scrollbar(); */
    
    //since we can't easily do the fake scrollbar like jQuery, for now at least wrap then with the necessary div
    
    var addScrollBar = function(scrollList)
        {
            var width = (scrollList.getWidth() - 20);
            
            var scrollContents = scrollList.wrap('div', { 'class': 'scrollbar_content' });
            
            var scrollContainer = scrollContents.wrap('div', {'class': 'scrollbar_container' });
            
            var scrollTrack = new Element('div', { 'class': 'scrollbar_track' })
                .insert(
                    new Element('div', { 'class': 'scrollbar_drag' })
                        .insert(new Element('div', { 'class': 'scrollbar_drag_top' }))
                        .insert(new Element('div', { 'class': 'scrollbar_drag_bottom' }))
                    )
                ;
            
                
            var scrollTrackContainer = new Element('div', {'class': 'scrollbar_track_container'});
            
            var upArrow = new Element('a', { 'href': '#', 'class': 'scrollbar_arrow_up', 'tabindex': -1 });
            var downArrow = new Element('a', { 'href': '#', 'class': 'scrollbar_arrow_down', 'tabindex': -1 });
            scrollTrackContainer
                .insert(upArrow)
                .insert(scrollTrack)
                .insert(downArrow);
                
            scrollContainer.insert({top: scrollTrackContainer});
                
            scrollTrack.setStyle({'height': (scrollTrackContainer.getHeight() - upArrow.getHeight() - downArrow.getHeight())+'px' });
                
            scrollContents.setStyle({
                'height': scrollList.getHeight()+'px', 
                'overflow': 'hidden'});
            scrollList.setStyle({'overflow': 'visible', 'height': 'auto'});
            
            var sb = new Control.ScrollBar(scrollContents, scrollTrackContainer);
            
            if(scrollTrackContainer.visible())
            {
                var w = {'width': width+'px'};
                scrollContents.setStyle(w);
                scrollList.setStyle(w);
            }
            
            upArrow.observe('click', function(e) { sb.scrollBy(-50); Event.stop(e); return false; });
            downArrow.observe('click', function(e) { sb.scrollBy(50); Event.stop(e); return false; });
            
            
        }
    
    $$('.module .list').each(addScrollBar); 
	
	/* -----------------------------------------------------------------------------------
	CSS OPACITY
	----------------------------------------------------------------------------------- */
	$$('.display .caption').invoke('setOpacity', 0.8);
	
	/* -----------------------------------------------------------------------------------
	SEARCH -- ADVANCED -- CUSTOM CHECKBOXES, RADIO BUTTONS
	----------------------------------------------------------------------------------- */
	/* jQuery('.search input[name=search_options[resource_type]], ul.view_options input[type=radio]').customInput(); */

	/* -----------------------------------------------------------------------------------
	SEARCH -- ADVANCED -- SUBJECTS -- TREEVIEW
	----------------------------------------------------------------------------------- */
	/* jQuery(".search .subjects ul").treeview({
		animated: "fast",
		collapsed: true,
		control: ".search .subject .navigation"
	});
	jQuery(".filter .subjects ul").treeview({
		animated: "fast",
		collapsed: true,
		control: ".filter .filter_navigation"
	}); */

	/* -----------------------------------------------------------------------------------
	SEARCH -- ADVANCED -- TOGGLE CHECKBOXES
	----------------------------------------------------------------------------------- */
	$$(".filter_section input.toggle_all").invoke('observe', 'click', function()
        {
            var checked = this.checked;
            
            $(this).up('.filter_section').select('input[type="checkbox"]').each(function(cb)
                {
                    cb.checked = checked;   
                });
            
        });
    
    /* -----------------------------------------------------------------------------------
	LOGIN LINKS (NICE HACK!)
	----------------------------------------------------------------------------------- */
    
    var buildUrl = '/auth'+window.location.pathname+window.location.search;
    
    $$('a.loginlink').invoke('setAttribute', 'href', buildUrl);
	
	/* -----------------------------------------------------------------------------------
	COLORBOX
	----------------------------------------------------------------------------------- */
	/* jQuery(".colorbox").colorbox({}, function(){
		//Re-apply scroll bars
		jQuery('.module .list').scrollbar();
		jQuery('#cboxLoadedContent').css("overflow","visible");
	}); */
	
	/* -----------------------------------------------------------------------------------
	GALLERY VIEW POPUP
	----------------------------------------------------------------------------------- */
	$$("a.quick_view").invoke('observe', 'click', function(e){
       
            var thisObj = $(this);
                
            if(thisObj.up().positionedOffset().left > thisObj.up().up().getWidth()-thisObj.up().getWidth()-50) {
                thisObj.up().down(".popup").setStyle({'right':"0",'left':"auto"});
            }
            
            var resourceBlock = thisObj.up(); 
            resourceBlock.setStyle({'zIndex': 70});
            
            var popUpWindow = thisObj.up().down(".popup");
            
            popUpWindow.setStyle({'zIndex': 100});
            var mouseOutFunc = function(){
                    this.windowTimeout = window.setTimeout(function(){
                        popUpWindow.stopObserving();
                        popUpWindow.hide();
                        resourceBlock.setStyle({'zIndex': 1});
                        /* new Effect.BlindUp(popUpWindow, { duration: 0.3,
                            afterFinish: function()
                            {
                                resourceBlock.setStyle({'zIndex': 1});        
                            }
                        }); */                            
                    }, 500);
                }
                
            popUpWindow.setStyle({'display': 'block'});
            popUpWindow.observe('mouseover', function(){
                clearTimeout(this.windowTimeout);
            });
        
            popUpWindow.observe('mouseout', mouseOutFunc);
            /* new Effect.BlindDown(popUpWindow, {
                    duration: 0.5,
                    afterSetup: function()
                    {
                        popUpWindow.setStyle({'display': 'block'});    
                    },
                    afterFinish: function()
                    {
                        popUpWindow.observe('mouseover', function(){
                                clearTimeout(this.windowTimeout);
                            });
                        
                        popUpWindow.observe('mouseout', mouseOutFunc);            
                    }
            }); */
            /* popUpWindow.setStyle({'display': 'block'}); */
            
            
            Event.stop(e);
            return false;
        
		});
    
	$$("div.popup a.close").invoke('observe', 'click', function(e){
        var popUpWindow = $(this).up('div.popup');
        popUpWindow.stopObserving();
        clearTimeout(popUpWindow.windowTimeout);
        popUpWindow.hide();
        popUpWindow.up('li').setStyle({'zIndex': 1});
        /* new Effect.BlindUp(popUpWindow, { duration: 0.3,
                afterFinish: function()
                {
                    popUpWindow.up('li').setStyle({'zIndex': 1});
                    
                }
            });
         */
        Event.stop(e);
		return false;
		});
	 
	$$('a._external').invoke('writeAttribute', 'target', '_blank');
	
});
