jQuery(document).ready(function($) {
    /* Index Slider */
    if (typeof $.fn.indexSlider != 'undefined') {
        $('#index #slider').indexSlider();
    }

    /* Cufon */
    if (typeof Cufon != 'undefined') {
        Cufon.replace('#top-menu > li > a', {
            fontFamily: 'MetaMediumC'/*,
            textShadow: '#0d0d0d 0 -1px'*/
        });

        Cufon.replace('#bottom-menu li a', {
            fontFamily: 'MetaMediumC',
            textShadow: '#f1f1f1 0 -1px',
            hover:      true
        });

        Cufon.replace('#sidebar menu li a', {fontFamily: 'MetaMediumC'});

        Cufon.replace('#header h1', {fontFamily: 'PF Agora Slab Pro'});
        Cufon.replace('#header > span', {fontFamily: 'MetaMediumC'});

        Cufon.replace('#content .daily h3:first-child', {fontFamily: 'MetaMediumC'});
        Cufon.replace('#content .daily h3:first-child b', {fontFamily: 'MetaBlackC'});
        Cufon.replace('#content .market-menu h4', {fontFamily: 'MetaMediumC'});
        Cufon.replace('#content form button, #content a.button', {
            fontFamily: 'MetaMediumC',
            textShadow: '#350b09 0 -1px'
        });

        /* Index */
        Cufon.replace('#index #sidebar .block h3', {
            fontFamily: 'MetaMediumC',
            textShadow: '#000 0 -1px'
        });

        Cufon.replace('#index #flag a span', {
            fontFamily: 'MetaMediumC',
            textShadow: '#000 0 -1px'
        });
        Cufon.replace('#index #flag a b', {
            fontFamily: 'MetaBlackC',
            textShadow: '#000 0 -1px'
        });
        $('#index #flag a').hover(function() {
            Cufon.replace('#index #flag a span', {
                fontFamily: 'MetaMediumC',
                textShadow: '#000 0 -1px',
                color: '#d03328'
            });
            Cufon.replace('#index #flag a b', {
                fontFamily: 'MetaBlackC',
                textShadow: '#000 0 -1px',
                color: '#d03328'
            });
        }, function() {
            Cufon.replace('#index #flag a span', {
                fontFamily: 'MetaMediumC',
                textShadow: '#000 0 -1px',
                color: '#fff'
            });
            Cufon.replace('#index #flag a b', {
                fontFamily: 'MetaBlackC',
                textShadow: '#000 0 -1px',
                color: '#fff'
            });
        });

        Cufon.replace('#index #slider .slide h1, #index #slider .slide a', {
            fontFamily: 'PF Agora Slab Pro',
            textShadow: '#999 0 -1px'
        });
        Cufon.replace('#index #slider ul.pagination li a', {
            fontFamily: 'MetaMediumC',
            hover:      true
        });

        Cufon.now();
    }

    /* Search Input */
    $('#top form input#s').each(function() {
        var $this = $(this);
        var text = $this.attr('value');

        $this.focusin(function() {
            if ($this.attr('value') == text) {
                $this.attr('value', '');
            }
        }).focusout(function() {
            if ($this.attr('value') == '') {
                $this.attr('value', text);
            }
        });
    });

    /* Top Menu */
    $(function() {
        var $topMenu = $('#top-menu');

        $topMenu.find('> li > menu.columns').each(function() {
            var $menu = $(this);

            var $liList = $menu.children('li').detach();
            $menu.append($('<div class="column-2"></div>').append($liList.filter(':even')))
                    .append($('<div class="column-2"></div>').append($liList.filter(':odd')));
        });

        $topMenu.find('> li > menu').each(function() {
            var $menu = $(this);
            $menu.css({
                width: ($topMenu.outerWidth() - ($menu.innerWidth() - $menu.width())) + 'px',
                left:  '-' + $menu.parent().position().left + 'px'
            });

            $menu.find('li > div').each(function() {
                var $div = $(this);
                var $li = $div.parent();

                $div.prev('a').hover(function() {
                    $div.stop().fadeTo(250, 1);
                }, function() {
                    $div.stop().fadeTo(250, 0, function() {
                        $div.css({display: 'none'});
                    });
                });
            });

            $menu.css({
                visibility: 'visible',
                display:    'none'
            });
            if (!($.browser.msie && $.browser.version < 9)) {
                $menu.parent('li').hover(function() {
                    $menu.stop().fadeTo(250, 1);
                }, function() {
                    $menu.stop().fadeTo(250, 0, function() {
                        $menu.css({display: 'none'});
                    });
                });
            } else {
                $menu.parent('li').hover(function() {
                    $menu.css({display: 'block'});
                }, function() {
                    $menu.css({display: 'none'});
                });
            }

            $('div.column-2', $menu).css({height: $menu.height() + 'px'});
        });
    });

    /* Sidebar Menu li.active background */
    $('#sidebar menu li.active').each(function() {
        var $li = $(this);
        var bgHeight = $li.find('a:first-child').outerHeight() + ($li.innerHeight() - $li.height());
        $li.css({
            backgroundPosition: 'left -' + (150 - bgHeight) + 'px'
        });
    });

    /* List of Cities (Map) */
    $('#content table.branch ul').each(function() {
        var liLeft = 0;
        $(this).find('li').each(function() {
            var $li = $(this);
            var curLiLeft = $li.position().left;
            if (curLiLeft < liLeft) {
                $li.css({
                    paddingLeft: 0,
                    borderLeft:  'none',
                    marginLeft:  0
                });
            }
            liLeft = curLiLeft;
        });
    });

    /* Content image shadow */
    if (typeof $.fn.boxShadow != 'undefined') {
        $('#content img.shadow').boxShadow(1, 1, 5, '#a6a6a6');
    }

    /* Striped table background */
    $(function() {
        var $trList = $('#content table.striped tbody tr');
        var i = 0;
        while($trList.slice(6 * i + 3, 6 * (1 + i++)).addClass('even').length);
    });

    /* Icons */
    $('#header > span, #content .news p, #index #sidebar .news div a, #top-menu li.arrow a').append('<i></i>');

    /* Required form field icon */
    $(function() {
        var $rList = $('#content form table td .required');
        $rList.each(function() {
            var $r = $(this);
            $r.parentsUntil('form').find('label[for="' + $r.attr('id') + '"]').before('<i></i>');
        });
    });

    /* Right Menu Tooltip */
    $('#content .market-menu li a + div').each(function() {
        var $tooltip = $(this);
        var $li = $tooltip.parent('li');
        var $a = $tooltip.prev('a');
        $tooltip.css({
            top : (-$tooltip.outerHeight() + 5) + 'px',
            left: (-18) + 'px'
        });
        if (!($.browser.msie && $.browser.version < 9)) {
            $a.hover(function(e) {
                $tooltip.stop().fadeTo(250, 1);
            }, function() {
                $tooltip.stop().fadeTo(250, 0, function() {
                    $tooltip.css({display: 'none'});
                });
            });
        } else {
            $a.hover(function(e) {
                $tooltip.css({display: 'block'});
            }, function() {
                $tooltip.css({display: 'none'});
            });
        }
    });

    /* Calendar */
    /*if (typeof $.fn.datepicker != 'undefined') {
        var today = new Date();
        $('#content input[type="text"].calendar').datepicker({
    		showOn: 'button',
    		buttonImage: '/img/calendar-icon.gif',
    		buttonImageOnly: true,
            buttonText: '',
            showAnim: ($.browser.msie ? '' : 'fadeIn'),
            beforeShow: function(input, inst) {
                $(inst.dpDiv).show(function() {
                    // Так должно быть))
                });
            }
    	}).attr('readonly', 'readonly').focus(function(e) {
    	   e.preventDefault();
        }).datepicker('setDate', '0');
    }*/

    /* Slider */
    /*if (typeof $.fn.slider != 'undefined') {
    	$('#content .slider-range-min').each(function() {
            var $slider = $(this).wrap('<div class="slider"></div>');
            var $sValue = $('<span class="value"></span>');
            var prefix = $slider.attr('prefix') || '';
            var suffix = $slider.attr('suffix') || '';
            $slider.append($sValue);
            $slider.slider({
                range: 'min',
                min: parseFloat($slider.attr('min')) || 0,
                max: parseFloat($slider.attr('max')) || 1,
                step: parseFloat($slider.attr('step')) || 1,
                value: parseFloat($slider.attr('value')) || 0,
                slide: function(event, ui) {
                	$sValue.text(prefix + ui.value + suffix);
                    setTimeout(function() {
                        $sValue.css({left: ($(ui.handle).position().left - 15) + 'px'});
                    }, 20);
                },
                change: function(event, ui) {
                    $sValue.css({left: ($(ui.handle).position().left - 15) + 'px'});
                }
            });
            $sValue.text(prefix + $slider.slider('value') + suffix);
            $sValue.css({left: ($slider.find('a.ui-slider-handle').position().left - 15) + 'px'});
    	});
     }*/

    $('#main .questionnaire').each(function() {
        var $questionnaire = $(this);
        var $ul = $questionnaire.find('.progress ul.step-list')
        var $liList = $ul.find('li');

        var sumWidth = 0;
        $liList.each(function() {
            sumWidth += $(this).outerWidth();
        }).filter(':gt(0)').each(function() {
            $(this).css({
                paddingLeft: Math.floor(($ul.width() - sumWidth) / ($liList.length - 1)) + parseInt($(this).css('paddingLeft'))
            });
        });

        sumWidth = 0;
        var i;
        for (i = 0; i < $liList.length; i++) {
            var $li = $($liList[i]);
            sumWidth += $li.outerWidth(true);
            if ($li.attr('step') == $li.closest('.questionnaire').attr('step')) {
                break;
            }
        }
        $questionnaire.find('.progress .progress-bar .value').css({
            width: i == $liList.length - 1 ? '100%' : sumWidth + 'px'
        });
    });

    /* IE7*/
    if ($.browser.msie && $.browser.version == 7) {
        $('#content table.side tbody tr:last-child td').css({paddingBottom: '11px'});
        $('#top #top-menu li:last-child a').css({borderRight: 'none'});
        $('#top #top-menu li:last-child').hover(function() {
            $(this).toggleClass('last-child');
        });
        $('#content .income select').each(function() {
            var $this = $(this);
            $this.next().after('<div class="clear"></div>');
        });
        $('#content .income-2 .result').wrapInner('<div></div>');
    }

    $(window).load(function() {
        $('#ajax-loader').hide();
    });

    var hMin = function() {
        $('#main').css({
            minHeight: $(window).height() - $('#top-container').outerHeight(true) - $('#footer').outerHeight(true)
        });
    };

    if (!$('body#index').length) {
        $(window).resize(hMin);
        $(document).ready(hMin);
    }
});
