﻿/**
 * @author Dan Entous
 * @version 2010-03-26 18.00 GMT +1
 */

var BannerRotator = {

    banner_count: 0,
    banner_to_rotate: 1,
    pe: null,    
    
    initialize: function() {
        
        $( 'banner1' ).setStyle({
            'opacity': '.1',
            'filter': 'alpha(opacity=01)'
        });
        $( 'banner1' ).appear({ duration: 2.0 });
        
        
        // Set-Up Banner Tabs
        $$( '#bannerTabs li' ).each( function( item ) {
               
            BannerRotator.banner_count++;
            if ( BannerRotator.banner_count == 1 ) {
                $( item.id ).removeClassName( 'bannerTabNotSelected' );
                $( item.id ).addClassName( 'bannerTabSelected' );
            }
            
            item.observe( 'click', function( event ) {
                Event.stop( event );
            });
            
            item.observe( 'mouseenter', function(event) {
                BannerRotator.toggleBanners( item.id.substr(9) );
                BannerRotator.pe.stop();
            });
            
            item.observe( 'mouseleave', function( event ) {
                BannerRotator.banner_to_rotate = item.id.substr(9);
                BannerRotator.timer();
            });
        });
        
                
        // Banner Divs & Timer
        if ( BannerRotator.banner_count > 1 ) {
            $$( '#banners .banner' ).each(function( item ) {
                item.observe( 'mouseenter', function( event ) {
                    BannerRotator.pe.stop();
                });
                
                item.observe( 'mouseleave', function(event) {
                    BannerRotator.timer();
                });
            });
            
            BannerRotator.timer();          
        }
        
    },
    
    toggleBanners: function( banner_id ) {
        
        $$("#bannerTabs li").each( function( item ) {
            if ( item.id.substr(9) == banner_id ) {
            	item.removeClassName('bannerTabNotSelected');
                item.addClassName('bannerTabSelected');
                
            } else {
				item.removeClassName('bannerTabSelected');
				item.addClassName( 'bannerTabNotSelected' );
                
            }
        });
        
        for ( var x = 1; x <= BannerRotator.banner_count; x++ ) {
            if ( x != BannerRotator.banner_id ) {
                $( "banner" + x ).hide();
            } 
        }
        
        $( 'banner' + banner_id ).appear({ duration: 2.0 });
    },
    
    timer: function() {
    
        BannerRotator.pe = new PeriodicalExecuter(        
            function( pe ) {
                if ( BannerRotator.banner_to_rotate == BannerRotator.banner_count ) {
                    BannerRotator.banner_to_rotate = 0;
                }
                
                BannerRotator.banner_to_rotate++;
                BannerRotator.toggleBanners( BannerRotator.banner_to_rotate );                    
                
            },
            10            
        );
    }
};
