﻿function justify() {
	widthOfItem = 110;
	fullWidth = $('#content').width() + 10;
	ost1 = fullWidth % widthOfItem;
	itemsOnLine = parseInt(fullWidth / widthOfItem);
	marg_ost = ost1 % itemsOnLine;
	marg = parseInt(ost1 / itemsOnLine);
	ost = marg % 2;
	marg = parseInt(marg / 2);
	marg_left = marg + 5 + 'px';
	marg_right = marg + ost + 5 + 'px';
	$('.item').css('margin-left', marg_left);
	$('.item').css('margin-right', marg_right);
}

function addToCart(id, name) {
	$.post("/order/addtocart", { cart_item_id: id } ,
		function(data) {
			if (data=="OK")	{
				$.jGrowl('Изделие <b>'+ name+'</b> добавлено в корзину');
			}	else if (data=="EXISTS") {
				$.jGrowl('Изделие <b>'+ name+'</b> уже в корзине');
			}	else if (data=="ABSENT") {
				$.jGrowl('Изделие <b>'+ name+'</b> временно отсутствует на складе');
			} else {
				$.jGrowl(data);
			}
		}
	);
	return false;
}
$(document).ready(function() {
	if ($('#container').width() < 1010) {
		$('#container').width(1010);
	}
 	if ($('#catalog').size() == 1) {
		justify();
	}
	$("#registration_form").validationEngine();

	$('a.big').fancybox({
		'scrolling' : 'no',
		'overlayOpacity': 0.8,
		'overlayColor': '#fff'
	});
	$(".a_login").fancybox({
		'href' : '/users/login/ajax',
		'scrolling' : 'no',
		'overlayOpacity': 0.8,
		'overlayColor': '#fff',
		'onComplete'	:	function() {
			$("#login_form").validationEngine({
				ajaxSubmit: true,
				ajaxSubmitFile: "/users/login/ajax",
				success :  function() {
					window.location.reload(true);
				},
				failure : function() {}
			});
		},
		'onCleanup' : function() {
			$.validationEngine.closePrompt('.formError',true);
		}
	});

	$(".a_remind_pass").fancybox({
		'href' : '/users/remind/ajax',
		'scrolling' : 'no',
		'overlayOpacity': 0.8,
		'overlayColor': '#fff',
		'onComplete'	:	function() {
			$("#login_form").validationEngine({
				ajaxSubmit: true,
				ajaxSubmitFile: "/users/remind/ajax",
				success :  function() {
					$.fancybox('На Ваш почтовый адрес выслан новый пароль.');
				},
				failure : function() {$('#login_error').show()}
			});
		},
		'onCleanup' : function() {
			$.validationEngine.closePrompt('.formError',true);
		}
	});

	$(".photo_element1 a").fancybox({
		'scrolling' : 'no',
		'overlayOpacity': 0.8,
		'overlayColor': '#fff',
		'cyclic': true
	});
	
	$('#tabs').delegate('td:not(.current)', 'click', function() {
		$(this).addClass('current').siblings().removeClass('current')
			.parents('div.section').find('div.box').hide().eq($(this).index()).fadeIn(150);
		document.location.hash = '#tab' + $(this).index();
	});

	if ((String(document.location.hash)).substring(0,4) == '#tab' && !isNaN(parseInt((String(document.location.hash)).substring(4)))) {
		$('#tabs td:eq(' + String(parseInt((String(document.location.hash)).substring(4))) + ')').click();
	} else {
		$('#tabs td:first').click();
	}

	$('#faq_header a').click(function() {
		$('span.faq_highlighted').removeClass('faq_highlighted');
		href = $(this).attr('href');
		if (href.substring(0, 5) == '/faq#') {
			hash = href.substring(5);
			$("a[name='" + hash + "']").next().children().addClass('faq_highlighted');
		}
	});
});
$(window).resize(function() {
	if ($('#container').width() < 1010) {
		$('#container').css('width', '1010px');
	}
 	if ($('#catalog').size() == 1) {
		justify();
	}
});

function genFilterLink(name, t, count) {
	var prefix = '/catalog';

	if (count == '') {
		if (t == 'type') {
			type = '';
		} else if (t == 'brand') {
			brand = '';
		} else if (t == 'color') {
			color = '';
		} else if (t == 'size') {
			size = '';
		}
		$('#select_' + t).prev().removeClass('select_header_b');
	} else {
		if (t == 'type') {
			type = '/type' + count;
		} else if (t == 'brand') {
			brand = '/brand' + count;
		} else if (t == 'color') {
			color = '/color' + count;
		} else if (t == 'size') {
			size = '/size' + count;
		}
		$('#select_' + t).prev().addClass('select_header_b');
	}
	$('#select_' + t).prev().children().text(name);

	var t = prefix + sex + type + brand + color + size + snew + sale + stock;
	$('#select_button').attr('href', t);
}

