    var numOfLi = 6;
    
    if (screen.width > '1024') {
        numOfLi = 8;
    }
    
    $(document).ready(function(){ 
        
        
        $(".announcement").hover(function(){
            $(this).addClass("active");
            $(this).find(".announcementMenu").show();
        }, function(){
            $(this).removeClass("active");
            $(this).find(".announcementMenu").hide();
        });
        
        
        
        
        var liLength = $('.jselect li').length;
        $("#countSelect").html( liLength );
    
        if( liLength > 0 )
        {
            switchSelect(true);     
        }
       
    
    
    
        initUl();
        initButtons();

        
        $('#del_all_select').click(function() { 
            if( confirm( 'Вы действительно хотите удалить все объявления?' ) == true )
            {
                $.ajax({
                    url: "/announcement/del_all_select/ajax/",
                    type: "POST",
                    cache: false,
                    success: function(html)
                    {                                   
                        $(".jselect li").remove();
                        $("#bookmarks").fadeOut(); 
                    }
                });          
            }         
            return false;            
        });

        $('.selectLink').click(function() {                        
            
            $("#selectSlider .jselect").append('<li class="load"><img src="-fimages/ajax-loader.gif"></li>');
            initUl(); 
            showSelect();  
              
            if( $('#bookmarks:hidden') )
            {
                $('#bookmarks').fadeIn();
            }           
            
            var announcement_id = $(this).attr("title");
            
            $.ajax({
                url: "/announcement/select/"+announcement_id,
                type: "POST",
                cache: false,
                data: "announcement_id="+announcement_id,
                success: function(html)
                {                   
                    $(".load").remove();
                    if( html )
                    {
                        $("#selectSlider .jselect").append('<li>'+html+'</li>');
                    
                        $('.jselect li:last').animate({
                            opacity: 0.2
                        }, 500, function() {
                            $(this).animate({opacity: 1}, 500);
                        }); 
                        $("#selectSlider").scrollLeft(10000);  
                        
                        $("#countSelect").html( $('.jselect li').length );
                        
                                          
                    }
                    initUl();
                    initButtons();
                }
            });
            return false;
        });  
    

        
        $("#selectSliderButtonRight").click( function(){
            var left = $(".jselect").position().left - $(".jselect li").outerWidth(true);
            $("#selectSlider").scrollLeft(-left); 
            initButtons();        
        });  
        
        $("#selectSliderButtonLeft").click( function(){
            var left = $(".jselect").position().left + $(".jselect li").outerWidth(true);
            $("#selectSlider").scrollLeft(-left);
            initButtons();        
        });      
    });
    
    function hideSelect()
    {
        $("#bookmarks").attr("className", "hideSelect");       
        $.cookie("switchSelect", 'hide', {path: "/"}); 
        $("#switchSelect").html("показать"); 
    }
    
    function showSelect()
    {   
        $.cookie("switchSelect", 'show', {path: "/"}); 
        $("#bookmarks").attr("className", "showSelect");
        $("#switchSelect").html("скрыть");
    }
    
    
    function switchSelect(check){
        if( check == true )
        {
            if( $.cookie("switchSelect") == 'hide' || $.cookie("switchSelect") == null )
            {
                hideSelect();
            } 
            else 
            {               
                showSelect();    
            } 
        }
        else
        {
            if( $.cookie("switchSelect") == 'hide' || $.cookie("switchSelect") == null )
            {
                showSelect();
            } 
            else 
            {
                hideSelect();
            }         
        }
        

    }
    
    function deleteSelect(announcement_id, el){              
        if( $(".jselect li").length == 1 ) $("#bookmarks").fadeOut(); 
        var li = $(el).parent();
        $.ajax({
            url: "/announcement/del_select/"+announcement_id,
            type: "POST",
            cache: false,
            data: "announcement_id="+announcement_id,
            success: function(html)
            {                   
                if( html == 'ok' )
                {
                    li.remove();  
                    var left = $(".jselect").position().left + $(".jselect li").outerWidth(true);
                    $("#selectSlider").scrollLeft(-left);   
                    
                    initUl();
                    initButtons();
                    $("#countSelect").html( $('.jselect li').length );                         
                }
            }
        });
             
    }  
    
    function initUl(){
        var liWidth = $(".jselect li").outerWidth(true);
        var liLength = $(".jselect li").length;
        
        var ulWidth =  liWidth*liLength;
        var wrapWidth = liWidth*numOfLi;
        
        $(".jselect").css("width", ulWidth);
        $("#selectSlider").css("width", wrapWidth); 
        
        $('.jselect li').hover(function() {
            $(this).find('.remove').show();
        }, function() {
            $(this).find('.remove').hide();
        });       
        
    }
    
    
    function initButtons(){
        var left = $(".jselect").position().left;
        if( left == 0 ) {
            $("#selectSliderButtonLeft").addClass("jselect-prev-disabled-horizontal");
        } else {
            $("#selectSliderButtonLeft").removeClass("jselect-prev-disabled-horizontal");
        }
        
        var liWidth = $(".jselect li").outerWidth(true);
        var liLength = $(".jselect li").length;
        
        var ulWidth =  liWidth*liLength;
        


        if(  $(".jselect").outerWidth(true) <  $("#selectSlider").outerWidth(true) || ulWidth + left - liWidth*numOfLi == 0 ) {
            $("#selectSliderButtonRight").addClass("jselect-next-disabled-horizontal");
        } else {
            $("#selectSliderButtonRight").removeClass("jselect-next-disabled-horizontal");
        }
        
    }
