function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}

jQuery(document).ready(function(){

    /* Меню */
    jQuery('#menu LI:first').css('background', 'none');

    
    /* Динамичный блок */
    if (jQuery('#dynamicBox').length) {
        var dynamicBox_height = jQuery('#dynamicBox .box:visible').height();
        jQuery('#dynamicBox').css('height', dynamicBox_height);

        var dynamicBox_current = 0;
        var dynamicBox_prevoius = 0;
        var dynamicBox_amount = jQuery('#dynamicBox DIV.box').length - 1;
        
        // навигация
        jQuery('#dynamicBox').before('<div style="text-align:center;"><div id="dynamicBox-navigation"><a id="dynamicBox-navigation-active"></a></div></div>');
        for (var i = 0; i < dynamicBox_amount; i++) {
            jQuery('#dynamicBox-navigation').append('<a></a>');
        };    
        
        jQuery('#dynamicBox-navigation A').click(
            function() {
                var this_id = jQuery(this).attr('id');
                var this_index = jQuery(this).index();
                
                // меняем высоту
                var height = jQuery('#dynamicBox .box:eq('+ this_index +')').height();
                jQuery('#dynamicBox').animate({'height': height}, 700);
                
                // проверяем чтобы нажатый пункт был не активным
                if ( this_id != 'dynamicBox-navigation-active' ) {
                    jQuery('#dynamicBox-navigation-active').removeAttr('id');
                    jQuery(this).attr('id', 'dynamicBox-navigation-active');
                    
                    jQuery('#dynamicBox DIV.box:eq('+ dynamicBox_prevoius +')').fadeOut(1000);
                    jQuery('#dynamicBox DIV.box:eq('+ this_index +')').hide().css('top', 0).fadeIn(1000);
                    
                    dynamicBox_current = this_index;
                    dynamicBox_prevoius = this_index;
                };
                
                return false;
            }
        );
        
        // слайдшоу
        var slideShowInterval = 5000;
        function sliderSlideShow() {
            dynamicBox_current++;
            if (dynamicBox_current > dynamicBox_amount) {dynamicBox_current = 0};

            jQuery('#dynamicBox-navigation A:eq(' + dynamicBox_current + ')').click();
        };
        
        var slideShow = setInterval(sliderSlideShow, slideShowInterval);
        
        // при наведении останавливаем слайдшоу
        jQuery('#dynamicBox, #dynamicBox-navigation').hover(
            function() {clearInterval(slideShow);},
            function() {slideShow = setInterval(sliderSlideShow, slideShowInterval);}
        );
    };

    
    /* Подробнее - скрыть */
    jQuery('.withHiddenText').find('.moreLink A').click(
        function() {
            var parents = jQuery(this).parents('.withHiddenText');
            
            parents.find('.moreLink').hide();
            
            if (jQuery.browser.msie && jQuery.browser.version < 9) {
                parents.find('.hiddenText').show();
            } else {
                parents.find('.hiddenText').fadeIn(300);
            };

        }
    );
    
    
    /* Смена фона */
    jQuery('#backgroundMenu B').click(
        function() {
            if ( !jQuery(this).hasClass('active') ) {
                jQuery('#backgroundMenu').find('.active').removeClass('active');
                jQuery(this).parent('DIV').addClass('active');
                
                var color = jQuery(this).css('backgroundColor');
                jQuery('.changeableBg').css('backgroundColor', color);

                expires = new Date();
                expires.setTime(expires.getTime() + (1000 * 86400 * 10000));
                setCookie('paspartuColor', color, expires, '/');
            };
        }
    );
    color = getCookie('paspartuColor');
    if (color)
    {
        if (jQuery('#backgroundMenu'))
        {
            jQuery('#backgroundMenu').find('.active').removeClass('active');
            
            jQuery('#backgroundMenu b').each(function(){
                if (jQuery(this).css('backgroundColor') == color)
                {
                    jQuery(this).parent('DIV').addClass('active');
                    jQuery('.changeableBg').css('backgroundColor', color);
                }
            });
        }
    }
    
    
    /* Язык */
    jQuery('#languageSmalled').hover(
        function() {
            jQuery(this).find('LI').stop(true, true).fadeIn(200);
        },
        function() {
            jQuery(this).find('LI:not(.active)').fadeOut(200);
        }
    );
    
    
    /* Переключатель (события) */
    /*jQuery('.switch').find('INS').click(
        function() {
            var currentIndex = jQuery(this).parents('.box').index();
            var parents = jQuery(this).parents('.switchWrapper');
            
            parents.find('.switch .box.active').removeClass('active');
            jQuery(this).parents('.box:first').addClass('active');

            parents.find('.switchContent').hide();
            parents.find('.switchContent:eq(' + currentIndex + ')').show();
            
            return false;
        }
    );*/

    
    /* Ховер картинки на заголовок */
    jQuery('.eventBox').find('.img A').hover(
        function() {
            jQuery(this).parents('.eventBox').find('H3 A').addClass('hover');
        },
        function() {
            jQuery(this).parents('.eventBox').find('H3 A').removeClass('hover');
        }
    );

    /* меню */
    var ul = jQuery('div#menu ul').get(0);
    var totalWidth = jQuery(ul).width();
    var liList = jQuery('li', ul);
//    var c = 0;
    for (i=0; i<liList.length; i++)
    {
        li = liList.get(i);
        /*if (i==0)
        {
            w = jQuery(li).width() + 18;
            jQuery('div', li).css('width', w+'px');
            totalWidth = totalWidth - jQuery(li).innerWidth();
        }
        else if (i == liList.length-1)
        {
            w = jQuery(li).width() + 19;
            jQuery('div', li).css('width', w+'px');
            totalWidth = totalWidth - jQuery(li).innerWidth();
        }
        else*/
        {
            totalWidth = totalWidth - jQuery(li).innerWidth();
//            c++;
        }
    }
    var additional = parseInt(totalWidth / liList.length);
    for (i=0; i<liList.length; i++)
    {
        li = liList.get(i);
        w = jQuery(li).width() + additional;
        jQuery('div', li).css('width', w+'px');
    }

    jQuery('#menu LI#menu-active div').css("behavior", "url('/bitrix/templates/inartis/js/pie.htc')");
    
    /*происходит выделение ссылки автора при наведении курсора на его изображение */
    jQuery('.authorFoto a img').hover(function () {   
             jQuery('.linkToAutor a').toggleClass("hover");
        },
        function () {
             jQuery('.linkToAutor a').toggleClass("hover"); 
        }
    );

//    behavior: url('/bitrix/templates/inartis/js/pie.htc')

    //jQuery('#menu').html(jQuery('#menu').html());
});
