$(function(){    

var aLogo = $("#main-swf ul li a img");
var bImg = $("#big_img");
var aLogoPic = '';
aLogo.hover(
        function () { 
$(this).css('border','solid 1px #bbb'); 
            aLogoPic = $(this).attr('alt');//big_img
            bImg.attr('src',aLogoPic);
     
        },
        function () {         
           $(this).css('border','solid 1px #fff');  
        }
        );
	
var arr = new Array();
aLogo.each(function(i) {
    arr.push($(this).attr('alt'));
});
		
		
		
		
		
		
	
    var timeout = 1000 * 10; // 10 sec
	var i = 0;
    function refreshData(){
			bImg.attr('src',arr[i]);
            setTimeout(refreshData,timeout);
			i++;
			if (i>arr.length)
				i=0
    }
              
    

    refreshData();
		
		
		
		
		
		
		
		
		
		
		
		
	//	alert(arr[3]);
    
 $('a.foto').fancybox();
 
 /*$('a.foto').lightBox({
	overlayBgColor: '#000',
	overlayOpacity: 0.7,
	imageLoading: '/media/plugin/lightbox/images/lightbox-ico-loading.gif',
	imageBtnClose: '/media/plugin/lightbox/images/lightbox-btn-close.gif',
	imageBtnPrev: '/media/plugin/lightbox/images/lightbox-btn-prev.gif',
	imageBtnNext: '/media/plugin/lightbox/images/lightbox-btn-next.gif',
	containerResizeSpeed: 300,
	txtImage: '',
	txtOf: 'из'
   });
	*/
        
        
    $('.main-nav-2 a').corner('bevel br 20px');
    //  $('ul.brands-logos li, .ac-list .add').corner('cc:#aaaaaa 8px');
    
  //  $('.oper-toggle').hide();
 //   $('#oper-open').show();
	
    $('#oper-open, #oper-hide').click(function(){   
        $('.oper-toggle').toggle();
        return false;
    });
    
    
    var ac = $("div.ac-list div[class^=:]");
    var brands = $("ul.brands-logos li");
    var f =  $("img[class^=ico-f]");
    var add =  $(".add");

    
    $('#map').click(function(){ 
        f.hide();
        add.hide();
        $('.ico-f').show();
        brands.removeClass('active');
        return false;
    });

    
    ac.hover(
        function () {  
            add.hide();
            $(this).find('.add').show();
            ac.css('z-index','100');
            $(this).css('z-index','110');
     
        },
        function () {
            $(this).find('.add').hide();
            
        }
        );
        
    
    var brand = '';
    $("ul.brands-logos li").hover(
        function () {    
            f.hide();
            add.hide();
            $('.ico-f-black').show();
            //ac.hide();
            brand = $(this).attr('id');
            // $("div.ac-list div[class*=:" + brand + "]").show();
            $("div.ac-list div[class*=:" + brand + "]").each(function() { 
                $(this).find('.ico-f').show();
                $(this).find('.ico-f-black').hide();
            });
            brands.removeClass('active');
            $(this).addClass('active');           
        },
        function () {
        //$('ul.brands-logos li').removeClass('active');
            
        }
        );
        
        
    var brandsList = '';
    
    
    ac.each(function() { 
        brandsList  = $(this).attr('class');
        brandsList = brandsList.split(':');
        var s = '<div class="add-info">';
        
        for(var i=1; i<brandsList.length-1; i++) {
           s += '<img src=/upload/image/brands_logos/Logo_' + brandsList[i] + '_48.png /> ';
        }
        s += '</div>';
        $(this).find('.add-info:first').before(s);
    });
        
    
    $('.brands .models:first').attr('id','first-models');
    $('.brands .logo:first').attr('id','first-logo');
            
        
        
    var cont = '';    
    $('.model').each(function() {
        cont = $(this).find('.small').html();                                    
        $(this).find('.full').prepend(cont); 
        
    });
    $(".model").hover(
        function () {     
            $(this).attr('id','active');
            $(this).find('.full').show();            
        },
        function () {
            $(this).find('.full').hide();
            $(this).attr('id','');
        }
        );

    $('div[id$="-cars-cont"]').hide();
    $('div[id$="-cars-cont"]:first').show();
	
    
	
    var banersCount = $("#new-cars-cont .items  .item").size() - 5;
    var yoshidaCount = $("#used-cars-cont .items  .item").size() - 5;
    var maxClicksCountBanners = Math.floor(banersCount/1); // 5 количество видимых банеров    
    var maxClicksCountYoshida = Math.floor(yoshidaCount/1); // 5 количество видимых банеров    
    var clicksCountBanners = 0;
    var clicksCountYoshida = 0;


    
    
    $("#new-cars-cont .next").click(function(){   
     
        if(clicksCountBanners < maxClicksCountBanners){
            clicksCountBanners ++;
            $("#new-cars-cont .items").animate({
                //"left": "-=960px"
                "left": "-=192px"
            }, "fast");
        }
    });

    $("#new-cars-cont .prev").click(function(){              
        if(clicksCountBanners > 0){
            clicksCountBanners --;        
            $("#new-cars-cont .items").animate({
                //"left": "+=960px"
                "left": "+=192px"
            }, "fast");
        }
    });
    
    
    $("#used-cars-cont .next").click(function(){          
        if(clicksCountYoshida < maxClicksCountYoshida){
            clicksCountYoshida ++;
            $("#used-cars-cont .items").animate({
                //"left": "-=960px"
                "left": "-=192px"
            }, "fast");
        }
    });

    $("#used-cars-cont .prev").click(function(){              
        if(clicksCountYoshida > 0){
            clicksCountYoshida --;        
            $("#used-cars-cont .items").animate({
                //"left": "+=960px"
                "left": "+=192px"
            }, "fast");
        }
    });



    $('a[id$="-cars"]').click(function() {
        $('a[id$="-cars"]').removeClass('active');
        $(this).addClass('active');
        var id = $(this).attr('id');        
        $('div[id$="-cars-cont"]').hide();
        $('#' + id + '-cont').show();					        
        return false;
    });
	
	
    var offset = $("#operator").offset();

    var topPadding = 15;

    $(window).scroll(function() {

        if ($(window).scrollTop() > offset.top) {

            $("#operator").stop().animate({
                marginTop: $(window).scrollTop() - offset.top + topPadding
            });

        }

        else {
            $("#operator").stop().animate({
                marginTop: 0
            });
        };
    
    });
	
	


});
