var fmaRotationTimeIncrement = 4000;
var fmaRotationIndex = 0;
var fmaRotationMaxIndex = 3;
var fmaSelectionMade = null;
function rotateFMA(recursing) {
	
	if (fmaSelectionMade == null) {
        fmaRotationMaxIndex = $$('.box-nav li a').length;
		
        $$('.box-nav li a').each(function(element, elementIndex) {
            if (elementIndex == fmaRotationIndex) {
                var ffb = $('homepage-fma-banner');
        
                if (recursing == true) {

                    ffbFX = new Fx.Tween(ffb,{
                        duration: 1000, 
                        onComplete: function(){
                                var elementId = element.getAttribute('id');
                                $$('div.feature-box-image').each(function (contentElement){
                                    if (contentElement.hasClass(elementId)) {
                                        contentElement.setStyle('display', '');
                                    } else {
                                        contentElement.setStyle('display', 'none');
                                    }
                                });
                                ffb.fade('in');
                                
                                //alert(element.getAttribute('id'));
                                
                                $$('.box-nav li a').each(function(switcherLink){
                                    if (switcherLink.hasClass('active')) {
                                        switcherLink.removeClass('active');
                                    }
                                });
                                
                                element.addClass('active');
                            }
                        });
                    ffbFX.start('opacity',1,0);
                } else {
                    var elementId = element.getAttribute('id');
                    $$('div.feature-box-image').each(function (contentElement){
                        if (contentElement.hasClass(elementId)) {
                            contentElement.setStyle('display', '');
                        } else {
                            contentElement.setStyle('display', 'none');
                        }
                    });
                    $$('.box-nav li a').each(function(sibling){
                        
                        if (sibling.hasClass('active')) {
                            sibling.removeClass('active')
                        }
                    });
                    
                    element.addClass('active');
                }
            }
        });
        
        if (fmaRotationIndex < fmaRotationMaxIndex) {
            //increment
            fmaRotationIndex++;
        } else {
            //reset
            fmaRotationIndex = 0;
        }
        window.setTimeout('rotateFMA(true)', fmaRotationTimeIncrement);
    }
}
function initFMABrowser() {
    try {
        
        $$('.box-nav li a').each(function(element, elementIndex) {
            
            element.addEvent('click', function(ev) {
                if (ev) ev.stop();
                fmaRotationIndex = elementIndex;
                if (!element.hasClass('active')) {
                    var ffb = $('homepage-fma-banner');
                    
                    ffbFX = new Fx.Tween(ffb,{
                        duration: 1000, 
                        onComplete: function(){
                                var elementId = element.getAttribute('id');

                                $$('div.feature-box-image').each(function (contentElement){
                                    if (contentElement.hasClass(elementId)) {
                                        contentElement.setStyle('display', '');                                        
                                    } else {
                                        contentElement.setStyle('display', 'none');
                                    }    
                                });
                                ffb.fade('in');
                            }
                        });
                    ffbFX.start('opacity',1,0);
                    
                    $$('.box-nav li a').each(function(switcherLink){
                        if (switcherLink.hasClass('active')) {
                            switcherLink.removeClass('active');
                        }
                    });
                    
                    element.addClass('active');
                }
                
                fmaSelectionMade = elementIndex;
                return false;
            });
            if (element.hasClass('active')) {
                //fmaSelectionMade = elementIndex;
            }
        });
        
        rotateFMA(false);
    } catch(e) {
        window.setTimeout('initFMABrowser()', 500);
    }
}
function initHomepageSlideUpEffect() {
	if ($('youth-committee-box') != null) {
		$('youth-committee-box').addEvents({
			mouseenter: function(ev){
				//alert('mouse-enter');
				if (ev) 
					ev.stop();
                var slideUpEffect = new Fx.Morph($('yc-box-slide-up-content'), {
	                duration: 300
	            });
	            slideUpEffect.start({
					'margin-top':-200,
					'height':200,
					'overflow-y':'visible'
	                
	            });
				$('youth-committee-box-link').setStyle('background-position', 'top left');
			},
			mouseleave: function(ev){
				//alert('mouse-leave');
				if (ev) 
					ev.stop();
				
				var slideUpEffect = new Fx.Morph($('yc-box-slide-up-content'), {
                    duration: 300
                });
                slideUpEffect.start({
                    'height':0,
					'margin-top': 0,
                    'overflow-y':'hidden'
                });
				$('youth-committee-box-link').setStyle('background-position', 'bottom left');
			}
		});
	}
	if ($('membership-box') != null) {
	   $('membership-box').addEvents({
            mouseenter: function(ev){
                //alert('mouse-enter');
                if (ev) 
                    ev.stop();
                var slideUpEffect = new Fx.Morph($('membership-box-slide-up-content'), {
                    duration: 300
                });
                slideUpEffect.start({
                    'margin-top':-200,
                    'height':200,
                    'overflow-y':'visible'
                });
				$('membership-box-link').setStyle('background-position', 'top left');
            },
            mouseleave: function(ev){
                //alert('mouse-leave');
                if (ev) 
                    ev.stop();
                
                var slideUpEffect = new Fx.Morph($('membership-box-slide-up-content'), {
                    duration: 300
                });
                slideUpEffect.start({
                    'height':0,
                    'margin-top': 0,
                    'overflow-y':'hidden'
                });
				$('membership-box-link').setStyle('background-position', 'bottom left');
            }
        });
	}
}
window.addEvent('domready', function() {
    var myAccordion = new Fx.Accordion($$('.acc-action'), $$('.acc-reaction'), {
	    display: 2,
	    alwaysHide: true
    });
	initFMABrowser();
	
	initHomepageSlideUpEffect();
	
});
