$(document).ready(function() {

function storeGrid() {

	// выстраиваем store сеточкой
	var storePageAmount = $("div.contentArea div.storeBlock div.storeItems div.item").size();
	var storePagewidth = $("div.contentArea div.storeBlock").width();
	var storePageCols = storePagewidth / 400;
	storePageCols = storePageCols.toString().split('.');
	storePageCols = storePageCols[0];
	
	if ( storePageCols < 1 ) return;

	var i = 0;
	$("div.contentArea div.storeBlock div.storeItems div.item").each(function() {
		$(this).css("float" , "left").css("clear" , "none" );
		if ( i != storePageCols ) {
			i++;
		} else {
			$(this).css("clear" , "left");
			i = 1;
		}
	});

}

function storePreviewProps() {
	if ( $("div.storePreview").length ) {
		var storeEachItemWidth = $("div.storeItems div.item").width();
		storeItemsWidth = $("div.storeItems div.item:visible").size() * (storeEachItemWidth + 27) + 15;
		$("div.storePreview div.storeBody div.storeItems").css( "width" , storeItemsWidth + "px" );
		storeMinX = $("div.storeBody").offset().left + 10;
		storeWidth = $("div.storeBody").width();
		$("div.storeItems").draggable( { axis: 'x' , containment: [ storeMinX + storeWidth - storeItemsWidth , 0, storeMinX ,0] });
	}
}


	var storeItemsWidth = 0;
	var storeMinX = 0
	var storeWidth = 0

	storeGrid();

	// действия при резайзе
	$(window).resize(function() {
		storeGrid();
		if ( $("div.storePreview").length ) {
			$("div.storePreview").draggable('disable').draggable('destroy');
		}
		storePreviewProps();
		if ( $(window).width() < 1140 ) {
			$("div.contentArea div.storeItems").addClass("w1000");
			$("div.topCoolMenu").addClass("w1000");
		} else {
			$("div.contentArea div.storeItems").removeClass("w1000");
			$("div.topCoolMenu").removeClass("w1000");
		}
	});

	if ( $(window).width() < 1140 ) {
		$("div.contentArea div.storeItems").addClass("w1000");
		$("div.topCoolMenu").addClass("w1000");
	}

	// МЕНЮ с клёвыми иконками
	$("div.topCoolMenu div.block").mouseenter(function() {
		if ( ! ($(this).is(".active") ) )
			$(this).addClass("hover");
	}).mouseleave(function() {
		$(this).removeClass("hover");
		$("div.topCoolMenu").css( "backgroundPosition" , "0px -101px");
	}).click(function() {
		if ( ! ($(this).is(".active") ) ) {
			link = $(this).find("a").attr("href");
			if ( link != 'undefined' )
				window.location.href = "/" + link;
		}
	});

	$("div.navigationBlock div[class^=block-]").click(function() {
		link = $(this).find("a").attr("href");
		if ( link != 'undefined' )
			window.location.href = "/" + link;
	});

	// дополнительный бекграундд к меню
	$("div.topCoolMenu div.block:first").mouseover(function() {
		if ( ! ($(this).is(".active") ) )
			$("div.topCoolMenu").css( "backgroundPosition" , "0px 0px");
	});
	
	// стрелка в корзине
	$("div.sideBar div.basket div.body-goods div.action-button").click(function() {
		var link = $("div.sideBar div.basket div.body-goods a").attr("href");
		if ( link != 'undefined' )
			window.location.href = "/" + link;
	});
	
	
	//	кнопка КУПИТЬ
	$("div.storeItems div.item .buttons span").mouseover(function() {
		$(this).addClass('hover');
		var pickAndMove = false;
	}).mouseout(function() {
		$(this).removeClass('hover');
		var pickAndMove = true;
	});

	// ХВАТАЙ И ТАЩИ
	storePreviewProps();
	
	
	// fancybox 
	$("a.zommedImageLink").fancybox({
		'hideOnContentClick': true,
		'overlayColor' : '#000',
		'overlayOpacity' : .8
	});

});
