$(document).ready(function(){

  $(".bookmarks td").click(function(){
    if ($(this).hasClass('pushed')) {
      return false;
    } else {
      $(".bookmarks td").removeClass('pushed');
      $(this).addClass('pushed');
      $(".product").hide();
      $($(".product")[$(".bookmarks td").index($(this))]).show();
    }
  });
	/*both navigations*/
	if ($('#item_image').height() > $('.largeim').height()) $('.largeim').height($('#item_image').height());

	$(".nav-horizontal p").hover(function() {

		if (!$(this).parent().hasClass('selected')) {
			$(this).parent().addClass('hover');
		}

	}, function() {
		if (!$(this).parent().hasClass('selected')) {
			$(this).parent().removeClass('hover');
		}
	});


	$(".vert-navigation li:not(.droplist)").hover(function() {

		if (!$(this).hasClass('selected')) {
			$(this).addClass('hover');
		}

	}, function() {
		if (!$(this).hasClass('selected')) {
			$(this).removeClass('hover');
		}
	});


	$(".nav-horizontal p, .vert-navigation li:not(.droplist)").click(function(){
		$('a', this).each(function() {
			window.location = $(this).attr('href');
		})
		return false;
	});

	$(".pic-area").hover(function() {
		var path = $(this).find('.intro');
		$(this).addClass('back');
		path.animate({opacity: 0}, 0);
		path.stop().show().animate({opacity: 1}, 100);

	}, function() {
		var path = $(this).find('.intro');
		$(this).removeClass('back');
		path.stop().hide().animate({opacity: 0}, 100 ,function() {
			path.hide();
		});
	});

	$(".search")

	.blur(function() {
		if ($(this).val() == '') { $(this).val('Search'); }
	})
	.focus(function() {
		if ($(this).val() == 'Search') { $(this).val(''); }
	})


	
	/*image gallary*/


	$(".prevs td:first").addClass('selected');
	$(".prevs img").each(function() {
		var image = $(this);
		var layer = $(this).parent();
		image.click(
		function(){
			if (layer.hasClass('selected')) {
				return false;
			} else {
				var i = $('#item_image1').css('display') == 'none' ?  $('#item_image') : $('#item_image1');
				var j = $('#item_image').css('display') == 'none' ?  $('#item_image') : $('#item_image1');
				i.fadeOut(200);
				$('.prevs td').removeClass('selected');
				layer.addClass('selected');
				setTimeout(function() {
					j.attr('src', image.attr('_src'))
					.attr('_src2', image.attr('_src2'))
					.attr('_w', image.attr('_w'))
					.attr('_h', image.attr('_h'))
					.fadeIn(100);
				}, 210);
				setTimeout(function() {
					$('.largeim').height(j.height());
				}, 220);
			}

		}
		);
	});

	$('.largeim').height($('#item_image').height());

	$('.largeim img').click(function(){
		if(!$(this).attr('_src2')) return false;
		var j = $('#item_image').css('display') == 'none' ?  $('#item_image1') : $('#item_image');
		$('<img class="bimg" src="'+j.attr('_src2')+'" width="'+j.attr('_w')+'" height="'+j.attr('_h')+'"/>').insertAfter('.main').ready(function () {
			$('.bimg').modal({closeClass: "bimg", opacity: 40, maxHeight: $('.bimg').height(), maxWidth: $('.bimg').width(), onClose: function () {$.modal.impl.close();$(".bimg").remove();}});
			$('#simplemodal-overlay').width($(window).width()).bgiframe();
		});
	});


	$('.prw2').click(function(){
		$('<img class="bimg" src="' + $(this).attr('_src2') + '"/>').insertAfter('.main').ready(function () {
			$('.bimg').modal({closeClass: "bimg", opacity: 40, maxHeight: $('.bimg').height(), maxWidth: $('.bimg').width(), onClose: function () {$.modal.impl.close();$(".bimg").remove();}});
			$('#simplemodal-overlay').width($(window).width()).bgiframe();
		});
	});

	/*dropdown on catalog-inner.html*/


	$(".dropdown-link span").toggle(
	function () {
		$('#'+$(this).attr('_id')).slideToggle(100);
		$(this).parent().addClass("marg")
		$(this).addClass("arrow-top");
		$(this).addClass("under");
		$(this).removeClass("unselected");
		$(this).find('b').html('Скрыть');
		scrollTo($('#'+$(this).attr('_id')));
	},
	function () {
		$('#'+$(this).attr('_id')).slideToggle(100 ,function() {
			$(".dropdown-link").removeClass("marg");
		});
		$(this).removeClass("arrow-top");
		$(this).removeClass("under");
		$(this).addClass("unselected");
		$(this).find('b').html('Посмотреть');
	}
	);


	/*solutions, inner-link span, order*/

	$(".border, .inner-link span, .order").click(
	function(){

		window.location = $('a', this).attr('href');
		return false;

	}
	);


	$('select[name=catalogue_id], select[name=brand_id]').change(function() {
		if ($('select[name=catalogue_id]').val() == 0 && $(this).attr('name') == 'catalogue_id') {
			$('select[name=brand_id] option[value!=0]').remove();
			$('select[name=item_id] option[value!=0]').remove();
		}
		else
		if ($('select[name=brand_id]').val() == 0 && $(this).attr('name') == 'brand_id') {
			$('select[name=item_id] option[value!=0]').remove();
		}else
		$.post($(this).val(), $(this).serializeArray(), function(data) {
			if (data.brands) {
				$('select[name=brand_id] option[value!=0]').remove();
				$.map(data.brands, function(item){
					$('select[name=brand_id]').append(
					$('<option />').text(item.name).attr('value', item.url)
					);
				});
			}
			$('select[name=item_id] option[value!=0]').remove();
			if (data.items) {
				$.map(data.items, function(item){
					$('select[name=item_id]').append(
					$('<option />').text(item.name).attr('value', item.url)
					);
				});
			}
		}, 'json');
		return false;
	});
	$('#filter input[type=submit]').click(function() {
		$('#filter select').each(function() {
			if ($(this).val() != 0)
			window.location = $(this).val();
		});
		return false;
	});

	$(".pages .marker div").hover(function() {
		if ($(this).hasClass('active')) {

			return false

		} else {
			$(this).addClass('hover');
		}

	}, function() {
		$(this).removeClass('hover');
	});


	$(".pages .arrow div, .pages .marker div").click(
	function(){

		if ($(this).hasClass('active')) {

			return false

		} else {
			window.location = $('a', this).attr('href');
			return false;
		}

	}
	);
});

function scrollTo(selector) {
	var targetOffset = $(selector).offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 500);
}
