
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console) {
    arguments.callee = arguments.callee.caller;
    var newarr = [].slice.call(arguments);
    (typeof console.log === 'object' ? log.apply.call(console.log, console, newarr) : console.log.apply(console, newarr));
  }
};

// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
{console.log();return window.console;}catch(err){return window.console={};}})());

(function($)
{
	/**
	* Returns the number of nights between two dates
	*
	* @return integer
	*/
	$.fn.number_of_nights = function(from, to)
	{
		if (from == '' || to == '')
		{
			// Can't set number of nights without enough data
			return false;
		}

		// Parse from and to
		from = $.datepicker.parseDate($.datepicker._defaults.dateFormat, from);
		to   = $.datepicker.parseDate($.datepicker._defaults.dateFormat, to);

		// Convert from and to into Date
		from = new Date(from),
		to   = new Date(to);

		// Get difference
		var difference = new Date(to - from);

		// Convert difference to days
		return Math.round(difference / 86400000);
	}

})(jQuery);

(function($)
{
	/**
	* Returns the base name of the date selector
	*
	* @return integer
	*/
	$.fn.get_date_selector_name = function(select)
	{
		// Split name
		var name = select.attr('name').split('[');

		// Return basic name
		return name[0];
	}

})(jQuery);

(function($)
{

	/**
	* Manage rate selection
	*
	* @return string
	*/
	$.fn.manage_rate_selection = function(select)
	{
		// Get the current room
		var room = select.parents('li.room').first().attr('id');

		// Get the total number of needed rooms for this room
		var needed = parseInt(select.find('option:last-child').val());

		// Default value for selected rooms
		var selected = 0;

		// Count number of selected rooms
		$.each
		(
			$('#' + room + ' option:selected'),
			function(index, value)
			{
				// Add the value to the selected rooms value
				selected = selected + parseInt($(value).val());
			}
		);

		if (selected === needed)
		{
			// Set up selects
			$.each
			(
				$('#' + room + ' select'),
				function(index, value)
				{
					// Remove incomplete class
					$(value).removeClass('incomplete');

					// Add a value holder hidden field
					$(value).not('[name="' + select.attr('name') + '"]').after('<input type="hidden" name="' + $(value).attr('name') + '" value="' + $(value).val() + '">')

					// Disable other selects
					$(value).not('[name="' + select.attr('name') + '"]').attr('disabled', 'disabled');
				}
			);
		}
		else
		{
			// Add incomplete class
			$('#' + room + ' select').addClass('incomplete');

			// Remove value holder hidden fields
			$('#' + room + ' input:hidden').remove();

			// Enable disabled selects
			$('#' + room + ' select:disabled').removeAttr('disabled');
		}
	}

})(jQuery);

(function($)
{
	/**
	 * Slides content
	 */
	$.fn.slide_fade_toggle = function(speed, easing, callback)
	{
		return this.animate
			(
				{
					opacity: 'toggle',
					height: 'toggle'
				},
				speed,
				easing,
				callback
			);
	};

})(jQuery);
