
	var main = new Object();
	main.busy = false;
	main.img;
	main.img_time=false;
	
	function setCookie (name, value) {
		document.cookie = name + "=" + escape(value) +";path=/";
	}
	function getCookie(name) {
		var cookie = " " + document.cookie;
		var search = " " + name + "=";
		var setStr = null;
		var offset = 0;
		var end = 0;
		if (cookie.length > 0) {
			offset = cookie.indexOf(search);
			if (offset != -1) {
				offset += search.length;
				end = cookie.indexOf(";", offset)
				if (end == -1) {
					end = cookie.length;
				}
				setStr = unescape(cookie.substring(offset, end));
			}
		}
		return(setStr);
	}
	function lexica ($n,$a,$b,$c) {
		$n=parseInt($n);
		$x= Math.abs($n>9? (($n+'').substr(-2)) :$n);
		return (($x%=100)>9&&$x<20||($x%=10)>4||$x==0?$c:($x==1?$a:$b));
	}	
	$(function() {
		
		$("#contacts .item").click(function() { // open
			if(!this._width){
				this._width = $(this).width();
				this._height = $(this).height();
				this._time = false;
			}
			if(this._time){clearTimeout(this._time); this._time=false;}
			if(this._open){
				main.out([this]);
			}else{
				this._open = true;
				$(this).stop();
				if ($(this).is(".nsk"))
					$(this).animate({width: 200, height: 100}, 'slow');
				else
					$(this).animate({width: 580, height: 585}, 'slow');
			}
			return false;
		});
		
		$("#contacts .item").mouseout(function() { 
			//this._time = setTimeout(main.out, 1000, [this]);
		});
		main.out = function() {
			if(!arguments[0][0]._open) return false;
			arguments[0][0]._open = false;
			$(arguments[0][0]).stop();
			$(arguments[0][0]).animate({width: '125px', height: '30px'}, 'slow');
		}
		
		/* Catalog */
		main.cart = getCookie('cart') ? unescape((getCookie('cart'))).split(',') : new Array();
		
		main.cartCheck= function(){
			var i = main.cart.length;
			if(i == 0){
				$('#cart span:eq(0)').text('Корзина пуста');
				$('#cart a').css('display','none');
				
				$('#cart .img').css('background-position', '0 -70px');
			}else{
				$('#cart a').css('display','');
				$('#cart span:eq(0)').html('В корзине <span class="pink">'+i+'</span> '+lexica(i, 'товар','товара','товаров'));
				$('#cart .img').css('background-position', '0 0');
			}
		};
		
		var catalog = $('.catalog .add');
		catalog.click(function() {
			var id = $(this).parent().find('.id').text();
			main.cart.push(id);
			setCookie('cart', main.cart.join(','));
			
			$(this).parent().append('<a class="added" href="/order">товар в корзине</a>');
			$(this).css('display','none');
			main.cartCheck();
			return false;
		});
		for(var i=0, length=catalog.length ; i<length ; i++){
			var id = $(catalog[i]).parent().find('.id').text();
			for(var j=0, jl=main.cart.length ; j<jl ; j++ ){
				if (main.cart[j]== id){
					$(catalog[i]).parent().append('<a class="added" href="/order">товар в корзине</a>');
					id = 0;
					break;
				}
			}
			if(id>0)$(catalog[i]).css('display','inline-block');
		}
		
		var catalog = $('.catalog .delete');
		catalog.click(function() {
			var id = $(this).parent().find('.id').text();
			for(var j=0, jl=main.cart.length ; j<jl ; j++ ){
				if (main.cart[j] == id){
					main.cart.splice(j,1);
					break;
				}
			}
			setCookie('cart', main.cart.join(','));
			$(this).parent().remove();
			main.cartCheck();
			return false;
		});
		$('.catalog input').keyup(function() {
			this.value = this.value.match(/[0-9]*/);
			$(this).parent().parent().find('.sum').text(parseInt($(this).parent().parent().find('.price').text())*this.value);
		});
		main.cartCheck();
		
		/* Show image */
		$('#img .shadow').css('opacity',0);
		$('#img .shadow').css('height', document.getElementsByTagName('html')[0].scrollHeight+'px');
		
		$('.catalog img.photo').click(function() { // open
			if(main.busy) return 1; 
			main.busy = true;
			var img = this.src.replace(/small/,'big');
			main.img = new Image();
			main.img.src = img;
			$('#img .center').css('display','block');
			$('#img .center div').css('background-image', 'url('+img+')'); 
			$('#img').css('display','block');
			$('#img .shadow').animate({opacity: .9}, 'slow',main.tb_open);
		});
		
		$('#img').click(function(){ // close
			if(!main.busy) return 1;
			if(main.img_time) clearTimeout(main.img_time);
			$('#img').stop();
			$('#img .center').animate({height: '0px', width: '0px'}, 'slow',function(){
				$('#img .center').css('display','none');
				$('#img .shadow').animate({opacity: 0}, 'slow', function(){main.busy=false;$('#img').css('display','none');$('#img img').css('display','');$('#img .r2 .c2 div').css({width: 'auto',height:'auto'})});	
			});
		});
		
		main.tb_open = function() {
			if(main.img.complete){
				$('#img .center').animate({width:main.img.width, height:main.img.height}, 'slow');
				$('#img img').css('display','none');
			}else{
				main.img_time = setTimeout(main.tb_open, 100);
			}
		}
	});