/* 
  Resfeber plugin for autocomplete. 
  Created by Håkan W, November 2010.
  Requirements: jquery 1.4+, jquery.autocomplete.js (our modified version)
  
  Example: $("#autocomplete_textbox").ResfeberAutocomplete("flight", "se", "#dh_dest_id", "#destination_not_found");
*/
(function($){
    $.fn.ResfeberAutocomplete = function(section, site, destIdElement, notFoundElement) {
        /* This is called to get the items to display in the menu. */
		function formatItem(row, index, totalNumItems, searchTerm) {
			return row[0];
		}
		/* This is called to get the value that goes into the textfield,
		for each item in the menu. */
		function formatResult(row, index, totalNumItems, searchTerm) {
			return row[0];
		}
		
		var options = { 
            max: 30,
            width: 250, 
            minChars: 2, 
            delay: 0, 
            matchContains: true, 
            extraParams: { section: section, site: site }, 
            formatResult: formatResult, 
            formatItem: formatItem, 
            notFoundCallback:function(){ 
                $(notFoundElement).css("visibility", "visible"); 
            } 
         };
		
        return this.each(function() {
		    $(this).autocomplete("/services/global/dest_id_autocomplete.cgi", options).result(function(event, data, formatted){
	            if (data) {
	                /* Something was selected from the menu */
	                $(this).addClass("destinationSelected");
	                $(destIdElement).val(data[1]);
                    $(notFoundElement).css("visibility", "hidden");
	            }
	            else {
	                $(this).removeClass("destinationSelected");
	            }
	        });
        });
    };
})(jQuery);

/* 
  Resfeber plugin for title text on textfields. The title text will disappear when the textfield is focused or clicked.
  The title text is implemented by having a "fake" textbox that will be replaced by the real textfield
  focused or clicked.
  
  Created by Håkan W, November 2010.
  Requirements: jquery 1.4+
  
  Example: $("#autocomplete_textbox").ResfeberTitleText("this is the title text", "#fake_textbox_element");
*/
(function($){
    $.fn.ResfeberTitleText = function(fakeElement, titleText) {
        return this.each(function() {
            var realTextbox = $(this);
		    // setup the click/focus events which will remove the fake element
		    $(fakeElement).bind("focus click", function() {
		        $(this).hide();
		        realTextbox.show().focus();
		    }).text(titleText);
        });
    };
})(jQuery);
	       
/* 
  Resfeber plugin for calendars.
  
  Created by Håkan W, November 2010.
  Requirements: jquery 1.4+, date.js, jquery.datePicker.js, se/no/dk.js (translated calendar day names etc)
  
  Example: 
*/
(function($){
    $.ResfeberTravelCalendar = function(startDateTextfield, endDateTextfield, options) {
        var settings = { 
            startDate: new Date().addDays(2).asString(),    /* 2 days from today */
            endDate: new Date().addDays(7).asString(),      /* 7 days from today */
            inline: true,
            startDateButtons: undefined,    /* ID/jquery object for elements that will bring up start date calendar popup */
            endDateButtons: undefined,      /* ID/jquery object for elements that will bring up start date calendar popup */
            startDateInput: undefined,      /* input that should get the selected start date value */
            endDateInput: undefined         /* input that should get the selected end date value */
        };
        
        $.extend(settings, options);
        
        var $startDatePicker = $(startDateTextfield), $endDatePicker = $(endDateTextfield);

        var calendarOpts = { inline: settings.inline };
        if (! settings.inline) {
            // settings for popup (non-inline) calendar
            calendarOpts.createButton = false, 
            calendarOpts.verticalPosition = $.dpConst.POS_TOP;
        }

        // setup calendars
        $startDatePicker.datePicker(calendarOpts).dpSetOffset(25, 0);
        $endDatePicker.datePicker(calendarOpts).dpSetOffset(25, 0);

        if (! settings.inline) {
            // buttons that will bring up start date and end date calendars
            if (settings.startDateButtons)
                $(settings.startDateButtons).bind('click', function(){ $startDatePicker.dpDisplay() });
            if (settings.endDateButtons)
                $(settings.endDateButtons).bind('click', function(){ $endDatePicker.dpDisplay()});
        }

        // setup handlers so date fields are updated when a date has been chosen
        $startDatePicker.bind('dateSelected', function(e, selectedStartDate){
            var selectedEndDate = $endDatePicker.dpGetSelected()[0];
            if (selectedStartDate > selectedEndDate) {
                $endDatePicker.dpSetSelected(selectedStartDate.addDays(1).asString());
            }
            if (settings.startDateInput)
                $(settings.startDateInput).val($startDatePicker.dpGetSelected()[0].asString());
        })
        $endDatePicker.bind('dateSelected', function(e, selectedEndDate){
            var selectedStartDate = $startDatePicker.dpGetSelected()[0];
            if (selectedEndDate < selectedStartDate) {
                $startDatePicker.dpSetSelected(selectedEndDate.addDays(-1).asString());
            }
            if (settings.endDateInput)
                $(settings.endDateInput).val($endDatePicker.dpGetSelected()[0].asString());
        })
        
        // set preselected dates
        if (settings.startDate)
            $startDatePicker.dpSetSelected(settings.startDate);
        if (settings.endDate)
            $endDatePicker.dpSetSelected(settings.endDate);
    }
})(jQuery);


