$(document).ready(function(){
  // adjusts the size of the image to the size of the wrapper div, preserving the aspect ratio and keeping 
  //the image in the middle of the wrapper div.
  jQuery.fn.adjustSize = function(ref_width, ref_height) {
    var width = $(this).width();
    var height = $(this).height();
    if (width > height) {
      $(this).width(ref_width);
      var new_height = Math.round(height*(ref_width/width));
      $(this).height(new_height);
      $(this).css("margin-top", (ref_height - new_height)/2);
      $(this).css("margin-bottom", (ref_height - new_height)/2);
    } else {
      $(this).height(ref_height);
      var new_width = Math.round(width*(ref_height/height));
      $(this).width(new_width);
      $(this).css("margin-left", (ref_width - new_width)/2);
      $(this).css("margin-right", (ref_width - new_width)/2);
    }
  };
  //list product images
  var ref_width, ref_height;
  ref_width = $("div.p_img_wrapper").width();
  ref_height = $("div.p_img_wrapper").height();
  $(".p_img_wrapper img").adjustSize(ref_width, ref_height);
  //latest products images
  ref_width = $("div.lp_img_wrapper").width();
  ref_height = $("div.lp_img_wrapper").height();
  $(".lp_img_wrapper img").adjustSize(ref_width, ref_height);
  //featured products images
  ref_width = $("div.fp_img_wrapper").width() - 14;
  ref_height = $("div.fp_img_wrapper").height() - 14;
  $(".fp_img_wrapper img").adjustSize(ref_width, ref_height);
  //category images
  ref_width = $("div.cat_img_wrapper").width();
  ref_height = $("div.cat_img_wrapper").height();
  $(".cat_img_wrapper img").adjustSize(ref_width, ref_height);
  //new products
  ref_width = $("div.np_img_wrapper").width();
  ref_height = $("div.np_img_wrapper").height();
  $(".np_img_wrapper img").adjustSize(ref_width, ref_height);
  //special products
  ref_width = $("div.sp_img_wrapper").width();
  ref_height = $("div.sp_img_wrapper").height();
  $(".sp_img_wrapper img").adjustSize(ref_width, ref_height);
  //shopping cart
  ref_width = $("div.sc_img_wrapper").width();
  ref_height = $("div.sc_img_wrapper").height();
  $(".sc_img_wrapper img").adjustSize(ref_width, ref_height);
  //product details image
  ref_width = $("div.pd_img_wrapper").width();
  ref_height = $("div.pd_img_wrapper").height();
  $(".pd_img_wrapper img").adjustSize(ref_width, ref_height);  

  var search_text = $("#mod_search_searchword").val();
  $("#mod_search_searchword").focus(function(){
    var points = $(this).val().substr($(this).val().indexOf('.'), 3);
    if (points == '...') {
    	$(this).val('');
    	$(this).css("color", "black");
    }
  });
  $("#mod_search_searchword").blur(function(){
    if ($(this).val() == '') {
        $(this).val(search_text);
    	$(this).css("color", "gray");
    }
  });
  $("#searchButton").click(function(){
    var points = $("#mod_search_searchword").val().substr($("#mod_search_searchword").val().indexOf('.'), 3);
    if (points == '...') {
      $("#mod_search_searchword").val('');
    }
    $("#mod_search_searchword").val()
  });
});
