/* (C) 2008 YOOtheme.com, base.js */

//window.addEvent('domready', function(){
//	var trans = Fx.Transitions.sineOut;
//	var panelSize = 140;
//	var effectDuration = 350;
//	var openString = "+ Login";
//	var closeString = "- Login";
//	var opacity =1;

//		$('fxTarget').fx=$('fxTarget').effect('height',{duration:effectDuration,transition:trans});
//		$('fxTarget').fx.set(0);
//		$('fxTarget').state=0;
//		$('fxPadding').style.visibility="visible";
//		$('fxTrigger').lastChild.nodeValue=openString;$('fxTab').addEvent('click',function(){
//			if($('fxTarget').state==1){$('fxTarget').fx.start(panelSize,0);
//				$('fxTarget').state=0;$('fxTrigger').lastChild.nodeValue=openString}
//				else{$('fxTarget').fx.start(0,panelSize);$('fxTarget').state=1;
//		$('fxTrigger').lastChild.nodeValue=closeString}})
//});


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')
			}
         }
	)}
	, addHeaderSpan : function(selector) {
		$$(selector).each(function(el, i) {
			var text = el.getText().replace(/[^\w]*(\w+)(.*)/,'$1$2');
			el.setHTML(text)
		})
	}
};


//**************************************************************************************
//			Style Switcher
//**************************************************************************************

var YOOStyleSwitcher = new Class( {
	initialize : function(wrappers, options) {
		this.setOptions( {
			widthDefault : 'width-wide', 
			widthThinPx : 780, 
			widthWidePx : 940, 
			widthFluidPx : 0.9, 
			transition : Fx.Transitions.bounceOut, 
			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('slstylefont', font, {
			path : '/'
		});
		
		this.fireEvent('afterSwitch')
	}, 
	
	widthSwitch : function(width) {
		var curWidth = this.getWidthPx(Cookie.get('slstylewidth') || this.options.widthDefault);
		var newWidth = this.getWidthPx(width);
		Cookie.set('slstylewidth', 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('slstylewidth') || 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.sineOut, 
			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);
/* (C) 2008 YOOtheme.com, accordionmenu.js */

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'); 
			span.addClass('expanded');
			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();
				if(span.hasClass('expanded')) {
					span.removeClass('expanded');
					span.addClass('collapse');
				}
				else span.addClass('expanded');
				
			})
		}.bind(this))
	}
});

YOOAccordionMenu.implement(new Options);/* (C) 2008 YOOtheme.com, fancymenu.js */
var YOOFancyMenu = new Class( {

	initialize: function(menu, options) {	
		this.setOptions(this.getOptions(), options);

		this.menu = $(menu), this.current = this.menu.getElement('li.active');
		
		this.menu.getElements('li').each(function(item){
			item.addEvent('mouseover', function(){ this.moveBg(item); }.bind(this));
			item.addEvent('mouseout', function(){ this.moveBg(this.current); }.bind(this));
			item.addEvent('click', function(event){ this.clickItem(event, item); }.bind(this));
		}.bind(this));
				
		this.back = new Element('li').addClass('background').adopt(new Element('div').addClass('left'),new Element('div').addClass('right')).injectInside(this.menu);
		this.back.fx = this.back.effects(this.options);
		if(this.current) this.setCurrent(this.current);
	},
	
	setCurrent: function(el, effect){
		this.back.setStyles({left: (el.offsetLeft)+'px', width: (el.offsetWidth)+'px'});
		(effect) ? this.back.effect('opacity').set(0).start(1) : this.back.setOpacity(1);
		this.current = el;
	},
	
	getOptions: function(){
		return {
			transition: Fx.Transitions.sineInOut,
			duration: 500, wait: false,
			onClick: Class.empty
		};
	},

	clickItem: function(event, item) {
		if(!this.current) this.setCurrent(item, true);
		this.current = item;
		this.options.onClick(new Event(event), item);
	},

	moveBg: function(to) {
		if(!this.current) return;
		this.back.fx.custom({
			left: [this.back.offsetLeft, to.offsetLeft],
			width: [this.back.offsetWidth, to.offsetWidth]
		});
	}
});

YOOFancyMenu.implement(new Options); var YOODropdownMenu = new Class( {
   initialize : function(element, options) {
      this.setOptions( {
         duration : 200, transition : Fx.Transitions.Expo.easeOut, wait : false}
      , options); $$(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( {
               'width':0, 'height':0, 'opacity':0}
            ); 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( {
                     'width':0, 'height':0, 'opacity':0}
                  )}
               }
            )}
         }
      )}
   }
);
YOODropdownMenu.implement(new Options); /* (C) 2008 YOOtheme.com */

var YOOTools = {
		
	start: function() {
		
		/* Match height of div tags */
		YOOTools.setDivHeight();

		/* Add span to header */
		YOOBase.addHeaderSpan('#left div.module-headline h3, #right div.module-headline h3');

		/* Menu color settings */
		var page = $('page');		
		
		/* color default */
		var currentColor = '#333333';
		var leaveColor = '#404040';

		/* Accordion menu  */
		new YOOAccordionMenu('ul.menu li.toggler', 'ul.accordion', { accordion: 'slide' });
		
		// Main Menu Morph Effect
		var MainMenuEnter = { 'color': '#26A3FF' };
		var MainMenuLeave = { 'color': '#666666' };
		
		new YOOMorph('div#menu li.level1 ul.level2 a span', MainMenuEnter, MainMenuLeave,
			{ transition: Fx.Transitions.sineIn, duration: 300 },
			{ transition: Fx.Transitions.sineIn, duration: 300 });
				
		// User Menu Morph Effect
		var UserMenuEnter = { 'background-color': '#cfcfcf' };
		var UserMenuLeave = { 'background-color': '#e2e2e2' };
		
		new YOOMorph('div.moduletable_usermenu li.level1 a', UserMenuEnter, UserMenuLeave,
			{ transition: Fx.Transitions.sineIn, duration: 300 },
			{ transition: Fx.Transitions.sineIn, duration: 300 });
				
		// Content Scroller Morph Effect
		var TabEnter = { 'text-indent': '25px', 'color': '#1b679f' };
		var TabLeave = { 'text-indent': '15px', 'color': '#969696' };
		
		new YOOMorph('div#featured div.rok-content-rotator ul li h2 a', TabEnter, TabLeave,
			{ transition: Fx.Transitions.sineIn, duration: 300 },
			{ transition: Fx.Transitions.sineIn, duration: 300 });
		
		// Body Links Morph Effect
		var LinkEnter = { 'color': '#969696', 'border-bottom': '1px solid #969696' };
		var LinkLeave = { 'color': '#26A3FF', 'border-bottom': '0' };
		
		new YOOMorph('body div#middle a', LinkEnter, LinkLeave,
			{ transition: Fx.Transitions.sineIn, duration: 300 },
			{ transition: Fx.Transitions.sineIn, duration: 300 });
		
		// Logo Morph Effect
		var logoEnter = {'margin': '5px 0 5px 10px'};
		var logoLeave = {'margin': '10px 0 0 10px'};
		
		new YOOMorph('div#header a.logo img', logoEnter, logoLeave,
			{ transition: Fx.Transitions.bounceOut, duration: 300 },
			{ transition: Fx.Transitions.bounceOut, duration: 300 });
		
		// Table Of Contents Links
		var TabEnter = { 'text-indent': '15px' };
		var TabLeave = { 'text-indent': '0' };
		
		new YOOMorph('table.contenttoc tbody tr td a.toclink', TabEnter, TabLeave,
			{ transition: Fx.Transitions.sineIn, duration: 300 },
			{ transition: Fx.Transitions.sineIn, duration: 300 });
		
		
		/* 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 });
		
		/* Fancy menu */
		new YOOFancyMenu($E('ul', 'menu'), { mode: 'slide', transition: Fx.Transitions.Sine.easeOut, duration: 700 });

		/* Dropdown menu */
		new YOODropdownMenu('div#menu ul.menu li.parent');

	},

	/* Match height of div tags */
	setDivHeight: function() {
		YOOBase.matchDivHeight('div.topbox div div div div', 0, 40);
		YOOBase.matchDivHeight('div.bottombox div div div div', 0, 40);
		YOOBase.matchDivHeight('div.maintopbox div div div div', 0, 40);
		YOOBase.matchDivHeight('div.mainbottombox div div div div', 0, 40);
		YOOBase.matchDivHeight('div.contenttopbox div div div div', 0, 40);
		YOOBase.matchDivHeight('div.contentbottombox div div div div', 0, 40);
	}

};

/* Add functions on window load */
window.addEvent('load', YOOTools.start);
