(function($)
{
    $.fn.glider = function(options)
    {
        options = $.extend(
        {
            iAnimSpeed: 1000,
            sEasing: "easeInOutCirc",
            iMinHeight: 170
        }, options || {});
    
        return this.each(function()
        {
            var oScrollContainer = $(this).find('.scroller');
            var oScrollContent = $(this).find('.content');
            
            var running = false
            var iSectionWidth = 0;
            var aCurrentIndex = [];
            var sCurrentTextIndex = '';
            
            // sections
            $(this).find('.section').each(function(iNumIndex, sTextIndex)
            {
                // init
                aCurrentIndex[sTextIndex.id] = iNumIndex;
                if(sCurrentTextIndex == '')
                {
                    sCurrentTextIndex = sTextIndex.id;
                    iSectionWidth = $(this).width() + parseInt($(this).css('paddingLeft')) + parseInt($(this).css('paddingRight'));
                }
                
                // hash?
                if(sTextIndex.id == location.hash.replace('#', ''))
                {
                    sCurrentTextIndex = sTextIndex.id;
                }
            });

            // click
            $(this).find('.controls a').click(function(iNumIndex, sTextIndex)
            {
                moveTo(this.href.split("#")[1]);
                return false;
            });
            
            // init hash
            // location.hash = '';
            
            // init height
            iStartHeight = $('#' + sCurrentTextIndex).height();
            iStartHeight = (iStartHeight < options.iMinHeight) ? options.iMinHeight : iStartHeight;
            oScrollContainer.height(iStartHeight);
            
            // init left
            iStartLeft = (-aCurrentIndex[sCurrentTextIndex] * iSectionWidth);
            oScrollContent.css({left: iStartLeft + 'px'})

            // init active tab
            $('#' + sCurrentTextIndex + 'Tab').addClass('active');
    
            function moveTo(sToTextIndex)
            {
                if(!running && sCurrentTextIndex != sToTextIndex)
                {
                    running = true;
                    $('#' + sCurrentTextIndex + 'Tab').removeClass('active');
                    $('#' + sToTextIndex + 'Tab').addClass('active');
                    /* location.hash = sToTextIndex; */
                            
                    // height
                    iTargetHeight = $('#' + sToTextIndex).height();
                    iTargetHeight = (iTargetHeight < options.iMinHeight) ? options.iMinHeight : iTargetHeight;                   
                    oScrollContainer.animate(
                        { height: iTargetHeight + 'px' }, 
                        options.iAnimSpeed, 
                        options.sEasing,
                        function()
                        {
                            running = false;
                            sCurrentTextIndex = sToTextIndex;
                        }
                    );
                    
                    // left
                    iTargetLeft = (-aCurrentIndex[sToTextIndex] * iSectionWidth);
                    oScrollContent.animate(
                        { left: iTargetLeft + 'px' }, 
                        options.iAnimSpeed, 
                        options.sEasing,
                        function(){}
                    );
                }
                return false;
            };
        });
    };
})(jQuery);