jQuery(function($) {

   
	// hor. menu
	$.fn.hm=function() {
		var $obj=$(this); var $links=$obj.find("a"); var w=0;
		return this.each(function() {
			$links.each(function() { w+=$(this).innerWidth()+6; });
			$(this).animate({width:w+((ie6)?16:14)},500,function() { $links.css({display:"block"}); });
			$(this).find("a:not(.activ)").bind("mouseenter",function() { $(this).addClass("activ"); });
			$(this).find("a:not(.activ)").bind("mouseleave",function() { $(this).removeClass("activ"); });
		});
	};
	// corner pic (class .corn)
	$.fn.corn=function() {
		return this.each(function() {
			var fl=$(this).css("float"); $(this).css({float:"none"});
			var $wrapper=$(this).wrap('<div class="img-corn" style="float:'+fl+';"></div>').parent();
			$wrapper.append('<div class="ctl"></div><div class="ctr"></div><div class="cbl"></div><div class="cbr"></div>');
			pad={
				t:$(this).css("padding-top").replace("px",""),
				r:$(this).css("padding-right").replace("px",""),
				b:$(this).css("padding-bottom").replace("px",""),
				l:$(this).css("padding-left").replace("px","")
			};
			mar={
				t:$(this).css("margin-top").replace("px",""),
				r:$(this).css("margin-right").replace("px",""),
				b:$(this).css("margin-bottom").replace("px",""),
				l:$(this).css("margin-left").replace("px","")
			};
			$img=$(this);
			$wrapper.width($(this).width());
			$wrapper.find("div").each(function() {
				switch ($(this).attr("class")) {
					case "ctl": $(this).css({left:(pad.l+mar.l)+"px",top:(pad.t+mar.t)+"px"}); break;
					case "ctr": $(this).css({left:$img.width()-pad.l-mar.l-5,top:pad.t+mar.t}); break;
					case "cbl": $(this).css({left:pad.l+mar.l,top:$img.height()-pad.t-mar.t-5}); break;
					case "cbr": $(this).css({left:$img.width()-pad.l-mar.l-5,top:$img.height()-pad.t-mar.t-5}); break;
				}
			});
		});
	};
	// butterfly
	$.fn.but=function() {
		return this.each(function() {
			var w=$(document).width(); var p=(w-1000)/2;
			if (w>1520) $(this).css({backgroundPosition:"100% 0px"});
			else $(this).css({backgroundPosition:(663+p)+"px 0px"});
		});
	};
	// vmenu
	$.fn.vm=function() {
		var $obj=$(this).find("li:not(.activ) > a");
		return this.each(function() {
			$obj.bind("mouseenter",function() { $(this).parent().addClass("activ"); });
			$obj.bind("mouseleave",function() { $(this).parent().removeClass("activ"); });
		});
	};

	$(function() {
		$("#hmenu").hm();
		$(".corn").corn();
		Cufon.replace("h1,h2,h3,h4");
		Cufon.replace(".phone-email,a.tit,#telefon-top",{hover:true});
		$("#butterfly").but();
		$("#logocontrol, #map_canvas > div:last").remove();
		$("#vmenu").vm();
	});
	$(window).resize(function() { $("#butterfly").but(); });
   
   
   // попап
   $('.zap').click(function(){
      $('.popup').fadeIn();
   });
   $('.close').click(function(){
      $('.popup').fadeOut();
   });

   // проверка формы
   $('#form1').submit(function(){
      var result = true;
      var req = 0;
      $(this).find('input, textarea').each(function(){
         if( $(this).hasClass('req') && $(this).val() == '' ) {
            $(this).parents('label').addClass('error');
            req++;
            result = false;
         }
      });
      
      var errors = [];
      if( req > 0 ) {
         errors.push( 'Не все обязательные поля были заполнены' );
      }
      
      if( errors.length > 0 ){
         alert( errors.join('\n') );
      }
      return result;
   });
   $('#capchlink').click(function(){
      $('#img').attr('src', $('#img').attr('src') + '&' + Math.random());
   });
   if( $('#form1 .error').length ){
      $('.popup').fadeIn();
   }
   
});

 function d(){if(window.console && window.console.log){window.console.log(arguments)}}
