$maxwidth = 320;
$maxheight = 300;

$(function(){
$("figure.feature").each(function(){

  $fw = $("figure").width();
  $bw = $("figure .block-container").width();

  mobile = ($(document).width() < 500) ? true : false;
  if(mobile)
  {
    $sw = parseInt($bw * 0.8);
  }
  else
  {
    $sw = parseInt(($fw - $bw) * 0.8);
  }
  $iw = $("figure img").width();

  $fh = $("figure").height(); 
  $ih = $("figure img").height();

  if( $ih > $fh || $iw > $sw )
  {
    $ir = 1;
    if($iw < $ih) 
    {
      $ir = ($fh / $ih) * .9; // make 90% of size
    }
    else
    {
      $ir = ($sw / $iw) * .9; // make 90% of size
    }

    $iw = parseInt($iw * $ir);
    $ih = parseInt($ih * $ir);
    
    if($iw > $maxwidth)
    {
      $("figure img").attr("width",$maxwidth);
    }
    else
    {
      $("figure img").attr("width",$iw);
    }

    if( $ih > $maxheight )
    {
      $("figure img").attr("height",$maxheight);
    }
    else
    {
      $("figure img").attr("height",$ih);
    }
  }

  if($fw > 0 && $bw > 0 && $iw > 0)
  {  
    if(!mobile)
    {
      $("figure img").css("padding-left",parseInt((($fw - $bw) - $iw) / 3)); 
    } 
  }

  if($fh > 0 && $ih > 0)
  {
    if(!mobile)
    {
      $("figure img").css("padding-top", parseInt(($fh - $ih) / 3) );
    }
    else
    {
      $("figure img").css("padding-top", "10px" );
    }
  }
}); // end each figure.feature


  $minheight = 120;
  $("section.feature.product").each(function(){ 
    $cw = $(this).find(".inside .content").first().width();
    $miw = ($cw / 2) - parseInt( $cw / 10); 
    $i = $(this).find(".inside .content img").first();
    $iw = $i.width(); 
    if($iw > $miw)
    {
      $ih = $i.height(); 
      if($iw < $ih) 
      {
        $ir = $iw / $ih;
      }
      else
      {
        $ir = $ih / $iw;
      }
      $iw = $miw;
      $ih = $iw * $ir;
      $i.attr("width",parseInt($iw));
      $i.attr("height",parseInt($ih));
    }

    if($(this).height() > $minheight)
    {
      $minheight = $(this).height();
    }
  });


$("section.feature.product").each(function(){$(this).css("height",parseInt($minheight));});

$select = $("<select></select>");

$("header nav ul").first().find("a").each(function(){
  $select.append('<option value="'+$(this).attr("href")+'">'+$(this).html()+'</option>');
});

$("header nav .products-submenu section").each(function(){

  $optgroup = $("<optgroup label='"+$("h4 a",this).html()+"'>");
  
  $("li a",this).each(function(){
    $optgroup.append('<option value="'+$(this).attr('href')+'">'+$(this).html()+'</option>');
  });

  $select.append($optgroup);
});

$mobile = $("<div class='mobile'><h4>Jump to page:</h4></div>");
$mobile.append($select);
$("header nav").append($mobile);

$("header nav select").change(function(){
  $url = $(this).attr("value");
  window.location = $url;
});


$("header nav select").blur(function(){
  $url = $(this).attr("value");
  window.location = $url;
});

});


