(function($) {
	$.fn.fparam=function() {
		return this.each(function() {
         var $obj=$(this);
         var $links=$obj.find("> a");
         var forms=$obj.find("form"),h=Math.max($(forms[0]).innerHeight(),$(forms[1]).innerHeight());
			$obj.height(h+70);
			//$obj.find("form:last").css({left:-271});
         $obj.find("form").each(function() {
            $(this).find('#article,#article').bind('focus',function() {
               if ($(this).val()=='не важно') { $(this).val(''); }
            }).bind('blur',function() {
               if (/^\s*$/.test($(this).val())) { $(this).val('не важно'); }
            }).end().bind('submit',function() {
               var art=$(this).find('#article,#article1');
               if (art.val()=='не важно') { art.val(''); }
            });
         });
			$links.bind("click",function() {
				var n=$links.index($(this));
				if (!$(this).hasClass("sel")) {
					$(this).addClass("sel"); (n>0)?$($links[0]).removeClass("sel"):$($links[1]).removeClass("sel");
				}
				if (n>0) {
					$obj.find("form:first").stop().animate({left:271},100,function() {
						$obj.find("form:last").stop().animate({left:30},100);
					});
				} else {
					$obj.find("form:last").stop().animate({left:-271},100,function() {
						$obj.find("form:first").stop().animate({left:30},100);
					});
				}
			});
		});
	};

	$.fn.porular=function() {
		var $obj=$(this); var $block=$obj.find(".cl"); var h=0;
		return this.each(function() {
			for (i=0;i<$block.size();i++) {
				$($block[i]).find(".note").each(function() { h=h<$(this).height()?$(this).height():h; });
				$($block[i]).find(".note").height(h);
			}
		});
	};

	$.fn.dealer=function() {
		var $obj=$(this); var $links=$obj.find("a");
		return this.each(function() {
			$links.bind("click",function() {
				if ($("#modal").css("display")=="none"&&!$(this).hasClass("exit")) {
					$("#modal").css({display:"block",height:($(document).height()),opacity:0}).animate({opacity:.63});
					l=($(window).width()-621)/2; t=($(window).height()-435)/2; t=t<0?0:t;
					$("#dealer-form").css({display:"block",left:($(document).width()-621)/2}).animate({top:t},function() {
						$(this).find(".close-form > a").bind("click",function() {
							$("#modal").animate({opacity:0},function() { $(this).css({display:"none"}); });
							$("#dealer-form").animate({top:-435},function() { $(this).css({display:"none"}); })
						});
					});
				}
			});
		});
	};

	$.fn.addGoods=function() {
		var $obj=$(this); var $modal=$("#modal"); var $form=$("#order-form");
		return this.each(function() {
			$obj.bind("click",function() {
				if ($modal.css("display")=="none") {
					$("#modal").css({display:"block",height:($(document).height()),opacity:0}).animate({opacity:.63});
					l=($(window).width()-$form.width())/2; t=($(window).height()-$form.height())/2; t=(t<0)?0:t;
					t=t+$(document).scrollTop();
					if (t+$form.height()>$(document).height()) { t=t-(t+$form.height()-$(document).height()); }
					$form.css({display:"block",left:l,top:-$form.height()}).animate({top:t},function() {
						$(this).find(".close-form > a").bind("click",function() {
							$("#modal").animate({opacity:0},function() { $(this).css({display:"none"}); });
							$form.animate({top:-($form.height()+$(document).scrollTop())},function() { $(this).css({display:"none"}); })
						});
					});
				}
			});
		});
	};

   function preventSelection(document) {
      var preventSelection = false;

      function removeSelection(){
         if (window.getSelection)
            window.getSelection().removeAllRanges();
         else if (document.selection && document.selection.clear)
            document.selection.clear();
      }

      function killCtrlA(event){
         var event = event || window.event;
         var sender = event.target || event.srcdocument;
         if (sender.tagName&&sender.tagName.match(/INPUT|TEXTAREA/i))
            return;
         var key = event.keyCode || event.which;
         // 'A'.charCodeAt(0) можно заменить на 65
         if (event.ctrlKey && key == 'A'.charCodeAt(0)) {
            removeSelection();
            if (event.preventDefault)
               event.preventDefault();
            else
               event.returnValue = false;
         }
      }

      // не даем выделять текст мышкой
      $(document).bind( 'mousemove', function(e) {
         if(preventSelection) removeSelection();
      });
      
      $(document).bind( 'mousedown', function( e ){
         var sender = e.target || e.srcdocument;
         preventSelection = sender&&sender.tagName&&!sender.tagName.match(/INPUT|TEXTAREA/i);
         
         if( e.button == 2 && preventSelection ) {
            alert('Использование правой кнопки мыши заблокировано!')
            e.preventDefault();
            e.stopPropagation();
         }
         
      })
      
      // борем dblclick
      // если вешать функцию не на событие dblclick, можно избежать
      // временное выделение текста в некоторых браузерах
      $(document).bind( 'mouseup', function(e){
         if (preventSelection) removeSelection();
         if( e.button == 2 && preventSelection ) {
            e.preventDefault();
            e.stopPropagation();
         }
         
         preventSelection = false;
      });

      // борем ctrl+A
      // скорей всего это и не надо, к тому же есть подозрение
      // что в случае все же такой необходимости функцию нужно
      // вешать один раз и на document, а не на элемент
      $(document).keydown( killCtrlA );
      $(document).keyup( killCtrlA );
   }
   
   $(function() {
		$("#select-param").fparam();
		$("#popular-goods").porular();
		$(".enter").dealer();
		$(".catalog-tab .add a").addGoods();
      $('#catalog-pic a').length&&$('#catalog-pic a').lightBox();

      //preventSelection(document);
     
	});

})(jQuery);
