jQuery.fn.autoresize = function (options) {
  options = options || {}
  return this.each(function () {
    //  Init Sizes
    var parent = $(this).parents('.photo');
    var imageWidth = $(this).width();
    var imageHeight = $(this).height();
    var parentWidth = parent.width();
    var parentHeight = parent.height();
    var ratio, newSide;

    if (imageWidth > imageHeight) {
      if (imageHeight > parentHeight) {
        ratio = parentHeight / imageHeight;
      } else {
        ratio = imageHeight / parentHeight;
      }
      newSide = ratio * imageWidth;
      $(this).height(parentHeight);
      if (newSide > parentWidth) {
        $(this).css({
          'margin-left': parseInt((newSide - parentWidth)) * - 1
        });
      } else {
        $(this).css({
          'margin-left': parseInt((parentWidth - newSide)) * - 1
        });
      }
    } else if (imageWidth < imageHeight) {
      if (imageWidth > parentWidth) {
        ratio = parentWidth / imageWidth;
      } else {
        ratio = imageWidth / parentWidth;
      }
      newSide = ratio * imageHeight;
      $(this).width(parentWidth);
      if (newSide > parentHeight) {
        $(this).css({
          'margin-top': parseInt((newSide - parentHeight)) * - 1
        });
      } else {
        $(this).css({
          'margin-top': parseInt((parentHeight - newSide)) * - 1
        });
      }
    } else {
      $(this).height(parentWidth);
    }
    if (options.corners) {
      if (parent.hasClass(options.corners)) {
        parent.append('<div class="tr"></div><div class="tl"></div><div class="br"></div><div class="bl"></div>');
      }
    }
  })
}

jQuery.fn.searchComplete = function (url, options) {
  return this.each(function(){
    $(this).autocomplete('/search/locations', {
      width : 253,
      dataType: "json",
      scroll : false,
      parse: function(data) {
        enterLock = true;
        setTimeout(function(){enterLock=false;}, 100);
        return $.map(data, function(row) {
          return {
            data: row,
            result: row.name
          }
        });
      },
      formatItem: function(item) {
        return item.name;
      },
      formatMatch : function (item) {
        return item.name;
      }
    });
  });
}

jQuery.fn.looptWindow = function() {
  return this.each(function(){
    // TODO: write a custom window / modal plugin */
  });
}

// TODO: Make sure .val() works with textarea, if not add support for .html()
jQuery.fn.looptInputClear = function() {
  return this.each(function(){
    var initVal = $(this).val();
    $(this).click(function(){
      if ($(this).val()==initVal) {
        $(this).val('');
      }
    });
    $(this).blur(function(){
      if ($(this).val()=='') {
        $(this).val(initVal);
      }
    });
  });
}

jQuery.fn.lockMap = function() {
  return this.each(function() {

    var map = $(this);
    var offsetTop = map.offset().top;
    var noMove = false;
    $(document).scroll(function(){
      moveMe(false);
    });
    
    function moveMe(unlocked) {
      if (unlocked==true) {
        var offset = offsetTop - $(document).scrollTop();
        if(offset <=-75) {
          map.animate({'margin-top':(offset*-1)});
        } else {
          map.animate({'margin-top':'0'});
        }        
      } else {
        if (noMove == false) {
          noMove = true;
          setTimeout(function(){noMove=false; moveMe(true);}, 400);          
        }
      }
    }
    moveMe(false);
  });
}


