$(document).ready(function () {
	// 
	var nSliderDataWidth = 0;
	var nSliderWidth = $('div.slider').width();
	var nSliderItemCount = 0;
	$('div.slider div.line > *').each(function () {
		nSliderDataWidth += $(this).width();
		nSliderItemCount++;
	});
	if (nSliderWidth < nSliderDataWidth)
	{
		var nSlideWidth = nSliderDataWidth - nSliderWidth;
		$('div.slide2').slider({
			min : 0,
			max : 1000,
			slide : function () {
				var nPos = $('div.slide2').slider('value');

				// Получаем область которую можем передвигать
				// nPos это доля процента от области которую мы можем передвинуть
				$('div.slider div.line').css('margin-left',-1 * Math.round(nPos * (nSlideWidth) / 1000) + 'px');
			}
		});
		// Вешаем обработчики на стрелки "Влево" и "Вправо"
		$('a.larr,a.rarr').click(function () {
			var nPos = $('div.slide2').slider('value');
			var nAvgItemWidth = nSliderDataWidth / nSliderItemCount;
			var nMarginLeft = Math.round(nPos * nSlideWidth / 1000);

			// Добавляем смещение
			nMarginLeft += $(this).attr('rel') * nAvgItemWidth
			// Проверяем на вылет за диапазон
			if ((nMarginLeft < 0) || (nMarginLeft > nSlideWidth))
			{
				if (nMarginLeft > nSlideWidth)
				{
					nMarginLeft = nSlideWidth;
					nPos = 1000;
				}
				else 
				{
					nPos = 0;
					nMarginLeft = 0;
				}
			}
			else
			{
				// Вычисляем новую позицию nPos
				nPos = Math.round(nMarginLeft / nSlideWidth * 1000) ;
				// И вставляем её обратно
				
			}

			$('div.slide2').slider('value',nPos);
			$('div.slider div.line').css('margin-left',-1 * (nMarginLeft) + 'px');
			return false;
		});
	}
	else
	{
		$('div.slide,a.larr,a.rarr').css('display','none');
	}
	$('a.order').click(function () {
		var link = $(this);
		$.post('/basket/',{
			ajax          : 1,
			document_name : link.next().val(),
			document_id   : link.next().next().val(),
			count         : link.next().next().next().val()
		},function (szCode) {
				if (szCode == 'Ok')
				{
					if (confirm('Продолжить оформление заказа?'))
					{
						window.location = '/basket/';
					}
					else
					{
					}
				}
				else
				{
					alert('Произошла ошибка при оформлении заказа');
				}
			});
		return false;
	});
	$('a.order_send').click(function () {
		// 
		if ($('input[name=email]').val() == 'e-mail')
		{
			alert('Вы не написали свой e-mail. Это минимальные данные, которые нужны для оформления заказа');
			return false;
		}
		// 
		if ($('input[name=payment_method]:checked').length < 1)
		{
			alert('Вы не выбрали метод оплаты');
			return false;
		}
		$('#order_form').submit();
		return false;
	});
	// Подключаем форму запроса на ремонт
	initDialog('a.diagnosis','#repair-form',function () {
		jQuery('#repair-form form').submit(function () {
			var bResult = true;
			var email = $('#repair-form form input[name=email]');
			var content = $('#repair-form form textarea');
			if (email.val() == 'e-mail')
			{
				alert('Вы не ввели ваш E-mail');
				bResult = false;
				email.focus();
			}
			if (content.val() == 'опишите возникшую поломку')
			{
				alert('Вы не описали поломку');
				bResult = false;
				content.focus();
			}
			return bResult;
		});
	});

	// Подключаем форму запроса на ремонт
	initDialog('a.send','#contact-form',function () {
		jQuery('#contact-form form').submit(function () {
			var bResult = true;
			var email = $('#contact-form form input[name=email]');
			var content = $('#contact-form form textarea');
			if (email.val() == 'e-mail')
			{
				alert('Вы не ввели ваш E-mail');
				bResult = false;
				email.focus();
			}
			if (content.val() == 'опишите возникшую поломку')
			{
				alert('Вы не описали поломку');
				bResult = false;
				content.focus();
			}
			return bResult;
		});
	});
	// Подключаем форму запроса на ремонт
	initDialog('a.sendotziv','#comment-form',function () {
		jQuery('#comment-form form').submit(function () {
			var bResult = true;
			var email = $('#comment-form form input[name=email]');
			var name = $('#comment-form form input[name=name]');
			var content = $('#comment-form form textarea');
			if (name.val() == 'ваше имя')
			{
				alert('Вы не представились');
				bResult = false;
				name.focus();
			}
			if (email.val() == 'e-mail')
			{
				alert('Вы не ввели ваш E-mail');
				bResult = false;
				email.focus();
			}
			if (content.val() == 'Ваш комментарий')
			{
				alert('Вы забыли написать сам Комментарий');
				bResult = false;
				content.focus();
			}
			return bResult;
		});
	});
	function initDialog(clickSelector,selector,func)
	{
		$(clickSelector).click(function () {
			$(selector).css('display','block').css('height',$(document).height());
			$(selector + ' a.close').click(function () {
				$(selector).css('display','none');
			});

			$(selector + ' a.submit').click(function () {
				$(selector + ' form').submit();
				return false;
			});
			func.call();
			return false;
		});
	}
});

