// JQuery UI-Date Picker Configuration
// author: john m howitt
// pass 1
// sets up the calendar for the when to purchase date
// set up so that starts two days in advance and excludes the weekends
// from selection

$(function() {
		$( "#datepicker" ).datepicker( { dateFormat: 'dd-M-yy' , 
		 									beforeShowDay: $.datepicker.noWeekends ,
											defaultDate: '+1d',
											minDate: '+1d',
											maxDate: '+1w',
											buttonImage: '/images/calendar-icon.png'
											});
	});
	


/*
	jQuery Clear-Input plugin
	v1.0
	Author: Aidan Feldman

	USAGE
	Add the class .clear-input to any text input element
	whose value you want cleared when it gains focus.
	The initial value will be replaced when the input
	loses focus, and no new text has been entered.

	If you prefer to not add classes to your elements,
	you can alternatively call clearInput() on any jQuery
	input object.
*/

	(function( $ ){
	  // define the initialValue() function
	  $.fn.initialValue = function(value) {
	    if (value) {
	      return this.attr('initial-value', value);
	    } else {
	      return this.attr('initial-value');
	    }
	  };

	  $.fn.clearInput = function() {
	    return this
	      .focus(function(){
	        if (this.value == $(this).initialValue()) {
	          this.value = '';
	        }
	      })
	      .blur(function(){
	        if (this.value == '') {
	          this.value = $(this).initialValue();
	        }
	      })
	      .each(function(index, elt) {
	        $(this).initialValue(this.value);
	      });
	  };

	  // apply plugin to all inputs with class ".clear-input"
	  $(function() {
	    $('input.clear-input').clearInput();
	  });
	})( jQuery );
	
	// implementation of tooltip
	// author john m howitt
	
	// Initialize.
	function init_tooltip() {

		// Does element exist?
		if (!$('.tooltip').length) {

			// If not, exit.
			return;
		}

		// Insert tooltip (hidden).
		$('body').append('<div id="tooltip"><div id="tooltip_inner"></div></div>');

		// Empty variables.
		var $tt_title, $tt_alt;

		var $tt = $('#tooltip');
		var $tt_i = $('#tooltip_inner');

		// Watch for hover.
		$('.tooltip').hover(function() {

			// Store title, empty it.
			if ($(this).attr('title')) {
				$tt_title = $(this).attr('title');
				$(this).attr('title', '');
			}

			// Store alt, empty it.
			if ($(this).attr('alt')) {
				$tt_alt = $(this).attr('alt');
				$(this).attr('alt', '');
			}

			// Insert text.
			$tt_i.html($tt_title);

			// Show tooltip.
			$tt.show();
		},
		function() {

			// Hide tooltip.
			$tt.hide();

			// Empty text.
			$tt_i.html('');

			// Fix title.
			if ($tt_title) {
				$(this).attr('title', $tt_title);
			}

			// Fix alt.
			if ($tt_alt) {
				$(this).attr('alt', $tt_alt);
			}

		// Watch for movement.
		}).mousemove(function(ev) {

			// Event coordinates.
			var $ev_x = ev.pageX;
			var $ev_y = ev.pageY;

			// Tooltip coordinates.
			var $tt_x = $tt.outerWidth();
			var $tt_y = $tt.outerHeight();

			// Body coordinates.
			var $bd_x = $('body').outerWidth();
			var $bd_y = $('body').outerHeight();

			// Move tooltip.
			$tt.css({
				'top': $ev_y + $tt_y > $bd_y ? $ev_y - $tt_y : $ev_y,
				'left': $ev_x + $tt_x + 20 > $bd_x ? $ev_x - $tt_x - 10 : $ev_x + 15
			});
		});
	}

	// Kick things off.
	$(document).ready(function() {
		init_tooltip();
	});
