var stepsCarousel, bannersCarousel, totalSteps, totalBanners;

/* Steps */

function steps_init() {
	$("div.steps").prepend('<ul></ul>').find('ul').end().css('display', 'block').jcarousel({
		animation: 500,
		scroll: 1,
		auto: 5,
		wrap: 'circular',
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: steps_initCallback,
		itemVisibleInCallback: { onBeforeAnimation: changeSteps},
		itemVisibleOutCallback: { onAfterAnimation: removeSteps }/**/
	});
}

function steps_initCallback(carousel) {
	stepsCarousel = carousel;
	totalSteps = $('#steps-hash li').size();

	$(carousel.buttonPrev).click(function() { // РџРѕ РєР»РёРєСѓ РЅР° > РєСЂСѓС‚РёРј РІРїРµСЂС‘Рґ
		stepsCarousel.prev();
		return(false);
	});
	$(carousel.buttonNext).click(function() { // РџРѕ РєР»РёРєСѓ РЅР° < РєСЂСѓС‚РёРј РЅР°Р·Р°Рґ
		stepsCarousel.next();
		return(false);
	});
}
function changeSteps(carousel, item, idx) { // Р”РѕР±Р°РІР»СЏРµРј РІ РєРѕР»СЊС†Рѕ
	var i = carousel.index(idx, totalSteps);
	var s = carousel.index(idx-5, totalSteps);
	var item_html = $('#steps-hash li').eq(i-1).html();
	carousel.add(idx, item_html);

	for (var j = 1, carousel_item, check_html; j <= totalSteps; j++) { // РџСЂРѕРІРµСЂРєР° РїСѓСЃС‚С‹С… СЌР»РµРјРµРЅС‚РѕРІ Рё Р·Р°РїРѕР»РЅРµРЅРёРµ РёС…
		carousel_item = carousel.get(j);
		if (carousel_item) {
			check_html = carousel_item.html();
			if(check_html == '') {
				carousel_item.html($('#steps-hash li').eq(j-1).html());
			}
		}
	}
	
	var bullets = $('.steps-nav a');
	bullets.attr('class', '').click(function() { // РџРѕ РєР»РёРєСѓ РЅР° Р±СѓР»Р»РёС‚ РєСЂСѓС‚РёРј РєР°СЂСѓСЃРµР»СЊ
		 if(this.className == 'selected') return(false);
		 var n = bullets.index(this) + 1;
		 stepsCarousel.scroll(n);
		 return(false);
	}).eq(s-1).addClass('selected'); // РџРѕРјРµС‡Р°РµРј Р±СѓР»Р»РёС‚ 
};

function removeSteps(carousel, item, i) { // РЈРґР°Р»СЏРµРј Р±Р°РЅРЅРµСЂ Р·Р° РїСЂРµРґРµР»Р°РјРё РІРёРґРёРјРѕСЃС‚Рё
	carousel.remove(i);
}


/* Banners */

var bannersCarousel, totalBanners;

function banners_init() {
	$("div.bannerss").prepend('<ul></ul>').find('ul').end().css('display', 'block').jcarousel({
		animation: 500,
		scroll: 1,
		auto: 5, 
		wrap: 'circular',
		buttonNextHTML:'<div> <span> </span> </div>' ,
		buttonPrevHTML:'<div> <span> </span> </div>' ,
		initCallback: banners_initCallback,
		itemVisibleInCallback: { onBeforeAnimation: changeBanners },
		itemVisibleOutCallback: { onAfterAnimation: removeBanners }
		/*itemFirstInCallback: {
			onBeforeAnimation: banners_changeBullets
		}*/
	});
}

function banners_initCallback(carousel) {
	bannersCarousel = carousel;
	totalBanners = $('#banners-hash li').size();

	if(totalBanners < 5) {
		carousel.options.auto = 0;
		$(carousel.buttonNext).add(carousel.buttonPrev).hide();
		carousel.options.buttonNextEvent = '';
		carousel.options.buttonPrevEvent = '';
	}
	else {
		$(carousel.buttonPrev).click(function() { // РџРѕ РєР»РёРєСѓ РЅР° > РєСЂСѓС‚РёРј Р±Р°РЅРЅРµСЂС‹ РІРїРµСЂС‘Рґ
			bannersCarousel.prev();
			return(false);
		});
		$(carousel.buttonNext).click(function() { // РџРѕ РєР»РёРєСѓ РЅР° < РєСЂСѓС‚РёРј Р±Р°РЅРЅРµСЂС‹ РЅР°Р·Р°Рґ
			bannersCarousel.next();
			return(false);
		});
	}
}

function changeBanners(carousel, item, idx) { // Р”РѕР±Р°РІР»СЏРµРј Р±Р°РЅРЅРµСЂС‹ РІ РєРѕР»СЊС†Рѕ
	var i = carousel.index(idx, totalBanners);
	var s = carousel.index(idx-2, totalBanners);
	var item_html = $('#banners-hash li').eq(i-1).html();
	carousel.add(idx, item_html);

	for (var j = 1, carousel_item, check_html; j <= totalBanners; j++) { // РџСЂРѕРІРµСЂРєР° РїСѓСЃС‚С‹С… СЌР»РµРјРµРЅС‚РѕРІ Рё Р·Р°РїРѕР»РЅРµРЅРёРµ РёС…
		carousel_item = carousel.get(j);
		if (carousel_item) {
			check_html = carousel_item.html();
			if(check_html == '') {
				carousel_item.html($('#banners-hash li').eq(j-1).html());
			}
		}
	}

};

function removeBanners(carousel, item, i) { // РЈРґР°Р»СЏРµРј Р±Р°РЅРЅРµСЂ Р·Р° РїСЂРµРґРµР»Р°РјРё РІРёРґРёРјРѕСЃС‚Рё
	carousel.remove(i);
}

/* Defaults */

$(document).ready(function() {

	steps_init();
	banners_init();
	
});

