window.onload = function(){ sa_Slider.initialize(); }

/**
* Step Aside - Slider
*
* Element slider
* category: dhtml
* copyright: (c) 2009 Mathias Petersson
* website: www.stepaside.se
* version: 1.0 (2009-02-25)
* license: Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
var sa_Slider =
{
    /**
    * Slider settings
    *   Values must be set
    *   Optional values can be empty or 0 (depending on variable types)
    */
    settings :
    {
        /**
        * containerID:string
        *   Element ID of slider
        *
        * required
        */
        containerID : 'container'
        
        /**
        * childContainerID:string
        *   Element ID of the box containing slide elements
        *
        * required
        */
        ,childContainerID : 'child_container'
        
        /**
        * childNodeName:string
        *   Node name of slide elements
        *
        * required
        */
        ,childNodeName : 'div'
        
        /**
        * childClassName:string
        *   Class name of slide elements
        *   If set, only slide elements with corresponding class name will be fetched
        *
        * optional
        */
        ,childClassName : 'child'
        
        /**
        * childMargin:int
        *   Slide elements margin to each other
        *
        * optional
        */
        ,childMargin : 10
        
        /**
        * slideMode:string
        *   How to slide
        *   Possible values:
        *     left, right, multi
        *
        * required
        */
        ,slideMode : 'multi'
        
        /**
        * refreshRate:int
        *   How often to run the slide in milliseconds
        *
        * required
        */
        ,refreshRate : 30
        
        /**
        * childEventOnClick:function
        *   Defines what to happen when a slide elements get clicked
        *
        * optional
        */
        ,childEventOnClick : ''
        
        /**
        * fixIEPNG:boolean
        *   If to fix transparency of 24-bits PNG images
        *
        * required
        */
        ,fixIEPNG : true
        
        /**
        * transparentPNGClassName:string
        *   Class name of PNG that should be IE-fixed for it's transparency
        *   If empty, all PNGs will be "fixed"
        *
        * optional
        */
        ,transparentPNGClassName : 'transparent_png'
        
        /**
        * overlay:class
        */
        ,overlay :
        {
            top :
            {
                /**
                * overlay
                *   id:string
                *     Element ID
                *   display:boolean
                *     If to display overlay or not
                */
                id : 'sa_Slider-ol_top'
                ,display : true
            }
            ,bottom :
            {
                id : 'sa_Slider-ol_bottom'
                ,display : true
            }
            ,left :
            {
                id : 'sa_Slider-ol_left'
                ,display : true
            }
            ,right :
            {
                id : 'sa_Slider-ol_right'
                ,display : true
            }
        }
    }
    
    /**
    * Slide modes and mode settings
    */
    ,modes :
    {
        /**
        * Slide mode:multi
        *   Slide possible in both left and right direction
        *   Controlled by visitor interaction with mouse
        */
        multi :
        {
            settings :
            {
                slideSpeed : 1
                
                /**
                * varSpeed:array
                *   Array containing various speeds
                *   Each array element consists of a class
                *     Each class consists of 'area' and 'speed' with a descending sorting (first element > last element)
                *       area: greater then 0 and less then 1 (procent of slide container) sets the area which the mouse corresponds to
                *       speed: slide speed to use within the area
                *
                * required
                */
                ,varSpeed :
                [
                    {area : 0.8, speed : 5}
                    ,{area : 0.6, speed : 4}
                    ,{area : 0.4, speed : 3}
                    ,{area : 0.1, speed : 2}
                    ,{area : 0, speed : 0}
                ]
                
                /**
                * initialSlideDirection:string
                *   Initial slide direction
                *   Possible values:
                *     left, right
                *
                * required
                */
                ,initialSlideDirection : 'left'
            }
        }
        
        /**
        * Slide mode:left
        *   Slide to left
        */
        ,left :
        {
            settings :
            {
                slideSpeed : 1
            }
        }
        
        /**
        * Slide mode:right
        *   Slide to right
        */
        ,right :
        {
            settings :
            {
                slideSpeed : 1
            }
        }
    }
    /**
    * Slider runtime settings
    *   Don't modify these
    *
    * static / script controlled
    */
    ,runtime :
    {
        isBusy : false
        ,childContainer : null
        ,childContainerWidth : 0
        ,container : null
        ,containerWidth : 0
        ,enableSlide : true
        ,nextChildMove : null
        ,slideSpeed : 0
        ,slideDirection : null
        ,slideInterval : null
        ,scrollCenter : null
        ,currentChildWidth : 0
        ,mouse :
        {
            x : null
            ,y : null
        }
    }

    ,initialize : function()
    {
        var objContainer = sa_Slider.ref(sa_Slider.settings.containerID);
        var objChildContainer = sa_Slider.ref(sa_Slider.settings.childContainerID);
        if(objContainer.found && objChildContainer.found)
        {
            sa_Slider.runtime.container = objContainer.dom;
            sa_Slider.runtime.childContainer = objChildContainer.dom;
            sa_Slider.runtime.containerWidth = sa_Slider.dimensions(sa_Slider.runtime.container).width;
            sa_Slider.runtime.scrollCenter = Math.floor(sa_Slider.runtime.containerWidth / 2);
            sa_Slider.runtime.container.style.cssText = 'overflow: hidden; visibility: visible;';
            sa_Slider.runtime.slideDirection = sa_Slider.settings.slideMode;
            var cssText, containerHeight, widestChildWidth = 0, childWidth = 0, childList = sa_Slider.runtime.container.getElementsByTagName(sa_Slider.settings.childNodeName);
            for(var i = 0, m = childList.length; i < m; ++i)
            {
                if(sa_Slider.settings.childClassName == null || childList[i].className == sa_Slider.settings.childClassName)
                {
                    childWidth = sa_Slider.dimensions(childList[i]).width;
                    if(sa_Slider.settings.childEventOnClick != '')
                    {
                        childList[i].onclick = eval(sa_Slider.settings.childEventOnClick);
                    }
                    sa_Slider.runtime.childContainerWidth += childWidth + sa_Slider.settings.childMargin;
                    if(childWidth > widestChildWidth)
                    {
                        widestChildWidth = childWidth;
                    }
                    childList[i].style.cssText = 'float: left; margin: 0px ' + sa_Slider.settings.childMargin + 'px 0px 0px;';
                }
            }
            if(sa_Slider.runtime.childContainerWidth < sa_Slider.runtime.containerWidth + widestChildWidth + sa_Slider.settings.childMargin)
            {
                sa_Slider.runtime.childContainerWidth = sa_Slider.runtime.containerWidth + widestChildWidth + sa_Slider.settings.childMargin;
            }
            cssText = 'width: ' + sa_Slider.runtime.childContainerWidth + 'px; ';
            cssText += 'margin: 0px 0px 0px -' + parseInt((sa_Slider.runtime.childContainerWidth - sa_Slider.runtime.containerWidth) / 2) + 'px';
            sa_Slider.runtime.childContainer.style.cssText = cssText;
            containerHeight = sa_Slider.dimensions(sa_Slider.runtime.container).height;
            for(var i = 0, m = childList.length; i < m; ++i)
            {
                if(sa_Slider.settings.childClassName == null || childList[i].className == sa_Slider.settings.childClassName)
                {
                    childList[i].style.height = containerHeight + 'px';
                }
            }
            for(var overlay in sa_Slider.settings.overlay)
            {
                if(sa_Slider.settings.overlay[overlay].id != '')
                {
                    if((ref = sa_Slider.ref(sa_Slider.settings.overlay[overlay].id)).found)
                    {
                        if(!sa_Slider.settings.overlay[overlay].display)
                        {
                            ref.style.display = 'none';
                        }
                        else
                        {
                            cssText = 'position: absolute; ';
                            switch(overlay)
                            {
                                case 'top':
                                    cssText += 'left: 0px; top: 0px;';
                                break;
                                case 'bottom':
                                    cssText += 'left: 0px; top: ' + (containerHeight - sa_Slider.dimensions(ref.dom).height) + 'px;';
                                break;
                                case 'left':
                                    cssText += 'left: 0px; top: 0px;';
                                break;
                                case 'right':
                                    cssText += 'right: 0px; top: 0px;';
                                break;
                            }
                            ref.style.cssText = cssText;
                        }
                    }
                }
            }
            if(sa_Slider.settings.fixIEPNG)
            {
                sa_Slider.fixIEPNG();
            }
            if(sa_Slider.runtime.slideDirection == 'multi')
            {
                sa_Slider.runtime.slideDirection = sa_Slider.modes.multi.settings.initialSlideDirection;
            }
            sa_Slider.alterSlideState(true);
        }
    }
    ,initializeMultiSlide : function()
    {
        sa_Slider.runtime.container.onmouseover = null;
        document.onmousemove = sa_Slider.multiSlideSettings;
    }
    ,resetMultiSlide : function()
    {
        sa_Slider.runtime.slideSpeed = sa_Slider.modes[sa_Slider.settings.slideMode].settings.slideSpeed;
        sa_Slider.runtime.container.onmouseover = sa_Slider.initializeMultiSlide;
        document.onmousemove = null;
    }
    ,multiSlideSettings : function(e)
    {
        if(!e)
        {
            var e = window.event;
        }
        var containerDimensions = sa_Slider.dimensions(sa_Slider.runtime.container);
        var mousePos = sa_Slider.getMousePos(e);
        sa_Slider.runtime.mouse.x = mousePos.x;
        sa_Slider.runtime.mouse.y = mousePos.y;
        var speed = sa_Slider.modes.multi.settings.slideSpeed;
        var mod = sa_Slider.runtime.mouse.x - containerDimensions.left - sa_Slider.runtime.scrollCenter;
        var ref = sa_Slider.runtime.containerWidth - sa_Slider.runtime.scrollCenter;
        if(mod > 0)
        {
            if(sa_Slider.runtime.slideDirection != 'left')
            {
                sa_Slider.runtime.slideDirection = 'left';
                sa_Slider.runtime.currentChildWidth = 0;
            }
            for(i = 0, m = sa_Slider.modes.multi.settings.varSpeed.length; i < m; ++i)
            {
                if(parseInt(ref * sa_Slider.modes.multi.settings.varSpeed[i].area) <= mod)
                {
                    speed = sa_Slider.modes.multi.settings.varSpeed[i].speed;
                    break;
                }
            }
        }
        else
        {
            if(sa_Slider.runtime.slideDirection != 'right')
            {
                sa_Slider.runtime.slideDirection = 'right';
                sa_Slider.runtime.currentChildWidth = 0;
            }
            for(i = 0, m = sa_Slider.modes.multi.settings.varSpeed.length; i < m; ++i)
            {
                if(parseInt(ref * sa_Slider.modes.multi.settings.varSpeed[i].area) * -1 >= mod)
                {
                    speed = sa_Slider.modes.multi.settings.varSpeed[i].speed;
                    break;
                }
            }
        }
        if(sa_Slider.runtime.mouse.x < containerDimensions.left || sa_Slider.runtime.mouse.y < containerDimensions.top || sa_Slider.runtime.mouse.x > containerDimensions.left + containerDimensions.width || sa_Slider.runtime.mouse.y > containerDimensions.top + containerDimensions.height)
        {
            sa_Slider.resetMultiSlide();
        }
        else
        {
            if(speed != sa_Slider.runtime.slideSpeed)
            {
                sa_Slider.runtime.slideSpeed = speed;
            }
        }
    }
    ,alterSlideState : function(state)
    {
        if(!sa_Slider.runtime.isBusy)
        {
            if(state == undefined)
            {
                sa_Slider.runtime.enableSlide = sa_Slider.runtime.enableSlide ? false : true;
            }
            else
            {
                sa_Slider.runtime.enableSlide = state;
            }
            if(sa_Slider.runtime.enableSlide)
            {
                sa_Slider.setInterval();
            }
            else
            {
                sa_Slider.clearInterval();
            }
        }
    }
    ,clearInterval : function()
    {
        if(sa_Slider.runtime.slideSpeed != 0)
        {
            clearInterval(sa_Slider.runtime.slideInterval);
        }
        sa_Slider.runtime.slideSpeed = 0;
        sa_Slider.runtime.currentChildWidth = 0;
        sa_Slider.runtime.nextChildMove = null;
        if(sa_Slider.settings.slideMode == 'multi')
        {
            sa_Slider.runtime.container.onmouseover = null;
            document.onmousemove = null;
        }
    }
    ,setInterval : function()
    {
        sa_Slider.runtime.slideSpeed = sa_Slider.modes[sa_Slider.settings.slideMode].settings.slideSpeed;
        sa_Slider.runtime.slideInterval = setInterval('sa_Slider.slide()', sa_Slider.settings.refreshRate);
        if(sa_Slider.settings.slideMode == 'multi')
        {
            sa_Slider.resetMultiSlide();
        }
    }
    ,slide : function()
    {
        var childContainerOffsetLeft = sa_Slider.runtime.childContainer.offsetLeft;
        switch(sa_Slider.runtime.slideDirection)
        {
            case 'left':
                sa_Slider.runtime.childContainer.style.marginLeft = (childContainerOffsetLeft - sa_Slider.runtime.slideSpeed) + 'px';
                if(sa_Slider.runtime.currentChildWidth == 0)
                {
                    sa_Slider.runtime.nextChildMove = sa_Slider.getFirstChild(sa_Slider.runtime.childContainer, 'DIV', sa_Slider.settings.childClassName);
                    sa_Slider.runtime.currentChildWidth = sa_Slider.dimensions(sa_Slider.runtime.nextChildMove).width;
                }
                if(parseInt((sa_Slider.runtime.childContainerWidth - sa_Slider.runtime.containerWidth) / 2) * -1 - sa_Slider.runtime.currentChildWidth > childContainerOffsetLeft)
                {
                    sa_Slider.runtime.childContainer.appendChild(sa_Slider.runtime.nextChildMove);
                    sa_Slider.runtime.childContainer.style.marginLeft = (childContainerOffsetLeft + sa_Slider.runtime.currentChildWidth + sa_Slider.settings.childMargin) + 'px';
                    sa_Slider.runtime.currentChildWidth = 0;
                }
            break;
            case 'right':
                sa_Slider.runtime.childContainer.style.marginLeft = (childContainerOffsetLeft + sa_Slider.runtime.slideSpeed) + 'px';
                if(sa_Slider.runtime.currentChildWidth == 0)
                {
                    sa_Slider.runtime.nextChildMove = sa_Slider.getLastChild(sa_Slider.runtime.childContainer, 'DIV', sa_Slider.settings.childClassName);
                    sa_Slider.runtime.currentChildWidth = sa_Slider.dimensions(sa_Slider.runtime.nextChildMove).width;
                }
                if(parseInt((sa_Slider.runtime.childContainerWidth - sa_Slider.runtime.containerWidth) / 2) * -1 < childContainerOffsetLeft - sa_Slider.runtime.currentChildWidth)
                {
                    sa_Slider.runtime.childContainer.insertBefore(sa_Slider.runtime.nextChildMove, sa_Slider.getFirstChild(sa_Slider.runtime.childContainer, 'DIV', sa_Slider.settings.childClassName));
                    sa_Slider.runtime.childContainer.style.marginLeft = (childContainerOffsetLeft - sa_Slider.runtime.currentChildWidth - sa_Slider.settings.childMargin) + 'px';
                    sa_Slider.runtime.currentChildWidth = 0;
                }
            break;
        }
    }
    ,ref : function(id)
    {
        if(document.getElementById(id))
        {
            res =
            {
                found : true
                ,dom : document.getElementById(id)
                ,style : document.getElementById(id).style
            }
        }
        else
        {
            res = { found : false }
        }

        return res;
    }
    ,dimensions : function(obj)
    {
        var result =
        {
            left : 0
            ,top : 0
            ,width : obj.offsetWidth
            ,height : obj.offsetHeight
        }
        if(obj.offsetParent)
        {
            do
            {
                result.left += obj.offsetLeft;
                result.top += obj.offsetTop;
            }
            while(obj = obj.offsetParent);
        }
        return result;
    }
    ,getFirstChild : function(container, nodeName, className)
    {
        if(!nodeName){ var nodeName = 'DIV'; }
        if(!className){ var className = ''; }
        nodeName = nodeName.toUpperCase();
        var child = null, list = container.getElementsByTagName(nodeName);
        for(var i = 0, m = list.length; i < m; ++i)
        {
            if(className != '')
            {
                if(list[i].className == className)
                {
                    child = list[i];
                    break;
                }
            }
            else
            {
                child = list[i];
                break;
            }
        }
        return child;
    }
    ,getLastChild : function(container, nodeName, className)
    {
        if(!nodeName){ var nodeName = 'DIV'; }
        if(!className){ var className = ''; }
        nodeName = nodeName.toUpperCase();
        var child = null, childList = container.getElementsByTagName(nodeName);
        for(var i = childList.length - 1; i >= 0; --i)
        {
            if(className != '')
            {
                if(childList[i].className == className)
                {
                    child = childList[i];
                    break;
                }
            }
            else
            {
                child = childList[i];
                break;
            }
        }
        return child;
    }
    ,getMousePos : function(e)
    {
        if(!e)
        {
            var e = window.event;
        }
        var pos = { x : 0, y : 0 }
        if(e.pageX || e.pageY)
        {
            pos.x = e.pageX;
            pos.y = e.pageY;
        }
        else if(e.clientX || e.clientY)
        {
            pos.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            pos.y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        }
        return pos;
    }
    ,fixIEPNG : function ()
    {
        var arVersion = navigator.appVersion.split("MSIE");
        var version = parseFloat(arVersion[1]);
        if((version >= 5.5) && (document.body.filters))
        {
            var images = sa_Slider.runtime.container.getElementsByTagName('IMG');
            for(var i = 0; i < images.length; ++i)
            {
                var img = images[i];
                var imgName = img.src.toUpperCase();
                if((sa_Slider.settings.transparentPNGClassName == '' || sa_Slider.settings.transparentPNGClassName == img.className) && imgName.substring(imgName.length - 3, imgName.length) == "PNG")
                {
                    var imgID = (img.id) ? "id='" + img.id + "' " : "";
                    var imgClass = (img.className) ? "class='" + img.className + "' " : "";
                    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
                    var imgStyle = "display:inline-block;" + img.style.cssText;
                    if(img.align == "left") imgStyle = "float:left;" + imgStyle;
                    if(img.align == "right") imgStyle = "float:right;" + imgStyle;
                    if(img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
                    var strNewHTML = "<span " + imgID + imgClass + imgTitle
                    + " style=\"font-size:1px !important;" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                    + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                    + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
                    img.outerHTML = strNewHTML;
                    i = i - 1;
                }
            }
        }
    }
}
