function imgPosition() {
  var cib = $('#cur-image-box');
  var xoffset = Math.floor($(window).width()/2-cib.width()/2);
  cib.css('left', xoffset + 'px');  
  cib.css('top', ($(this).scrollTop()+50) + "px");
  cib.redrawShadow();
 }

 $(document).ready(function() {

    // positioning and behaviour of fastorder-box
    var anchor = $("a#fastorder-opener").offset();
    $("#fastorder-box")
     .css('left', Math.floor(anchor.left-$("#fastorder-box").width()*0.3)+'px')
     .css('top', Math.floor(anchor.top+30)+'px');
    $("a#fastorder-close").click(function() {
	    $("#fastorder-box").fadeOut("slow");
	    return false;
    });
    $("a#fastorder-opener").click(function() {
	    if ($("#fastorder-box").css('visibility') == 'hidden') {
		    $("#fastorder-box").css('visibility', 'visible');		    
	    } else {
		    $("#fastorder-box").fadeIn("slow");
            }
	    return false;
    });
    
    
   // hiding brand and subsections block of a section
	$("#producers-collapse").click(function() {
		if ($("#producers-collapse-box").css('display') == 'none') {
			$("#producers-collapse-box").slideDown(300);
			$("#producers-collapse").html('Скрыть производителей');
		} else {
			$("#producers-collapse-box").slideUp(300);
			$("#producers-collapse").html('Показать производителей');
		}
	});



    // positioning and behaviour of email-box
    anchor = $("a#email-opener").offset();
    $("#email-box")
     .css('right', '20px')
     .css('top', Math.floor(anchor.top+30)+'px');
    $("a#email-close").click(function() {
	    $("#email-box").fadeOut("slow");
	    return false;
    });
    $("a#email-opener").click(function() {
	    if ($("#email-box").css('visibility') == 'hidden') {
		    $("#email-box").css('visibility', 'visible');		    
	    } else {
		    $("#email-box").fadeIn("slow");
            }
	    return false;
    });

    // working with preview

      var inbox1 = $(".thumb-box");

      // цикл вставления маленьких картинок
      $(".thumbs a.viewer-opener").each(function(i) {
           var curthu = $(this);
       	   inbox1.append('<div class="thumb-box-a"><a href="'+
		   curthu.attr('href')+'" class="thumb-pic">'+
		   '<img src="'+$('#'+curthu.attr('id')+' img').attr('src')+'" alt="x"></a></div>');
      });

      var myd = $("#cur-image-box");
      var mys = myd.dropShadow({left: 0, top: 0, blur: 5, opacity: 0.7});
      mys.css('visibility', 'hidden');

      // Закрытие простмотра
      $("#cur-image-box *:not(.thumb-box *)").click(function() {
      
         $('#cur-image-box').css('visibility','hidden');
	 $('#cur-image-box_dropShadow').css('visibility','hidden');
         return false;
         
      });

      // переключение картинок
      $("a.thumb-pic, a.thumb-pic-active").click(function() {

	var sh = $(this).attr('href');
      	var dd = $("a.thumb-pic-active");
      	dd.removeClass('thumb-pic-active');
      	dd.addClass('thumb-pic');
      	$(this).removeClass('thumb-pic');
      	$(this).addClass('thumb-pic-active');
      	$("img#big-image").attr('src', sh);
      	return false;
      	
      });  


    // pop-up viewer 
    $("a.viewer-opener").click(function() {
      var pathimg = $(this).attr('href');
      imgPosition();
      $('#cur-image-box_dropShadow').css('visibility', 'visible');
      $('#cur-image-box').css('visibility', 'visible');
      
      // выделение нужной миниатюры
      $('a.thumb-pic, a.thumb-pic-active').each(function(i) {
	      var classname = '';
	      if ($(this).attr('href') == pathimg) classname = 'thumb-pic-active';
	      else classname = 'thumb-pic';
	      $(this).removeClass('thumb-pic-active');
	      $(this).removeClass('thumb-pic');
	      $(this).addClass(classname);
      });

      // max-width and max-height emulation for IE
      var wi = 550;
      var he = 350;

//      if (jQuery.browser.msie == true) {
      	$("img#big-image").load(function() {

		var thimg = $(this);
		thimg.css('height', 'auto');
		thimg.css('width', 'auto');
		if ((thimg.height() > he) && (thimg.width() > wi)) {
			if (thimg.height()/he > thimg.width()/wi) {
				thimg.css('height', he+'px');
			} else {
				thimg.css('width', wi+'px');
			}
		} else if (thimg.height() > he) {
			thimg.css('height', he+'px');
		} else if (thimg.width() > wi) {
			thimg.css('width', wi+'px');
		}
      	});
//      }

      $("img#big-image").attr('src', pathimg);

      return false;
    });

 });
