/* 
  author: Julia Nikul
 */

	var banners, switchers, indicators, interval = null, nextIndex = 1, sliderAminationDuration = 3000;

	function slideSwitch() {
		var $active = jQuery('#banner_container LI.b-active');
		if ($active.length == 0) $active = jQuery('#banner_container LI:last');
		var $next = (nextIndex > (banners.length - 1)) ? banners.eq(0) : banners.eq(nextIndex);

		if (!$next.hasClass('b-active')) {
			var $switcher = jQuery('#banner_indiator li.b-active');
			if ($switcher.length == 0) $switcher = jQuery('#banner_indiator li:last');
			var $next_indicator = (nextIndex > (indicators.length - 1)) ? indicators.eq(0) : indicators.eq(nextIndex);

			$active.addClass('b-last_active');
			$next.css({opacity: 0.0})
					.addClass('b-active')
					.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('b-active b-last_active');
			});

			$switcher.removeClass('b-active');
			$next_indicator.addClass('b-active');
		}
		nextIndex = (nextIndex == (banners.length - 1)) ? 0 : (nextIndex + 1);
	}
	function setBannerSwitchers() {
		jQuery.each(indicators, function (n) {
			indicators.eq(n).click(function() {
				nextIndex = n;
				if (interval != null) {
					clearInterval(interval);
					slideSwitch();
					interval = setInterval("slideSwitch()", sliderAminationDuration);
				}
			});
		});

		jQuery.each(switchers, function (n) {
			switchers.eq(n).click(function() {
				if (n == 0) {
					if (banners.length > 2) {
						var prevIndex = nextIndex - 2;
						nextIndex = (prevIndex < 0) ? (banners.length + prevIndex) : prevIndex;
					}
				}
				if (interval != null) {
					slideSwitch();
					clearInterval(interval);
					interval = setInterval("slideSwitch()", sliderAminationDuration);
				}
			});
		});
		jQuery.each(banners, function (n) {
			var b = banners.eq(n);
			b.mouseover(function(e) {
				if (interval != null) {
					clearInterval(interval);
				}
			});
			jQuery.each(b.find('a'), function() {
				$(this).mouseover(function(e) {
					if (interval != null) {
						clearInterval(interval);
					}
				});
				$(this).mouseout(function(e) {
					if (interval != null) {
						clearInterval(interval);
						interval = setInterval("slideSwitch()", sliderAminationDuration);
					}
				})
			});
			b.mouseout(function(e) {
				if (interval != null) {
					clearInterval(interval);
					interval = setInterval("slideSwitch()", sliderAminationDuration);
				}
			});
		})

		jQuery.each(indicators, function (n) {
			var b = indicators.eq(n);
			b.mouseover(function(e) {
				if (interval != null) {
					clearInterval(interval);
				}
			});
			jQuery.each(b.find('a'), function() {
				$(this).mouseover(function(e) {
					if (interval != null) {
						clearInterval(interval);
					}
				});
				$(this).mouseout(function(e) {
					if (interval != null) {
						clearInterval(interval);
						interval = setInterval("slideSwitch()", sliderAminationDuration);
					}
				})
			});
			b.mouseout(function(e) {
				if (interval != null) {
					clearInterval(interval);
					interval = setInterval("slideSwitch()", sliderAminationDuration);
				}
			});
		})

		jQuery.each(switchers, function (n) {
			var b = switchers.eq(n);
			b.mouseover(function(e) {
				if (interval != null) {
					clearInterval(interval);
				}
			});
			jQuery.each(b.find('a'), function() {
				$(this).mouseover(function(e) {
					if (interval != null) {
						clearInterval(interval);
					}
				});
				$(this).mouseout(function(e) {
					if (interval != null) {
						clearInterval(interval);
						interval = setInterval("slideSwitch()", sliderAminationDuration);
					}
				})
			});
			b.mouseout(function(e) {
				if (interval != null) {
					clearInterval(interval);
					interval = setInterval("slideSwitch()", sliderAminationDuration);
				}
			});
		})
	}

	function initSwitch(elementsId, pagesId, switcherId) {
		var $items = jQuery(elementsId);
		var $pages = jQuery(pagesId);
		var $switchers = jQuery(switcherId);
		var nextIndex = 1;

		var calcElem = function() {
			var $active = jQuery(elementsId + '.b-active');
			if ($active.length == 0) $active = jQuery(elementsId + ':last');
			var $next = (nextIndex > ($items.length - 1)) ? $items.eq(0) : $items.eq(nextIndex);

			var $page_active = jQuery(pagesId + '.b-active');
			if ($page_active.length == 0) $page_active = jQuery(pagesId + ':last');
			var $next_indicator = (nextIndex > ($pages.length - 1)) ? $pages.eq(0) : $pages.eq(nextIndex);

			$active.addClass('b-last_active');
			$next.css({opacity: 0.0})
					.addClass('b-active')
					.animate({opacity: 1.0}, 100, function() {
				$active.removeClass('b-active b-last_active');
			});

			$page_active.removeClass('b-active');
			$next_indicator.addClass('b-active')
			nextIndex = (nextIndex == ($items.length - 1)) ? 0 : (nextIndex + 1);
		}

		jQuery.each($pages, function (n) {
			$pages.eq(n).click(function() {
				nextIndex = n;
				calcElem();
			});
		});

		jQuery.each($switchers, function (n) {
			$switchers.eq(n).click(function() {
				if (n == 0) {
					if ($items.length > 2) {
						var prevIndex = nextIndex - 2;
						nextIndex = (prevIndex < 0) ? ($items.length + prevIndex) : prevIndex;
					}
				}
				calcElem();
			});
		});
	}

	$(document).ready(function() {
		$(function() {
			banners = $("#banner_container li");
			indicators = $("#banner_indiator li");
			switchers = $('#banner_switcher a');
			setBannerSwitchers();
			interval = setInterval("slideSwitch()", sliderAminationDuration);
		});
	});



