﻿/*

Global javascript for iTRavex

*/

// Namespace
var iTravex = new Object();

//
// Init
//
jQuery(document).ready(function ()
{
   jQuery("#main-search").click(function ()
   {
      iTravex.Message("Simply enter an iTravex property ID as 1234.<br /><br />Enter a VRBO property as vrbo1234.<br /><br />Enter an Owner Direct property ID as odvr1234.");
      return false;
   });

   // Generic button click method
   jQuery(".generic-button").click(function ()
   {
      window.location.href = jQuery(this).next().html();
   });

   // end of init function
});

//
// Format date object into our standard display
//
iTravex.FormatDate = function (date)
{
   var day = new Array(7);
   day[0] = "Sun";
   day[1] = "Mon";
   day[2] = "Tue";
   day[3] = "Wed";
   day[4] = "Thu";
   day[5] = "Fri";
   day[6] = "Sat";

   var month = new Array(12);
   month[0] = "Jan";
   month[1] = "Feb";
   month[2] = "Mar";
   month[3] = "Apr";
   month[4] = "May";
   month[5] = "Jun";
   month[6] = "Jul";
   month[7] = "Aug";
   month[8] = "Sep";
   month[9] = "Oct";
   month[10] = "Nov";
   month[11] = "Dec";

   var d = new Date(date);
   var text = day[d.getDay()] + ' ' + month[d.getMonth()] + ' ' + d.getDate() + '/' + String(d.getFullYear()).substr(2, 2);
   return text;
}

//
// Adds specified number of days to provided date, returning new date value
//
iTravex.AddDaysToDate = function (date, days)
{
   var d2 = new Date(date.getTime() + (days * 24 * 60 * 60 * 1000));
   return d2;
}

//
// Gets today
//
iTravex.Today = function ()
{
   var now = new Date();
   var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
   return today;
}

//
// Centers an element on the screen
//
jQuery.fn.center = function ()
{
   this.css("position", "absolute");
   this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
   this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
   return this;
}

//
// Calculates num nights between two dates
//
iTravex.GetNights = function(date1, date2)
{
   // get nights
   var day = 1000 * 60 * 60 * 24;
   var ms = Math.abs(date1.getTime() - date2.getTime());
   var nights = Math.round(ms / day);
   return nights;
}

//
// Extension to remove buttons from jqury UI dialogs
// Usage: $('#dialog-selector').dialog('removebutton', 'button-name');
//
$.extend($.ui.dialog.prototype,
{
   'removebutton': function (buttonName)
   {
      var buttons = this.element.dialog('option', 'buttons');
      delete buttons[buttonName];
      this.element.dialog('option', 'buttons', buttons);
   }
});

//
// Puts up a confirmation dialog with two buttons.
// button 1 (OK by default) and button 2 (Cancel by default).
// Callback param is 1 or 2 for the button that was clicked.
//
iTravex.Confirm = function (message, title, callback, button1, button2)
{
   var close = false;  // tracks if closed by means other than provided buttons

   var dialog_buttons = {};
   dialog_buttons[button1 == null ? "OK" : button1] = function ()
   {
      close = true;
      jQuery(this).dialog("close");
      callback(1);
   }
   dialog_buttons[button2 == null ? "Cancel" : button2] = function ()
   {
      close = true;
      jQuery(this).dialog("close");
      callback(2);
   }

   jQuery("#master-layout-confirm").html("<br />" + message);
   jQuery("#master-layout-confirm").dialog(
   {
      modal: true,
      title: title == null ? "iTravex" : title,
      width: 500,
      buttons: dialog_buttons,
      close: function (event, ui)
      {
         if (close == false)
         {
            callback(0)
         }
      }
   });
}

//
// Puts up a message dialog
//
iTravex.Message = function (message, title, button_text)
{
   var dialog_buttons = {};
   dialog_buttons[button_text == null ? "OK" : button_text] = function ()
   {
      jQuery(this).dialog("close");
   }

   if (title == null)
   {
      title = "iTravex";
   }

   jQuery("#master-layout-confirm").html("<br />" + message);
   jQuery("#master-layout-confirm").dialog(
   {
      modal: true,
      title: title,
      width: 500,
      buttons: dialog_buttons
   });
}

//
// Shows the wait popup
//
iTravex.ShowWaitMessage = function (message, modal)
{
   if (message == null)
   {
      message = "Please wait...";
   }
   jQuery("#dialog-wait-message").html(message);
   jQuery("#dialog-wait").dialog(
         {
            modal: modal == null || modal == true,
            title: "iTravex",
            width: 400
         });
}

//
// Hides the wait popup
//
iTravex.HideWaitMessage = function ()
{
   jQuery("#dialog-wait").dialog("close");
}

//
// Replace newlines with breaks
//
iTravex.Newline2Break = function (source)
{
   return source.replace(/\r\n|\r|\n/g, '<br />');
};

//
// Replace encoded breaks
//
iTravex.Encoded2Break = function (source)
{
   return source.replace(/&lt;br \/&gt;/g, '<br />');
};

//
// Replace breaks with newlines
//
iTravex.Break2Newline = function (source)
{
   return source.replace(/\<br(\s*\/|)\>/g, "\n");
};




