jQuery(function($){
	
	$('div.menu-hor-block a > span').each(function(){
		$(this).width($(this).parent().width());
	});
	
	$('div.menu-vert-block > ul > li').hover(
		function(){ $(this).addClass('hover') },
		function(){ $(this).removeClass('hover') }
	);
	
//	jQuery('div.list-block > ul').jcarousel({scroll: 1});
    var carousel_data = [];
    $('div.list-block > ul > li').each(function() {
        carousel_data[carousel_data.length] = $(this).html();
        //if ($('div.list-block > ul > li').index(this) > 0)
            $(this).remove();
    });
    /*$('div.list-block > ul').jcarousel({
        scroll: 1,
        wrap: 'circular',
        itemVisibleInCallback: {onBeforeAnimation: function(carousel, item, i, state, evt) {
               var idx = carousel.index(i, carousel_data.length);
               carousel.add(i, carousel_data[idx - 1]);
            }
        },
        itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) {
            carousel.remove(i);
            }
        },
        auto: 3
    });*/
    rvk_slide.init(carousel_data);

})

var rvk_slide = {
    
    img_count: 0,
    data: [],
    cur_el: null,
    cur_index: 0,
    timer_period: 4300,
    timer: 0,
    
    init: function(data) {
        if (data.length > 0) {
            this.data = data;
            this.show();
        }
    },
    
    show: function() {
        this.clear_timer();
        var self = this;
        if ($('div.list-block > ul > li').size() > 1) {
            $('div.list-block > ul > li img').unbind('load');
            $('div.list-block > ul > li').remove();
            self.show();
        } else if ($('div.list-block > ul > li').size() > 0) {
            $('div.list-block > ul > li').fadeOut(800, function(){
                $('img', this).unbind('load');
                $(this).remove();
                self.show();
            });
        } else {
            this.correct_index();
            this.cur_el = $('<li/>').html(this.data[this.cur_index]).hide();
            
            this.img_count = $('img', this.cur_el).size();
            $('img', this.cur_el).bind('load', function(){
                self.img_count--;
                if (self.img_count == 0) {
                    if ($('a.img > img', self.cur_el).height() > 0)
                        $('a.img > img', self.cur_el).css({marginTop: (($('a.img', self.cur_el).height() - $('a.img > img', self.cur_el).height()) / 2) + 'px'});
                    self.timer = window.setTimeout(function(){self.next();}, self.timer_period);
                }
            });
            
            if ($('a.img > img', this.cur_el).height() > 0)
                $('a.img > img', this.cur_el).css({marginTop: (($('a.img', this.cur_el).height() - $('a.img > img', this.cur_el).height()) / 2) + 'px'});
            
            $('div.list-block > ul').append(this.cur_el);
            this.cur_el.fadeIn(200, function(){
                //self.timer = window.setTimeout(function(){self.next();}, self.timer_period);
            });
        }
    },
    
    next: function() {
        this.cur_index++;
        this.show();
    },
    
    clear_timer: function() {
        window.clearTimeout(this.timer);
    },
    
    correct_index: function() {
        if (this.cur_index >= this.data.length)
            this.cur_index = 0;
        else if (this.cur_index < 0)
            this.cur_index = this.data.length - 1;
    }
}
