var tmp = '';
var obj = '';

function validation(str,re)
{
	var VRegExp = new RegExp(re);
	var VResult = str.replace(VRegExp,'');
	if(VResult.length > 0) return false;
		else return true;
}

$(document).ready(function(){

	$('img').hover(
		function()
		{
			if( (!$(this).hasClass('act')) && ($(this).attr('hover')) )
			{
				tmp = $(this).attr('src');
				$(this).attr('src', $(this).attr('hover'));
			}
		},
		function()
		{
			if( (!$(this).hasClass('act')) && ($(this).attr('hover')) )
				$(this).attr('src', tmp);
		}
	);

	$('.to_cart, .to_cart3').hover(
		function()
		{
			$(this).addClass('hovered');
		},
		function()
		{
			$(this).removeClass('hovered');
		}
	);

	$('#vl').click(
		function()
		{
			if($(this).val()==1) $(this).val('');
		});

	$('input[name=count]').click(
		function()
		{
			if($(this).val()==1) $(this).val('');
		});

	$('.wj_list .it a').click(function(){
		return false;
	});

	$('.wj_list .it a').mouseover(function(){
		show = $(this).attr('show');
		if($('#'+show).css('display') == 'none') $('#'+show).show(200);
	});

	$('.wj_list .it a').mouseout(function(){
		show = $(this).attr('show');
		if($('#'+show).css('display') == 'block')
		{
			$('.what_jovi_text').hide(200);
			//$('#'+show).show(400);
		}
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

	$('input').focus(function(){
		if($(this).val() == $(this).attr('default')) $(this).attr('value', '');
	});

	$('input').blur(function(){
		if($(this).val() == '') $(this).attr('value', $(this).attr('default'));
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

	$('div.to_cart a[href=javascript:;], div.to_cart2 a[href=javascript:;], div.to_cart3 a[href=javascript:;]').click(function(){

		obj = this;

		$.ajax({
			'url':'/index.html',
			'dataType':'json',
			'type':'POST',
			'data':'f=basket_add&itemID='+$(this).attr('itemID')+'&count='+($('#vl').val() ? $('#vl').val() : ( $('input#'+$(this).attr('itemID')).val() ? $('input#'+$(this).attr('itemID')).val() : 1 ) ),
			'success':function(data){
				$('div.total_h').html(data['html']['header']);
				$('div.total_f').html(data['html']['footer']);
			}
		});

		$(obj).html('готово!');
		setTimeout(function(){
			$(obj).html('в корзину');
		}, 3000);
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

	$('td.inp2 input[name=item_total]').keyup(function(){
		$.ajax({
			'url':'/index.html',
			'dataType':'json',
			'type':'POST',
			'data':'f=basket_add&itemID='+$(this).attr('itemID')+'&count='+($(this).val() ? $(this).val() : 1),
			'success':function(data){
				$('div.total_h').html(data['html']['header']);
				$('div.total_f').html(data['html']['footer']);

				$('p.bl').html(data['html']['delivery']);
				$('span.price5').html(data['html']['price']);
			}
		});
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

	$('div.form_block div.inp3 input[type=text]').blur(function(){
		if($(this).val() == '')
		{
			$(this).addClass('err');
			$(this).attr('value', 'Информация введена некорректно');
		}
		else if($(this).attr('name') == 'email')
		{
			if(!validation($(this).val(),/([a-z_\-\.\d]+)+@+([a-z\d]+)+\.+([a-z]{2,4})/gi))
			{
				$(this).addClass('err');
				$(this).attr('value', 'Информация введена некорректно');
			}
		}
	});

	$('div.form_block div.inp3 input[type=text]').focus(function(){
		if($(this).val() == 'Информация введена некорректно')
		{
			$(this).removeClass('err');
			$(this).attr('value', '');
		}
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

	$('div.form_block div.inp4 textarea').blur(function(){
		if($(this).val() == '' && $(this).attr('name') != 'comments')
		{
			$(this).addClass('err');
			$(this).attr('value', 'Информация введена некорректно');
		}
	});

	$('div.form_block div.inp4 textarea').focus(function(){
		if($(this).val() == 'Информация введена некорректно' && $(this).attr('name') != 'comments')
		{
			$(this).removeClass('err');
			$(this).attr('value', '');
		}
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

	$('form#formOrder a.submit').click(function(){
		error = 0;

		$('form#formOrder input[type=text]').each(function(){
			if($(this).attr('class') == 'err' || $(this).val() == '')
			{
				error = 1;
				$(this).addClass('err');
				$(this).attr('value', 'Информация введена некорректно');
			}
		});

		$('form#formOrder textarea').each(function(){
			if(($(this).attr('class') == 'err' || $(this).val() == '') && $(this).attr('name') != 'comments')
			{
				error = 1;
				$(this).addClass('err');
				$(this).attr('value', 'Информация введена некорректно');
			}
		});

		if(!error) $('form#formOrder').submit();
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

	$('table.menu a.submit').click(function(){
		$('form#formSearch').submit();
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

		$('form#formfeedback div.inp3 input[type=text]').blur(function(){
		if($(this).val() == '')
		{
			$(this).addClass('err');
			$(this).attr('value', 'Информация введена некорректно');
		}
		else if($(this).attr('name') == 'email')
		{
			if(!validation($(this).val(),/([a-z_\-\.\d]+)+@+([a-z\d]+)+\.+([a-z]{2,4})/gi))
			{
				$(this).addClass('err');
				$(this).attr('value', 'Информация введена некорректно');
			}
		}
	});

	$('form#formfeedback div.inp3 input[type=text]').focus(function(){
		if($(this).val() == 'Информация введена некорректно')
		{
			$(this).removeClass('err');
			$(this).attr('value', '');
		}
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

	$('form#formfeedback div.inp4 textarea').blur(function(){
		if($(this).val() == '')
		{
			$(this).addClass('err');
			$(this).attr('value', 'Информация введена некорректно');
		}
	});

	$('form#formfeedback div.inp4 textarea').focus(function(){
		if($(this).val() == 'Информация введена некорректно')
		{
			$(this).removeClass('err');
			$(this).attr('value', '');
		}
	});

	/* ------------------------------------------------------------------------------------------------------------------- */

	$('form#formfeedback a.submit').click(function(){
		error = 0;

		$('form#formfeedback input[type=text]').each(function(){
			if($(this).attr('class') == 'err' || $(this).val() == '')
			{
				error = 1;
				$(this).addClass('err');
				$(this).attr('value', 'Информация введена некорректно');
			}
		});

		$('form#formfeedback textarea').each(function(){
			if($(this).attr('class') == 'err' || $(this).val() == '')
			{
				error = 1;
				$(this).addClass('err');
				$(this).attr('value', 'Информация введена некорректно');
			}
		});

		if(!error) $('form#formfeedback').submit();
	});
});

