// PDP unit-level javascript file
// Requires jQuery
var pdp = {};
pdp.imageScroller = {}; // image scroller namespace

pdp.saveToFavorites = function(obj) {
  // Degradable AJAX 'Save To Favorites'
  $(obj).bind("click", {obj:obj} ,function(e) {
    var obj = $(e.data.obj);
    var justFav = (obj.attr("href") == favLink);
    
    if (justFav) {  // if is favorite
      return true;  // return to continue link
    } else {
      var yPos = obj.position().top - 23;
      var xPos = obj.position().left + 55;
      obj.changeUriParam("next_url",0); // set next_url to 0 so it does not cause another AJAX call to property
      $.get(obj.attr("href")); // Get the URL from href and make it an AJAX call
      $(".property_name").prepend("<img src='/images/icon_favorite_small.gif' width='15' height='14' border='0' alt='Favorite Property'>&nbsp;"); // Add Favorites Icon next to Property Name
      obj.find("img").attr("src","/images/ViewFavorites.gif"); // Change Image and Link 
      obj.attr("href", favLink);  // this variable is set at the top of pdp-unit-level
      var favCount = parseInt($("#favCount").html() || 0);
      $("#favCount").html(favCount + 1); // Increment the Favorites in Topbar
      var $fav_message = $("#fav_message");  // store object in var for smoother animation
      $fav_message.css({ top:yPos+'px', left:xPos+'px', "z-index":2000 }) // Fav added message
                  .fadeIn(500)
                  .animate({ opacity:1.0 }, 2000 )
                  .animate({ opacity:0 }, 500 );
      return false;
    }  // if not isFav
  return false;
  }); // bind click()
} // end pdp.saveToFavorites()


var logImage = new Image();
function logPropertyViewing(property_id, name, session_email_id) {
  logImage.src =
    '/property/ajax/log-property-viewing.html?name='
    + name + '&property_id='
    + property_id + '&session_email_id='
    + session_email_id;
}


$().ready( function() {

  // Enable JS Print Links & add onclick print call
  // Print links on the PDP need to call logPropertyViewing, passing parameters via link attributes.
  $(".print_page").show();
  $(".print_link").click( function() {
    if ($(this).attr("property_id")) {
        logPropertyViewing($(this).attr("property_id"), "print", $(this).attr("session_email_id") || "" );
    }
    rent.win.printWindow();
  });
  
  // --- set up tooltip ---
  $.extend($.fn.Tooltip.defaults, {track: true, delay: 0, showURL: false, showBody: " - " });
  $('.tooltip').Tooltip();

  if ($("#nav_map").length >= 1 && $("#nav_overview").length >= 1 && $("#nav_moving").length >= 1 ) {
    // Navigation Tab Rollovers
    pdp.tab = $().getUriParam("tab");

    if (pdp.tab == 'overview' || !pdp.tab) {
      $("#nav_map").hover(
        function() { $(this).attr("src","/images/tab_map_over.gif"); }, 
        function() { $(this).attr("src","/images/tab_map.gif"); }
      );
    } else {
      $("#nav_overview").hover(
        function() { $(this).attr("src","/images/tab_details_over.gif"); }, 
        function() { $(this).attr("src","/images/tab_details.gif"); }
      );
    }
    $("#nav_moving").hover(
      function() { $(this).attr("src","/images/tab_moving_quotes_on.gif"); }, 
      function() { $(this).attr("src","/images/tab_moving_quotes.gif"); }
    );
  } // elements exist

  $("#show_all_floorplans").toggle(function(){
    if ($(".non_vaultware").length) { // make sure not to apply rowspan to any vaultware properties
      var oRows = $("#unit_matrix_table tr");
      var iRowCount = (oRows.length) - 2; // subtract 1 row for header and another row for "Show all..." link  row    
      $(".availability_column").attr({rowSpan: iRowCount}); // add ALL of the floorplans to rowspan
    }
    $(".hidden").show();
    defText = $("#show_all_floorplans").html();
    $(this).html("Collapse floor plans"); // change text
    return false;
  },function(){
    $(".hidden").hide();
    $(this).html(defText); // change text back to original
    if ($(".non_vaultware").length) { // make sure not to apply rowspan to any vaultware properties
      $(".availability_column").attr({rowSpan: "5"}); // bring the rowspan back down to 5
    }
    return false;
  });
  
  
  // --- Image Slideshow ---
  // define var for slide fading control
  pdp.imageScroller.firstSlide = 1;

  pdp.imageScroller.picLoading = function() {

    var index = $(this).parent().children().index(this);
    var $this = $(this) // turn to var to make more efficient
    // math for downscaling images to center horizontally
    var dimRatio = $this.height() / $this.width();
    var maxWidth  = 750;
    var maxHeight = 550;
    
    if ($this.width() > maxWidth) {
      $this.width(maxWidth).height(maxWidth * dimRatio );
    }
    if ($this.height() > maxHeight) {
      $this.height(maxHeight).width(maxHeight / dimRatio );
    }
    
    // runtime loading of current/previous/next images 
    pdp.imageScroller.loadImgs(index,pdp.imageScroller.overviewImages);
    
    $("#preview_pics").animate({ height:$(this).height(), width:$(this).width() }, 750);
    
    if (pdp.imageScroller.firstSlide == 1) {
      pdp.imageScroller.firstSlide = 0;
    } else {
      $("#pic_info").fadeOut("fast");
      $("#enlarge_icon").fadeOut("fast");
    }
    $("#loading_anim").show();
  } // end loadingAnim()
  
  pdp.imageScroller.picLoaded = function() {
      var index = $(this).parent().children().index(this); 
      $("#loading_anim").hide();
      $("#pic_name").html(this.alt);
      $("#pic_num").html(index + 1);
      $("#pic_info").fadeIn("fast");
      $("#enlarge_icon").fadeIn("fast");
      //$("#enlarge_link").changeUriParam("starting_slide",index);
  } // end afterLoad()


  pdp.imageScroller.arrayLoader = function(els,opts) {
    for (var i = 0; i < els.length; i++) {
      els[i].src = opts.imageArray[i].uri;
      els[i].alt = opts.imageArray[i].name;
    }
  } // end arrayLoader()
  
  // dynamically load previous and next images
  pdp.imageScroller.loadImgs = function(index,arr) {
    var arrLast = arr.length-1;
    pdp.imageScroller.loadImgSource(arr,index); // load current image
    if (index == 0) pdp.imageScroller.loadImgSource(arr,arrLast);
    if (index < arrLast ) pdp.imageScroller.loadImgSource(arr,index+1);
    else if (index == arrLast) pdp.imageScroller.loadImgSource(arr,index-1);
  } // end loadImgs()
  
  // load the image if the src is not already available
  pdp.imageScroller.loadImgSource = function(arr,img) {
    if ($("#slidepic_"+img).attr("src") == undefined) { // if the source of the image is blank only then load the image
      $("#slidepic_"+img).attr("src",arr[img].uri);
    } else {
      return false;
    }
  } // end loadImgSource()
  
}); // end onready()
