var travel = new function(){
		
		this.buildHotelUri = function (form) {
			var urlSubmit = $(form).attr('action') + '?';
    		
    		/** Add all the hidden inputs **/
    		$('#' + $(form).attr('id') + ' :input:not(input[type=button], input[type=image])').each(function(){
    			if ( $(this).attr('id') != 'city') {
    				urlSubmit += $(this).attr('name') + '=' + $(this).val() + '&';
    			}
    		});    		
    		/** Add the splitted dates and number of occupants/rooms**/
    		dayOfCheckIn = ote.Utils.Date.splitDateString( $('#HotelSearch_chk_in').val() );
    		dayOfCheckOut = ote.Utils.Date.splitDateString( $('#HotelSearch_chk_out').val() );
    		
    		urlSubmit += "doa_mm=" + dayOfCheckIn.month;
    		urlSubmit += "&doa_dd=" + dayOfCheckIn.day;
    		urlSubmit += "&doa_yy=" + dayOfCheckIn.year;
    		
    		urlSubmit += "&dod_mm=" + dayOfCheckOut.month;
    		urlSubmit += "&dod_dd=" + dayOfCheckOut.day;
    		urlSubmit += "&dod_yy=" + dayOfCheckOut.year;
    		
    		return urlSubmit;
		};
		
		this.buildAirUri = function (form) {
			var urlSubmit = $(form).attr('action') + '?';
    		
    		/** Add all the hidden inputs **/
    		$('#' + $(form).attr('id') + ' :input:not(input[type=button],  input[type=image])').each(function(){    			
    			if($(this).attr('type') == 'radio') {
    				if($(this).attr('checked') == true) {
    					urlSubmit += $(this).attr('name') + '=' + $(this).val() + '&';
    				}	
    			}	else	{    			
	    			if($(this).attr('type') == 'checkbox' && $(this).attr('checked') == true) {
	    				urlSubmit += $(this).attr('name') + '=1&';    				
	    			}
	    			
	    			if( $(this).attr('id') != 'goingTo_combo' && $(this).attr('id') != 'leavingFrom_combo'){ 
		    			if ( $(this).attr('id') == 'leavingFrom' || $(this).attr('id') == 'goingTo') {   				
		    				urlSubmit += $(this).attr('name') + '=' + $('#' + $(this).attr('id') + '_combo').val() + '&';    				
		    			}	else	{
		    				urlSubmit += $(this).attr('name') + '=' + $(this).val() + '&';
		    			}
	    			}
    			}
    		});    		
    		
    		urlSubmit += 'aspHeader=air&aspFooter=air'; 
    	
    		return urlSubmit;
		};
		
		this.buildAirHotelUri = function (form) {
			var urlSubmit = $(form).attr('action') + '?';
    		
    		/** Add all the hidden inputs **/
    		$('#' + $(form).attr('id') + ' :input:not(input[type=button],  input[type=image])').each(function(){   			

    			if( $(this).attr('id') != 'leavingFromHotel_combo' && $(this).attr('id') != 'goingToHotel_combo'){ 
		    		if ( $(this).attr('id') == 'leavingFromHotel' || $(this).attr('id') == 'goingToHotel') {   				
		    			urlSubmit += $(this).attr('name') + '=' + $('#' + $(this).attr('id') + '_combo').val() + '&';    				
		    		}	else	{
		    			urlSubmit += $(this).attr('name') + '=' + $(this).val() + '&';
		    		}
	    		}
    			
    		});    		
    		dayOfCheckIn = ote.Utils.Date.splitDateString( $('#airHotelSearch_chk_in').val() );
    		dayOfCheckOut = ote.Utils.Date.splitDateString( $('#airHotelSearch_chk_out').val() );
    		
    		urlSubmit += "dateLeavingMonth=" + dayOfCheckIn.month;
    		urlSubmit += "&dateLeavingDay=" + dayOfCheckIn.day;
    		
    		urlSubmit += "&dateReturningMonth=" + dayOfCheckOut.month;
    		urlSubmit += "&dateReturningDay=" + dayOfCheckOut.day;
    	
    		return urlSubmit;
		};
		
		this.buildCarUri = function (form) {
			var urlSubmit = $(form).attr('action') + '?';
    		
    		/** Add all the hidden inputs **/
    		$('#' + $(form).attr('id') + ' :input:not(input[type=button],  input[type=image])').each(function(){   			

    			if($(this).attr('id') == 'FillCity') {
    				if($(this).attr('checked') == true) {
    					urlSubmit += $(this).attr('name') + '=on&';    				
    				}	else	{
    					urlSubmit += $(this).attr('name') + '=off&';
    				}
    			}
    			if( $(this).attr('id') != 'docity_combo' && $(this).attr('id') != 'pucity_combo'){ 
		    		if ( $(this).attr('id') == 'docity' || $(this).attr('id') == 'pucity') {   				
		    			urlSubmit += $(this).attr('name') + '=' + $('#' + $(this).attr('id') + '_combo').val() + '&';    				
		    		}	else	{
		    			urlSubmit += $(this).attr('name') + '=' + $(this).val() + '&';
		    		}
	    		}
 
    			
    		});    		
    		dayOfCheckIn = ote.Utils.Date.splitDateString( $('#CarSearch_chk_in').val() );
    		dayOfCheckOut = ote.Utils.Date.splitDateString( $('#CarSearch_chk_out').val() );
    		
    		urlSubmit += "pudate_mo=" + dayOfCheckIn.month;
    		urlSubmit += "&pudate_dy=" + dayOfCheckIn.year;
    		
    		urlSubmit += "&dodate_mo=" + dayOfCheckOut.month;
    		urlSubmit += "&dodate_dy=" + dayOfCheckOut.year;
    	    		
    		return urlSubmit;
		};
		
		this.selfComplete = function (elID, mode, elPopulateID) {
			$( "#" + elID ).autocomplete({
				source: function( request, response ) {			
					$.post('/ajax/travel.php', {cityName: request.term, resource: mode}, function(data, status) {
						response( $.each( data.items, function( iterator, item) {					
								if( typeof( item.name ) !=='undefined' ){							
									return {						
										label: item.name,
										value: item.name,
										save: item.value
									};
								}
							}));				
					}, 'json');
					
				},
				minLength: 2,
				select: function( event, ui ) {
					$('#' + elPopulateID).val(ui.item.storeVal);			
				},
				open: function() {
					$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
				},
				close: function() {
					$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
				}
			});
		};
		
		this.multiDestinationForm = function() {			
			return false;
		};
		
		this.initDestinations = function(elm) {			
			init_originations();
			if( $(elm).attr('id') == 'deals') {
				
				init_originations();
			}
		};
		
		this.initDate = function () {
			var nnw = 0;
						
			var orig = $('#inOriginationDateKey');
			$(orig).empty();
			var i = 0;
			for (o in originations) {
				$(orig).append($("<option></option>").attr("value", o).text(originations[o]));			
				i++;
			}
			var idx = 0;
			i = 0;
			for (k in originations) {
				if (k == 371)
					break;
		            i++;
		        }
			orig.selectedIndex = i;
		   
			
			var ds = $('#inStartDate');
			var de = $('#inEndDate');
			
			$(ds).append($("<option></option>").attr("value", '1187154000,1187240400,1187326800,1187413200').text('This weekend'));
			$(de).append($("<option></option>").attr("value", '1187154000,1187240400,1187326800,1187413200').text('This weekend'));   
			
			
			 
			if (! nnw)
				$(ds).append($("<option></option>").attr("value", '1187758800,1187845200,1187931600,1188018000').text('Next weekend'));
						 
			var i = (nnw) ? 1 : 2;
			
			for (var j = 0; j < starts.length; i++, j++) {
				$(ds).append($("<option></option>").attr("value", starts[j].t).text(starts[j].v));				
			}
			
			de.selectedIndex = 0;
		};
	};

$('document').ready(function(){	
	/** ote setup **/
	ote.Setup.setTemplateID(6);
	ote.Setup.setEnvironment("SBX");
	
	travel.initDestinations($('#deals'));
	
	travel.initDate();
	
	

	/** Tabs setup **/
	$('.triggerTab').contentTabs();
	var cache = {}, lastXhr;

	$('#inStartDate').change(function(){
		  var ds = $('#inStartDate');
			var de = $('#inEndDate');
			
			start_idx = $(ds).attr('selectedIndex');
			
			$(de).empty();
			
			if (start_idx == 0) {
				$(de).append($("<option></option>").attr("value", '1187499600,1187586000,1187672400,1187758800').text('This weekend'));
			}
			else if (start_idx == 1 && !nnw) {
				$(de).append($("<option></option>").attr("value", '1188363600,1188450000,1188536400,1188622800,1188709200,1188795600,1188882000,1188968400').text('Next weekend'));					
			}
			else {
				var e = (nnw) ? start_idx - 1 : start_idx - 2; 					
		        for (var i = 0; i < ends[e].length; i++) {
		        	$(de).append($("<option></option>").attr("value", ends[e][i].t).text(ends[e][i].v));			           
		        }
		   }
		   
		  de.selectedIndex = 0;
	});
	
	/** Bind city focus **/	
	$('input[type=text]').focus(function(){
		$(this).addClass('focused');		
	}).blur(function(){		
		$(this).removeClass('focused');
	});
	
	$('#city').focus(function(){
		if($(this).val() == 'enter city'){
			$(this).val('');
		}
	});
	
	/** Autocomplete field **/
	travel.selfComplete('city', 'Hotel', 'selected_city_combo');
	travel.selfComplete('leavingFrom', 'AIR', 'leavingFrom_combo');
	travel.selfComplete('goingTo', 'AIR', 'goingTo_combo');
	travel.selfComplete('leavingFromHotel', 'AIR', 'leavingFromHotel_combo');
	travel.selfComplete('goingToHotel', 'AIR', 'goingToHotel_combo');
	travel.selfComplete('pucity', 'AIR', 'pucity_combo');
	travel.selfComplete('docity', 'AIR', 'docity_combo');

	/** Update rules for validation **/
	$.validator.addMethod("notValue", function(value, element, param) {
	    return value !== param;
	}, "");
	
	/** Hotel tab **/
	$('#HotelSearch_chk_in').datepicker({minDate: 0, weekHeader: 'W'});
	$('#HotelSearch_chk_in').change(function(){
		if(ote.Utils.Date.compare($('#HotelSearch_chk_in').val(), $('#HotelSearch_chk_out').val)) {			
			$('#HotelSearch_chk_out').val('');		
		}
		
		$('#HotelSearch_chk_out').datepicker("option", {minDate: ote.Utils.Date.stringToObject($('#HotelSearch_chk_in').val(), '+1d'), weekHeader: 'W'});
		$('#HotelSearch_chk_out').datepicker("refresh");
	});
	$('#HotelSearch_chk_out').datepicker({minDate: ote.Utils.Date.stringToObject($('#HotelSearch_chk_in').val())});
	
	
	  /** Hotel Form validation **/
	  $("#HotelSearch").validate({ 
	    	errorPlacement: function(error, element) {
	    		$(element).removeClass('focused');
	    	},
	    	errorClass: 'warning',
	    	focusInvalid: false,
	    	rules: {
	    		city: {notValue: 'enter city'},
	    		HotelSearch_chk_out: {notValue: 'mm/dd/yy'},
	    		HotelSearch_chk_in: {notValue: 'mm/dd/yy'}
	    	},
	    	submitHandler: function(form) {
	    		/** Build the URL **/
	    		uri = travel.buildHotelUri(form);
	    		$.fn.colorbox({href: uri, iframe: true, width: 875, height: 600});
	    		return false;
	    	}

	    });
	
	/** Air tab **/
	
	  $('#AirSearch_chk_in').datepicker({minDate: 0, weekHeader: 'W'});
		$('#AirSearch_chk_in').change(function(){
			if(ote.Utils.Date.compare($('#AirSearch_chk_in').val(), $('#AirSearch_chk_out').val)) {			
				$('#AirSearch_chk_out').val('');		
			}
			
			$('#AirSearch_chk_out').datepicker("option", {minDate: ote.Utils.Date.stringToObject($('#AirSearch_chk_in').val(), '+0d'), weekHeader: 'W'});
			$('#AirSearch_chk_out').datepicker("refresh");
		});
			
	  $('#AirSearch_chk_out').datepicker({minDate: ote.Utils.Date.stringToObject($('#AirSearch_chk_in').val())});	
	
	
	/** Set up dialog **/
	$( ".advancedOptions" ).unbind('click').click(function() {
		$( "#advancedAirOptions" ).dialog({
			resizable: false,	
			draggable: false,
			modal: true,
			height: 250,
			width: 400,
			title: "More Search Options",
			buttons: {
				"Save changes": function() {
					$('<div id="airOptionsSaved">Preferences saved </div>').insertAfter($('.advancedOptions'));
					$( this ).dialog( "close" );
					
				},
				Cancel: function() {
					$(':input','#advancedAirOptions')
					 .not(':button, :submit, :reset, :hidden').each( function() {
						if(this.type == 'select-one') {
							$(this).val($("#" + $(this).attr('id') + " option:first").val());
						} else	{
							$(this).val('');
							$(this).removeAttr('checked')
							 	   .removeAttr('selected');
						}
						
						
					 });
					$('#airOptionsSaved').remove();
					$( this ).dialog( "close" );					
				}
			}
		});
		
		return false;
	});
	
	$('#AirSearchMulti').submit( function() {
		travel.multiDestinationForm();
	});
	
	/** Bind buttons **/
	$('#flightTypeMultiCity').popupWindow({height:600, width:800, resizable: 1, scrollbars: 1, left: 20, top: 20, windowURL: 'http://search.trothop.com/nexres/start-pages/gateway.cgi?Service=TPN&affiliateId=10019158&subAffiliateId=&flightType=multicity&engine=air&action=searchoptions', windowName: 'multidestination'});
	$('#flightTypeOneWay').click(function(){		
		$('#dateReturningTime').attr('disabled', 'disabled');
		$('#returnDateFlexibility').attr('disabled', 'disabled');
		$('#AirSearch_chk_out').attr('class', $('#AirSearch_chk_out').attr('class').replace('warning', ''));
		$('#AirSearch_chk_out').attr('class', $('#AirSearch_chk_out').attr('class').replace('focused', ''));
		$('#AirSearch_chk_out').attr('disabled', 'disabled');
		
	});
	
	$('#flightTypeRoundTrip').click(function(){
		$('#AirSearch_chk_out').attr('disabled', false);
		$('#dateReturningTime').attr('disabled', false);
		$('#returnDateFlexibility').attr('disabled', false);
	});
	
	
	/** children selection **/
	$('#num_childrenair').unbind('change').bind('change', function(){
		$('.minorsAge').hide();
		if( $(this).val() == 0 ) {
			$('#childrenAge').css('visibility', 'hidden');
			
		}	else	{			
			loopEnd = $(this).val();			
		
			for(i = 1; i <= loopEnd; i++) {
				$('#minorsAge' + i).show();
			}
			
			$('#childrenAge').css('visibility', 'visible');
		}	
	});
	
	$('#num_childrenairhotel').unbind('change').bind('change', function(){
		$('.minorsAge').hide();
		if( $(this).val() == 0 ) {
			$('#airHotelChildren').hide();
			
		}	else	{			
			loopEnd = $(this).val();			
		
			for(i = 1; i <= loopEnd; i++) {
				$('#minorsAgeHotel' + i).show();
			}
			
			$('#airHotelChildren').show();
		}	
	});
	
	
	$("#airSearch").validate({ 
    	errorPlacement: function(error, element) {
    		$(element).removeClass('focused');
    	},
    	errorClass: 'warning',
    	focusInvalid: false,
    	rules: {
    		leavingFrom: {required: true},
    		goingTo: {required: true},
    		leavingDate: {notValue: 'mm/dd/yy'},
    		returningDate: {notValue: 'mm/dd/yy'}
    	},
    	submitHandler: function(form) {
    		/** Build the URL **/
    		uri = travel.buildAirUri(form);	    	

    		$.fn.colorbox({href: uri, iframe: true, width: 875, height: 600});
    		return false;
    	}

    });
	
	$('#airHotelSearch_chk_in').datepicker({minDate: 0, weekHeader: 'W'});
	$('#airHotelSearch_chk_in').change(function(){
		if(ote.Utils.Date.compare($('#airHotelSearch_chk_in').val(), $('#airHotelSearch_chk_out').val)) {			
			$('#airHotelSearch_chk_out').val('');		
		}
		
		$('#airHotelSearch_chk_out').datepicker("option", {minDate: ote.Utils.Date.stringToObject($('#airHotelSearch_chk_in').val(), '+1d'), weekHeader: 'W'});
		$('#airHotelSearch_chk_out').datepicker("refresh");
	});
	$('#airHotelSearch_chk_out').datepicker({minDate: ote.Utils.Date.stringToObject($('#HotelSearch_chk_in').val())});
	
	$("#airHotelSearch").validate({ 
    	errorPlacement: function(error, element) {
    		$(element).removeClass('focused');
    	},
    	errorClass: 'warning',
    	focusInvalid: false,
    	rules: {
    		leavingFromHotel: {required: true},
    		goingToHotel: {required: true},
    		airHotelSearch_chk_in: {notValue: 'mm/dd/yy', required: true},
    		airHotelSearch_chk_out: {notValue: 'mm/dd/yy', required: true}
    	},
    	submitHandler: function(form) {
    		/** Build the URL **/
    		uri = travel.buildAirHotelUri(form);	    	
 
    		$.fn.colorbox({href: uri, iframe: true, width: 875, height: 600});
    		return false;
    	}

    });
	
	
	/** Deals selects **/	
	$('#inOriginationKey').change(function(){
		change_origination($(this).val());
	});
	
	$('#inDestFromKey').change(function(){
		change_state();
	});
	
	$('#dealsDestinationFormTrigger').click(function(){
		$('.dealsDateForm').hide();
		$('.dealsDestinationForm').show();
	});
	
	$('#dealsDateFormTrigger').click(function(){		
		$('.dealsDestinationForm').hide();
		$('.dealsDateForm').show();
	});
	
	/** Cars **/
	$('#FillCity').click(function(){
		if ($(this).attr('checked') == true ) {
			$('#docity').attr('disabled', true);
		}	else {
			$('#docity').attr('disabled', false);
		}	
			
	});
	
	 $('#CarSearch_chk_in').datepicker({minDate: 0, weekHeader: 'W'});
		$('#CarSearch_chk_in').change(function(){
			if(ote.Utils.Date.compare($('#CarSearch_chk_in').val(), $('#CarSearch_chk_out').val)) {			
				$('#AirSearch_chk_out').val('');		
			}
			
			$('#CarSearch_chk_out').datepicker("option", {minDate: ote.Utils.Date.stringToObject($('#CarSearch_chk_in').val(), '+0d'), weekHeader: 'W'});
			$('#CarSearch_chk_out').datepicker("refresh");
		});
			
	  $('#CarSearch_chk_out').datepicker({minDate: ote.Utils.Date.stringToObject($('#CarSearch_chk_in').val())});	
	  
	  $("#carSearch").validate({ 
	    	errorPlacement: function(error, element) {
	    		$(element).removeClass('focused');
	    	},
	    	errorClass: 'warning',
	    	focusInvalid: false,
	    	rules: {
	    		pucity: {required: true},	    		
	    		CarSearch_chk_in: {notValue: 'mm/dd/yy', required: true},
	    		CarSearch_chk_out: {notValue: 'mm/dd/yy', required: true}
	    	},
	    	submitHandler: function(form) {
	    		/** Build the URL **/
	    		uri = travel.buildCarUri(form);	    	
	    	
	    		$.fn.colorbox({href: uri, iframe: true, width: 875, height: 600});
	    		return false;
	    	}

	    });
	  
	
	/** Cruises **/
	$('.linkOption').each(function(){
		$(this).change(function(){			
			var toLocation = $(this).val();
			var windowLabel = 'cruiseSearchBy' + $(this).attr('name') + $(this).attr('selectedIndex');		
			$('<a id="trigger' + $(this).attr('id') + '" href="'+ toLocation+'" name="'+windowLabel+'" ></a>').insertAfter($(this));
			$('#trigger' + $(this).attr('id')).popupWindow({
									height:650,
									width:450,
									resizable: 1,
									left: 20,
									top: 20,									
									toolbar: 1,
									status: 1
								});
			$('#trigger' + $(this).attr('id')).click();
		});
	});
	
	$('.oteWindow').click(function(){
		ote.Utils.popup($(this));
		return false;
	});
	
});
