var SEARCH_WIZARD = {

    destinationLayer: {

        initialize: function () {
            /* Some code regarding to the accessibility - will be implemented later*/
            jQuery(".destinationLayerContainer ~ .nonJavascriptVersion").remove();
            jQuery(".destinationLayerInputField").show();
        },

        bindEvents: function () {
            // if the field got focus, let's display the destination layer
            jQuery(".destinationLayerContainer input, div.swArrowIconWrapper").focusin(function () {
				jQuery(".ui-autocomplete-input").removeClass("swIcon swErrorIcon");
				jQuery("#autoSuggestErrorMessage").removeClass("visible");
				jQuery(".sspSucheBlueButton, .sspSucheWhiteButton, #search_button1")
						.removeAttr( "disabled" )
						.css("opacity", "1" );
                jQuery(".leftSearchPanel").addClass("overflowVisible");
                jQuery(this).parents(".destinationLayerContainer").children(".destinationLayer").stop(true, true).slideDown();
            });

            // do not let type into the field
            jQuery(".destinationLayerContainer input").keypress(function (event) {
                return false;
            })

            // closing the destination layer
            jQuery(".swCloseIcon").click(function () {
                jQuery(this).parents(".destinationLayer").stop(true, true).fadeOut('400', function () {
                    jQuery(".leftSearchPanel").removeClass("overflowVisible");
                });
            });
			
			jQuery("#resortSelect").change(function(){
				jQuery(".ui-autocomplete-input").removeClass("swIcon swErrorIcon");
				jQuery("#autoSuggestErrorMessage").removeClass("visible");
				jQuery(".sspSucheBlueButton, .sspSucheWhiteButton, #search_button1")
						.removeAttr( "disabled" )
						.css("opacity", "1" );
				var selectedResort=jQuery("option:selected", this).text();
				if(selectedResort != null)
				{
					jQuery(".ui-autocomplete-input").attr("value", selectedResort.replace(/^\s+|\s+$/g, ''));
				}
			});
			
			jQuery("#destinationSelect").change(function(){
				jQuery(".ui-autocomplete-input").removeClass("swIcon swErrorIcon");
				jQuery("#autoSuggestErrorMessage").removeClass("visible");
				jQuery(".sspSucheBlueButton, .sspSucheWhiteButton, #search_button1")
						.removeAttr( "disabled" )
						.css("opacity", "1" );
				jQuery(".ui-autocomplete-input").attr("value", "");
			});

            var showSelectedItem = function (obj) {

                var selected = obj.parent(".destinationLayer").find("input:checked");
                var selectedDestinationText = jQuery.trim(selected.parents("label").text());
                var selectedDestinationId = selected.attr("id");

                jQuery("#autoSuggestText, #hotelAutoSuggestText, #pkgAutoSuggestText, #lmAutoSuggestText").attr("value", selectedDestinationText);

                switch (selectedDestinationId) {
                    case "0|F": 
                    	jQuery("#resortSelect").attr('disabled', 'disabled');  
                    	jQuery("#autoSuggestText, #hotelAutoSuggestText, #pkgAutoSuggestText, #lmAutoSuggestText").attr("value", "");
                        break;
                    case "1|F": 
                    	jQuery("#resortSelect").attr('disabled', 'disabled'); 
                    	jQuery("#autoSuggestText, #hotelAutoSuggestText, #pkgAutoSuggestText, #lmAutoSuggestText").attr("value", "");
                        break;
                    case "2|F": 
                    	jQuery("#resortSelect").attr('disabled', 'disabled'); 
                    	jQuery("#autoSuggestText, #hotelAutoSuggestText, #pkgAutoSuggestText, #lmAutoSuggestText").attr("value", "");
                        break;
                    default: jQuery("#resortSelect").removeAttr('disabled');
                        	 jQuery("#resortSelect").removeAttr('style');
                }

				// jQuery(".destinationLayerContainer input").attr("value", selectedDestinationText);
                /*
                * Functionality must be clarified
                var inputText = "Top Reizeziele";

                if ( selectedDestinationText != "beliebig" ) {
                inputText = selectedDestinationText;
                }
                */
                jQuery(".destinationLayerContainer input[id=topDestionation]").attr("value", selectedDestinationText);
                jQuery(".destinationLayerContainer input[id=pkgTopDestionation]").attr("value", selectedDestinationText);
                jQuery(".destinationLayerContainer input[id=lmTopDestionation]").attr("value", selectedDestinationText);
                jQuery(".destinationLayerContainer input[id=destinationSelect]").attr("value", selectedDestinationId);

                obj.parents(".destinationLayer").stop(true, true).slideUp('400', function () {
                    jQuery(".leftSearchPanel").removeClass("overflowVisible");
                });

                onDestinationChange();
            }


            jQuery(".ubernehmen").click(function () { showSelectedItem(jQuery(this)); });
            jQuery(".swCloseIcon").click(function () { showSelectedItem(jQuery(this)); });
        }

    },

    autoSuggestSearchField: {
    	inputField 	:	null,
        source 		:	"",
        sourceArray :	[],
        errorMessage: 	jQuery("<div id='autoSuggestErrorMessage'>Kein passendes Reiseziel gefunden.</div>"),

        initializeMembers: function(inputField) {
            this.inputField = jQuery(inputField);
    	},
    	
        setSource:	function( destinationsAndRegions ) {
            
            this.sourceArray = [];
            for (i = 0; i < destinationsAndRegions.length; i++) {
            		this.sourceArray.push(eval('(' + destinationsAndRegions[i] + ')'));
            }
        },

        initializeErrorMessage : function( field ) {
            // add to the DOM
            this.errorMessage.appendTo( "body" );

            // Position it relative to the autoSuggest input field
            var errorMessagePosition = field.offset();
            errorMessagePosition.left += field.innerWidth();
            errorMessagePosition.top -= 5;

            this.errorMessage.offset( errorMessagePosition );
        },

        showErrorMessage	: 	function(field) {
            this.initializeErrorMessage(field);
            field.addClass("swIcon swErrorIcon");
            this.errorMessage.delay(1000).addClass("visible"); // .addClass("visible");
            jQuery(".sspSucheBlueButton, .sspSucheWhiteButton, #search_button1")
                    .attr("disabled", "disabled")
                    .css("opacity", "0.75" );

        },

        hideErrorMessage	: 	function(field) {
            field.removeClass("swIcon swErrorIcon");
            this.errorMessage.removeClass("visible");
            jQuery(".sspSucheBlueButton, .sspSucheWhiteButton, #search_button1")
                    .removeAttr( "disabled" )
                    .css("opacity", "1" );
        },

    	initializeAutocomplete: function(sourceArray, inputField) {

            this.initializeMembers(inputField);
        	//this.setSource(sourceArray);
        	var dataArray = [];
            for (i = 0; i < sourceArray.length; i++) {
                
            	dataArray.push(eval('(' + sourceArray[i] + ')'));
            }

            this.inputField.autocomplete({
                    source : dataArray,
                    delay: 0,
                    open: function(event, ui) {
                            SEARCH_WIZARD.autoSuggestSearchField.hideErrorMessage(jQuery(this));
                        },

                    search: function(event, ui) {
                         SEARCH_WIZARD.autoSuggestSearchField.showErrorMessage(jQuery(this));
                    },
                    
                    select: function(event, ui) {
							jQuery("#resortSelect").removeAttr("disabled");
                            SEARCH_WIZARD.autoSuggestSearchField.hideErrorMessage(jQuery(this));
                                               

                            if ((jQuery(this).parents("form").find("#pkgTopDestionation").length == 0) 
                                    && (jQuery(this).parents("form").find("#lmTopDestionation").length == 0 )) {
                                if ( ui.item.type == "D") {
                                    jQuery("#topDestionation").attr("value", ui.item.label);
                                    jQuery("#destinationSelect").attr("value", ui.item.id);
    
                                    onDestinationChange();
                                } else if ( ui.item.type == "R" ) {
                                    
                                    if (ui.item.isQuickSearch == "false"){
                                        onRegionUpdate(ui.item.id);
                                    }
                                	
                                	if (jQuery(this).parents("form").find("#resortSelect").length > 0){

                                	    jQuery("#resortSelect option[value=" + ui.item.id + "]").attr("selected", "selected");
                                	}
                                }
                            } else {
                            	jQuery("#lmTopDestionation").attr("value", ui.item.label);
                            	jQuery("#pkgTopDestionation").attr("value", ui.item.label);
                            	jQuery(this).parents("form").find("#destinationSelect").attr("value", ui.item.id);
                                
                            }
                            
                        }
            }).data( "autocomplete" )._renderItem = function( ul, item ) {

            	var toDisplay  = item.label.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + jQuery.ui.autocomplete.escapeRegex(this.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");

                    return jQuery( "<li></li>" )
                            .data( "item.autocomplete", item )
                            .append( "<a>" + toDisplay + "</a>" )
                            .appendTo( ul );
            };

//            this.inputField.keypress( function() {
//
//                    SEARCH_WIZARD.autoSuggestSearchField.showErrorMessage(jQuery(this));
//            });

            this.inputField.keyup( function() {
                    if ( jQuery(this).val().length == 0  ) {
                            SEARCH_WIZARD.autoSuggestSearchField.hideErrorMessage(jQuery(this));
                    }
            });

            jQuery(".spTabs ul li").click( function() {
                SEARCH_WIZARD.autoSuggestSearchField.hideErrorMessage( SEARCH_WIZARD.autoSuggestSearchField.inputField );
            })

        }
    }
}

jQuery(function () {

    SEARCH_WIZARD.destinationLayer.initialize();

    SEARCH_WIZARD.destinationLayer.bindEvents();

});



