$(document).ready(function(){
	$('ul.card dt').click(function(){
		window.location = $('ul.card dt>a').attr('href');
	});

	if ($.browser.msie){
		$('ul.menu li, ul.row li, .new ul li, .popular ul li').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		});
	}

	var pricehint;

	if ($('.bg1').length){
		var active = parseInt(Math.random()*5)+1;
		$('.bg1').css({'opacity': 0});
		$('.bg'+active).css({'opacity': 1});
		$('div.description div.dv div').html($('.bg'+active).html());		
		
		$('div.description a').click(function(){
			var rn = parseInt(Math.random()*5)+1;
			while (rn==active){
				rn = parseInt(Math.random()*5)+1;
			}
			$('.bg'+active).animate({'opacity': 0},1000);
			active = rn;
			$('.bg'+rn).animate({'opacity': 1},1000);
			$('div.description div.dv div').html($('.bg'+rn).html());
			return false;
		});
	}

	$('span.notinstock').live('click',function(){		var crdb = $(document.body).height() - $(this).offset().top;
		var crdl = $(this).offset().left;
		if (pricehint){
			pricehint.remove();
		}
		pricehint = $('<div class="price-hint">Поступление товара ожидается в ближайшее время<span class="str-b"></span></div>')
		.appendTo($(document.body)).css({
			'bottom': crdb,
			'left': crdl
		});
		$(document.body).one('click',function(){
			pricehint.remove();
		});
		return false;	});

	$('.color a').click(function(){
		$('ul.product-gallery li').removeClass('active');
		var preview = $(this).parent().parent().find('.preview');
		var simg = preview.find('img:eq(0)');
		var hhref = this.href;
		var hrel = this.rel;
		var adc = preview.find('a.add-to-card');
		if (adc.length){
			adc.get(0).href="/cart/?add="+hrel;
			preview.find('a.view').get(0).href=this.title;
			var clk = preview.find('a:eq(0)');
			clk.unbind('click');
			clk.click(function(){
				document.location = this.href+'#'+hrel;
				return false;
			});
		}else{
			if ($('a.a-to-card').length){
				$('a.a-to-card').get(0).href="/cart/?add="+this.rel;
			}
			if (pricehint) pricehint.remove();
			if ($('span.notinstock').length) $('span.notinstock').remove();
			if ($(this).hasClass('notinstock')){				var hint = $('<span class="notinstock">Товар ожидается</span>').insertAfter('div.cnt h2.prz');			}
		}
		preview.addClass('loading');
		simg.animate({opacity: 0},100,function(){
			simg.attr('src',' ');
			simg.one('load',function(){
				$(this).animate({opacity: 1},100);
				preview.removeClass('loading');
			});

			simg.attr('src',hhref);
		});
		return false;
	});

	$('ul.product-gallery a').click(function(){
		$('ul.product-gallery li').removeClass('active');
		$(this).parent().addClass('active');
		var preview = $(this).parent().parent().parent();
		var simg = preview.find('img:eq(0)');
		var hhref = this.href;
		preview.addClass('loading');
		simg.animate({opacity: 0},100,function(){
			simg.attr('src',hhref);
			simg.one('load',function(){
				$(this).animate({opacity: 1},100);
				preview.removeClass('loading');
			});
		});
		return false;
	});
	var pimg;
	$('div.preview a.view').click(function(){
		var aimg = $(this).parent().find('img:eq(0)');
		var apos = aimg.offset();
		var ahref= this.href;
		if (pimg){
			pimg.remove();
		}
		pimg = $('<img src="'+ahref+'" class="view-img" />').appendTo('body').css({
			'left': -1000,
			'top': -1000
		}).load(function(){
			$(this).css({
				'width': 160,
				'height':120,
				'left': apos.left,
				'top': apos.top
			});
			$(this).animate({
				left: '-=120',
				top: '-=90',
				width: 400,
				height: 300
			},200,function(){
				$(document).one('click',function(){
					pimg.animate({
						left: '+=120',
						top: '+=90',
						width: 160,
						height: 120
					},200,function(){
						pimg.remove();
					});
				});
			});
		});
		return false;
	});
	$('input.search').blur(function(){
		this.value = (this.value=='')? 'поиск': this.value;
	});
	$('input.search').focus(function(){
		this.value = (this.value=='поиск')? '': this.value;
	});
	if ($('div.product div.reviews').length){
		var ind = location.href.indexOf('#');
		if (ind!=-1){
			var l = location.href.substring(ind+1);
		}
		var rshow = 1;
		var rurl = $('div.product a[href$=#reviews]').get(0).href;
		rurl = rurl.substring(0,(rurl.indexOf('#')+1));
		if (l!='reviews'){
			$('div.product div.reviews').hide();
			rshow = 0;
		}
		$('div.product-reviews h2 a').click(function(){
			if (rshow==0){
				$(this).attr('href',rurl+'reviews');
				$('div.product div.reviews').show();
				rshow = 1;
			}else{
				$(this).attr('href',rurl);
				$('div.product div.reviews').hide();
				rshow = 0;
			}
			$('div.footer p.copy').get(0).innerHTML += '';
		});
	}
	//personal manager
	$('div.mns a').click(function(){
		var incnt = $('div.mns').parent().parent().parent();
		$('div.mns').parent().animate({
			height: 220
		},500);
		incnt.animate({
			width: '45%'
		},500,function(){
			$('div.mns').hide();
			$('div.mnh').show();
		});
		$('div.cover').show();
		return false;
	});

	if ($('.factive').length){
		$('div.mns a').click();
	}

	$('a.c-online').click(function(){
		$('body, html').animate({scrollTop:0},500);
		$('div.mns a').click();
		return false;
	});
	$('div.cover, div.mnh a.close').click(function(){
		var incnt = $('div.mns').parent().parent().parent();
		$('div.cover').hide();
		$('div.mns').parent().animate({
			height: 40
		},500);
		incnt.animate({
			width: '19.04%'
		},500,function(){
			$('div.mns').show();
			$('div.mnh').hide();
		});
		return false;
	});
	$('div.auth-form .frame:eq(1)').hide();
	$('#show-auth').click(function(){
		$('.auth-form').toggle();
		return false;
	});

	$('div.auth-form .frame:eq(0) a').click(function(){
		$('div.auth-form .frame:eq(1)').show();
		$('div.auth-form .frame:eq(0)').hide();
		return false;
	});

	$('div.auth-form .frame:eq(1) a').click(function(){
		$('div.auth-form .frame:eq(0)').show();
		$('div.auth-form .frame:eq(1)').hide();
		return false;
	});

	$('div.footer a.sl-show').click(function(){
		$('div.footer div.sl').toggle();
		$(document.body).one('click',function(){
			$('div.footer div.sl').hide();
		});
		return false;
	});

	$('h2.prz sup span').click(function(){
		var crdb = $(document.body).height() - $(this).offset().top;
		var crdl = $(this).offset().left;
		if (pricehint){
			pricehint.remove();
		}
		pricehint = $('<div class="price-hint">наши цены незначительно отличаются от цен производителя<span class="str-b"></span></div>')
		.appendTo($(document.body)).css({
			'bottom': crdb,
			'left': crdl
		});
		$(document.body).one('click',function(){
			pricehint.remove();
		});
		return false;
	});
	if ($(".town").length > 0) {
		$.getJSON("towns.json", function(data) {
			$(".town").autocompleteArray(data.towns,{
				maxItemsToShow:10
			});
		});
	}
});
