var navHover = Class.create();

navHover.prototype = {
	
	initialize: function(){
	   var els = $('nav').select('.top');
	   els.invoke('observe', 'mouseenter', this.mouseEnter.bindAsEventListener(this));
	   els.invoke('observe', 'mouseleave', this.mouseLeave.bindAsEventListener(this));
	},
    
    getHoverNav: function(e){
        var hovEl = e.element();
        if(!hovEl.hasClassName('top')){
            hovEl = hovEl.up('.top');
        }
        if(hovEl.down('.sub')){
            return false;
        }
        hovEl.down(0).toggleClassName('topHover');
        return hovEl.down('.navHover');
    },
	
    mouseEnter: function(e){
        var hover = this.getHoverNav(e);
        if(hover === false){
            return true;
        }
        hover.show();
    },
	
    mouseLeave: function(e){
        var hover = this.getHoverNav(e);
        if(hover === false){
            return true;
        }
        hover.hide();
    }
};

//------------------------------

var catTypes = Class.create();

catTypes.prototype = {

    initialize: function(){
        $$('#byCategoryType > div > a').invoke('observe', 'click', this.onClick.bindAsEventListener(this));
        $('byCategoryType').select('.silverTab').invoke('observe', 'mouseleave', this.onMouseOut.bindAsEventListener(this));
    }, 
    
    open: function(e){
        Effect.Queues.get(e.id).invoke('cancel');
        new Effect.SlideDown(e, {
            duration:0.3,
            queue:{scope: 'catTypes', position: 'end'},
            afterFinish: function(){
                e.setStyle({height: 'auto'});
            }
        });
        return false;
    },
    
    close: function(e){
        Effect.Queues.get(e.id).invoke('cancel');
        new Effect.SlideUp(e, {
            duration:0.3,
            queue:{scope: e.id, position: 'end'},
            afterFinish: function(){
                e.setStyle({height: 'auto'});
            }
        });
        return false;
    },
    
    onClick: function(e){
        var el = e.findElement('div.silverTab').childElements()[1];
        e.stop();
        if(el.visible()){
            this.close(el);
        }else{
            this.open(el);
        }
    },
    
    onMouseOver: function(e){
        var el = e.findElement('div.silverTab').childElements()[1];
        e.stop();
        if(el.visible()){
            this.close(el);
        }else{
            this.open(el);
        }
    },
    
    onMouseOut: function(e){
        var el = e.findElement('div.silverTab').childElements()[1];
        e.stop();
        if(el.visible()){
            this.close(el);
        }
    }

};

//------------------------------

/*
var silverTabs = Class.create();

silverTabs.prototye = {

    initialize: function(ul){
        $(ul).childElements().each(this.setup.bind(this));
        ul.observe('mouseenter', this.onEnter);
        ul.observe('mouseleave', this.onLeave);
        
    },
    
    setup: function(li){
        var imgElement = li.down('img');
        var img = new Image();
        img.src = imgElement.src.replace(".jpg", "Over.jpg");
    },
    
    onEnter: function(event){
        event.stop();
        var imgElement = this.down('img');
        if(-1 == imgElement.src.search(/Over.jpg$/)){
            imgElement.src = imgElement.src.replace(".jpg", "Over.jpg");
        }
    },
    
    onLeave: function(event){
        event.stop();
        var imgElement = this.down('img');
        if(imgElement.src.search(/Over.jpg$/) > -1){
            imgElement.src = imgElement.src.replace("Over.jpg", ".jpg");
        }
    }

};

document.observe('dom:loaded', function(){
    $$('ul.silverTabs').each(function(ul){ new SilverTabs(ul) });
});
*/

//------------------------------

/*
CSS Browser Selector v0.2.7
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
var css_browser_selector = function() {var ua=navigator.userAgent.toLowerCase(),is=function(t){return ua.indexOf(t) != -1;},h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;}();

var CategoryTeaser = Class.create({
	
	teasers: [],
	teaserLinks: [],
	originalHeight: [],
	
	TEXT_MORE: "More",
	TEXT_LESS: "Less",
	
	initialize: function()
	{
		Event.observe(window, 'load', this.domReady.bindAsEventListener(this));
	},
	
	domReady: function()
	{			
		this.teasers = $$(".categoryTeaser");
		this.teaserLinks = $$(".categoryTeaser a");
		
		this.teasers.each(function(teaser, index) {
			this.originalHeight[index] = teaser.offsetHeight;
			teaser.setStyle({ height: "36px", overflow: "hidden" });
		}, this);
		
		this.teaserLinks.each(function(link) {
			link.observe('click', this.onLinkClick.bindAsEventListener(this));
		}, this);
	},
	
	onLinkClick: function(event)
	{
		event.stop();
		
		var a = event.findElement('a'), 
			span = a.down('span'), 
			teaser = a.up('.categoryTeaser'),
			index = this.teasers.indexOf(teaser);
			
		if (span.innerHTML == this.TEXT_MORE) {
			teaser.morph("height:" + (this.originalHeight[index] + 12) + "px", 
				{ 
					duration: 0.5, 
					afterFinish: (function() { 
						span.update(this.TEXT_LESS);
					}).bindAsEventListener(this) 
				}
			);
		} else if (span.innerHTML == this.TEXT_LESS) {
			teaser.morph("height: 36px", 
				{
					duration: 0.5,
					afterFinish: (function() {
						span.update(this.TEXT_MORE);
					}).bindAsEventListener(this)
				}
			);
		}
		
		return false;
	}
	
});
