//////////////////////////////////////////////////////
//             Js libs for askstore                 //
//////////////////////////////////////////////////////

////////////// CONFIG ////////////////////////////////
var si_conf = Object();
si_conf.gallery = Object();
si_conf.shop = Object();

// Состояние галлереи по умолчанию.
// true  - Баннеры будут прокручиватся
// false - Баннеры остановленны
si_conf.gallery.play = true;

// Время между сменой баннера
// 1 секунда = 1000 ms
si_conf.gallery.timeout = 9000;

// Скорость смены баннера, возможны следующие значения:
// "fast" - что равно 200 ms
// "slow" - что равно 600 ms
// Свое значение в милисекундах
si_conf.gallery.speed = "slow";

// Будет ли происходить смена баннера сразу же после клика по кнопке play
// true  - Да будет.
// false - Нет не будет.
si_conf.gallery.nextAfterPlay = true;


// Будут ли подгруженны все баннеры до начала проигрывания
// true  - Да будет.
// false - Нет не будет.
si_conf.gallery.preload = false;



// Скорость смены баннера, возможны следующие значения:
// "fast" - что равно 200 ms
// "slow" - что равно 600 ms
// Свое значение в милисекундах
si_conf.shop.speed = 1000;



jQuery(document).ready(function () {
	
	////////// GALLERY ///////////////////////////////
	var g_interval = null;
	var g_current  = null;
	var g_cnt_img  = null;
	
	function g_change( vector ) {
		if (g_cnt_img == null) {
			g_cnt_img = jQuery('.gallery').find('.pics').find('a').length;
		}
		if (g_current == null) {
			g_current = jQuery('.gallery').find('.pics').find('a').index(
				jQuery('.gallery').find('.pics').find('a.first')
			);
		}
		
		var next = (vector == 'next') ? g_current + 1 : g_current - 1;
		if ( next < 0 ) next = g_cnt_img - 1;
		if ( next >= g_cnt_img ) next = 0;
		
		jQuery('.gallery').find('.pics').find('a:eq(' + g_current + ')').css('zIndex', 200);
		jQuery('.gallery').find('.pics').find('a:eq(' + next + ')').css({display:'block', zIndex:199});
		
		jQuery('.gallery').find('.pics').find('a:eq(' + g_current + ')').fadeOut(si_conf.gallery.speed, function () {
			jQuery(this).css({display:'none', zIndex:'auto'});
		});
		g_current = next;
	}
	
	if (si_conf.gallery.preload) {
		jQuery('.gallery').find('.pics').find('a.first').css('zIndex', 200);
		jQuery('.gallery').find('.pics').find('a:not(.first)').css({display:'block', zIndex:'auto'});
	}
	
	jQuery('.gallery').find('.control').find('a').click(function() {
		var $this = jQuery(this);
		
		// Клик по кнопке play
		if ( $this.hasClass('play') ) {
			if (si_conf.gallery.nextAfterPlay) {
				// Запускаем смену
				g_change('next');
			}
			// Включаем автоматиче прокрутку
			g_interval = setInterval(function(){ g_change('next') }, si_conf.gallery.timeout);
			// Меняем кнопку на stop
			$this.removeClass('play').addClass('stop');
			
			return false;
		}
		
		// Клик по кнопке stop
		if ( $this.hasClass('stop') ) {
			// Выключаем автоматическую прокрутку
			clearInterval(g_interval);
			// Меняем кнопку на play
			$this.removeClass('stop').addClass('play');
			
			return false;
		}
		
		// Клик по кнопке prev
		if ( $this.hasClass('prev') ) {
			// Если автопрокрутка включена, перезапускаем её
			if ($this.next().hasClass('stop')) {
				clearInterval(g_interval);
				g_interval = setInterval(function(){ g_change('next') }, si_conf.gallery.timeout);
			}
			// Меняем баннер на предидущий
			g_change('prev');
			
			return false;
		}
		
		// Клик по кнопке next
		if ( $this.hasClass('next') ) {
			// Если автопрокрутка включена, перезапускаем её
			if ($this.prev().hasClass('stop')) {
				clearInterval(g_interval);
				g_interval = setInterval(function(){ g_change('next') }, si_conf.gallery.timeout);
			}
			// Меняем баннер на следующий
			g_change('next');
			
			return false;
		}
	});
	
	if (si_conf.gallery.play) {
		jQuery('.gallery').find('.control').find('a.play').click();	
	}
	
	////////// I-SHOP ////////////////////////////////
	jQuery('.tovar').find('.tovar-img').click(function () {
		var html = jQuery(this).parents('li').html();
		$('.tovar-popup-item').html(html);
		
		var src = jQuery(this).attr('href');
		$('.tovar-popup-item').find('.tovar-img').attr('href', '#');
		$('.tovar-popup-item').find('.tovar-img').find('img').remove();
		$('.tovar-popup-item').find('.tovar-img').html('<img src="'+src+'" alt="" />');
		
		jQuery('.tovar-popup').css('display', 'block');
		return false;
	});
	
	jQuery('.tovar-popup-bg').click(function () {
		jQuery('.tovar-popup').css('display', 'none');
		return false;
	});
	
	jQuery('.tovar-popup').find('.tovar-img').live('click', function () {
		jQuery('.tovar-popup').css('display', 'none');
		return false;
	});
	
	////////// SHOP //////////////////////////////////
	var s_default = Object();
		s_default.bottom = '130px';
		s_default.right  = '174px';
		s_default.width  = '527px';
		s_default.height = '410px';
	var s_annimate = false;
	var s_annimateCount = 0;
	
	function s_stop() {
		s_annimateCount++;
		if (s_annimateCount == 2) {
			s_annimateCount = 0;
			s_annimate = false;
		}
	}
	
	jQuery('.shop-conteiner').find('.imgset').find('div.item1').addClass('current');
	
	jQuery('.shop-conteiner').find('.imgset').find('div').click(function () {
		var $this = jQuery(this);
		if ($this.hasClass('current')) {
			return false;	
		}
		
		if (s_annimate) return false;
		s_annimate = true;
		
		var s_new = Object();
			s_new.bottom = $this.css('bottom');
			s_new.right  = $this.css('right');
			s_new.width  = $this.css('width');
			s_new.height = $this.css('height');
		
		// Запускаем анимацию
		$this.animate({
			bottom: s_default.bottom,
			right:  s_default.right,
			width:  s_default.width,
			height: s_default.height
		}, si_conf.shop.speed, function () {
			jQuery(this).addClass('current');
			s_stop();
		});
		
		jQuery('.shop-conteiner').find('.imgset').find('div.current').animate({
			bottom: s_new.bottom,
			right:  s_new.right,
			width:  s_new.width,
			height: s_new.height
		}, si_conf.shop.speed, function () {
			jQuery(this).removeClass('current');
			s_stop();
		});
		
	});
	
	$('.shop-menu').find('li:not(".open")').find('ul').hide();
	$('.shop-menu > li > a').click(function(){
		// Если кликнули по открытой, ничего не делаем.
		if ($(this).parent().hasClass('open')) {
			return false;	
		}
		// Иначе закрываем остальные, и открываем кликнутый.
		$(this).parents('.shop-menu').find('.open').removeClass('open').find('ul').animate({height:'toggle'});
		$(this).parent().addClass('open').find('ul').animate({height:'toggle'});
		return false;
	});
	
	////////// FRANCH ////////////////////////////////
	jQuery('.franch-form').find('.inp-radio').find('input').addClass('superhide');
	
	jQuery('.franch-form').find('.inp-radio').find('input:checked').each(function() {
		jQuery(this).addClass('checked');
	});
	
	jQuery('.franch-form').find('.inp-radio').find('input').change(function () {
		jQuery(this).parents('td').find('label').removeClass('checked');
		jQuery(this).parents('label').addClass('checked');
		return false;
	});
	
	////////// PREIODICALS ////////////////////////////////
	jQuery('.zoom').each(function(){
		$(this).attr('href', $(this).find('img').attr('src').replace('preview', 'scan')).fancybox({
			padding:0,
			hideOnContentClick: true,
			overlayShow:false,
			titleShow:false,
			transitionIn:'elastic',
			transitionOut:'elastic',
			showCloseButton:false,
			showNavArrows:false
		});
	});
	
});
