jQuery(document).ready(function () {
	jQuery('[rel=tooltip]').tooltip({
		bodyHandler: function () {
			return '<div id="hover" class="hover1"><img src="/img/ppp.jpg"></div>';
		},
		showURL : false,
		fade: false
	}).length;
});
jQuery(document).ready(function () {
	jQuery('#where').click(function () {
		jQuery.get('/where/',{},function (szHTML) {
			jQuery('body').append(szHTML);
		});
	});
});
jQuery(document).ready(function () {
	var $ = jQuery;

	if ($('#credit-form').length == 0) { return ; } 
	$('#credit-form input[type=text]').each(function () {

		$(this).val($(this).attr('rel'));

		$(this).focus(function () {
			if ($(this).val() == $(this).attr('rel'))
			{
				$(this).val('');
			}
		}).blur(function () { 
			if ($(this).val() == '')
			{
				$(this).val($(this).attr('rel'));
			}
		})
	});
	$('#credit-form').submit(function () {
		var result = true;
		$('#credit-form input[type=text]').each(function () {
			if (($(this).val() == $(this).attr('rel'))
				|| ($(this).val() == ''))
			{
				alert('Вы не заполнили поле "'+ $(this).attr('rel')+ '"');
				$(this).focus();
				result = false;
				return false
			}
		});
		return result;
	});
	
});
jQuery(document).ready(function () {
	jQuery('#contact-form').each(function () {
		jQuery('#name').focus(function () {
			if (jQuery('#name').val() == 'Контактное лицо')
			{
				jQuery('#name').val('')
			}
		}).blur(function () {
			if (jQuery('#name').val() == '')
			{
				jQuery('#name').val('Контактное лицо')
			}
		});

		jQuery('#email').focus(function () {
			if (jQuery('#email').val() == 'E-mail')
			{
				jQuery('#email').val('')
			}
		}).blur(function () {
			if (jQuery('#email').val() == '')
			{
				jQuery('#email').val('E-mail')
			}
		});

		jQuery('#subject').focus(function () {
			if (jQuery('#subject').val() == 'Тема сообщения')
			{
				jQuery('#subject').val('')
			}
		}).blur(function () {
			if (jQuery('#subject').val() == '')
			{
				jQuery('#subject').val('Тема сообщения')
			}
		});

		jQuery('#message').focus(function () {
			if (jQuery('#message').val() == 'Сообщение')
			{
				jQuery('#message').val('')
			}
		}).blur(function () {
			if (jQuery('#message').val() == '')
			{
				jQuery('#message').val('Сообщение')
			}
		});

		jQuery('#company').focus(function () {
			if (jQuery('#company').val() == 'Название компании')
			{
				jQuery('#company').val('')
			}
		}).blur(function () {
			if (jQuery('#company').val() == '')
			{
				jQuery('#company').val('Название компании')
			}
		});

		jQuery('#url').focus(function () {
			if (jQuery('#url').val() == 'URL')
			{
				jQuery('#url').val('')
			}
		}).blur(function () {
			if (jQuery('#url').val() == '')
			{
				jQuery('#url').val('URL')
			}
		});

		jQuery('#address').focus(function () {
			if (jQuery('#address').val() == 'Фактический адрес')
			{
				jQuery('#address').val('')
			}
		}).blur(function () {
			if (jQuery('#address').val() == '')
			{
				jQuery('#address').val('Фактический адрес')
			}
		});

		jQuery('#phone').focus(function () {
			if (jQuery('#phone').val() == 'Контактный номер телефона')
			{
				jQuery('#phone').val('')
			}
		}).blur(function () {
			if (jQuery('#phone').val() == '')
			{
				jQuery('#phone').val('Контактный номер телефона')
			}
		});
	})
});
jQuery(document).ready(function () {
	jQuery('#basket').each(function () {
		jQuery('#name').focus(function () {
			if (jQuery('#name').val() == 'Ф.И.О.')
			{
				jQuery('#name').val('')
			}
		}).blur(function () {
			if (jQuery('#name').val() == '')
			{
				jQuery('#name').val('Ф.И.О.')
			}
		});
		jQuery('#city').focus(function () {
			if (jQuery('#city').val() == 'Город')
			{
				jQuery('#city').val('')
			}
		}).blur(function () {
			if (jQuery('#city').val() == '')
			{
				jQuery('#city').val('Город')
			}
		});
		jQuery('#street').focus(function () {
			if (jQuery('#street').val() == 'Улица')
			{
				jQuery('#street').val('')
			}
		}).blur(function () {
			if (jQuery('#street').val() == '')
			{
				jQuery('#street').val('Улица')
			}
		});
		jQuery('#house').focus(function () {
			if (jQuery('#house').val() == 'Номер дома')
			{
				jQuery('#house').val('Номер дома')
			}
		}).blur(function () {
			if (jQuery('#house').val() == 'Номер дома')
			{
				jQuery('#house').val('Номер дома')
			}
		});
		jQuery('#apartment').focus(function () {
			if (jQuery('#apartment').val() == 'Квартира')
			{
				jQuery('#apartment').val('')
			}
		}).blur(function () {
			if (jQuery('#apartment').val() == '')
			{
				jQuery('#apartment').val('Квартира')
			}
		});
		jQuery('#phone').focus(function () {
			if (jQuery('#phone').val() == 'Номер телефона')
			{
				jQuery('#phone').val('')
			}
		}).blur(function () {
			if (jQuery('#phone').val() == '')
			{
				jQuery('#phone').val('Номер телефона')
			}
		});
		jQuery('#email').focus(function () {
			if (jQuery('#email').val() == 'E-Mail')
			{
				jQuery('#email').val('')
			}
		}).blur(function () {
			if (jQuery('#email').val() == '')
			{
				jQuery('#email').val('E-Mail')
			}
		});

		jQuery('input.num,input[name=delivery_id]').change(function () {
			var fPrice = 0;
			jQuery('input.num').each (function () {
				fPrice += jQuery('#' + (this.id).replace('count','price')).val() * parseInt(this.value);
			});
			jQuery('div.resul span').html(' ' + fPrice + ' USD');

			// 
			jQuery('input[name=delivery_id]').each(function () {
				if (this.checked)
				{
					var aData = this.value.split(':');
					fPrice += parseFloat(aData[1]);
					jQuery('#delivery_cost').html('<b> ' + parseFloat(aData[1]) +' USD</b> ' + jQuery(this).next('span').html() +' :');
				}
			});
			//
			jQuery('#total_cost').html(' ' + fPrice + ' USD');
			
		})
		jQuery('#basket').submit(function () {
			if ((jQuery('#email').val().length == 0) || (jQuery('#email').val() == 'E-Mail'))
			{
				alert('Заполните поле `E-Mail`');
				jQuery('#email').focus();
				return false;
			}

			if ((jQuery('#phone').val().length == 0) || (jQuery('#phone').val() == 'Номер телефона'))
			{
				alert('Заполните поле `Номер телефона`');
				jQuery('#phone').focus();
				return false;
			}
			var bFound = false;
			jQuery('input[name=pay_method_id]').each(function () {
				if (this.checked)
				{
					bFound = true;
				}
			});
			if (!bFound)
			{
				alert('Выберите метод платежа');
				return false;
			}

			var bFound = false;
			jQuery('input[name=delivery_id]').each(function () {
				if (this.checked)
				{
					bFound = true;
				}
			});
			if (!bFound)
			{
				alert('Выберите тип доставки');
				return false;
			}
		

			var szAddress = '';
			szAddress = 'Фио: ' + jQuery('#name').val() + "\r\n" + ' Адрес:' + jQuery('#city').val() + ' ' + jQuery('#street').val() + ' ' + jQuery('#house').val() + ' ' + jQuery('#apartment').val() ;
			jQuery('#basket').append('<input type="hidden" name="address" value="' + szAddress + '"/>');
		});
	});
});
jQuery(document).ready(function () {
	jQuery('#commentform input[type=image]').click(function () { 
		var name = jQuery('#commentform input[name=name]');
		if (name.val() == 'Ваше имя')
		{
			name.val('');
		}
		var content = jQuery('#commentform textarea');
		if (content.val() == 'Ваш отзыв')
		{
			content.val('');
		}
	});
	jQuery('#commentform').validate({
		rules : {
			name : {
				required : true
			},
			content : {
				required : true,
				minlength : 5
			}
		},
		messages : {
			name : {
				required : 'Обязательное поле',
			},
			content : {
				required : 'Обязательное поле',
				minlength : 'Минимальная длина сообщения 5 символов'
			}
		}
	});
});
jQuery.noConflict();
