$(function() {
	/* инициализация слайдера */
	var min_val = parseInt($('#amount1').val());
	var max_val = parseInt($('#amount2').val());
	var gl_min_val = parseInt($('#amount3').val());
	var gl_max_val = parseInt($('#amount4').val());
	$("#slider-range").slider({
		range: true,
		min: gl_min_val,
		max: gl_max_val,
		values: [min_val, max_val],
		slide: function(event, ui) {
			$("#amount1").val(ui.values[0]);
			$("#amount2").val(ui.values[1]);
		}
	});
//	$("#amount1").val($("#slider-range").slider("values", 0) );
//	$("#amount2").val($("#slider-range").slider("values",1));
	/* //инициализация слайдера */

	/* инициализация отображения оверлея с изображением */
	$('.a_m1').overlay({
		mask: {
			color: '#ffffff',
			loadSpeed: 0,
			opacity: 0,
			zIndex: 9998
		},
		closeOnClick: true,
		closeSpeed: 0,
		speed: 0
	});
	$('.zoom a').overlay({
		mask: {
			color: '#ffffff',
			loadSpeed: 0,
			opacity: 0,
			zIndex: 9998
		},
		closeOnClick: true,
		closeSpeed: 0,
		speed: 0
	});
	$('#image_container').overlay({
		mask: {
			color: '#ffffff',
			loadSpeed: 0,
			opacity: 0,
			zIndex: 9998
		},
		closeOnClick: true,
		closeSpeed: 0,
		speed: 0
	});
	/* //инициализация отображения оверлея с изображением */

	/* показать/скрыть фильтр товаров */
//	$('.up_arrow, .down_arrow').click(function(){
//		class = $(this).attr('class');
//		id = $(this).attr('rel');
//
//		if (class == 'up_arrow') {
//			$(this).attr('class', 'down_arrow');
//			$('#' + id).slideUp();
//		} else {
//			$(this).attr('class', 'up_arrow');
//			$('#' + id).slideDown();
//		}
//	});
//	$('.filter').click(function(){
//		arrow_container = $(this).find('a.arrow');
//		id = arrow_container.attr('rel');
//
//		if (arrow_container.hasClass('up_arrow')) {
//			arrow_container.removeClass('up_arrow').addClass('down_arrow');
//			$('#' + id).slideUp();
//		} else {
//			arrow_container.removeClass('down_arrow').addClass('up_arrow');
//			$('#' + id).slideDown();
//		}
//	});
	/* //показать/скрыть фильтр товаров */

	/* выпадающее меню */
	$(".menu ul li a").mouseover(function() {
		var self = $(this);

		if (self.hasClass('trigger')) self.css('background-image', "url('/i/menu_bg_.png')");
		self.next().show();
	
		self.parent().hover(function() {}, function(){
			if (self.hasClass('trigger')) self.css('background-image', "url('/i/menu_bg.png')");
			self.parent().find("ul.sub_menu").hide();
		});
	});
	/* //выпадающее меню */

	/* Примеры для полей */
	$("#s").example(function(){
		return $(this).attr('title'); 
	}, { className: 'example_text' });
	/* //Примеры для полей */


	/* Вывод слайдшоу на титульной странице */
	if ($("#showcase").hasClass('main_img1')) {
		var json_showcase;
		$.ajax({
			dataType: 'json',
			url: '/netcat/modules/ajax/json_showcase.php',
			success: function(data) {
	
				$("#showcase").showcase({
					animation: { type: "fade" },
					images: data,
					navigator: {
						item:{
							css: {
								height:"10px",
								width:"10px",
								"margin-bottom":"0"
							}
						},
						orientation: "gorizontal",
						position: "bottom-right"
					},
					titleBar: {}
				});
	
			}
		});
	}

	/* Вывод слайдшоу на титульной странице */
	
	$('.unactive_image').css('opacity', 0.5);

	
	$('.show_buttons, .hov').hover(function(){
		id = $(this).attr('rel');
		$(id).show();
	},function(){
		id = $(this).attr('rel');
		$(id).hide();
	});

	$('.submit_form img').hover(function(){
		$(this).attr('src', '/i/add_to_cart_hover.gif');
	}, function(){
		$(this).attr('src', '/i/add_to_cart.gif');
	});
	$('.submit_form').click(function(){
		that = $(this);
		id = that.attr("rel");
		
		that.children().attr('src', '/i/add_to_cart_click.gif');
		
		$(this).parent().ajaxSubmit({
			// $.ajax options
			complete: function(msg) {
				if (msg.responseText) {
					that.next().remove();
					that.after("<p class='pink font11'>товар добавлен в корзину</p>");
					that.remove();
					$("#added_message_" + id).show();
					$.ajax({
						url: "/netcat/modules/ajax/cart.php",
						cache: false,
						success: function(html){
							$('#cart_contents').html(html);
						}
					});
				}
			}
		});
		return false;
	});

	$(".more_info img").hover(function(){
		$(this).attr('src', '/i/more_info_hover.png');
	}, function(){
		$(this).attr('src', '/i/more_info.png');
	});
	$(".more_info img").click(function(){
		$(this).attr('src', '/i/more_info_click.png');
	});


	$(".add_to_cart img").hover(function(){
		$(this).attr('src', '/i/add_to_cart_2_hover.png');
	}, function(){
		$(this).attr('src', '/i/add_to_cart_2.png');
	});
	$(".add_to_cart").click(function(){
		that = $(this);
		id = that.attr("rel");
		
		that.children().attr('src', '/i/add_to_cart_2_click.png');
		
		$(this).parent().ajaxSubmit({
			// $.ajax options
			complete: function(msg) {
				if (msg.responseText) {
					$("#added_message_" + id).prev().remove();
					$("#added_message_" + id).before("<p class='pink font11'>товар добавлен в корзину</p>");
					$("#photo" + id).hide();
					$("#added_message_" + id).show();
					$.ajax({
						url: "/netcat/modules/ajax/cart.php",
						cache: false,
						success: function(html){
							$('#cart_contents').html(html);
						}
					});
				}
			}
		});
		return false;
	});

	$('.jNice').hover(
		function(){
			$(this).toggleClass('gray');
		},
		function(){
			$(this).toggleClass('gray');
		}
	);
	
	$('.remove_from_cart').click(function(){
		id = $(this).attr('rel');
		$('#goods_' + id).removeAttr('disabled');
		$('#netshop_cart_contents').submit();
		return false;
	});

// заказ товара
// выбор метода оплаты
	$('.payment_method').change(function(){
		if ($(this).val() == 6) {
			$('#buy').hide();
			$('#order').show();
			$('#delivery_1').click();

			// обнуляем стоимость доставки
//			$('#submit_type').val(0);
//			$('#naked').val(0);
			$.ajax({
				url: "/netcat/modules/ajax/get_delivery_price.php?type=1",
				cache: false,
				success: function(html){
					$('#small_cart').html(html);
				}
			});

			$('#delivery_2').attr('disabled','disabled');
			$('#delivery_3').attr('disabled','disabled');
			$('#delivery_4').attr('disabled','disabled');
		} else {
			$('#order').hide();
			$('#buy').show();
			$('#delivery_2').removeAttr('disabled');
			$('#delivery_3').removeAttr('disabled');
			$('#delivery_4').removeAttr('disabled');
		}
	});

// выбор метода доставки
	$('.delivery_method').change(function(){

		$('.select_city').hide();
		id = $(this).val();

		if (id != 1) {
			$('#payment_6').attr('disabled','disabled');
			$('#payment_6').removeAttr('checked');
			$('#delivery_block_' + id).show();
		} else {
			$('#payment_6').removeAttr('disabled');
			//$('#payment_6').click();

//			$('#submit_type').val(0);
//			$('#naked').val(0);

			$('#delivery_type').val(1);
			$('#delivery_id').val(0);

			$.ajax({
				url: "/netcat/modules/ajax/get_delivery_price.php?type=1",
				cache: false,
				success: function(html){
					$('#small_cart').html(html);
				}
			});
		}
	});

	$('.select_city select').change(function(){
		var self = $(this);
		var city = $('#city');
		var city_id = self.val();
		var country = $('#country');
		var country_id = country.val();
		self.attr('disabled','disabled');
		city.attr('disabled','disabled');
		country.attr('disabled','disabled');
		$('#delivery_type').val(self.parent().prev().prev().prev().val());
		$('#delivery_id').val(self.val());
		$.ajax({
			url: "/netcat/modules/ajax/get_delivery_price.php?type=" + self.parent().prev().prev().prev().val() + "&id=" + city_id,
			cache: false,
			success: function(html){
				if (country_id == 1) {
					city.val(city_id).removeAttr('disabled');
					if (id == 2 && city_id == 1) {
						$('#delivery_block_2').hide();
						$('#payment_6').removeAttr('disabled');
						$('#delivery_1').removeAttr('disabled');
						$('#delivery_1').attr('checked','checked');
					} else {
						$('#payment_6').attr('disabled','disabled');
						$('#delivery_1').attr('disabled','disabled');
//						$('#delivery_2').click();
//						$('#delivery_block_2').show();
					}
				} else {
					country.val(city_id).removeAttr('disabled');
				}
				self.removeAttr('disabled');
				country.removeAttr('disabled');
				$('#small_cart').html(html);
			}
		});
	});


	$('#country').change(function(){
		//загружаем города
		$('#city').attr('disabled','disabled');
		var selected_val = $(this).val();
		// Россия
		if (selected_val == 1) {
			$('#delivery_2').click();
			$('#delivery_block_2').show();
			$('#delivery_block_3').hide();
			$('#delivery_block_4').hide();
		}
		// Страны СНГ
		if (selected_val > 1 && selected_val <= 10) {
			$('#delivery_1').attr('disabled','disabled');
			$('#payment_6').attr('disabled','disabled');
			$('#delivery_3').click();
			$('#delivery_block_2').hide();
			$('#delivery_block_3').show();
			$('#delivery_block_4').hide();
			
			current_select = $('#delivery_block_3 select');
			current_select.val(selected_val).attr('disabled','disabled');

			$('#delivery_type').val(current_select.parent().prev().prev().prev().val());
			$('#delivery_id').val(current_select.val());
			$.ajax({
				url: "/netcat/modules/ajax/get_delivery_price.php?type=" + current_select.parent().prev().prev().prev().val() + "&id=" + current_select.val(),
				cache: false,
				success: function(html){
					current_select.removeAttr('disabled');
					$('#small_cart').html(html);
				}
			});

		}
		// Страны мира
		if (selected_val > 10) {
			$('#delivery_1').attr('disabled','disabled');
			$('#payment_6').attr('disabled','disabled');
			$('#delivery_4').click();
			$('#delivery_block_2').hide();
			$('#delivery_block_3').hide();
			$('#delivery_block_4').show();

			current_select = $('#delivery_block_4 select');
			current_select.val(selected_val).attr('disabled','disabled');

			$('#delivery_type').val(current_select.parent().prev().prev().prev().val());
			$('#delivery_id').val(current_select.val());
			$.ajax({
				url: "/netcat/modules/ajax/get_delivery_price.php?type=" + current_select.parent().prev().prev().prev().val() + "&id=" + current_select.val(),
				cache: false,
				success: function(html){
					current_select.removeAttr('disabled');
					$('#small_cart').html(html);
				}
			});
		}
		$.ajax({
			url: "/netcat/modules/ajax/get_cities.php?id=" + $('#country').val(),
			cache: false,
			success: function(html){
				if (html) {
					$('#city').html(html).change(function(){
//						$('#delivery_block_2 select').val($(this).val());
						if ($(this).val() != 1) {
							$('#payment_6').attr('disabled','disabled');
							$('#delivery_1').attr('disabled','disabled');
						} else {
							$('#payment_6').removeAttr('disabled');
							$('#delivery_1').removeAttr('disabled');
						}

						current_select = $('#delivery_block_2 select');
						current_select.val($(this).val()).attr('disabled','disabled');
			
						$('#delivery_type').val(current_select.parent().prev().prev().prev().val());
						$('#delivery_id').val(current_select.val());
						$.ajax({
							url: "/netcat/modules/ajax/get_delivery_price.php?type=" + current_select.parent().prev().prev().prev().val() + "&id=" + current_select.val(),
							cache: false,
							success: function(html){
								current_select.removeAttr('disabled');
								$('#small_cart').html(html);
							}
						});
					});
					$('#city').removeAttr('disabled');
					$('#show_cities').show();
				} else {
					$('#show_cities').hide();
				}
			}
		});
	});

	$('#city').change(function(){
		if ($(this).val() != 1) {
			$('#payment_6').attr('disabled','disabled');
			$('#delivery_1').attr('disabled','disabled');
			$('#delivery_2').click();
			$('#delivery_block_2').show();
		} else {
			$('#delivery_block_2').hide();
			$('#payment_6').removeAttr('disabled');
			$('#delivery_1').removeAttr('disabled');
			$('#delivery_1').attr('checked','checked');
		}

		current_select = $('#delivery_block_2 select');
		current_select.val($(this).val()).attr('disabled','disabled');
	
		$('#delivery_type').val(current_select.parent().prev().prev().prev().val());
		$('#delivery_id').val(current_select.val());
		$.ajax({
			url: "/netcat/modules/ajax/get_delivery_price.php?type=" + current_select.parent().prev().prev().prev().val() + "&id=" + current_select.val(),
			cache: false,
			success: function(html){
				current_select.removeAttr('disabled');
				$('#small_cart').html(html);
			}
		});
	});

	$('#order').hide();
	$('#buy').hide();
	$('#buy').click(function(){
		if ($('#payment_7').attr('checked')) {
			$('#submit_type').val(1);
			$('#naked').val(1);
			
			type = $('#delivery_type').val();
			id = $('#delivery_id').val();
			
			$.ajax({
				url: "/netcat/modules/ajax/get_delivery_price.php?type=" + type + "&id=" + id + "&submit_type=" + $('#submit_type').val(),
				cache: false,
				success: function(html){
					$('#total_price').val(html);
					$('#order_form').ajaxSubmit({
						// $.ajax options
						complete: function(msg) {
							$('#order_id').val(msg.responseText);
							$.ajax({
								url: "/netcat/modules/liqpay/cnb12.php?order_id=" + $('#order_id').val() + "&total_price=" + $('#total_price').val(),
								cache: false,
								success: function(html){
									$('#order_form').after(html);
									$('#liqpay_form').submit();
								}
							});
						}
					});
				}
			});
		} else {
			$('#submit_type').val(0);
			$('#naked').val(0);
		}

		return false;
	});
// заказ товара

	$("ul.tabs").tabs("div.panes > div", {
		current: 'selected'
	});
	$(".recommend div, .viewed div").hover(function(){
		$(this).css('border', 'solid 1px #CCCCCC');
	}, function(){
		$(this).css('border', 'solid 1px #FFFFFF');
	});
	
	$('.send_feedback').click(function(){
		$(this).parent().hide();
		$('.feedback_item').hide();
		$('#feedback_form').show();
		return false;
	});

	$(".photos").click(function(){
		path = $(this).attr('href');
		$('#image_big').animate({display: 'hide'},0,function(){
			$(this).attr("src", path).bind('load',function(){
				$(this).animate({display: 'show'},0);
			});
		});
		$(".middle img").attr("src", path);
		return false;
	});
	
/*
	height = $('.comparation').height();
	$(".recommend").css('top', height+112);
	$('.compare').click(function(){
		self = $(this);
		type = self.attr('type');
		$.ajax({
			url: "/netcat/modules/ajax/add_to_comparation.php?id=" + self.attr('rel') + "&type=" + type,
			cache: false,
			success: function(html){

				if (type == 'list') {
					$('.comparation_line').html(html).show();
					$('.delete').click(function(){
						that = $(this);
						$.ajax({
							url: "/netcat/modules/ajax/delete_from_comparation.php?id=" + that.attr('rel'),
							cache: false,
							success: function(html){
								that.parent().remove();
								$(".compare").show();
							}
						});
					});
				} else {
					$('.comparation').html(html).show();
					height = $('.comparation').height();
					$(".recommend").css('top', height+112);
					$('.delete').click(function(){
						that = $(this);
						$.ajax({
							url: "/netcat/modules/ajax/delete_from_comparation.php?id=" + that.attr('rel'),
							cache: false,
							success: function(html){
								that.parent().remove();
								height = $('.comparation').height();
								$(".recommend").css('top', height+112);
								$(".compare").show();
							}
						});
					});
				}
				self.hide();
			}
		});
		return false;
	});

	$('.comparation_line .compare').unbind('click');

	$('.delete').click(function(){
		that = $(this);
		$.ajax({
			url: "/netcat/modules/ajax/delete_from_comparation.php?id=" + that.attr('rel'),
			cache: false,
			success: function(html){
				if (that.hasClass('reload')) {
					document.location.href='/watches/compare/';
				} else {
					that.parent().remove();
					height = $('.comparation').height();
					$(".recommend").css('top', height+112);
				}
			}
		});
	});
*/
	height = $('.comparation').height();
	$(".recommend").css('top', height+110);
	$('.compare').click(function(){
		self = $(this);
		type = self.attr('type');
		$.ajax({
			url: "/netcat/modules/ajax/add_to_comparation.php?id=" + self.attr('rel') + "&type=" + type,
			cache: false,
			success: function(html){

				if (type == 'list') {
					$('.comparation_line').html(html).show();
					$('.delete').click(function(){
						that = $(this);
						$.ajax({
							url: "/netcat/modules/ajax/delete_from_comparation.php?id=" + that.attr('rel'),
							cache: false,
							success: function(html){
								that.parent().remove();
								$(".compare").show();
							}
						});
					});
				} else {
//					$('#discount_banner').html(html).show();
					height = $('.comparation').html(html).height();
					$(".recommend").css('top', height+110);
					$('.delete').click(function(){
						that = $(this);
						$.ajax({
							url: "/netcat/modules/ajax/delete_from_comparation.php?id=" + that.attr('rel'),
							cache: false,
							success: function(html){
								that.parent().next().remove();
								that.parent().remove();
								height = $('.comparation').height();
								$(".recommend").css('top', height+110);
								$(".compare").show();
							}
						});
					});
				}
				self.hide();
			}
		});
		return false;
	});

	$('.comparation_line .compare').unbind('click');

	$('.delete').click(function(){
		that = $(this);
		$.ajax({
			url: "/netcat/modules/ajax/delete_from_comparation.php?id=" + that.attr('rel'),
			cache: false,
			success: function(html){
				if (that.hasClass('reload')) {
					document.location.href='/watches/compare/';
				} else {
					that.parent().remove();
					height = $('.comparation').height();
					$(".recommend").css('top', height+110);
				}
			}
		});
	});
	
	$('#discount_banner img').hover(function(){
		$(this).attr('src', '/netcat_files/Image/discount_active.png');
	},function(){
		$(this).attr('src', '/netcat_files/Image/discount_unactive.png');
	}).mouseover(function(){
		$(this).attr('src', '/netcat_files/Image/discount_active.png');
	}).mouseout(function(){
		$(this).attr('src', '/netcat_files/Image/discount_unactive.png');
	});
	
	$('#clear_functions').click(function(){
		$('#watches_type_form input:checkbox').each(function(){
			$(this).removeAttr('checked');
		});
		$('#watches_type_form').submit();
		return false;
	});

// - 373 - 15 - 16

	window_height = $(window).height();
	container_height = $("#conteiner").height();
	if (container_height < window_height) $(".menu").next().height(window_height - 373 - 15 - 16);
//	$(".cont_text").height(height);
//	$(".cont_news").height(height);
//	alert(window_height + " " + container_height);

	$(".close_cart, .left_button").click(function(){
		$(".added_message").hide();
		return false;
	});

	$("#close_question, #close_bubble").click(function(){
		$(this).parent().remove();
		return false;
	});
	
	$("#question").hover(
		function(){
			$(this).hide();
			$('#bubble').show();
		},
		function(){
			return false;
//			$('#bubble').hide();
//			$(this).show();
		}
	);

	$("#bubble").hover(
		function(){
			return false;
//			$('#bubble').hide();
//			$(this).show();
		},
		function(){
			$(this).hide();
			$('#question').show();
		}
	);

	$(".hov p").css('opacity', 0.8);
	
	$("#first_img").load(function(){
		first_img_height = $(this).height();
		$(".first_news").css('min-height', first_img_height + 10);
	});
});
