var YOOBase = {
    matchDivHeight: function(selector, divBorder, minWidth) {
        var maxHeight = 0;
        var matchDivs = [];
        var selectors = selector.split(" ");
        var elements = selectors.shift();
        var script = '';
        selectors.each(function(el, i) {
            script += '.getElement("' + el + '")'
        });
        $ES(elements).each(function(element, i) {
            eval('matchDivs.push(element' + script + ');')
        });
        matchDivs.each(function(div, i) {
            if (!$chk(div)) return;
            var divHeight, divPadding;
            if (div.offsetHeight) {
                divHeight = div.offsetHeight;
                divPadding = 0;
                divPadding += div.getStyle('padding-top').toInt();
                divPadding += div.getStyle('padding-bottom').toInt();
                divHeight -= divPadding;
                if (divBorder != undefined) {
                    divHeight -= divBorder
                }
            } else if (div.style.pixelHeight) {
                divHeight = div.style.pixelHeight
            }
            maxHeight = Math.max(maxHeight, divHeight)
        });
        if (minWidth != undefined) {
            maxHeight = Math.max(maxHeight, minWidth)
        }
        matchDivs.each(function(div, i) {
            if (!$chk(div)) return;
            if (window.ie6) {
                div.setStyle('height', maxHeight + 'px')
            } else {
                div.setStyle('min-height', maxHeight + 'px')
            }
        })
    }
};
var YOOStyleSwitcher = new Class({
    initialize: function(wrappers, options) {
        this.setOptions({
            widthDefault: 'width-wide',
            widthThinPx: 780,
            widthWidePx: 940,
            widthFluidPx: 0.9,
            transition: Fx.Transitions.quadOut,
            duration: 500,
            afterSwitch: Class.empty
        },
        options);
        this.fontSmall = 'font-small',
        this.fontMedium = 'font-medium',
        this.fontLarge = 'font-large',
        this.widthThin = 'width-thin';
        this.widthWide = 'width-wide';
        this.widthFluid = 'width-fluid';
        this.wrappers = $$(wrappers);
        this.htmlbody = new Element(document.body);
        this.addEvent('afterSwitch', this.options.afterSwitch);
        this.widthStyle = '';
        var switchWidthThin = $E('#switchwidththin');
        var switchWidthWide = $E('#switchwidthwide');
        var switchWidthFluid = $E('#switchwidthfluid');
        var switchFontSmall = $E('#switchfontsmall');
        var switchFontMedium = $E('#switchfontmedium');
        var switchFontLarge = $E('#switchfontlarge');
        if (switchWidthThin) switchWidthThin.addEvent('click',
        function() {
            this.widthSwitch(this.widthThin)
        }.bind(this));
        if (switchWidthWide) switchWidthWide.addEvent('click',
        function() {
            this.widthSwitch(this.widthWide)
        }.bind(this));
        if (switchWidthFluid) switchWidthFluid.addEvent('click',
        function() {
            this.widthSwitch(this.widthFluid)
        }.bind(this));
        if (switchFontSmall) switchFontSmall.addEvent('click',
        function() {
            this.fontSwitch(this.fontSmall)
        }.bind(this));
        if (switchFontMedium) switchFontMedium.addEvent('click',
        function() {
            this.fontSwitch(this.fontMedium)
        }.bind(this));
        if (switchFontLarge) switchFontLarge.addEvent('click',
        function() {
            this.fontSwitch(this.fontLarge)
        }.bind(this))
    },
    fontSwitch: function(font) {
        var fonts = [this.fontSmall, this.fontMedium, this.fontLarge];
        fonts.each(function(currentFont, i) {
            if (currentFont == font) {
                this.htmlbody.addClass(font)
            } else {
                this.htmlbody.removeClass(currentFont)
            }
        }.bind(this));
        Cookie.set('ytstylefont', font, {
            path: '/'
        });
        this.fireEvent('afterSwitch')
    },
    widthSwitch: function(width) {
        var curWidth = this.getWidthPx(Cookie.get('ytstylewidth') || this.options.widthDefault);
        var newWidth = this.getWidthPx(width);
        Cookie.set('ytstylewidth', width, {
            path: '/'
        });
        this.wrappers.each(function(wrapper, i) {
            var fx = wrapper.effect('width', this.options);
            fx.addEvent('onComplete', this.widthSwitchComplete.bind(this)).addEvent('onComplete', this.options.afterSwitch);
            fx.start(curWidth, newWidth)
        }.bind(this))
    },
    widthSwitchComplete: function() {
        var widthStyle = Cookie.get('ytstylewidth') || this.options.widthDefault;
        if (widthStyle == this.widthFluid) {
            this.wrappers.each(function(wrapper, i) {
                wrapper.setStyle('width', (this.options.widthFluidPx * 100) + '%')
            }.bind(this))
        }
    },
    getWidthPx: function(width) {
        if (width == this.widthThin) return this.options.widthThinPx;
        if (width == this.widthFluid) return parseInt((Window.getWidth()) * this.options.widthFluidPx);
        return this.options.widthWidePx
    }
});
YOOStyleSwitcher.implement(new Events);
YOOStyleSwitcher.implement(new Options);
var YOOMorph = new Class({
    initialize: function(menu, enter, leave, enterFx, leaveFx) {
        this.setOptions({
            duration: 500,
            transition: Fx.Transitions.expoOut,
            wait: false,
            ignoreClass: ''
        },
        enterFx);
        var options = this.options;
        $$(menu).each(function(el, i) {
            var liFxs = new Fx.Styles(el, options);
            if (! ($chk(options.ignoreClass) && el.hasClass(options.ignoreClass))) {
                el.addEvent('mouseenter',
                function(e) {
                    liFxs.setOptions(options, enterFx).start(enter)
                });
                el.addEvent('mouseleave',
                function(e) {
                    liFxs.setOptions(options, leaveFx).start(leave)
                })
            }
        })
    }
});
YOOMorph.implement(new Options);
/* accordionmenu */
var YOOAccordionMenu = new Class({
    initialize: function(togglers, elements, options) {
        this.setOptions({
            accordion: 'default'
        },
        options);
        this.togs = togglers;
        this.elms = elements;
        switch (this.options.accordion) {
        case 'slide':
            this.createSlide();
            break;
        default:
            this.createDefault()
        }
    },
    createDefault: function() {
        var options = {};
        if (!$defined(this.options.display)) {
            options = {
                show: -1
            }
        }
        $ES(this.togs).each(function(tog, i) {
            if (tog.hasClass('active')) options = {
                show: i
            }
        }.bind(this));
        var accordionMenu = new Fx.Accordion(this.togs, this.elms, $extend(this.options, options))
    },
    createSlide: function() {
        $ES(this.togs).each(function(tog, i) {
            var span = tog.getElement('span');
            var ul = tog.getElement(this.elms);
            var fx = new Fx.Slide(ul, {
                transition: Fx.Transitions.linear,
                duration: 250
            });
            if (! (tog.hasClass('active') || this.options.display == 'all' || this.options.display == i)) {
                fx.hide()
            }
            span.addEvent('click',
            function() {
                fx.toggle()
            })
        }.bind(this))
    }
});
YOOAccordionMenu.implement(new Options);
/* fancymenu */

/*var YOOFancyMenu = new Class({
    initialize: function(menu, options) {
        this.setOptions({
            transition: Fx.Transitions.sineInOut,
            duration: 500,
            wait: false,
            onClick: Class.empty,
            opacity: 1,
            mode: 'move',
            slideOffset: 30,
            itemSelector: 'li.level1',
            activeSelector: 'li.active'
        },
        options);
        this.menu = $(menu),
        this.current = this.menu.getElement(this.options.activeSelector);
        this.li = [];
        this.div = [];
        this.menu.getElements(this.options.itemSelector).each(function(item, i) {
            this.createBackground(item, i);
            item.addEvent('click',
            function(event) {
                this.clickItem(event, item)
            }.bind(this));
            item.addEvent('mouseenter',
            function() {
                this.mouseenterItem(item, i)
            }.bind(this));
            if (this.options.mode == 'move') {
                item.addEvent('mouseleave',
                function() {
                    this.mouseleaveItem(this.current, i)
                }.bind(this))
            } else {
                item.addEvent('mouseleave',
                function() {
                    this.mouseleaveItem(item, i)
                }.bind(this))
            }
        }.bind(this));
        if (this.options.mode == 'move') {
            if (this.current) {
                this.setCurrent(this.current)
            } else {
                this.setCurrent(this.menu.getElement('li'))
            }
        }
    },
    createBackground: function(item, i) {
        if (this.options.mode == 'move' && i != 0) return;
        this.div[i] = new Element('div').adopt(new Element('div'));
        this.div[i].fx = this.div[i].effects(this.options);
        this.li[i] = new Element('li').addClass('fancy').addClass('bg' + (i + 1)).adopt(this.div[i]).injectInside(this.menu);
        this.li[i].fx = this.li[i].effects(this.options)
    },
    setCurrent: function(item) {
        this.li[0].setStyles({
            'left': item.offsetLeft,
            'width': item.offsetWidth,
            'visibility': 'visible',
            'opacity': this.options.opacity
        });
        this.current = item
    },
    clickItem: function(event, item) {
        if (!this.current) this.setCurrent(item);
        this.current = item;
        this.options.onClick(new Event(event), item)
    },
    mouseenterItem: function(item, i) {
        switch (this.options.mode) {
        case 'fade':
            this.fadeFx(item, i, true);
            break;
        case 'slide':
            this.slideFx(item, i, true);
            break;
        default:
            this.moveFx(item, 0)
        }
    },
    mouseleaveItem: function(item, i) {
        switch (this.options.mode) {
        case 'fade':
            this.fadeFx(item, i, false);
            break;
        case 'slide':
            this.slideFx(item, i, false);
            break;
        default:
            this.moveFx(item, 0)
        }
    },
    moveFx: function(item, i) {
        if (!this.current) return;
        this.li[i].fx.custom({
            'left': [this.li[i].offsetLeft, item.offsetLeft],
            'width': [this.li[i].offsetWidth, item.offsetWidth]
        })
    },
    fadeFx: function(item, i, show) {
        if (show) {
            this.li[i].fx.setOptions(this.options);
            this.li[i].fx.set({
                'left': item.offsetLeft,
                'width': item.offsetWidth
            });
            this.li[i].fx.custom({
                'opacity': [0, 1]
            })
        } else {
            var dur = this.options.duration * 2;
            this.li[i].fx.setOptions({
                duration: dur
            });
            this.li[i].fx.custom({
                'opacity': [1, 0]
            })
        }
    },
    slideFx: function(item, i, show) {
        var offset = this.options.slideOffset;
        if (show) {
            this.li[i].fx.set({
                'opacity': 1,
                'left': item.offsetLeft,
                'width': item.offsetWidth
            });
            this.div[i].fx.set({
                'margin-top': offset
            });
            this.div[i].fx.custom({
                'margin-top': [offset, 0]
            })
        } else {
            this.div[i].fx.set({
                'margin-top': 0
            });
            this.div[i].fx.custom({
                'margin-top': [0, offset]
            })
        }
    }
});
YOOFancyMenu.implement(new Options);*/
/* dropdownmenu */
var YOODropdownMenu = new Class({
    initialize: function(element, options) {
        this.setOptions({
            mode: 'default',
            duration: 600,
            transition: Fx.Transitions.linear,
            wait: false
        },
        options);
        var reset = {
            'width': 0,
            'height': 0,
            'opacity': 0
        };
        switch (this.options.mode) {
        case 'width':
            reset = {
                'width': 0,
                'opacity': 0
            };
            break;
        case 'height':
            reset = {
                'height': 0,
                'opacity': 0
            };
            break
        }
        $$(element).each(function(li) {
            var ul = li.getElement('ul');
            if (ul) {
                var fx = new Fx.Styles(ul, this.options);
                var styles = ul.getStyles('width', 'height', 'opacity');
                ul.setStyles(reset);
                li.addEvents({
                    mouseenter: function() {
                        var parent = li.getParent();
                        if (parent.getStyle('overflow') == 'hidden') parent.setStyle('overflow', 'visible');
                        fx.element.setStyle('overflow', 'hidden');
                        fx.start(styles)
                    },
                    mouseleave: function() {
                        fx.stop();
                        ul.setStyles(reset)
                    }
                })
            }
        }.bind(this))
    }
});
YOODropdownMenu.implement(new Options);

/* process*/
var YOOTools = {
		
	start: function() {
		
		/* Match height of div tags */
		YOOTools.setDivHeight();

		/* Accordion menu */
		new YOOAccordionMenu('div#middle ul.menu li.toggler', 'ul.accordion', { accordion: 'slide' });

		/* Fancy menu */
		/*new YOOFancyMenu($E('ul', 'menu'), { mode: 'fade', transition: Fx.Transitions.linear, duration: 500 });*/

		/* Dropdown menu */
		new YOODropdownMenu('div#menu li.parent', { mode: 'height', transition: Fx.Transitions.Expo.easeOut });

		/* Menu color settings */
		/* color default */
		var currentColor = '#252525';
		var leaveColor = '#323232';

		/* Main menu (sub) */
		var menuEnter = { 'background-color': currentColor };
		var menuLeave = { 'background-color': leaveColor };
		
		new YOOMorph('div#menu li.level2 a, div#menu li.level2 span.separator', menuEnter, menuLeave,
			{ transition: Fx.Transitions.expoOut, duration: 300 },
			{ transition: Fx.Transitions.sineIn, duration: 500 });
		
		/* Sub menu color settings */
		/* color default */
		var currentColor = '#1E1E1E';
		var leaveColor = '#252525';
		
		/* Sub menu all levels */
		var submenuEnter = { 'background-color': currentColor};
		var submenuLeave = { 'background-color': leaveColor};

		new YOOMorph('div#middle ul.menu a, div#middle ul.menu span.separator', submenuEnter, submenuLeave,
			{ transition: Fx.Transitions.expoOut, duration: 300 },
			{ transition: Fx.Transitions.sineIn, duration: 500 });

		/* Style switcher */
		/*new YOOStyleSwitcher($ES('.wrapper'), { 
			widthDefault: YtSettings.widthDefault,
			widthThinPx: YtSettings.widthThinPx,
			widthWidePx: YtSettings.widthWidePx,
			widthFluidPx: YtSettings.widthFluidPx,
			afterSwitch: YOOTools.setDivHeight,
			transition: Fx.Transitions.expoOut,
			duration: 500
		});	*/	
		
		/* Smoothscroll */
		new SmoothScroll({ duration: 500, transition: Fx.Transitions.Expo.easeOut });
	},

	/* Include script */
	include: function(library) {
		$ES('script').each(function(s, i){
			var src  = s.getProperty('src');
			var path = '';
			if (src && src.match(/yoo_tools\.js(\?.*)?$/)) path = src.replace(/yoo_tools\.js(\?.*)?$/,'');
			if (src && src.match(/template\.js\.php(\?.*)?$/)) path = src.replace(/template\.js\.php(\?.*)?$/,'');
			if (path != '') document.write('<script language="javascript" src="' + path + library + '" type="text/javascript"></script>');
		});
	},

	/* Match height of div tags */
	setDivHeight: function() {
		YOOBase.matchDivHeight('div.topbox div.deepest', 0, 40);
		YOOBase.matchDivHeight('div.bottombox div.deepest', 0, 40);
		YOOBase.matchDivHeight('div.maintopbox div.deepest', 0, 40);
		YOOBase.matchDivHeight('div.mainbottombox div.deepest', 0, 40);
		YOOBase.matchDivHeight('div.contenttopbox div.deepest', 0, 40);
		YOOBase.matchDivHeight('div.contentbottombox div.deepest', 0, 40);
	}

};


/* Add functions on window load */
window.addEvent('load', YOOTools.start);
