var eventMode = 'implicit';
var vars = {};
window.addEvent('domready', function(){
		vars.theMenu = ( document.getElementById('element_392357') ) ? $('element_392357') : $('element_372539');
		vars.theOuter = ( document.getElementById('element_392351') ) ? $('element_392351') : $('element_372533');
		vars.theMenu.coords = vars.theMenu.getCoordinates();
		vars.theMenuTop = vars.theMenu.getStyle('top').toInt();
		vars.theMenuLeft = vars.theMenu.getStyle('left').toInt();
		vars.theMenu.positions = [
			{position: 'absolute', top: vars.theMenuTop+'px', left: vars.theMenuLeft+'px'},
			{position: 'fixed', top: '20px', left: vars.theMenu.coords.left.toInt()+'px'},
			{position: 'absolute', top: (vars.theOuter.getCoordinates().top.toInt()-vars.theMenu.coords.height.toInt()-vars.theMenu.coords.top.toInt()+vars.theMenuTop)+'px', left: vars.theMenuLeft+'px'}
		];
		vars.theSideLinks = vars.theMenu.getElements('a[href*=#]');
		vars.theContainers = $$('div.online-website-builder-container');
		vars.theSideLinks.each(function(link){
			var theName = link.getProperty('href').split('#')[1];
			var theAnchor = $$('a[name='+theName+']');
			link.setProperty('id','link_'+theName);
			link.topCoord = link.getCoordinates().top.toInt() - vars.theMenu.topCoord;
			if ( theAnchor.length == 1 ) {
				theAnchor = theAnchor[0];
				theAnchor.addClass('anchor');
				if ( !theAnchor.getProperty('id') || theAnchor.getProperty('id').length == 0 ) {
					theAnchor.setProperty('id',theAnchor.getProperty('name'));
				}
				var theParent = theAnchor.getParent();
				while ( theParent && !theParent.hasClass('online-website-builder-container') ) {
					theParent = theParent.getParent();
				}
				link.anchorParent = theParent;
				link.addEvent('click',function(){
					eventMode = 'explicit';
					link.anchorParent.fireEvent('scrollInto');
					(function(){eventMode = 'implicit';}).delay(2000);
				});
			}
		});
		vars.theContainers.each(function(div){
			div.addEvent('scrollInto',function(){
				vars.theContainers.removeClass('active');
				if ( typeof CMS == 'undefined' ) { div.addClass('active'); }
				div.getElements('a.anchor').each(function(anchor){
					var theLink = $('link_'+anchor.getProperty('name'));
					if ( theLink && !theLink.hasClass('active') ) {
						vars.theSideLinks.removeClass('active');
						if ( typeof CMS == 'undefined' ) { theLink.addClass('active'); }
					}
				});
			});
		});
		window.addEvent('scroll', function(){
			var theScroll = window.getSize().scroll;
			var rangeStart = vars.theMenu.coords.top.toInt()-20;
			var rangeEnd = vars.theMenu.positions[2].top.toInt()-vars.theMenuTop;
			var rangePos = Math.floor((theScroll.y+rangeEnd-rangeStart)/rangeEnd);
			if ( typeof CMS == 'undefined' ) { 
				if ( rangePos > -1 && rangePos < vars.theMenu.positions.length ) {
					vars.theMenu.setStyles(vars.theMenu.positions[rangePos]);
				}
			} else {
				vars.theMenu.setProperty('style','');
			}
			if ( eventMode == 'implicit' ) {
				if ( theScroll.y < vars.theMenu.coords.top.toInt() && vars.theContainers.length > 0 ) {
					vars.theContainers[0].fireEvent('scrollInto');
				} else {
					vars.theContainers.some(function(div){
						div.theTop = div.theTop || div.getCoordinates().top;
						var scrollTop = div.theTop - theScroll.y;
						if ( scrollTop > -30 && scrollTop < 180 ) {
							div.fireEvent('scrollInto');
							return true;
						} else {
							return false;
						}
					});
				}
			}
		}).fireEvent('scroll');
		var theFX = new SmoothScroll();
	}
);

