function preloadHoverImages() {
  $("img").each(function() {
    var src = $(this).attr("src");
    if(src && src.indexOf("base") != -1) {
      var img = new Image();
      img.src = src.replace(/base/, "active");
    }
  });
}


function resizeGalleryThumbWidth(gallImg)
{
  var newWidth = (117/gallImg.height())*gallImg.width();
  return newWidth;
}

function resizeGalleryThumbHeight(gallImg)
{
  var newHeight = (117/gallImg.width())*gallImg.height();
  return newHeight;
}


function wPaddingNeeded(gallImg)
{
  var wPaddingNeeded = 0;
  if( gallImg.width() < 117 )
  {
    wPaddingNeeded = ((117 - gallImg.width())/2);
  }
  return wPaddingNeeded;
}


function hPaddingNeeded(gallImg)
{ 
  var hPaddingNeeded = 0;
  if( gallImg.height() < 117 )
  {
    var hPaddingNeeded = (117 - gallImg.height())/2;
  }
  return hPaddingNeeded;
}


var gallArr;
function setImageDownloadButton(gallIcon) {  //gallImg = .gallery-icon
  var imgs_dir = 'http://' + location.host + '/wp-content/themes/epk/bnd/images';
  var tmp_filename;
  var tmp_uploadpath;

  tmp_filename = gallArr[gallIcon]['filename'];
  tmp_uploadpath = gallArr[gallIcon]['upload-path'];

  $("#sb_download").append('<div id="dwnld" class="download-button"><a href="/download.php?file=' + tmp_filename + '&path=' + tmp_uploadpath + '"><img src="' + imgs_dir + '/download.jpg" alt="Download" title="Download" border="0" /></a></div>');
}

/*
function equalHeightCols()
{
  // create equal height columns
  if( $("#content").outerHeight() > $("#primary").outerHeight() )
  {
    $("#primary").height($("#content").height());
  }
  if( $("#left-sidebar").length )
  {
    $("#left-sidebar").height($("#primary").outerHeight());
  }
}
*/

function equalHeightCols()
{
  // create equal height columns
  contentHeight = $("#content").height();
  leftHeight = $("#left-sidebar").height();
  rightHeight = $("#primary").height();

  //If #content (content area) height is greatest, set all to content height
  if ( contentHeight > leftHeight && contentHeight > rightHeight ) {
    $("#left-sidebar").height(contentHeight);
    $("#primary").height(contentHeight);
  }

  //If #primary (right sidebar) height is greatest, set all to content height
  else if ( rightHeight > leftHeight && rightHeight > contentHeight ) {
    $("#content").height(rightHeight);
    $("#left-sidebar").height(rightHeight);
  }

  //If #left-sidebar (left sidebar) height is greatest, set all to content height
  else if ( leftHeight > rightHeight && leftHeight > contentHeight ) {
    $("#primary").height(leftHeight);
    $("#content").height(leftHeight);
  }
}


$(document).ready(function() {
  preloadHoverImages();

  // assign the rollover code
  $(".ro").hover(
    function() {
      var src = $(this).attr("src");
      if (src) {
        $(this).attr("src", src.replace(/base/, "active"));
      }
    },
    function() {
      var src = $(this).attr("src");
      if (src) {
        $(this).attr("src", src.replace(/active/, "base"));
      }
    }
  );


  // remove headings if empty (ie)
  $("ul.xoxo h3.widget-title").each(function() {
    //alert($(this).text().length);
    if( $(this).text() == '' || $(this).text() == ' ')
    {
      $(this).remove();
    }
  });


  // set selected state of menu items
  var hostname = "http://"+window.location.hostname+"/";
  var pathname = location.href;
  var pageH1 = $("#content h1:first").text();

  $("#left-sidebar .menu li a").each(function() {
    if( pathname == $(this).attr("href") )
    {
      $(this).parent().addClass("menu-selected");
    }
  });

  $(".menu-header li").each(function() {
    if( $(this).text() == pageH1)
    {
      $(this).addClass("current_page_item");
    }
    else if ( pathname == hostname)
    {
      $(".menu-header li:first").addClass("current_page_item");
    }
  });


  $('.tag-item').click(function() {
    var tag_val = $(this).attr('title');
    $('#s2').val(tag_val);
    $('#searchtagform').submit();
  });


  $('select.products').change(function() {
    var option_val = $('select.products option:selected').val();
    $('#s3').val(option_val);
    $('#product-form').submit();
  });

});



$(window).load(function() {
  $("#content.gallery-parent .entry-content img").each(function() {
    if( $(this).width() > 320 )
    {
      $(this).removeAttr('height');
      $(this).attr('width',320);
    }

    if( $(this).height() > 350 )
    {
      $(this).removeAttr('width');
      $(this).attr('height',350);
    }
  });

  //square-off thumbnails for gallery images
  $("#content.sub-gallery .entry-content img.size-thumbnail").not('.download-button img').each(function() {
    $(this).css('padding', hPaddingNeeded($(this))+'px '+wPaddingNeeded($(this))+'px');
  });

  $(".gallery-icon a img").not('.download-button img').each(function() {
    $(this).css('padding', hPaddingNeeded($(this))+'px '+wPaddingNeeded($(this))+'px');
  });

  $("#content .gallery .gallery-icon a img").not('.download-button img').each(function() {
    if( $(this).height() > $(this).width() )
    { 
      if( $(this).height() > 117 )
      {
        $(this).width(resizeGalleryThumbWidth($(this)));
        $(this).height(117);
      }
    }
    else
    {
      if( $(this).width() > 117 )
      { 
        $(this).height(resizeGalleryThumbHeight($(this)));
        $(this).width(117);
      }
    }
    $(this).css('padding', hPaddingNeeded($(this))+'px '+wPaddingNeeded($(this))+'px');
  });

  // add download button to shadowbox
  if( $('#sb-info').length )
	{
	  $('#sb-nav').before('<div id="sb_download"></div>');

    gallArr = {};
    $('.gallery-icon, .related-thumbs').each(function() {
      gallArr[$(this).children('a').children('img').attr('src')] = {};
      gallArr[$(this).children('a').children('img').attr('src')]['filename'] = $(this).children("input.filename").val();
      gallArr[$(this).children('a').children('img').attr('src')]['upload-path'] = $(this).children("input.upload-path").val();
    });

    $('.gallery-icon, .related-thumbs').click(function() {
      setImageDownloadButton($(this).children('a').children('img').attr('src'));
    });
  }

  var a = Shadowbox.change; 
  Shadowbox.change = function(index) { 
    var tmp_filename;
    var tmp_uploadpath;
    a(index);

    //console.log(Shadowbox.getCurrent());
    tmp_filename = gallArr[Shadowbox.getCurrent().content]['filename'];
    tmp_uploadpath = gallArr[Shadowbox.getCurrent().content]['upload-path'];
    $('#dwnld a').attr('href', '/download.php?file=' + tmp_filename + '&path=' + tmp_uploadpath);
  };

  equalHeightCols();
});

