var $imgs;
var $imgsW = new Array();
var $imgsH = new Array();
var widestImgH = 0;
var widestImgW = 0;
var multiplyer = 1;
var $resize = true;

function startResize($_imgs, $_resize) {
  if ($_imgs.length > 0) {
    $imgs = $_imgs;
    if ($_resize != undefined) {
      $resize = $_resize;
    }
    $imgs.each(function() {
      $imgsW.push($(this).width());
      $imgsH.push($(this).height());
    });
    $(window).resize(doThaDance);
    doThaDance();
  }
}
function doThaDance() {
  //alert($imgs);
    $winW = $(window).width();
    $winH = $(window).height();
    $w = $imgs.first().parents('#image-main-wrapper');
    $p = $imgs.first().parent();
    margin = parseInt($winH * 0.075);
    if ($resize) {
      $w.css('margin', margin);
      $w.css('margin-bottom', margin*1.5);
      $w.css('width', $winW - margin*2);
      $w.css('height', $winH - margin*2.5);
    } else {
      $w.css('margin-top', margin);
      $w.css('height', $winH - margin);
    }
    wide = 0;
    i = 0;
    $imgs.each(function() {
      if ($imgsW[i]/$imgsH[i] > wide) {
        wide = $imgsW[i]/$imgsH[i];
        widestImgW = $imgsW[i];
        widestImgH = $imgsH[i];
        if ($imgsW[i]/$imgsH[i] > $p.width()/$p.height()) {
          multiplyer = $p.width() / $imgsW[i];
        } else {
          multiplyer = ($p.height() - $('#gallery-title').height()) / $imgsH[i];
        }
      }
      i++;
    });
    i = 0;

    $imgs.each(function() {
      if ($resize) {
        $(this).width($imgsW[i] * multiplyer);
        $(this).height($imgsH[i] * multiplyer);
        $(this).css('margin-left', ($w.width() - widestImgW * multiplyer)/2);
      } else {
        $(this).css('margin-left', ($w.width() - widestImgW)/2);
      }
      i++;
      });
    if ($resize) {
      $('#gallery-title').width(widestImgW * multiplyer);
      $('#gallery-title').css('padding-left', ($w.width() - widestImgW * multiplyer)/2);
    } else {
      $('#gallery-title').width(widestImgW);
      $('#gallery-title').css('padding-left', ($w.width() - widestImgW )/2);
    }
}

