window.onload = window.onresize = valign;

function valign(snd) {
  var h = ($(document).height() - $('.main').height() )/2;
  $('.main').css({display:"none",top:(h>0?h:0)+'px'});
  $('.main').css({display:"block"});
  //if (!snd) valign(true);
} 

var pos=0;

$(".gal_right").click(function(){
  if ((1015*(pos+1))<$(".fg_pics").width()) pos++;
  $(".fg_pics").animate({marginLeft:(-1015*pos)+"px"},1000);
});


$(".gal_left").click(function(){
  if (pos>0) pos--;
  $(".fg_pics").animate({marginLeft:(-1015*pos)+"px"},1000);
});

$(".fg_pic img").click(function(){
  var offset = $(this).offset();
  
  var modal=$("<div>").css({width:$(window).width()+"px",height:$(window).height()+"px",position:"absolute",top:0,left:0,background:"silver",zIndex:2000}).addClass("modal").fadeTo(0,0), 
    img=$("<img>").attr("src",$(this).attr("src").replace(/_th/,"")).css({height:"140px"}).hide().addClass("modal_img");

  $("body").append(modal).append($("<div>").addClass("imgdiv").css({textAlign:"center",position:"absolute",zIndex:3000,top:offset.top+"px",left:offset.left+"px", height:"140px", width:"199px"}).append(img));
  img.load(function(){
    var top = Math.round(($(window).height() - 640 )/2),
      left = Math.round(($(window).width() - 853 )/2);
    $(".imgdiv").show().animate({width:"853px",height:"640px",top:(top>0?top:0)+"px", left:(left>0?left:0)+"px"},1000);
    $(this).show().animate({height:"640px",top:(top>0?top:0)+"px", left:(left>0?left:0)+"px"},1000);
    $(".modal").fadeTo(1000,0.7);
  });
  $(".modal_img, .modal").click(function(){
    $(".modal_img, .modal").fadeOut(1000,function(){
      $(".modal_img, .modal, .imgdiv").remove();
    });
  });
});



