$(document).ready(function()
{
	$(".logo table div.hover").css({opacity:0});

	$('.firstValueDeleted').one('click',function(){this.def = $(this).val();$(this).val('');});
	$('.firstValueDeleted').blur(function(){if($(this).val() == ''){$(this).val(this.def)}});
	$('.firstValueDeleted').click(function(){if($(this).val() == this.def){$(this).val('');}});

	/*logo*/
	$(".logo table").hover(
	function(){
		$(this).stop().animate({ backgroundColor: "#3e3e3e" }, 400);
		$(this).find('div.hover').animate({opacity:1},300)
		$(this).find('div.nothover').animate({opacity:0},300);
	}
	,function(){
		$(this).stop().animate({ backgroundColor: "#555555" }, 400);
		$(this).find('div.hover').animate({opacity:0},300)
		$(this).find('div.nothover').animate({opacity:1},300);
	}
	);

	/*horizontal menu with drop-down*/
	$(".nav-hor td").each(function(){
		var td = $(this);
		var p = $(this).find('p');
		var div = $(this).find('div');
		var height = div.height();
		div.css({height: 0});
		td.hover(
		function(){
			div.stop().show().animate({height: height}, 200);
			if (!p.hasClass('selected')){p.addClass('hovered');}
		}
		,function(){
			div.stop().animate({height: 0}, 100, function(){div.hide();});
			if (!p.hasClass('selected')){p.removeClass('hovered');}
		}
		);
	});

	$.datepicker.setDefaults($.datepicker.regional['ru']);
	$("#date1").datepicker({"minDate":"-3y","maxDate":"+0d","dateFormat":"yy-mm-dd"});
	$("#date2").datepicker({"minDate":"-3y","maxDate":"+0d","dateFormat":"yy-mm-dd"});

	/*basket*/
	$(".basket").hover(
	function(){$(this).addClass('script-link');}
	,function(){$(this).removeClass('script-link');}
	);

	/*slider*/
	$('.slide-right').click(function(){
		$('.slider').animate({marginLeft: -196}, 300, function(){
			$('td.sliding-group:last', this).after($('td.sliding-group:first', this));
			$(this).css({marginLeft: 0});
		});
	});

	$('.slide-left').click(function(){
		$('.slider').each(function(){
			$('td.sliding-group:first', this).before($('td.sliding-group:last', this));
			$(this).css({marginLeft: -196}).animate({marginLeft: 0}, 300);
		});
	});

	/*slider-buttons*/
	$(".sliding-group div").hover(
	function(){$('.button', this).addClass('hovered-button')}
	,function(){$('.button', this).removeClass('hovered-button')}
	);

	/*slider-links*/
	$(".sliding-group div.kamimamita").click(
	function(){window.location = $(this).parent().find('a').attr('href');return false;}
	);

	/*sub-cat link*/
	$(".large-link").hover(
	function(){$(this).addClass('large-hovered');}
	,function(){$(this).removeClass('large-hovered');}
	);

	/*map*/
	$(".map").hover(
	function(){$(this).addClass('script-link')},
	function(){$(this).removeClass('script-link')}
	);

	/*buttons*/
	$(".button").hover(
		function(){
			if (!$(this).hasClass('kamimamita')){$(this).addClass('hovered-button');}
		}
		,function(){
			if(!$(this).hasClass('kamimamita')){$(this).removeClass('hovered-button');}
		}
	);

	/*links*/
	$(".map, .basket, .nav-hor p, .logo table, .large-link").click(
	function(){
		window.location = $('a', this).attr('href');
		return false;
	}
	);


	$(".banner div").click(function(){window.location = $('a', this).attr('href');return false;});

	$('#subFormSubmit').click(function(){$('#subForm').submit();return false;});
	$('#submitSearchFrom').click(function(){$('#searchFrom').submit();return false;});
	$('#doSubmitregForm').click(function(){$('#regForm').submit();return false;});
	$('#doSubmitregForm2').click(function(){$('#regForm2').submit();return false;});
	$('#doSubmitLogForm').click(function(){$('#logForm').submit();return false;});
	$('#refreshBasket').click(function(){document.location.href='/korzina/';return false;});
	$('#doResoteLogForm').click(function(){document.location.href='/restorePassword/';return false;});
	$('#viewDocHistoryPrint').click(function(){document.location.href=document.location.href+'&print=1';return false;});
	
	$('.delFromDoc').click(function(){
		if(confirm('Вы действительно хотите удалить заказ?'))
		$.post('/orderHistory/?'+$(this).attr('href'),{del:$(this).attr('id')},function(){
			document.location.replace('/orderHistory/?date1='+$('#date1').val()+'&date2='+$('#date2').val());
		});
		return false;
	});
	
	$('#viewDocHistoryListPrint').click(function()
	{
		var tmp = '?date1='+$('#date1').val()+'&date2='+$('#date2').val()+'&print=1';
		window.open(document.location.href+tmp);
		return false;
	});
	
	$('#viewStatmentListPrint').click(function()
	{
		var tmp = '?date1='+$('#date1').val()+'&date2='+$('#date2').val()+'&print=1';
		window.open(document.location.href+tmp);
		return false;
	});

	$('#cat_group_select').change(function(){document.location.replace($('#cat_group_select').val());return false;});
	$('#doSearchCatForm').click(function(){$('#searchCatForm').submit();return false;});

	$('.slide-left, .slide-right').hide();
	setTimeout("$('.slider').animate({marginLeft:-"+(784)+"},3000)",500);
	setTimeout("slideStop()",3550)


	$('img[zoom=yes]').attr('style','cursor:pointer;cursor:hand;')
	.click(function(){ims($(this).attr('src1'));});
	
	$('.to_cart').click(to_cart);
	
	var basketCountFlag = false;
	$('#reCalcBasket').click(function(){
		basketCountFlag = true;
		if($('#basketForm input.iCountInput, #basketForm input.iCountInputPack').each(function(){
			if(!validCnt($(this).val())){
				basketCountFlag = false;
				return false;
			}
		}))
		if(basketCountFlag) $('#basketForm').submit();
		
		return false;
	});
	
	$('#historyZak').click(function(){document.location.href='/orderHistory/';})
	$('#statement').click(function(){document.location.href='/statement/';})
	$('#viewDocHistoryFile').click(function(){document.location.href=document.location.href+'&inFile=1';return false;})
	
	$('#reViewOrderHistory').click(function(){
		if($('#date1').val() && $('#date2').val()) $('#reViewOrderHistoryForm').submit()
		else alert('Заполните период формирования отчета');
		
		return false;
	});
	
	$('#clearKorz').click(function(){
		if(confirm('Вы действительно хотите отчистить корзину?'+"\n"+'Подтверждение полной очистки корзины приведет к безвозвратному удалению всех выбранных товаров!'))
		{
			document.cookie = "basket=;expires=Thu, 01-Jan-1970 00:00:01 GMT;path= /;";
			document.location.href='/korzina/';
		}
		return false;
	})
	
	$('#copyOrderToCart').click(function(){
		var c = getCookie();
		if(confirm('Очистить предварительно корзину ?') || !c){
			setCookie($(this).find('input').val());
		}
		else
		{
			setCookie(c+','+$(this).find('input').val());
		}
		
		setSummForCart();
		
		return false;
	});
	
	$('#basketRun').click(function(){
		$('#basketRun').unbind('click');
		$('#basketForm').attr('action','/korzina/order/');
		$('#basketForm').submit();
		return false;
	});
	
	$('.delFromBasket').click(function(){
		if(confirm('Действительно удалить товар из корзины?')){
			var id = $(this).attr('href');
			$.post('/korzina/',{del:id},function(){document.location.replace('/korzina/')});
		}
		return false;
	});
	
	$('.delFromBasketPack').click(function(){
		if(confirm('Действительно удалить товар из корзины?')){
			var id = $(this).attr('href');
			$.post('/korzina/',{del:id,pack:1},function(){document.location.replace('/korzina/')});
		}
		return false;
	});

	setSummForCart();
	$('#infocart p').click(function(){document.location.replace('/korzina/')});
});

function ims(i,width) {
	$("#xs").animate({opacity: "0.4", height: $(".main-table").height()}, 200);
	w = width > 600 ? 600 : width
	$("#mpp").html('<img class="nw" alt=""  width="'+w+'" src="'+i+'" onclick="imsh()"/>');
	$("#mpp").animate({ opacity: "show" }, 600);
};
function imsh() {
	$("#xs").animate({opacity:"0.0",height:"0"},200);
	$("#mpp").animate({opacity:"hide"},600);
};

function slideStop() {
	$('.slider').css({marginLeft:0});
	$('td.sliding-group:first', $('.slider')).remove();
	$('td.sliding-group:first', $('.slider')).remove();
	$('td.sliding-group:first', $('.slider')).remove();
	$('td.sliding-group:first', $('.slider')).remove();
	$('.slide-left, .slide-right').show();
}

function zoom_print(name){
	var p=window.open('/zoom.php?n='+name,'preview_window',"top=0,left=0,location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,fullscreen=0");
	if (p) p.focus();
	return false;
}


jQuery(function($){
$.datepicker.regional['ru'] = {clearText: 'Очистить', clearStatus: '',
closeText: 'Закрыть', closeStatus: '',
prevText: '&lt;Пред', prevStatus: '',
nextText: 'След&gt;', nextStatus: '',
currentText: 'Сегодня', currentStatus: '',
monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
monthNamesShort: ['Янв','Фев','Мар','Апр','Май','Июн','Июл','Авг','Сен','Окт','Ноя','Дек'],
monthStatus: '', yearStatus: '',
weekHeader: 'Не', weekStatus: '',
dayNames: ['воскресенье','понедельник','вторник','среда','четверг','пятница','суббота'],
dayNamesShort: ['вск','пнд','втр','срд','чтв','птн','сбт'],
dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
dayStatus: 'DD', dateStatus: 'D, M d',
dateFormat: 'dd.mm.yy', firstDay: 1,
initStatus: '', isRTL: false};
$.datepicker.setDefaults($.datepicker.regional['ru']);
});
