/**
 * @package		Gantry Template Framework - RocketTheme
 * @version		1.5.0 April 1, 2011
 * @author		RocketTheme http://www.rockettheme.com
 * @copyright 	Copyright (C) 2007 - 2011 RocketTheme, LLC
 * @license		http://www.rockettheme.com/legal/license.php RocketTheme Proprietary Use License
 */


(function(){
	
	var MenuAnchors = {
		
		top: 'rt-top',
		divider: 'rt-header-panel-divider',
		options: {
			wait: false,
			transition: Fx.Transitions.Expo.easeInOut,
			onComplete: function(){
				if (window.fusionmenu){
					if (!window.gecko) window.fusionmenu.disableScroll = false;
					else (function(){ window.fusionmenu.disableScroll = false; }).delay(500);
				}
			}
		},
		
		init: function(){
			MenuAnchors.menuItems = $$('.menutop a').filter(function(item){
				return item.href.contains('#') || item.className.contains('active-to-top');
			});

			MenuAnchors.items = $$('.menutop');
			if (MenuAnchors.items.length) MenuAnchors.items = MenuAnchors.items[0].getChildren();
			else return;
			
			if (MenuAnchors.menuItems.length){
				MenuAnchors.scroller = new Fx.Scroll(window, MenuAnchors.options);
				MenuAnchors.menuItems.each(MenuAnchors.grabAnchor);
			}
		},
		
		grabAnchor: function(item){
			var id = item.getProperty('href');
			id = id.split('#')[1];
			
			var el = $(id),
				top = $(MenuAnchors.top),
				divider = $(MenuAnchors.divider);
			
			var height = top.getSize().size.y;
			
			item.addEvent('click', function(e){
				if (e) e = new Event(e);
				var itemHref = item.href.split('#'),
					locaHref = window.location.href.split('#');

				if (item.aSubMenu){
					if (item.aSubMenu.parentLinks && item.aSubMenu.parentLinks.length){
						item.aSubMenu.parentLinks[0].aSubMenu.hideOtherSubMenus();
						item.aSubMenu.parentLinks[0].aSubMenu.hideSubMenu();
					} else {
						if (window.fusionmenu){
							window.fusionmenu.disableScroll = true;
						}
						item.aSubMenu.childMenu.fireEvent('hide');
						item.aSubMenu.btn.getParent().fireEvent('mouseleave');
						item.aSubMenu.hideOtherSubMenus();
						item.aSubMenu.hideSubMenu();
					}
				}
					
				if (itemHref[0] == locaHref[0] || itemHref[0] == locaHref[0]+'index.php' || itemHref[0]+'index.php' == locaHref[0]) e.stop();
				else return;
				
				if (el) MenuAnchors.scroller.scrollTo(0, el.getPosition().y - height);
				else MenuAnchors.scroller.toTop();
				
				
				MenuAnchors.items.removeClass('active');
				item.getParent().addClass('active');
			});
			
			if (id == window.location.hash.replace('#', '')){
				if (id.length){
					MenuAnchors.items.removeClass('active');
					item.getParent().addClass('active');
				}
				if (el) MenuAnchors.scroller.scrollTo(0, el.getPosition().y - height + ((window.gecko) ? -50 : 0));
			}
		}
		
	};

	window.addEvent('domready', MenuAnchors.init);

})();

