var first_slide = 1; // id of the first slide in carousel var itemsShowCount = 5; // how many items to show var cntPreloaded = 7; // how many items loaded (div containers initialized) var nextOrderId = cntPreloaded - 1; // the orderId of next slide for ajax load var change_timer = false; var currentSlide = itemsShowCount + Math.floor( (cntPreloaded - itemsShowCount) / 2); function controlTimer(timer) { if(timer == true) { change_timer = setTimeout(function(){ var select_slide = Math.floor(Math.random()*itemsShowCount); if(select_slide >= itemsShowCount) select_slide = itemsShowCount - 1; select_slide += first_slide; selectSlide(timer, select_slide); }, 10000); } else { if(change_timer) { clearTimeout(change_timer); } } } function selectSlide(timer, id) { $("#pictures div[class*=selected]").removeClass('selected'); $("#pictures #pic_" + id).addClass('selected'); $("#carousel div[class*=selected]").removeClass('selected'); $("#carousel #cr_" + id).addClass('selected'); controlTimer(timer); } $(function() { var arr = $("#carousel .carouselItem").map(function(){return $(this).height();}).get(), h = Math.max.apply(Math,arr); var elems = $("#carousel .carouselItem").get(); //$("#carousel").height(h); if(arr.length > itemsShowCount) { $("#carousel").ulslide({ width: 175, height: h, effect: { type: 'carousel', // slide or fade showCount: itemsShowCount, distance: 0 }, nextButton: '#prev_photo', prevButton: '#next_photo', duration: 350, statusbar: true, ajaxNavigation: true, current: currentSlide, onAnimateStop: function() { if(typeof this.loaded == 'undefined') { this.loaded = true; controlTimer(true); }else { var slideDirection = ''; var slide_idx = 0; var orderValue = 0; if(this.direction == 'b') { slideDirection = 'next'; orderValue = nextOrderId; slide_idx = cntPreloaded - this.current - 2; if(slide_idx < 0) slide_idx = cntPreloaded - 1; first_slide++; if(first_slide > cntPreloaded - 1) first_slide = 0; } else { slideDirection = 'prev'; orderValue = prevOrderId; var cr = this.current + cntPreloaded; if(this.current > itemsShowCount + 1) cr = this.current; slide_idx = cntPreloaded - cr + itemsShowCount + 1; first_slide--; if(first_slide == -1) first_slide = cntPreloaded - 1; } selectSlide(false, first_slide); $.ajax({ url: '/ajax/carousel/'+ slideDirection +'/' + orderValue, data: "", beforeSend: function() { $('#carousel #cr_'+slide_idx).addClass('carousel_loading'); }, complete: function() { $('#carousel #cr_'+slide_idx).removeClass('carousel_loading'); }, success: function(data){ if(typeof(data) == 'object') { var balancer_number = Math.floor(Math.random()*(balancers.length)); if(balancer_number >= balancers.length) balancer_number = balancers.length - 1; var balancer = balancers[balancer_number]; var elem = ''; if(data.carousel_item.type == 'Маршрут') elem += ''; elem += ''; elem += '' + data.carousel_item.title + ''; elem += ''; elem += ''; $('#carousel #cr_'+slide_idx).html(elem); $('#pictures #pic_'+slide_idx +' #img').attr('src', balancer + '/c600x338/' + data.carousel_item.media.path_600x338); $('#pictures #pic_'+slide_idx +' a').attr('href',data.carousel_item.href); $('#pictures #pic_'+slide_idx +' .h2 a').html(data.carousel_item.title); $('#pictures #pic_'+slide_idx +' .editorsChoiceItem_annonce').html(data.carousel_item.description); $('#pictures #pic_'+slide_idx +' .editorsChoiceItem_type').html(data.carousel_item.type); if(data.carousel_item.type == 'Маршрут') $('#pictures #pic_'+slide_idx +' .ym-g600').append('
'); else $('#pictures #pic_'+slide_idx +' .icon_route_big').remove(); nextOrderId = data.carousel_item.nextOrderId; prevOrderId = data.carousel_item.prevOrderId; } } }); } } }); } else { $("#carousel").css({ "height": h + "px", "width": (170 * arr.length) + "px" }); } $("#carousel .carouselItem").bind("mouseover",function() { var str = new String(this.id.substr(this.id.indexOf('_') + 1,this.id.length - this.id.indexOf('_'))); selectSlide(false, str); }); });