$(function() {
    $('.faq-hide').css('display', 'none');

    /*if ($('#sensitive-faq').length > 0) {
        var _l = $('<ul><li><a href="#sensitive-faq"></a><li></ul>');
        $('#sensitive-faq').before(_l);
        
        var imgs = Array(3);
        
        imgs[0] = '/pics/bg_faq_3rdlevel_blue.png';
        imgs[1] = '/pics/bg_faq_2ndlevel_blue.png';
        imgs[2] = '/pics/bg_faq_active_blue.png';
        
        _l.hide().listrotator({ imgs : imgs }).children('li').children('a').click();
    }*/

    if($('#sensitive-faq').length > 0)
        $('#sensitive-faq').listrotator();
});

(function($) {
    
    $.fn.listrotator = function(opts) {
        
        var lst = $('<ul class="rotatingList"></ul>');
        
        var prev = $('<div class="rLPrev"><span>Prev</span></div>');
        var next = $('<div class="rLNext"><span>Next</span></div>');
        
        var box = $('<div class="rotatingBox"></div>');
        var box_heading = $('<h5 class="rBHeading"></h5>');
        var box_content = $('<p class="rBContent"></p>');
        
        var clearing = $('<div class="clear"></div>');
        
        $(this).after(lst);
        
        lst.before(prev);
        lst.after(next);
        
        next.after(box);
        box.append(box_heading);
        box.append(box_content);
        
        $(this).after(clearing);
        
        next.click(animateNext);
        prev.click(animatePrev);
        
        box.hide();
        prev.hide();
        next.hide();
        lst.hide();

        var _lstContent = $(this).children('li').clone(true);
        
        _lstContent.children('a').click(function(e) {
            e.preventDefault();
            var _anchor = $(this).attr('href');
            var _aa = _anchor.split('#');
            _anchor = '#' + _aa[_aa.length - 1];
            
            if(box.css('display') == 'block') {
                box.stop(true, true).fadeTo('fast', 0.01, function() {
                    box_heading.empty().append($(_anchor).html());
                    box_content.empty().append($(_anchor).next('dd').eq(0).html());
                    box.fadeTo('fast', 1);
                });
            }
            else {
                box_heading.empty().append($(_anchor).html());
                box_content.empty().append($(_anchor).next('dd').eq(0).html());
                box.fadeIn('fast');
            }
            
            var _i = lst.children('li:visible').index($(this).parent('li'));
            if (_i != -1 && _i != 2) {
                switch(_i) {
                    case 0: animateNext(); animateNext(); break;
                    case 1: animateNext(); break;
                    case 3: animatePrev(); break;
                    case 4: animatePrev(); animatePrev(); break;
                }
            }
            
        });
        
        
        var imgs = Array(3);

        imgs[0] = '/pics/bg_faq_3rdlevel_blue.png';
        imgs[1] = '/pics/bg_faq_2ndlevel_blue.png';
        imgs[2] = '/pics/bg_faq_active_blue.png';

        
        var opts = Array(5);
        
        opts[0] = {
            width: 246,
            height: 17,
            marginLeft: -123,
            top: 0,
            lineHeight: 17
        }
        
        opts[1] = {
            width: 341,
            height: 21,
            marginLeft: -171,
            top: 15,
            lineHeight: 21
        }
        
        opts[2] = {
            width: 394,
            height: 23,
            marginLeft: -197,
            top: 34,
            lineHeight: 23
        }
        
        opts[3] = clone(opts[1]);
        jQuery.extend(opts[3], { top: 56 });
        
        opts[4] = clone(opts[0]);
        jQuery.extend(opts[4], { top: 74 });
        
        if (lst.css('display') == 'block') 
            lst.fadeOut('fast', function(){
                lst.empty().append(_lstContent);
                positionChildren();
                showRotatingList();
            });
        else {
            lst.empty().append(_lstContent);
            positionChildren();
            showRotatingList();
        }
        
        var _animSpeed = 'normal';
        var _animEasing = 'linear';
        
        function showRotatingList() {
            prev.fadeIn('fast');
            lst.fadeIn('fast');
            next.fadeIn('fast');
        }
        
        function positionChildren() {
            lst.prepend(lst.children('li').eq(lst.children('li').length - 1));
            lst.prepend(lst.children('li').eq(lst.children('li').length - 1));
            
            var els = lst.children('li:lt(5)');
            
            lst.children('li').prepend('<img src="" />');
            
            els.eq(0).css('z-index', 10).children('a').css('font-size', '11px');
            els.eq(4).css('z-index', 10).children('a').css('font-size', '11px');
            
            els.eq(1).css('z-index', 20).children('a').css('font-size', '13px');
            els.eq(3).css('z-index', 20).children('a').css('font-size', '13px');
            
            els.eq(2).css('z-index', 30).children('a').css({ 'font-size': '18px', 'color': '#fff', 'font-weight': 'bold' } );
            
            changeImage(els.eq(0).animate(opts[0], 0).children('img'), 0);
            changeImage(els.eq(4).animate(opts[4], 0).children('img'), 0);
            
            changeImage(els.eq(1).animate(opts[1], 0).children('img'), 1);
            changeImage(els.eq(3).animate(opts[3], 0).children('img'), 1);
            
            changeImage(els.eq(2).animate(opts[2], 0).children('img'), 2);
            
            els.show();
            
            els.eq(2).children('a').click();
        }
        
        function animateNext() {
            if (lst.children('li:visible').length > 5)
                lst.children('li:visible:gt(4)').stop(true, true).hide();
            
            var children = lst.children('li:visible');
            
            lst.children('li:visible:last').stop().fadeOut(_animSpeed);
            
            children.eq(0).stop().animate(opts[1], _animSpeed, _animEasing,
                function() {
                    changeImage($(this).children('img'), 1);
                })
                .css('z-index', 20)
                .children('a').stop().animate({ fontSize: '13px' }, _animSpeed);
            
            children.eq(1).stop().animate(opts[2], _animSpeed, _animEasing,
                function() {
                    fadeImage($(this).children('img'), 2);
                    $(this).children('a').stop().animate({ 'color': '#ffffff' }, 'fast').css('font-weight', 'bold');
                })
                .css('z-index', 30)
                .children('a').stop().animate({ fontSize: '18px' }, _animSpeed);
            
            fadeImage(children.eq(2).children('img'), 1);
            children.eq(2).stop().animate(opts[3], _animSpeed, _animEasing)
                .css('z-index', 20)
                .children('a').stop().animate({ fontSize: '13px', 'color': '#62c5ee' }, _animSpeed).css('font-weight', 'normal');
            
            children.eq(3).stop().animate(opts[4], _animSpeed, _animEasing,
                function() {
                    changeImage($(this).children('img'), 0);
                })
                .css('z-index', 10)
                .children('a').stop().animate({ fontSize: '11px' }, _animSpeed).css('color', '#62c5ee');
            
            lst.prepend(lst.children('li').eq(lst.children('li').length - 1));
            
            lst.children('li').eq(0).stop().animate(opts[0], 0).css('z-index', 10).fadeIn(_animSpeed);
            changeImage(lst.children('li:visible').eq(0).children('img'), 0);
            
            children.eq(1).children('a').click();
        }
        
        function animatePrev() {
            if (lst.children('li:visible').length > 5)
                lst.children('li:visible:gt(4)').stop(true, true).hide();
            
            var children = lst.children('li:visible');
            
            lst.children('li:visible:first').stop().fadeOut(_animSpeed);
            
            children.eq(1).stop().animate(opts[0], _animSpeed, _animEasing,
                function() {
                    changeImage($(this).children('img'), 0);
                })
                .css('z-index', 10)
                .children('a').stop().animate({ fontSize: '11px' }, _animSpeed).css('color', '#62c5ee');
            
            fadeImage(children.eq(2).children('img'), 1);
            children.eq(2).stop().animate(opts[1], _animSpeed, _animEasing)
                .css('z-index', 20)
                .children('a').stop().animate({ fontSize: '13px', 'color': '#62c5ee' }, _animSpeed).css('font-weight', 'normal');
            
            children.eq(3).stop().animate(opts[2], _animSpeed, _animEasing,
                function() {
                    fadeImage($(this).children('img'), 2);
                    $(this).children('a').stop().animate({ 'color': '#ffffff' }, 'fast').css('font-weight', 'bold');
                })
                .css('z-index', 30)
                .children('a').stop().animate({ fontSize: '18px' }, _animSpeed);
            
            children.eq(4).stop().animate(opts[3], _animSpeed, _animEasing,
                function() {
                    changeImage($(this).children('img'), 1);
                })
                .css('z-index', 20)
                .children('a').stop().animate({ fontSize: '13px' }, _animSpeed);
            
            lst.append(lst.children('li').eq(0));
            
            changeImage(lst.children('li').eq(lst.children('li:visible').length - 1).children('img'), 0);
            lst.children('li').eq(lst.children('li:visible').length - 1).stop().animate(opts[4], 0).css('z-index', 10).fadeIn(_animSpeed);
            
            children.eq(3).children('a').click();
        }
        
        function changeImage(elem, index) {
            if (index >= 0 && index < imgs.length)
                elem.attr('src', imgs[index]);
        }
        
        function fadeImage(elem, index) {
            if (index >= 0 && index < imgs.length) {
                elem.before(elem.clone()).stop(true, true).hide().attr('src', imgs[index]).fadeIn('fast');
                elem.prev('img').eq(0).remove();
            }
        }
        
        if (window.location.hash != '' && window.location.hash != '#') {
            var _str = window.location.hash;
            
            var _item = lst.find('a[href*=' + _str + ']');

            if (_item.is(':hidden')) {
                while (_item.is(':hidden')) {
                    animateNext();
                }
            }
            _item.click();
        }
        
        return $(this);
    }
    
    function clone(obj){
        if(obj == null || typeof(obj) != 'object')
            return obj;

        var newObj = {};
        for(var key in obj)
            newObj[key] = clone(obj[key]);
        return newObj;
    }
    
})(jQuery);

