/** 
 * @author Milovan, 22.7.2008
 */

/**
 * airportBox::
 * el - element to initialize;
 */
$.airportBox = function(opts) {
	this.init(opts);
}
$.airportBox.prototype = {
	/**
	 * initialize airportBox:
	 */
	init : function(opts) {
		this.form 	= $("#quickReservationForm");			
		this.el		= opts.el;
		this.titleEl= this.el.find('h4');	
		this.titleEl.html('Enter flight details:');
		this.list	= this.el.find('ul');	
		var html 	=
					"<li>"
						+ "<label for='pickup.airline'>Airline&nbsp;*&nbsp;:</label>"
						+ "<div class='floatRight'>"
							+ "<div class='cmbWrapper cmbAirlineCode'>"
								+ "<a class='cmbArrow' href='#' style='z-index:1000 !important;'></a>"
								+ "<input type='text' class='cmbInputBox input' tabindex=2 name='pickup.airline'/>"
								+ "<div class='cmbSuggestionBox'><div class='boxUpperShadow'></div><div class = 'cmbSuggestionListHolder'><div class='ml'></div><div class='mm'>"
								+ "<ul class='cmbSuggestions'></ul></div><div class='mr'></div></div><div class='boxBottomShadow'><div class='bl'></div><div class='bb'></div><div class='br'></div></div>"
								+ "<!--[if lte IE 6.5]><iframe></iframe><![endif]--></div>"
							+ "</div>"
						+ "</div><div style='clear:both'></div>"
					+"</li>"
					+"<li>"
						+ "<label for='pickup.flight'>Flight #* :</label>"
						+ "<input type='text' class='input' tabindex=3 name='pickup.flight' />"
					+"</li>"
					+"<li>"
						+ "<label for='pickup.arrivingFrom'>Arriving from :</label>"
						+ "<input type='text' class='input' tabindex=4 name='pickup.arrivingFrom' />"
					+"</li>"
					+"<li>"
						+ "<label for='pickup.terminal'>Terminal :</label>"
						+ "<input type='text' class='input' tabindex=5 name='pickup.terminal' />"
					+"</li>"
					+"<li>"
						+"<label for=''>Meeting area:</label>"
						+"<select tabindex=6 name='pickup.inOut'>"
							+"<option value='true'>Inside</option>"
							+"<option value='false'>Outside</option>"
						+"</select>"
						+"<p style='padding:0px;font-size:11px;color:#777;float:right;text-align:right;width:100%;'>Inside: Driver Meets at Baggage claim</p>"
						+"<p style='padding:0px;font-size:11px;color:#777;float:right;text-align:right;width:100%'>Outside: Driver Curbside, no fee</p>"
					+"</li>"
					+"<li>"
						+"<p style='clear:both'>NOTE:"
						+"LimoRes tracks your flight to help prevent unnecessary Waiting Time. We will then dispatch your car based on Gate Arrival Time.</p>"
					+"</li>"
					+"<li>"
						+"<a href='#' tabindex=7 class ='cmbWidgetButton'>OK</a>"
					+"</li>";
		this.list.html(html);
		
		/**
		 * Initialize various element variables::
		 */
		this.button 		= this.list.find('a.cmbWidgetButton');
		this.infoLabel		= $('#cmbAirportPickupInfo');
		
		this.txtFlightId 	= this.list.find("input[name='pickup.flight']");
		this.txtAirline		= this.list.find("input[name='pickup.airline']");
		this.txtArrivingFrom= this.list.find("input[name='pickup.arrivingFrom']");
		this.optMeetingArea	= this.list.find("select[name='pickup.inOut']");
		this.txtTerminal	= this.list.find("input[name='pickup.terminal']");
		
		/**
		* Init airport combo::
		*/
		this.cmbAirline = new $.cmbBoxSimple({
			  el : "input[name=pickup.airline]"
			, emptyText			: 'Enter airline'
			, store : {
				  mode	: 'remote'
				, urlSuggest	: ajaxUrls.airline.urlSuggest
			  }
			, displayHiddenValue : true
		});

		
		
		
		/**
		 * initialize events:
		 */ 
		this.initEvents();
		
		
		
	}
	/**
	 * render this::
	 */
	,render : function () {
		this.expand();
		this.initValidation();
		/*
		* Set Value of airline - SPECIFIC to google::
		*/
		/*this.txtAirline.val('b6');*/
		this.el.find('input:first').focus();
		
	}
	
	
	/**
	 * Get element
	 */
	,getEl : function() {
		return this.el;
	}
	
	/**
	 * Init events for airportBox::
	 */
	,initEvents : function () {
		var $this = this;
		this.button
			.click(function(){
				$this.onClickOK();
				return false;
			})
		this.el
			.keydown(function(e){
				switch(e.keyCode) {
					case 13:
						$this.onClickOK();
						return false;
					  break;
				}
			})

	}
	
	
	/**
	* on Click OK button
	*/
	,onClickOK : function() {
		if(this.isValid()) {
			this.collapse();
			this.sendAirportInfo();
			/**
			 * Try to focus next input/select element::
			 */
			var tabindex = parseInt(this.button.attr('tabindex'));
			$('form#quickReservationForm select:visible, form#quickReservationForm input:visible ').filter(function(){
				return (parseInt($(this).attr('tabindex'))==tabindex+1 );
			}).focus();
			
		}
	}
	
	
	/**
	 * Send airport info to server::
	 */
	,sendAirportInfo : function() {
		var $this = this;
			$.ajax({
				type	: "GET"
			  , url		: ajaxUrls.pickup.urlFlight.url
			  , dataType: "json"
			  , cache	: false
			  , success	: function(a){}
			  , error	: function() {} 
	
			  , data	: {
					'pickup.model.airline'				: $this.form.find('input[name=pickup.airline]').val()
				  , 'pickup.model.flight'				: $this.form.find('input[name=pickup.flight]').val()
				  , 'pickup.model.arrivingFrom'			: $this.form.find('input[name=pickup.arrivingFrom]').val()
				  , 'pickup.model.meetingInside'		: $this.form.find('select[name=pickup.inOut]').val()
				  , 'pickup.model.terminal'				: $this.form.find('input[name=pickup.terminal]').val()
			  }
			});
	}
	
	/**
	* return if airportBox is valid::
	*/
	,isValid : function() {
		//MUST BE CODED LIKE THIS: (because when you type "return V1 & V2" and than  if first condition is false, other is not calculated!!
		var value1 = this.form.validate().element(this.txtFlightId);
		var value2 = this.form.validate().element(this.txtAirline);
		return value1 && value2;
	}
	
	
	
	/**
	 * Inits validation for fields::
	 */
	,initValidation : function() {
		this.txtFlightId.rules('add',
			{
				  required: true
				, number	: true
			}
		);
		this.txtAirline.rules('add',	
			{
				  required	: true
				, maxlength	: 3
			}
		);
	}	
	
	/**
	 * Clears validation rules(usually do this when collapsing an element)::
	 */	
	,clearValidation : function () {
		this.txtFlightId.rules('remove');
		this.txtAirline.rules('remove');
	}
	
	/**
	 * Renders airline info label and displays below field::
	 */
	, renderInfoLabel : function() {
		
		var $this = this;
		
			var values = {
				  txtAirline		: $this.txtAirline.val()
				, txtFlightId		: $this.txtFlightId.val()
				, txtArrivingFrom	: $this.txtArrivingFrom.val()
				, txtTerminal		: $this.txtTerminal.val()
				//, optMeetingArea	: $this.optMeetingArea.find('option[selected]').text()
				, optMeetingArea	: $('select[name=pickup.inOut] option[selected]').text()
			}

		var html = 		"<a href='#' id='cmbPickupFlightDetailsLabel'>Flight details:</a> "
					+	"Airline code "	+ values.txtAirline 	+ ', '
					+	"Flight # "	+ values.txtFlightId 	+ ', '
					+	(values.txtArrivingFrom	? ' Arriving from ' + values.txtArrivingFrom 	+ ', '	:'')
					+	(values.txtTerminal		? ' Terminal ' 		+ values.txtTerminal 		+ ', '	:'')
					+	"Meeting area " + values.optMeetingArea;	

		this.infoLabel
				.html(html)
				.show();
		
		/**
		 * init event:: 
		 */
		var $this = this;
		$("a#cmbPickupFlightDetailsLabel").click(function() {
			$this.render();
			$this.clearInfoLabel();
			return false;
		});
		 
	}
	
	/**
	 * Clears and hides info label below input field::
	 */
	, clearInfoLabel : function() {
		/**
		 * Delete element and remove event handlers::
		 */
		$("a#cmbPickupFlightDetailsLabel").unbind();
		this.infoLabel.hide();
	}
	
	/**
	* clear field values::
	*/
	, clearFields : function() {
		this.el.find('input').val('');			
		this.el.find('option:selected').removeAttr('selected');

		//assign "outside" pickup as selected::
		this.optMeetingArea.find('option[value=false]').attr('selected','true');
	}
	
	
	
	/**
	 * checks whether element is showed or not
	 */
	,isExpanded		: function() {
		return (this.el.css('display')!='none');
	}

	/**
	 * Expands element!
	 */
	,expand			: function(clearFields) {

		if(this.isExpanded()){
			return;
		}
		/**
		 * Reset pickup field values::
		 */
		this.el.show();

	}
	
	/**
	 * Collapse element (clear validation rules, ALSO!!)::
	 */
	,collapse		: function() {

		if(!this.isExpanded()){
			return;
		}
		this.clearValidation();
		this.renderInfoLabel();
		this.el.hide();
	}
	
}




/**
 * areaBox::
 */

$.areaBox = function(){
	this.init();
}

$.areaBox.prototype = {
	/**
	 * Initialize areaBox
	 */
	init			: function() {
		this.form 	= $("#quickReservationForm");			
	 	//this.html 	= "<div id='cmbAreaList' class='cmbAddrBox'><div class='baloonArrow'/><div class='cmbAddrExplanation'>Select one area from a list:</div><ul class='cmbAddr'></ul></div>"; 
		this.txtHour= $('input#txtByTheHour');
		//this.txtHour.after(this.html);
		this.el		= $('#cmbDestinationWidget');
		this.titleEl= this.el.find('h4');	
		this.list	= this.el.find('ul');
	}
	
	
	/**
	 * renders list!!
	 * @param {Object} listData
	 */ 
	,renderList		: function(listData) {
		if (listData && listData.results) {
			var li = '';
			/*var a = {
				"results": [{
					"minimumHours": 2,
					"name": "Manhattan",
					"type": 3
				}]
			}*/
			var el = this.el;
			$.each(listData.results, function(index, j){
				li += 
					"<li value='" + j.type + "' minimumHours="+j.minimumHours+">" 
						+'<input type="radio" name="'+el.attr('id')+'Radiogroup" id="'+el.attr('id')+'Radio-'+j.type+'"/>' 
						+'<label for="'+el.attr('id')+'Radio-'+j.type+'">'+j.name+'</label>'					
					+"</li>";
			});
			this.list.html(li);
			this.titleEl.html('Select one area from a list:');
			this.initListEvents();
		}	
	}
	
	/**
	 * handles onclick event!! adds minimum hours and type to txtByTheHour input element
	 * @param {Object} minimumHours
	 * @param {Object} type
	 */
	,onViewClick : function() {
		var li = this.list.find("li.hover:first");
		
		var minimumHours 	= li.attr('minimumHours');
		var type			= li.attr('value');
		
		this.txtHour.rules('remove');
		this.txtHour.rules('add', {
			    	required: true,
			    	min: minimumHours
			})
		this.txtHour.data('type',type);
		this.collapse();
		this.txtHour.focus();			
	}
	
	/**
	 * Initialize List events::
	 */
	,initListEvents	: function() {
		this.listItems = this.list.find("li");
		var $this=this;
		this.listItems
			.hover(function(){
				$(this).addClass('hover');
				$(this).find('label').addClass('hover');
			  }
			, function(){
				$(this).removeClass('hover');
				$(this).find('label').removeClass('hover');
			}
		)
		.click(function() {
			$this.onViewClick();
		});
	}

	/**
	 * checks whether element is showed or not
	 */
	,isExpanded		: function() {
		return (this.el.css('display')!='none');
	}

	/**
	 * Expands element!
	 */
	,expand			: function() {
		if(this.isExpanded()){
			return;
		}
		this.el.show();
	}
	
	/**
	 * Collapse element::
	 */
	,collapse		: function() {
		if(!this.isExpanded()){
			return;
		}
		this.el.hide();
	}
}//areaBox


/**
 * InputBoxes::
 */
var inputBoxes = {
	
	/**
	 * Handles inputboxes initialization
	 */		
	init: function() {

		var $this = this;
		/**
		 * Initialize combos!
		 */
		 
		this.airportBox = new $.airportBox({
			 el: $('#cmbPickupWidget')
		})
		 
		this.cmbBoxPickup = new $.cmbBox({
			  el : "input#txtPickup"
			, airportBox : $this.airportBox
			, emptyText			: 'Enter Address, Airport, or Landmark'
			, store : {
				  mode	: 'remote'
				 
				/**
				 * This needs for proper forming of AJAX urls, like::
				 * suggest-address.html?query=....
				 */  
				, urlAddresses	: ajaxUrls.pickup.urlAddresses
				, urlValidate	: ajaxUrls.pickup.urlValidate
				, urlSelect		: ajaxUrls.pickup.urlSelect
	  		  }
		});
		
		/*
		* initialize comboDestination box::
		*/
		this.cmbBoxDestination = new $.cmbBox({
			  el : "input#txtDestination"
			, emptyText			: 'Enter Address, Airport, or Landmark'
			, store : {
				  mode	: 'remote'
				 
				/**
				 * This needs for proper forming of AJAX urls, like::
				 * suggest-address.html?query=....
				 */  
				, urlAddresses	: ajaxUrls.destination.urlAddresses
				, urlValidate	: ajaxUrls.destination.urlValidate
				, urlSelect		: ajaxUrls.destination.urlSelect
	  		  }
		});
		
		
		this.areaBox = new $.areaBox();
		
		
		this.txtByTheHour 		= $('input#txtByTheHour');
		this.switchArrows		= $('a#cmbSwitchArrows');
		this.form				= $("#quickReservationForm");

		this.hourLink			= $('span#spHour');
		this.destinationLink	= $('span#spDestination');

		this.defaultHourValue= 'Enter number of hours'
		this.defaultDestValue= 'Enter Address, Airport, or Landmark'
	
		/**
		 * Initialize elements needed for fading::
		 */
		this.txtDestinationArrow = $('#txtDestinationWrapper').find('.cmbArrow');
		this.spAddrExample	= $('#txtDestinationWrapper').find('.cmbAddrExample');
		
		/**
		 * Initialize byTheHour::
		 */
		this.txtByTheHour
			.val($this.defaultHourValue)
			.addClass('defaultText');
		
		/**
		* Initialize events and validation
		*/
		this.initValidation();
		this.initEvents();
		
		

	}


	,getStatus : function(){
		return this.isByTheHourActive;
	}
	
	,setStatus : function(newStat){
		this.isByTheHourActive = newStat;
	}
	
	//returns form
	,getForm : function() {
		return this.form;
	}
	
	//returns cmbBoxPickup
	,getCmbBoxPickup : function() {
		return this.cmbBoxPickup;
	}
	
	
	//returns cmbBoxDestination
	,getCmbBoxDestination : function() {
		return this.cmbBoxDestination;
	}
	
	//returns airportBox
	,getAirportBox : function() {
		return this.airportBox;
	}
	
	/**
	 * This is called ONLY from addressBean.jsp when user comes back from another page
	 * this FORCES hour component to be initialized:: 
	 */
	,initHourComponent : function(params) {
			//switch to bth control:
			this.onHourClick(this);
			this.txtByTheHour.val(params.value)
				.data('type',params.type)
				.focus();
				
			//this.onHourFocus.call(this,this);
			this.txtByTheHour.data('type',null);
	}


	
	/**
	 * get Empty text for By the hour
	 */		
	,getBthEmptyText:function() {
		return this.defaultHourValue;
	}

	/**
	 * Initiazizes validation
	 */
	,initValidation:function() {

		var $this = this;
		
		/**
		 * initalizing validation::
		 */
		$.validator.addMethod('isNotEmptyText', function(value, element, param)  { 
			return value!=param[0];
		}, 'This field is required.');
		
		this.destinationRules = {
			required : true
			,isNotEmptyText : [$this.cmbBoxDestination.getEmptyText()]
		}

		this.pickupRules = {
			required : true
			,isNotEmptyText : [$this.cmbBoxPickup.getEmptyText()]
		}
		
		this.byTheHourRules = {
			required : true
			,isNotEmptyText : [$this.getBthEmptyText()]
			,number  : true
		}
		
		this.form.validate({
			errorClass : 'reservationError'
			,focusInvalid	: true
			,rules: {
				/*txtByTheHour : {
					required : true
					,number	 : true
				}*/
				'txtDestination' 	: $this.destinationRules
				,'txtPickup' 		: $this.pickupRules
				//,"txtByTheHour"			: $this.byTheHourRules
			}
		});	
		
		$this.cmbBoxPickup.getEl().rules('add', $this.pickupRules);
		if(!$this.getStatus()){
		$this.cmbBoxDestination.getEl().rules('add', $this.destinationRules);
		}
	}
	
	/**
	 * initializes all events for fields::
	 */
	,initEvents : function() {
	
		var $this = this;
		
		/**
		 * Initialize switch arrows event::
		 */
		this.switchArrows
			.click( function(){
				$this.onArrowClick($this);
		});
		
		/**
		 * handles click on hour link::
		 */
		this.hourLink.parent().click(
			function(){
				$this.onHourClick($this);
			}
		);

		
		/**
		 * initialize byTheHour txt events:
		 */
		this.txtByTheHour
			.focus(function(){
					$this.onHourFocus.call(this,$this);
				})	
			.blur(function(){
					$this.onHourBlur.call(this,$this);
				});
				
		/**
		 * Prevent submitting default value of byTheHour field::
		 */
		this.form
			.submit(function(){
				if($this.txtByTheHour.val() == $this.getBthEmptyText()) {
					$this.txtByTheHour.val('');
				}
			});	
	} //initEvents

	/**
	* Mark field as valid!
	*/
	,markValid : function() {
		this.txtByTheHour.addClass('valid');
	}

	/**
	* Clear valid mark::
	*/
	,clearValid : function() {
		this.txtByTheHour.removeClass('valid');
	}
	
	
	/**
	* trigger on Blur
	*/
	,onHourBlur : function($this) {
		
		
		 if ($this.form.validate().element( $(this))) {
			/**
			 * element has not blank value, and validate is successfull
			 * reset validation::
			 */

			$this.markValid();
			$this.initValidation();
			
			/**
			 * send ajax call for pricing 
			 */
			var ajaxObj = {
				type	: "GET"
			  , url		: ajaxUrls.byTheHour.urlValidate
			  , dataType: "json"
			  , cache	: false
			  , success	: function(a){
			  }
			  , error	: function() {} 
	
			  , data	: {
					duration 	: $this.txtByTheHour.val()
				  , type		: $this.txtByTheHour.data('type')	
			  }
			} 			
			//ajaxObj.data[duration] = $this.val();
			$.ajax(ajaxObj);

			} else if($(this).val()=='') {
			/**
			 * if element has blank value, reset it...
			 */
			$(this).attr('maxlength',50)
			$(this).val($this.defaultHourValue).addClass('defaultText');
			
		}
		
		/**
		* Reset  validation type...
		**/
		$(this).data('type',null);

	}
	
	
	/**
	 * provide AJAX call etc. on hour focus
	 */
	,onHourFocus : function($this) {
		
		
		
		$(this).removeClass('defaultText');
		$this.clearValid();
		
		if ($(this).val() == $this.defaultHourValue) {
			$(this).attr('maxlength', 2)
			$(this).val('');
		}
		
		//$this.form.validate().element($(this));
		
		//If data rules is NOT set, trigger default AJAX action for focus::
		if(isNaN(parseInt($(this).data('type')))) {

			if ($this.ajaxCall) {
				$this.ajaxCall.abort();
			}
			var ajaxObj = {
				type: "GET",
				url: ajaxUrls.byTheHour.urlInit, 
				dataType: "json",
				cache: false,
				success: function(a){
					/**
					 * Add validation to txtByTheHour
					 */
					if(a.results && a.results.length && a.results.length>1) {
						/**
						 * render Manhattn issue!
						 */
						$this.areaBox.renderList(a);
						$this.areaBox.expand();
					} else  if(a.results && a.results.length) {
						/**
						* init validation for txtByTheHour:
						*/
						$this.txtByTheHour.rules('add', {
							required: true,
							min: a.results[0].minimumHours,
							number: true
						});
						$this.txtByTheHour.data('type', a.results[0].type);
					}
				},
				error: function(){
				
				},
				data: {}
			}
			
			$this.ajaxCall = $.ajax(ajaxObj);
		
		}

	} //onHourFocus

	
	/**
	 * handles click on By the hour section
	 */
	,onHourClick : function($this) {

		//remove validation rules::
		$this.txtByTheHour.rules('remove');
		$this.cmbBoxDestination.getEl().rules('remove');
		$this.setStatus(true);
		
		$this.txtByTheHour.rules('add',$this.byTheHourRules);
		
		$this.destinationLink.wrap("<a href='#'></a>").parent()
			.click(function(){
				$this.onDestinationClick($this);
			});
		
		/**
		 * this weird code is used to remove link:: 
		 */
		$this.hourLink.parent().after($this.hourLink).remove();
		
		//hide stuff::
		//$this.txtDestinationArrow.hide();
		$this.switchArrows.addClass('notShown').unbind('click');
		//$this.spAddrExample.hide();
		$this.cmbBoxDestination.getEl().hide();
		$this.cmbBoxDestination.applyEmptyText();
		$("label.reservationError[for='txtDestination']").hide(function(){$(this).remove();});

		//show stuff::
		$this.txtByTheHour.show();
		$(this).val($this.defaultDestValue).addClass('defaultText').removeClass('addrFound').removeClass('addrNotFound');

		return false;			
	}//onHourClick
	

	
	
	/**
	 * handles click on destination link
	 */
	,onDestinationClick : function($this) {
		
		//arrange validation rules::
		$this.cmbBoxDestination.getEl().rules('add', $this.destinationRules);
		$this.txtByTheHour.rules('remove');
		$this.setStatus(false);
		$this.cmbBoxDestination	.clearUndoLabel();
		
		
		//abort ajax call::
		if ($this.ajaxCall) {
			$this.ajaxCall.abort();
		}


		$this.hourLink.wrap("<a href='#'></a>").parent()
			.click(function(){
				$this.onHourClick($this);
			});
		
		/**
		 * this weird code is used to remove a link::
		 */
		$this.destinationLink.parent().after($this.destinationLink).remove();
		
		// hide stuff:
		$this.txtByTheHour.removeClass('reservationError').hide().attr('maxlength',50).val($this.defaultHourValue).addClass('defaultText');;
		$("label.reservationError[for='txtByTheHour']").hide(function(){$(this).remove();});

		//show stuff::
		$this.cmbBoxDestination.getEl().show();
		//$this.spAddrExample.show();
		//$this.txtDestinationArrow.show();
		$this.switchArrows.removeClass('notShown')
			.click(function(){
				$this.onArrowClick($this);
		});
			
		return false;
	}//onDestinationClick



	/**
	 * Arrow click::substitute values from destination and pickup
	 */
	,onArrowClick : function($this) {

		/**
		* handler for two arrows between  combos::
		*/
		if(($this.cmbBoxPickup.getRawValue()!='' || $this.cmbBoxDestination.getRawValue()!='')) {
			
			//clear info label for pickup::
			$this.cmbBoxPickup.airportBox.clearInfoLabel();
			$this.cmbBoxPickup		.clearUndoLabel();
			$this.cmbBoxDestination	.clearUndoLabel();
		
			/**
			 * Get pickup values and classes::
			 */			
			var pickupClasses = {
				  addrNotFound	: $this.cmbBoxPickup.getEl().hasClass('addrNotFound') 
				, addrFound		: $this.cmbBoxPickup.getEl().hasClass('addrFound')
			}
			var pickupValues = {
				  text	: $this.cmbBoxPickup.getRawValue()
				, value	: $this.cmbBoxPickup.getValue()
			}

			var ajaxObj = {
					type: "GET",
					url: ajaxUrls.reverseAddress.url, 
					dataType: "json",
					cache: false,
					success: function(a){
						/**
						 * Set pickup values ::
						 */
						$this.cmbBoxPickup.setValue({
							  text	: $this.cmbBoxDestination.getRawValue()
							, value	: $this.cmbBoxDestination.getValue()
						});
			
						/**
						 * Set destination values::
						 */
						$this.cmbBoxDestination.setValue(pickupValues);
	
						/**
						* switch classes::
						*/
						$this.cmbBoxPickup.getEl().removeClass('addrFound').removeClass('addrNotFound');			
						$this.cmbBoxDestination.getEl().removeClass('addrNotFound').removeClass('addrFound');
					},
					error: function(){
					
					},
					data: {}
				}
			
			$this.ajaxCall = $.ajax(ajaxObj);

			/**
			 * apply default value, if needed: 
			 */
			if($this.cmbBoxDestination.getRawValue() ==''){
				$this.cmbBoxDestination.applyEmptyText();
			}

			if($this.cmbBoxPickup.getRawValue() ==''){
				$this.cmbBoxPickup.applyEmptyText();
			}
						
		}
	} //onArrowClick
}

$(function(){

/**
 * This is main init():::
 */

var init = function(){


	/**
	 * Expand on click *:: 
	 */
	$('a.exclusive-offer').click(function(){
		$('p#rates-notice').fadeIn();
		return false;
	});
	
	/**
	 * Collapse on click on close button::
	 */
	$('p#rates-notice a#close-link').click(function(){
		$('p#rates-notice').fadeOut();
		return false;
	})



	/**
	 * Close Button assign event::
	 */
	$('#minibooker div.cmbAddrBox a.close-button').click(function(){
		var $noneOfAbove = $('div.cmbAddrBox:visible ul li[none=true]');
		$noneOfAbove.triggerEvent('mouseover');
		$noneOfAbove.triggerEvent('click');
	})


	/**
	 * Close button - Airport box - assign event:
	 */
	$('#minibooker div#cmbPickupWidget.cmbAddrBox a.close-button').click(function(){
		//$('#minibooker div#cmbPickupWidget.cmbAddrBox').hide();
		
		//Trigger click on OK button: this is a workaround for not-enterign right results for airport pickup:
		$('#minibooker div#cmbPickupWidget.cmbAddrBox a.cmbWidgetButton').triggerEvent('click');
		return false;
	});
	
	/**
	 * Close button - Destination "By the hour" issue:
	 */
	$('#minibooker #cmbDestinationWidget.cmbAddrBox a.close-button').click(function(){
		$('#minibooker div#cmbDestinationWidget.cmbAddrBox').hide();
		return false;
	});
	
	
	
	/**
	 * init reservation combos::
	 */
	inputBoxes.init();
	

	/**
	 * DatePicker init::
	 */
	$.datePicker.setDateFormat("mm/dd/yyyy");
	$("input.date").datePicker();
	$("input.date").attr("autocomplete", "on");
	
	
	
	/**
	 * Init special instructions::
	 */
	var $form = $("#quickReservationForm");
	var defaultValue = 'Insert stops here';
	var $specialInstructions = $('input#specialInstructions');
	if (!$specialInstructions.val().length || $specialInstructions.val() == defaultValue) {
		$specialInstructions.addClass('defaultText').val(defaultValue)
	}
	$specialInstructions.focus(function(){
		$(this).removeClass('defaultText');
		if ($(this).val() == defaultValue) {
			$(this).val('');
		}
	}).blur(function(){
		$(this).addClass('defaultText')
		if ($(this).val() == '') {
			$(this).val(defaultValue);
		}
	});
	
    
    $form.submit(function(){
		
		var isValidationFinished = function() {
			return 		!inputBoxes.getCmbBoxDestination().ajaxValidationCall	// ensure that validation calls   
					&&	!inputBoxes.getCmbBoxPickup().ajaxValidationCall		// ..are finished!!
		}
		
		
		/**
		 * First, make sure that form is  validated::
		 */
		if(	$form.validate().form()) { 
			
			/**
			 * then ask if validation is finished::
			 */		
	        if (isValidationFinished()) 
			{
				/**
				 * Correct specialInstructions field behaviour::
				 */
	            if ($specialInstructions.val() == defaultValue) {
	                $specialInstructions.val('');
	            }
				
				/**
				 * send airportInfo::
				 */
	            if (inputBoxes.airportBox.isExpanded() == true) {
	                inputBoxes.airportBox.sendAirportInfo();
	            }
	            
	            /**
	             *	Show overlay box
	             **/
	            $('#progressWin').show();
	            $('#progressWin').modal({
	                onClose: progressBar.close,
	                overlay: 1
	            });
	            setTimeout('document.images["loading"].src = document.images["loading"].src', 1);
	        } else  {
				
				/**
				 * highlights pickup & destination, if validation AJAX call is active::
				 */
				if(inputBoxes.getCmbBoxPickup().ajaxValidationCall) {
					inputBoxes.getCmbBoxPickup().largeWrapper.highlight();
				}

				if(inputBoxes.getCmbBoxDestination().ajaxValidationCall) {
					inputBoxes.getCmbBoxDestination().largeWrapper.highlight();
				}
				
				
				
				/**
				 * Checks repeatedly if validation is finished 
				 */
				/**var id;
				var checkValidationFinish = function() {
					if(isValidationFinished()) {
						clearInterval(id);
						$form.submit();
					}
				}
				
				id = setInterval(checkValidationFinish, 50) */
				return false;
			}
		}
    });
	

	
	
	
	//pricingGallery.init();


	
	
}();	

})


