(function() {
	$.fn.showPic=function() {
		var $obj=$(this); var $box=$(this).find("#small-pic"); var $img=$box.find("img");
		var shBox=false; // flag show box pic
		var num=-1; // number shop img (click)
		var box=false; // building box
		var shPic=false; // show big pic
		var $boxPic;
		var $bigPic;
		var $imgLoader;
		
		/* ----------------------------------------------- */
		buildBox=function() {
			html='<div id="modal"></div>'+
								'<div id="box-pics">'+
								'<table><tbody><tr><td class="box-tl"></td><td class="box-tc"></td>'+
								'<td class="box-tr"></td></tr>'+
								'<tr><td class="box-cl"></td><td class="box-content">'+
								'<div class="box-content-pd"><div class="cl"><div class="prev"></div>'+
								'<div class="big-pic"><img src="" class="pic-loaded"></div>'+
								'<div class="next"></div><div class="close"></div></div></div></td>'+
								'<td class="box-cr"></td></tr>'+
								'<tr><td class="box-bl"></td><td class="box-bc"></td>'+
								'<td class="box-br"></td></tr></tbody></table>'+
								'</div>';
			$("body").append(html); $boxPic=$("#box-pics"); $boxPic.css({left:($(document).width()-$boxPic.width())/2});
			$bigPic=$boxPic.find(".big-pic");
			$imgLoader=$boxPic.find(".pic-loaded");
			$("#modal").css({display:"block",height:$(document).height()-69,top:69});
			
			$boxPic.find(".close").bind("click",function() {
				$imgLoader.css({display:"none"}); $($img[num]).css({opacity:1}).attr({hover:1,sh:0});
				shBox=shPic=false; $boxPic.css({display:"none"});
				$box.stop().animate({marginTop:-39,opacity:.4},500);
				$("#modal").css({display:"none"});
			});
			$boxPic.find(".prev,.next").attr({hover:1}).bind("mouseover mouseout",function() {
				if ($(this).attr("hover")==1) $(this).css({backgroundPosition:"-66px 50%"}).attr({hover:0});
				else $(this).css({backgroundPosition:"0px 50%"}).attr({hover:1});
			});
			$boxPic.find(".prev").bind("click",function() {
				if (num>0) {
					$($img[num]).css({opacity:1}).attr({hover:1,sh:0}); num--;
					$($img[num]).css({opacity:.4}).attr({hover:0,sh:1});
					imgInit($($img[num]));
				}
			});
			$boxPic.find(".next").bind("click",function() {
				if (num<$img.size()) {
					$($img[num]).css({opacity:1}).attr({hover:1,sh:0}); num++;
					$($img[num]).css({opacity:.4}).attr({hover:0,sh:1});
					imgInit($($img[num]));
				}
			});
			return true;
		};
		
		imgInit=function(img) {
			if (box) $boxPic.css({display:"block"});
			path=$(img).attr("big"); 
         // n=name.lastIndexOf("/"); path=name.substring(0,n+1); name=name.substring(n+1);
			if (shPic) {
				$imgLoader.stop().animate({opacity:0},100,function() { $(this).css({display:"none"}); imgLoad(path); });
			} else { imgLoad(path); }
		};
		
		imgLoad=function(p) {
			$imgLoader.attr({src:p}).one("load",function() {
				picH=$(this).height(); picW=$(this).width();
				$bigPic.animate({height:picH,width:picW},function() {
					$imgLoader.css({display:"block",opacity:0}).animate({opacity:1},100);
				});
				$boxPic.animate({left:($(document).width()-picW-132)/2});
				$boxPic.find(".prev,.next").animate({height:picH-74});
			});
		};

		/* ----------------------------------------------- */
		return this.each(function() {
			$img.wrap('<td></td>'); $box.wrapInner('<table><tbody><tr></tr></tbody></table>').css({marginTop:-39});
			$img.attr({sh:0,hover:1});
			// show box pic
			$obj.bind("mouseenter mouseleave",function() {
				if (!shBox) { $box.stop().animate({marginTop:0,opacity:1},500); shBox=true; }
				else if (!shPic) { $box.stop().animate({marginTop:-39,opacity:.4},500); shBox=false; }
			});
			// hover small img
			$img.bind("mouseover mouseout",function() {
				if ($(this).attr("hover")==1&&$(this).attr("sh")==0) { $(this).css({opacity:.4}).attr({hover:0}); }
				else if ($(this).attr("sh")==0) { $(this).css({opacity:1}).attr({hover:1}); }
			});
			// click small img
			$img.bind("click",function() {
				if ($(this).attr("sh")==0) {
					if (num!=-1&&num!=$($img).index(this)) $($img[num]).css({opacity:1}).attr({hover:1,sh:0});
					num=$($img).index(this); $(this).css({opacity:.4}).attr({hover:0,sh:1});
					if (!box) box=shPic=buildBox(); else shPic=true;
					$("#modal").css({display:"block"}); imgInit(this);
				}
			});
		});
	};
})(jQuery);

var introW = 868;
if(0) (function($) {
   // var introW = $('#tab-scr .items > td').width();
   var animating = false;
	$.fn.intro=function() {
		var $obj=$(this); var $div=$obj.find("div"); var $links=$div.find("> a"); var $activ=$obj.find(".activ");
		var $tscr=$("#tab-scr"); var scrX; var scrXleft; var scrXright;
		boxScroll=function() {
			$("#next").bind("click",function() {
				if( !animating ) {
               scrX=$tscr.position().left;
               if (scrX>scrXright) { 
                  animating = true;
                  scrX-=introW; 
                  $tscr.animate({left:scrX},500,function(){
                     updateControls();
                     animating = false;
                  });
               } else {
                  animating = true;            
                  scrX = scrXleft;
                  $tscr.animate({left:scrX},500,function(){
                     updateControls();
                     animating = false;
                  }); 
               
               }//if
            }//if
			});
			$("#prev").bind("click",function() {
				if( !animating ) {
               scrX=$tscr.position().left;
               if (scrX<scrXleft) {
                  animating = true;            
                  scrX+=introW; 
                  $tscr.animate({left:scrX},500,function(){
                     updateControls();
                     animating = false;
                  }); 
               } else {
                  animating = true;            
                  scrX = scrXright;
                  $tscr.animate({left:scrX},500,function(){
                     updateControls();
                     animating = false;
                  }); 
               
               }//if
            }//if
			});
		};
      
      updateControls = function() {
         var scrXt=$tscr.position().left;
         if (scrXt>=scrXleft) { 
            // $("#prev").hide();
         } else {
            // $("#prev").show();
         }
         if (scrXt<=scrXright) {
            // $("#next").hide();
         } else { 
            // $("#next").show(); 
         }
         
         // console.log( getIntroActiveEl() )
         var dir_id = $('#tab-scr .items > td').eq( getIntroActiveEl() )[0].className.replace('introDir', '');
         $('#intro div').removeClass('activ').eq(dir_id-1).addClass('activ');
         
      }//updateControls
      
      $links.each(function() {
         var i = $links.index(this) + 1;
         var n = $('#tab-scr .items > td').index( $('.introDir'+i) ) ;
         if( n >= 0 ) {
            $(this).click(function() {
               if( !animating ) {
                  if (!$(this).parent().hasClass("activ")) {
                     $activ.removeClass("activ"); 
                     $activ=$(this).parent();
                        $(this).parent().addClass("activ"); 
                        animating = true;
                        $tscr.animate({left:-( introW*n - 70 )},500,function(){
                           updateControls();animating = false;
                        });  // display: block; left: -2534px;
                  }//if
               }//if
               return false;
            });
            
         } else {
            $(this).hide();
         }//if
         
      });
      
		this.each(function() {
			w=$obj.width(); for (i=0;i<$div.size();i++) { w-=$($div[i]).width(); } w=parseInt(w/($div.size()-1));
			for (i=0;i<$div.size()-1;i++) { $($div[i]).css({marginRight:w-5}); }
			scrX=(($(document).width()-introW)/2)-introW; scrW=$tscr.width(); $tscr.css({display:"block",left:scrX});
			scrXleft=scrX+introW; scrXright=scrX-((($tscr.width()/introW)-2)*introW);
			// if (ie6) IEPNGFix.update();
			boxScroll();
		});
      updateControls();
      return this;
	};
})(jQuery);

(function($) {
	$.fn.glink=function() {
		return this.each(function() {
			$(this).append('<div class="glink-tl"></div><div class="glink-tr"></div><div class="glink-bl"></div><div class="glink-br"></div>');
			$(this).wrap('<div class="cl"></div>');
			if (ie6) {
				h=$(this).height(); w=$(this).width();
				$(this).find(".glink-bl,.glink-br").css({top:h+16});
				$(this).find(".glink-tr,.glink-br").css({left:w+26});
			}
		});
	}
})(jQuery);

(function($) {
	$.fn.form=function() {
		var $obj=$(this).find(".inf"); var flag=true; var $note;
		buildNote=function() {
			$("body").append('<div id="form-note"><div id="form-note-pd"></div><div id="form-note-bottom"></div><div id="form-note-arrow"></div></div>');
			return $("#form-note");
		};
		return this.each(function() {
			$obj.each(function() {
				$(this).attr({txt:$obj.attr("title")}).attr({title:""});
			});
			$obj.bind("mouseenter mouseleave",function() {
				if (!$("#form-note").is("div")) $note=buildNote();
				pos=$(this).position(); $note.find("#form-note-pd").html($(this).attr("txt"));
				x=(pos.left+27)-($note.width()/2); y=pos.top-$note.height()-27;
				if ($(document).width()-30-(x+$note.width())<0) {
					pX=$(document).width()-30-(x+$note.width());
					x=x+pX;
					$note.find("#form-note-arrow").css({left:186-pX});
				}
				if (flag) { $note.css({display:"block",left:x,top:y}); flag=false; }
				else { $note.css({display:"none"}); $note.find("#form-note-arrow").css({left:186}); flag=true; }
			});
		});
	};
})(jQuery);

jQuery(window).resize(function() {
	if ($("#box-pics").is("div")) {
		$("#box-pics").css({left:($(document).width()-$("#box-pics").width())/2});
		$("#modal").css({height:$(document).height()-69,top:69});
	}
});


getIntroActiveEl = function() {

   var $tscr=$("#tab-scr");
   var iCnt = $('#tab-scr .items > td').length;
   var scrXt=$tscr.position().left;
   return Math.abs( ( scrXt - 70 )/introW );
   
}//getIntroActiveEl


var inputCounter = 1;
jQuery(function($){

	$("#show-pic").showPic();
	if( $("#intro").length ) {
      // $("#intro").intro();
   }
   
	$(".good-link").glink();
	$("#box-form").form();
   

   $('body').addClass( 'js' );
   
   var $fileInput = $('.fileInput');
   $fileInput.val('');
   $fileInput.hover( function(){
      $(this).parent().find( '.ad-file' ).addClass( 'active' );
   }, function(){
      $(this).parent().find( '.ad-file' ).removeClass( 'active' );
   });
   $fileInput.live('change', function(){
      $(this).parent().clone(true).appendTo('.add-file').find('input').val('').attr('name', 'file' + inputCounter++);
      $(this).hide().siblings().hide();
      file = $(this).val();
      reWin = /.*\\(.*)/;
      var fileTitle = file.replace(reWin, "$1"); //выдираем название файла
      reUnix = /.*\/(.*)/;
      fileTitle = fileTitle.replace(reUnix, "$1"); //выдираем название файла
      $(this).parent().find('.fileName').show().find('span').html( fileTitle );
   });
  
   var $wrap = $('.fileInputWrapper');
   $wrap.append( '<div class="fakeButton"><a class="ad-file" href="javascript://">Загрузить файл</a></div>' );
   $wrap.append( '<div class="blocker"></div>' );
   $wrap.prepend( '<div class="fileName"><span></span><a class="del-ico" href="javascript://"><img height="16" width="16" src="/i/form-delete-file.gif"></a><br clear="all"></div>' );
  
   $('.del-ico').live('click', function(){
      $(this).parent().parent().remove();
      return false;
   });
   
   $('#faq_list .expandQ').click(function(){
      $(this).hide().parent().find('.hiddenQ').slideDown();
      location.hash = this.href.split('#')[1];
      return false;
   });
   
   var h = location.hash.slice(1);
   if( h != '' ) {
      $('#' + h).find('.expandQ').trigger('click');
   }
   
   function mycarousel_initCallback(carousel) {
      $('#intro a').bind('click', function() {
         $('#intro > div').removeClass('activ');
         $(this).parent().addClass('activ');
         // $('#intro a').index(this)+1
         $(this)
         dir = this.className.replace( 'introDir', '' );
         var n = $('#carousel > li > table').index( $('#carousel > li > table.introItemDir' + dir )[0] ) + 1;
         // console.log(n)
         carousel.scroll( n );
         return false;
      });
   }//mycarousel_initCallback
    
   function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
      // console.log(arguments)
      
      $('#intro > div').removeClass('activ');
      // var dir = $(item).find('table.introItemTbl')[0].className.replace('introItemDir', '');
      var arr = /introItemDir(\d+)/i.exec( $(item).find('table.introItemTbl')[0].className );
      var dir=arr[1];
      // console.log(dir)
      dir = isNaN( parseInt( dir ) ) ? 0 : dir;
      $('#intro a.introDir' + dir).parent().addClass('activ');
      
      if( ie6 ) {
         // IEPNGFix.update();
      }
      
      return false;

      // $('.jcarousel-prev').click(function(){
      // parseInt( $('#carousel').css('left') )
      var n = Math.abs( parseInt( $('#carousel').css('left') )/868 );
      var dir = parseInt( $('#carousel li.jcarousel-item').eq( n )[0].className.replace('introItemDir', '') );
      // console.log(n)
      // dir = isNaN(dir) ? 0 : dir;
      // console.log(dir)
      $('#intro > div').removeClass('activ');
      $('#intro a.introDir' + dir).parent().addClass('activ');
    
   }//mycarousel_itemVisibleInCallback

   $('#carousel').jcarousel({
      scroll:1, 
      wrap: 'circular', 
      initCallback: mycarousel_initCallback ,
      itemVisibleInCallback: {onAfterAnimation: mycarousel_itemVisibleInCallback }
   });
   
});

