var bd = $('body');
var cls = bd.attr('class') + '_narrow';

function repositionBg(){
	var w = $('.bem-wrap').width()
	if( w < 1340 ){
		$('.bem-wrap_bg').css({'left' : -170 + ( 1340 - w ) / 2})
	} else $('.bem-wrap_bg').css({'left' : '-170px'})
	
	if( w > 1050 ){
		bd.removeClass(cls);
		$('.bem-wrap_inner').add($('.bem-foot')).css('margin-left', '50px')
	} else {
		bd.addClass(cls);
		$('.bem-wrap_inner').add($('.bem-foot')).css('margin-left', 0)
	}
}

function fadeInit(obj){
	var img = obj.find('img')
	var imgF = img.first().addClass('act')
	obj.css({'height': imgF.height(), 'width': imgF.width()})
	img.css({'position':'absolute', 'top':0, 'left':0})
	for( i=0; i < img.length; i++ ){
		img.eq(i).attr('q', i).css('z-index', img.length - i)
	}
	if( img.length > 1 ) fadeImg(obj)
}

function fadeImg(obj){
	var act = obj.find('.act')
	if( act.attr('q') == obj.find('img').length - 1 ) var next = obj.find('img').first() 
	else var next = act.next()
	next.addClass('next')
	act.fadeOut(2500, function(){
		act.removeClass('act')
		next.removeClass('next').addClass('act')
		act.show()
		fadeImg(obj)
	})
}

var pr = $('.bem-photos-list')
var maxB = $('.bem-photos-list .item:first img').length
var maxA = $('.bem-photos-list .item').length

function animInit(obj){
	
	var img = obj.find('img')
	var imgF = img.first()
	obj.find('.bem-slide-img2').css({'height': imgF.height(), 'width': imgF.width()})
	img.css({'position':'absolute', 'top':0, 'left':0})
	var k = 0, m, z = 1 
	
	$('.bem-photos-list .item').each(function(i){
		m = i
		var y = i++
		if( y % 4 == 0 ){
			k++		
		}
		if( k % 2 == 0 ){
			m = 4*k - z
			z++
		} else z = 1
		$(this).addClass('item' + m).find('img').each(function(j){
			$(this).attr('q', j).css('z-index', maxB - j)
		})
	})
	if( maxB > 1 ) anim(0, 0)
}
function anim(a, b){
	
	var animImg = pr.find('.item' + a + ' img').eq(b)
	animImg.addClass('act')
	if( animImg.attr('q') == maxB - 1 ) var next = pr.find('.item' + a + ' img').eq(0)
	else var next = animImg.next()
	next.addClass('next')
	
	animImg.fadeOut(1500, function(){
		animImg.removeClass('act')
		next.removeClass('next').addClass('act')
		animImg.show()
		a++;
		if( a >= maxA ){
			a = 0;
			b++;
			if( b >= maxB ){
				b = 0
			}
		}
		anim(a, b)
	})
}

function resizeBg(){
	/*var t = parseInt($('.bem-wrap_bg').css('top'));
	var h = $('.bem-wrap').height() - t;
	$('.bem-wrap_bg').css('height', h)*/
}

$(window).resize(function(){
	repositionBg()
	resizeBg()
})
$(window).load(function(){
	$('.bem-slide-img').each(function(){
		fadeInit($(this))
	})
	animInit(pr)
})

$(document).ready(function(){
	repositionBg()
	resizeBg()
	$('.bem-search_cat input:text').focus(function(){
		if( $(this).val() == 'Поиск по каталогу' ) $(this).val('')
	}).blur(function(){
		if( $(this).val() == '' ) $(this).val('Поиск по каталогу')
	})
	$('div.img_left2').wrapInner('<div class="img_left2_inner"></div>').append('<i class="lt"></i><i class="rt"></i><i class="lb"></i><i class="rb"></i>');
	$('.bem-filter dt').click(function(){
		var dt = $(this)
		dt.next('dd').slideToggle("slow")
		if( !dt.hasClass('act') ) dt.addClass('act')
		else dt.removeClass('act')
	})
	$('table.table3 tr:nth-child(even)').addClass('even')
	$('table.table3 tr:eq(1)').addClass('tr2').find('td').wrapInner('<div class="inner"></div>')
	$('table.table3 .inner').append('<i class="bg"></i>')
	$('.scrollable a:first').addClass('active')
	
	var ul = '<ul class="bem-gallery_ul">'
		
	$('.bem-good .scrollable a').each(function(i){
		$(this).attr('elIndex', i)
		ul = ul + '<li><a href="' + $(this).find('i').attr('title') + '"  rel="group1"><img src="' + $(this).find('img').attr('alt')  + '" alt=""/></a></li>'
	})
	ul = ul + '</ul>'
	$('.bem-good .img_left').css('cursor', 'pointer');
	
	$('.bem-good .img_left').click(function(){
		
		var _href = $(this).next().attr("alt")

		$(".bem-gallery_ul a").each(function(){
			if($(this).attr("href") == _href)
				$(this).trigger('click')
		})
	})
	$('.bem-good .img').prepend(ul)
	
	if( $('.scrollable').length > 0 ) $('.scrollable').scrollable({item: 'a', size: 4}).circular()
	$('.bem-good .scrollable a').click(function(){
		var a = $(this)
		var w = 225
		//var obj = $('.bem-good .img ul').animate({'left': -1 * w * $(this).attr('elIndex') })
		
		return false
	})
	
	$('.bem-good .scrollable .items a').click(function(){
		var _href = $(this).attr("href")
		
		$(".bem-gallery_ul a").each(function(){
			if($(this).attr("href") == _href)
				$(this).trigger('click')
		})
	})
	
	var $gal = $('.bem-photogal a[href*=/upload/]');
	if( $gal.length > 0 ) 
		$gal.fancybox({
			'titlePosition' :   'inside',
			'overlayOpacity':   '0.8',
			'overlayColor'  :   '#000',
			'titleFormat'   : function(title, currentArray, currentIndex, currentOpts) {
			    				return  (title && title.length ? '<b>' + title + '</b>' : '' ) + '<span id="fancybox-title-over">Фото ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
							  }
		});
	if( $('.bem-gallery_ul a').length > 0 )
		$('.bem-gallery_ul a').fancybox({
			'titlePosition' :   'inside',
			'overlayOpacity':   '0.8',
			'overlayColor'  :   '#000',
			'titleFormat'   : function(title, currentArray, currentIndex, currentOpts) {
			    				return  (title && title.length ? '<b>' + title + '</b>' : '' ) + '<span id="fancybox-title-over">Фото ' +  (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
							  }
		});
	if( $('.bem-video a').length > 0 ) 
		$('.bem-video a').fancybox({
			'titlePosition' :   'inside',
			'overlayOpacity':   '0.8',
			'overlayColor'  :   '#000',
			'padding'       :   20
		});
	
	$('.bem-nav_top li:first').addClass('act').prepend('<i class="bg"></i>')
	$('.bem-tabs dt').click(function(){
		$(this).siblings().removeClass('act').end().next('dd').andSelf().addClass('act')
	});
	$('.subitem').each(function(index, element){
	  var i = $(element).attr("class");
	  i = $.trim(i.split("subitem").join(""));
	  $(".item" + i).hover(function(e){
	    pos = $(".item"+i).position();
			position = ($(".zlevel").size()) ? {"bottom":"70px", "left":((pos["left"]-18) + "px")} : {"top":((pos["top"]-30) + "px")};
	    $('.subitem').hide().filter('.subitem' + i).css(position).fadeIn('fast');
		}, function(){
			var t = setTimeout(function(){
			  $('.subitem' + i).fadeOut('slow').attr('timer', 0);
			}, 7000);
      $('.subitem' + i).attr("timer", t);
		});
  	$('.subitem' + i).hover(function(){
  	  var tr = $(this).attr('timer');
  	  if (tr){
				clearTimeout(tr);
				$(this).attr('timer', 0);
			}
  	  $(this).show();
		}, function(){
		  /*var tr = $(this).attr('timer');
		  if (tr){
				clearTimeout(tr);
				$(this).attr('timer', 0);
			}*/
			$(this).fadeOut('slow');
		});
	});
	if ($("#reg_form p font.errortext").size()) $('#reg_form').show();
	$("[name='close_form']").click(function(){$('#reg_form').slideUp('fast');});
	$(".bem-rcol h2, .bem-right1 h2").click(function(){$('#opinion_form').slideToggle('fast')});
	/*if((decodeURIComponent(location.search) == '?strIMessage=Элемент+успешно+добавлен')&&(!location.href.search('#iblock_add_anchor'))){
	  location.href = location.href.replace(location.search, "#ib" + location.search;
	}*/
})
