// Initializes User Scripts:

$(document).ready(function() {

  // Selects text in Search Box when focused:
  $('input.txtSearch').focus(function() {
    this.select();
  });

  // Footer Links - pop up in new window / tab:
  $('a#smartzLink').click(function() {
    window.open($(this).attr("href"), 'window2', '');
    return false;
  });

  $('#Menu').navMenu({
    menuWidth: 200,
    containerElement: "#navBar",
    menuElement: "div",
    rightArrow: '<img src="/subnav-arrow.gif" width="7" height="8" style="padding-top: 4px;" alt=" »" />'
  });

  // Initialize Pixelsilk Email This URL Plugin:
  //$('#emailFriendLink').emailUrl();

  if ($("#calendarImage").attr("src") == '') {
    $("#calendarImage").hide();
  }
  
  if ( document.URL.indexOf('login.aspx') > -1 && document.URL.indexOf('property-owners') > -1 ) {
    $("form[action*='login.aspx']").prepend('<h2>Property Logins are provided to deeded property owners only</h2><p>Please contact Katie Williams at 541-595-1510 or kwilliams@BlackButteRanch.com if you are experiencing login difficulties.</p>')
      .after('<br /><p><a href="/lost-password/">I forgot my password!</a></p>');
  }
});

$("img#primaryImageSrc").load(function(){
  $("#rentalImageBorder").height($("#primaryImageSrc").height() - 4);
});
$(window).load(function() {
  // Add inner white borders to images in content area...
  var html, imgWidth, imgHeight, left, top;
  var htmltop = '<div style="position: absolute;';
  var htmlbottom = 'border: 1px #fff solid; background: transparent; z-index: 10;"> </div>';
  var mainLeft = findLeft(document.getElementById("mainInner"));
  var mainTop = findTop(document.getElementById("mainInner"));
  if (document.getElementById("rentals")) {
    $(".rentalThumb a img").each(function() {
      $(this).css("border", "none");
      imgWidth = $(this).width();
      imgHeight = $(this).height();

      html = htmltop;
      html += 'left: 1px; top: 1px;';
      html += 'width: ' + (imgWidth - 4) + 'px; height: ' + (imgHeight - 4) + 'px;';
      html += htmlbottom;
      $(this).parent().append(html);
    });
  } else if (document.getElementById("imageColumn")) {
    $("#rentalImageBorder").height($("#primaryImageSrc").height() - 4);
    $("#rentalImageBorder").css("visibility", "visible");
    $(".rentalThumbs a img").each(function() {
      $(this).css("border", "none");
      imgWidth = $(this).width();
      imgHeight = $(this).height();

      html = htmltop;
      html += 'left: 1px; top: 1px;';
      html += 'width: ' + (imgWidth - 4) + 'px; height: ' + (imgHeight - 4) + 'px;';
      html += htmlbottom;
      $(this).parent().append(html);
    });
  } else if (!document.getElementById("photoItems")) {
    $("#mainInner img").each(function() {
      if ($(this).parent().get(0).tagName != 'A') {
        $(this).css("border", "none");
        imgWidth = $(this).width();
        imgHeight = $(this).height();
        left = findLeft(this) - mainLeft;
        top = findTop(this) - mainTop;

        html = htmltop;
        html += 'left: ' + (left + 1) + 'px; top: ' + (top + 1) + 'px;';
        html += 'width: ' + (imgWidth - 4) + 'px; height: ' + (imgHeight - 4) + 'px;';
        html += htmlbottom;
        $("#mainInner").append(html);
      }
    });
  }
  divHeightStart();

});

var ColumnDiv = 'mainColumn';
var Target = 'footerDiv';

function adjustHeight() {
  var colHeight = $('#' + ColumnDiv).height();
  var tgt = document.getElementById(Target);
  var col = document.getElementById(ColumnDiv);

  $('#' + ColumnDiv).height(colHeight + (findTop(tgt) - (colHeight + findTop(col)) - 30));
}
function divHeightStart() {
  adjustHeight();
  //document.body.onresize = adjustHeight;
  //window.onresize = adjustHeight;
  $(window).resize(function() {
    adjustHeight();
  });
}
function findTop(obj) {
  var curtop = 0;
  if (obj.offsetParent) {
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curtop += obj.offsetTop
    }
  }
  return curtop;
}
