var splashHandler = {
    
    timeout : 8000,
    
    fadeSpeed : 2000,
    
    switchSpeed : 200,

	basePath : '',
    
    timer : 0,
    
    imageData : [        
        {
            url     : 'http://tek.phparch.com/',
            image   : 'splash_2.jpg'
        },
        
        {
            url     : '/magazine/index',
            image   : 'splash_1.jpg'
        },
        
        {
            url     : '/training/index',
            image   : 'splash_3.jpg'
        },

        {
            url     : '/books/index',
            image   : 'splash_4.jpg'
        }
    ],
    
    images : [
    ],
    
    imageCount : 0,
    
    buttons : [],

    init : function(aBasePath) {
        var i;
        var img;

		this.basePath = aBasePath;
        
        // Set click value
        
        $('#mainSplashContainer').click(splashHandler.handleClick);
        
        // Load button images
        
        $('#mainSplashButtons').css("left", 800 - splashHandler.imageData.length * 35);
        $('#mainSplashButtons').css('width', (35 * splashHandler.imageData.length) + 'px' );
        
        for (i = 0; i < splashHandler.imageData.length; i++) {
            img = $('<img class="mainSplashButton" id="mainSplashButton ' + (i + 1) + '" src="' + this.basePath + '/splash_nav_' + (i + 1) + '.gif" />');
            
            $(img).appendTo('#mainSplashButtons');
            $(img).hover (function() {
                this.src = this.src.replace(/.gif/, '_over.gif');
            }, function() {
                this.src = this.src.replace(/_over.gif/, '.gif');
            });
            $(img).click(splashHandler.handleButtonClick);
            
            splashHandler.buttons.push[img];
        }
        
        $('#mainSplashButtonBg').css('left', 800 - splashHandler.imageData.length * 35 - 3);
        if ($.browser.msie) {
            $('#mainSplashButtonBg').css('top', Number($('#mainSplashButtonBg').css('top').replace('px', '')) + 6);
        }
        $('#mainSplashButtonBg').css('opacity', '0.4');
        $('#mainSplashButtonBg').css('display', 'block');
        
        // Load splash images

        for (i = 0; i < splashHandler.imageData.length; i++) {
            img = $('<img class="mainSplash" src="' + this.basePath + "/" + splashHandler.imageData[i].image + '" />');
            img.load(splashHandler.splashLoad);
            splashHandler.images.push(img);
        }
    },
    
    handleClick : function() {
        window.location.href = splashHandler.imageData[splashHandler.imageCount % splashHandler.images.length].url;
    },

    splashLoad : function() {
        splashHandler.imageCount = splashHandler.imageCount + 1;

        $(this).appendTo("#mainSplashImages");
        $(this).css('opacity', 0.0);
        
        if (splashHandler.imageCount == splashHandler.images.length) {
            splashHandler.imageCount = 0;
            $(splashHandler.images[0]).animate({opacity: 1.0}, splashHandler.fadeSpeed);
            
            splashHandler.timer = setTimeout('splashHandler.changeSplash()', splashHandler.timeout);
        }
    },
    
    setSplash : function(splashId, speed) {
        if (splashId == splashHandler.imageCount) {
            // Can't force move to the same image
            
            return;
        }
        
        $(splashHandler.images[splashHandler.imageCount % splashHandler.images.length]).animate({opacity: 0.0}, speed);
        
        $(splashHandler.images[splashId % splashHandler.images.length]).animate({opacity: 1.0}, speed);
        splashHandler.imageCount = splashId;

        $('#mainSplashButtonBg').animate({left: 800 - splashHandler.imageData.length * 35 - 3 + (splashId % splashHandler.images.length) * 34}, speed);
        
        splashHandler.timer = setTimeout('splashHandler.changeSplash()', splashHandler.timeout);
    },
    
    changeSplash : function() {
        splashHandler.setSplash(splashHandler.imageCount + 1, splashHandler.fadeSpeed);
    },
    
    handleButtonClick : function(e) {
        var id = Number(this.src.replace(/[^\d]/g, '')) - 1;
        
        clearTimeout(splashHandler.timer);
        $(splashHandler.images[splashHandler.imageCount % splashHandler.images.length]).stop();
        splashHandler.setSplash(id, splashHandler.switchSpeed);
        
        e.stopPropagation();
    }
};