function checkSearchFormStatus () {    
    //View
    $('#search_view_form').show();    
    
    //Location
    var option_region = "";
    var option_city = "";
    var option_city_old = "";
    
    if($("#option_region").length)
        option_region   = $('#option_region').val();
    if($("#option_city").length)
        option_city     = $('#option_city').val();
    if($("#option_city_old").length)
        option_city_old = $('#option_city_old').val();

    if (option_region != "" || option_city != "" || option_city_old != "") {
        $("#search_location_head").removeClass("navibox_head");
        $("#search_location_head").addClass("navibox_head_active");
        $('#search_location_form').show();
    } else {
        $("#search_location_head").removeClass("navibox_head_active");
        $("#search_location_head").addClass("navibox_head");
        $('#search_location_form').hide();
    }
    
    //Date
    var dateinput_arrival   = $('#dateinput_arrival').val();
    var dateinput_departure = $('#dateinput_departure').val();
    if (dateinput_arrival != "" || dateinput_departure != "") {
        $("#search_date_head").removeClass("navibox_head");
        $("#search_date_head").addClass("navibox_head_active");
        $('#search_date_form').show();
    } else {
        $("#search_date_head").removeClass("navibox_head_active");
        $("#search_date_head").addClass("navibox_head");
        $('#search_date_form').hide();
    }
    
    //Lodging
    var choice_house_category   = $('#choice_house_category').val();
    var slider_lodging_value    = $('#slider-lodging-value').val();
    var slider_adult_value      = $('#slider-adult-value').val();
    var slider_child_value      = $('#slider-child-value').val();
    var choice_house_value = $("input.house_value_more:checked").length;   
     
    if (choice_house_value >= 1 || choice_house_category != "" || slider_lodging_value > 1 || slider_adult_value >= 2 || slider_child_value > 0 ) {
        $("#search_lodging_head").removeClass("navibox_head");
        $("#search_lodging_head").addClass("navibox_head_active");
        $('#search_lodging_form').show();
    } else {
        $("#search_lodging_head").removeClass("navibox_head_active");
        $("#search_lodging_head").addClass("navibox_head");
        $('#search_lodging_form').hide();
    }
    
    //Property
    var properties = $("input.choice_house_property:checked").length;
    if (properties > 0) {
        $("#search_property_head").removeClass("navibox_head");
        $("#search_property_head").addClass("navibox_head_active");
        $('#search_property_form').show();
    } else {
        $("#search_property_head").removeClass("navibox_head_active");
        $("#search_property_head").addClass("navibox_head");
        $('#search_property_form').hide();
    }
    
    //Food
    var properties_food = $("#choice_lodging_type").val();
    if (properties_food != "") {
        $("#search_food_head").removeClass("navibox_head");
        $("#search_food_head").addClass("navibox_head_active");
        $('#search_food_form').show();
    } else {
        $("#search_food_head").removeClass("navibox_head_active");
        $("#search_food_head").addClass("navibox_head");
        $('#search_food_form').hide();
    }
    
    //Target
    var target = $("#choice_house_target").val();
    if (target != "") {
        $("#search_target_head").removeClass("navibox_head");
        $("#search_target_head").addClass("navibox_head_active");
        $('#search_target_form').show();
    } else {
        $("#search_target_head").removeClass("navibox_head_active");
        $("#search_target_head").addClass("navibox_head");
        $('#search_target_form').hide();
    }   
    
  // Decoration
    var decoration = $("#choice_house_decoration").val();
    if (decoration != "") {
        $("#search_decoration_head").removeClass("navibox_head");
        $("#search_decoration_head").addClass("navibox_head_active");
        $('#search_decoration_form').show();
    } else {
        $("#search_decoration_head").removeClass("navibox_head_active");
        $("#search_decoration_head").addClass("navibox_head");
        $('#search_decoration_form').hide();
    }   
}

function toggleSlide (link, id) {
    if ($(id).css("display") == "none")
        $(link).text(translation[language]['detail_off']);
    else
        $(link).text(translation[language]['detail_on']);
        
    $(id).slideToggle('normal');    
}

function changeSlider (type, status) {
    var step = 0;
    if (type == "child")
        step = status*10;
    else
        step = status*10-10;
    
    if (status != 0 && status !="")
    	$("#slider-" + type).slider( "moveTo", step );
}

function initSlider (type, start) {
    //alert(type+":"+start+":"+move);
    $('#slider-' + type).slider({
            steps: 10,
            range: true,
            change: function (event, ui) {
                //Slider bei jedem Aufruf des Menüs neu initialisieren
                $("#slider-" + type + "-value").val(Math.round(ui.value/10) + start);
                //Alter Kinder ein/ausblenden
                if (type == "child" && Math.round(ui.value/10) >= 0) {
                    if (Math.round(ui.value/10) == 0)
                        $('#age_child_box').hide();
                    else
                        $('#age_child_box').show();
                    for (var i = 1; i <= 10; i++) {
                        if (i <= Math.round(ui.value/10))
                            $('#age_child_' + i).show();
                        else
                            $('#age_child_' + i).hide();                        
                    }
                }
            }
    });
}

function initSearchNavi() {
    initSlider('lodging', 1);
    initSlider('adult', 1);
    initSlider('child', 0);
    $('#navi_search a').click(
        function() {      
            $(this).next('div.navibox').slideToggle('normal');
            return false;
        }
    );
    $('#navi_search div.navibox').hide();
}

function initCorner() {
    $('.corner').corner("5px");
    $('.cornerbottom').corner("bottom 5px");
    $('.cornertop').corner("top 5px");
}

// Customize two date pickers to work as a date range 
function customRange(input) { 
    return {
        minDate: (input.id == 'dateinput_departure' ? $('#dateinput_arrival').datepicker('getDate') : null), 
        maxDate: (input.id == 'dateinput_arrival' ? $('#dateinput_departure').datepicker('getDate') : null)
    }; 
} 

function initDatePicker() {
    $.datepicker.regional['de'] = {
        showOn: 'both',
        buttonImage: 'img/calendar.gif',
        buttonImageOnly: true,
        clearText: translation[language]['DatePicker_clearText'], clearStatus: translation[language]['DatePicker_clearStatus'],
        closeText: translation[language]['DatePicker_closeText'], closeStatus: translation[language]['DatePicker_closeStatus'],
        prevText: translation[language]['DatePicker_prevText'], prevStatus: translation[language]['DatePicker_prevStatus'],
        nextText: translation[language]['DatePicker_nextText'], nextStatus: translation[language]['DatePicker_nextStatus'],
        currentText: translation[language]['DatePicker_currentText'], currentStatus: '',
        monthNames: translation[language]['DatePicker_monthNames'],
        monthNamesShort: translation[language]['DatePicker_monthNamesShort'],
        monthStatus: translation[language]['DatePicker_monthStatus'],
        yearStatus: translation[language]['DatePicker_yearStatus'],
        weekHeader: translation[language]['DatePicker_weekHeader'],
        weekStatus: translation[language]['DatePicker_weekStatus'],
        dayNames: translation[language]['DatePicker_dayNames'],
        dayNamesShort: translation[language]['DatePicker_dayNamesShort'],
        dayNamesMin: translation[language]['DatePicker_dayNamesShort'],
        dayStatus: translation[language]['DatePicker_dayStatus'],
        dateStatus: translation[language]['DatePicker_dateStatus'],
        dateFormat: 'D, dd.mm.yy', firstDay: 1, 
        initStatus: translation[language]['DatePicker_initStatus'],
        minDate: 0,
        maxDate: 730,
        beforeShow: customRange,
        isRTL: false
    };
    $.datepicker.setDefaults($.datepicker.regional['de']);
    $("#dateinput_arrival").datepicker();
    $("#dateinput_departure").datepicker();
}

function update_location() {
    $("#option_city").hide();
    $("#progress_city").show();
    var id = $("#option_region").get(0).options[ $( "#option_region" ).get(0).selectedIndex ].value;
    $.get( "ajax.php?f=c&id="+id, update_location_select );
}

function update_location_select(xml){
    // alle einträge löschen der selectbox löschen     
    $( "#option_city" ).find( "option" ).each( function(i) {
        var option = this;
        $(option).remove();
    });
    var options = '<option value="">' + translation[language]['option_city_all'] + '</option>';
    $( "n", xml ).each( function(i) {
        var id = $("id",this).text();
        var name = $("c",this).text();
        var options_tmp = options;
        options = options_tmp + '<option value="' + id + '">'+ name +'</option>';
    });
    
    $("#option_city").html(options);
    
    $("#progress_city").hide();    
    $("#option_city").show();
    
}


function newpopup(w,h,ziel) {
	  h = h - 20; var x=0, y=0, parameter="";
	  if (w < screen.availWidth || h < screen.availHeight) {
	    x = (screen.availWidth - w - 12) / 2;
	    y = (screen.availHeight - h - 104) / 2;
	    if (window.opera) y = 0; // Opera positioniert unter den Symbolleisten
	    if (x<0 || y<0) { x=0; y=0; }
	    else parameter = "width=" + w + ",height=" + h + ",";
	  }
	  parameter += "left=" + x + ",top=" + y;
	  parameter += ",menubar=0,location=0,toolbar=0,status=0";
	  parameter += ",resizable=1,scrollbars=1";
	  var Fenster = window.open(ziel,"",parameter);
	  if (Fenster) Fenster.focus();
	  return !Fenster;
}

function geo_loader (type, txt) {
    if (type == "on") {
        $('#loader_text').html(txt);
        $('#loader').show();
    } else {
        $('#loader').hide();
    }
}

function geo_status (switching, txt, type) {
    $('#status').removeClass();
    var status_class = "status_default";
    if (type)
        status_class = type;    

    if (switching == "on") {
        $('#status').html(txt);
        $('#status').addClass(status_class);
        //$('#status').show();
        $('#status').fadeIn(1500);
    } else {
        $('#status').hide();
        //$('#status').fadeOut(1500);
    }
}

function parseXML (xml) {

    var count_houses = $(xml).find('countpoints').text();
    
    if (count_houses == 0) {
        geo_loader('off');
        geo_status('on', translation[language]['geo_status_err1'], 'status_warning');
        return;
    }
    
    if (count_houses >= 500) {
        geo_loader('off');
        geo_status('on', translation[language]['geo_status_err2'], 'status_warning');
        return;
    }
    
    
    map.clearOverlays();
    var counter = 0;
    var points = new Array();   
    var bounds = new GLatLngBounds();
    
    var baseIcon = new GIcon();
        baseIcon.iconSize=new GSize(32,32);
        baseIcon.shadowSize=new GSize(56,32);
        baseIcon.iconAnchor=new GPoint(16,32);
        baseIcon.infoWindowAnchor=new GPoint(16,0);
    
    var icon_arr = new Array();
        //HOTELICON
        icon_arr["house_category_fewo"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
        icon_arr["house_category_farm"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
        icon_arr["house_category_wine"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
        icon_arr["house_category_private"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
        icon_arr["house_category_hotel"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
        icon_arr["house_category_guest"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
        icon_arr["house_category_cure"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
        icon_arr["house_category_groups"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
        icon_arr["house_category_camp"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
    
    $(xml).find('host').each(function(t){
        var id = $("id",this).text();
        var housename = $("housename",this).text();
        var lat = $("lat",this).text();
        var lon = $("lon",this).text();
        var lastminute = $("lastminute",this).text();
        if (lastminute == 1) {
            var lastminute_end = $("lastminute_end",this).text();
            var lastminute_headline = $("lastminute_headline",this).text();
        }
        
        //Standardicon
        var toubiz_icon = icon_arr["house_category_hotel"];
        //Schauen nach welcher Cat gesucht wurde
        //if ($('#choice_house_category').val() != "") {
        //    var cat = $('#choice_house_category').val().split("#");
        //    var toubiz_icon = icon_arr[cat[1]];
        //Icon wird in XML definiert
        //} else {
        //    var toubiz_icon = icon_arr[$("icon",this).text()];
        //}
            
        var point = new GLatLng( lat, lon );
        var tooltip = housename;
        //var marker = new GMarker( point, {title: tooltip, icon: toubiz_icon} );
        var marker = new GMarker( point, {icon: toubiz_icon} );
                
        GEvent.addListener( marker, "click", function() {
            geo_loader('on', translation[language]['load_gg']);
            $.ajax({
              type: "POST",
              url: "result.php",
              data: "geoteaser=1&result_view=geo&id_house=" + id,
              success: function(html){
                //marker.openInfoWindowHtml( html , { "maxWidth":"360" });
                var anreise_html = '<div style="width:360px"><form action="http://maps.google.com/maps" method="get" target="_blank">' + translation[language]['anreise_html1'] + '<br /><br /><input type="text" name="saddr" id="saddr" value="' + translation[language]['anreise_html2'] + '" style="width:200px" />&nbsp;<input name="submit" value="' + translation[language]['anreise_html3'] + '" type="submit"/><input type="hidden" name="daddr" value="'+lat+','+lon+'"/></form></div>';
                if (lastminute == 1) {
                    var lastminute_html = '<div style="width:360px"><h4>' + translation[language]['lastminute_html1'] + ' ' + lastminute_end +'</h4>'+ lastminute_headline +'<div class="c5"></div><div style="float:right;width:80px"><a class="link_1 corner" href="./house.php?id_house='+ id +'">' + translation[language]['lastminute_html2'] + '</a></div></div>';
                    marker.openInfoWindowTabsHtml([new GInfoWindowTab(translation[language]['lastminute_html3'], html), new GInfoWindowTab(translation[language]['lastminute_html4'], lastminute_html), new GInfoWindowTab(translation[language]['lastminute_html5'], anreise_html)]);
                } else {
                    marker.openInfoWindowTabsHtml([new GInfoWindowTab(translation[language]['lastminute_html3'], html), new GInfoWindowTab(translation[language]['lastminute_html5'], anreise_html)]);
                }                
                geo_loader('off');
              }
            });         
        });        
        
        map.addOverlay( marker );
        bounds.extend( point );
        counter ++;
    });
    
    if (counter != 0) {
        $("#gmapper_map").show();
        
        if (counter < 3)
            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds) - 2)
        else        
            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds) - 0)
        
        geo_loader('off');
        geo_status('on', translation[language]['geo_status_count1'] + counter + ' ' + translation[language]['geo_status_count2']);
    } else {
        geo_loader('off');
        //$("#gmapper_map").hide();
        geo_status('on', translation[language]['geo_status_err1'], 'status_warning');
    }    
}

function search_geo_category (category) {
    geo_status('off');
    geo_loader('on', translation[language]['geo_loader_on']);
    $.ajax({
        type: "POST",
        url: "search.php",
        data: "choice_house_category=" + category + "&search_new=1&geosearch=1&search_action=1",
        success: parseXML
    });
    $('#choice_house_category').val(category);
    $("#search_lodging_head").addClass("navibox_head_active");
    $('#search_lodging_form').show();
}

function search_geo_property_or (category) {
    geo_status('off');
    geo_loader('on', translation[language]['geo_loader_on']);

	var category_val = "";

    for (var i = 0; i < search_geo_property_or.arguments.length; ++i) {
        category_val = category_val + "choice_house_property_or[]=" + search_geo_property_or.arguments[i] + "&";
    }
    
    $.ajax({
        type: "POST",
        url: "search.php",
        data: category_val + "search_new=1&geosearch=1&search_action=1",
        success: parseXML
    });

    $('#choice_house_category').val(category);
    $("#search_lodging_head").addClass("navibox_head_active");
    $('#search_lodging_form').show();
}

function search_toubiz(form, type, file) {

    if (type == "geo" && file != "house.php") {

        geo_status('off');
        geo_loader('on', translation[language]['geo_loader_on']);
        
        var query = "";
        
        //Location
        if ($('#option_region').length || $('#option_city').length || $('#option_city_old').length) {
            if($("#option_region").length && $('#option_region').val() != "")
                query = query + "choice_region_new=" + $('#option_region').val() + "&";
                
            if($('#option_city').length && $('#option_city').val() != "")
                query = query + "location_new=" + $('#option_city').val() + "&";
                
            if($('#option_city_old').length && $('#option_city_old').val() != "")
                query = query + "choice_location=" + $('#option_city_old').val() + "&";
        }
        
        //Date
        if ($('#dateinput_arrival').val() != "" && $('#dateinput_departure').val() != "") {
            query = query + "choice_arrival_period=default#arrival_period#yes&";
            query = query + "choice_dateinput_arrival=" + $('#dateinput_arrival').val() + "&";
            query = query + "choice_dateinput_departure=" + $('#dateinput_departure').val() + "&";
        } else {
            query = query + "choice_arrival_period=default#arrival_period#no&";
        }
        
        //Bookable -> Lodging Property
        if ($('#only_bookable').val() != "" && $('#only_bookable').val() != undefined) {
            query = query + "choice_lodging_property[]=" + $('#only_bookable').val() + "&";
        }        
        
        //Lodging
        if ($('#choice_house_category').val() != "" || $('#slider-lodging-value').val() > 1 || $('#slider-adult-value').val() > 1 || $('#slider-child-value').val() > 0) {
            if ($('#choice_house_category').val() != "")
                query = query + "choice_house_category=" + $('#choice_house_category').val() + "&";

            if ($('#slider-lodging-value').val() != "")
                query = query + "choice_number_room_fewo=" + $('#slider-lodging-value').val() + "&";
                
            if ($('#slider-adult-value').val() != "")
                query = query + "choice_number_adult=" + $('#slider-adult-value').val() + "&";
                
            if ($('#slider-child-value').val() != "")
                query = query + "choice_number_child=" + $('#slider-child-value').val() + "&";                                
        }
        
        //Property
        jQuery("input.choice_house_property:checked").each(function(id) {
                 message_house = jQuery("input:checked").get(id);
                 query = query + "choice_house_property[]=" + message_house.value + "&";
        });
        
        //Food
        if ($("#choice_lodging_type").val() != "" && $("#choice_lodging_type").val() != undefined) {
            query = query + "choice_lodging_type=" + $('#choice_lodging_type').val() + "&";
        }
        
        //Target
        if ($("#choice_house_target").val() != "" && $("#choice_house_target").val() != undefined) {
            query = query + "choice_house_target=" + $('#choice_house_target').val() + "&";
        }
        
      //Decoration
        if ($("#choice_house_decoration").val() != "" && $("#choice_house_decoration").val() != undefined) {
            query = query + "choice_house_decoration=" + $('#choice_house_decoration').val() + "&";
        }
            
        //Hausname
        if ($("#entry_name_search").val() != "") {
            $search_name = $('#entry_name_search').val();
            query = query + "entry_name_search=" + $search_name + "&";
        }

        checkSearchFormStatus();
        //alert("search_action=1&" + query + "geosearch=1&search_new=1");
        $.ajax({
            type: "POST",
            url: "search.php",
            data: "search_action=1&" + query + "geosearch=1&search_new=1",
            success: parseXML
        });
           
    } else if (type == "quick") {
        geo_status('off');
        geo_loader('on', translation[language]['geo_loader_on']);

        $.ajax({
            type: "POST",
            url: "search.php",
            data: "geosearch=1&quicksearch=1",
            success: parseXML
        });    
    } else {
        document.navi_search.submit();
    }
}

function popup(page, name, w, h) {
	   LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	   TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	   settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,resizable,location=no,menubar=no,status=yes';
	   fenster = window.open(page, name, settings);
	   fenster.focus();
	}

/**
*   Einfache auf/zuklapp funktion
*/
function show(div) {
   with(document.getElementById(div).style){
     if(display=="none"){
       display="block";
     }else{
       display="none"; }
   }
}

