
function RentSwitchButtonEvent ()
 {
  $("span#label_for_final_rent").toggleClass ('rent_type_disabled');
  $("span#label_for_base_rent").toggleClass ('rent_type_disabled');
  $("span#label_for_final_rent").toggleClass ('rent_type_enabled');
  $("span#label_for_base_rent").toggleClass ('rent_type_enabled');

  if ($("input#rent_type").val () == 'base')
   {
    $("#base_rent_from").attr ('name', 'final_rent_from');
    $("#base_rent_to").attr ('name', 'final_rent_to');

    $("#base_rent_from").attr ('id', 'final_rent_from');
    $("#base_rent_to").attr ('id', 'final_rent_to');

    $("input#rent_type").val ('final');
   }
  else
   {
    $("#final_rent_from").attr ('name', 'base_rent_from');
    $("#final_rent_to").attr ('name', 'base_rent_to');

    $("#final_rent_from").attr ('id', 'base_rent_from');
    $("#final_rent_to").attr ('id', 'base_rent_to');

    $("input#rent_type").val ('base');
   }

  if ($("#placeholder_search_count_top").size ())
   {
    updateSearchCount ();
   }

  if ($("#placeholder_search_count").size ())
   {
    updateSearchCount ();
   }

  if ($("#container-for-results").size ())
   {
    updateSearchEntries ();
   }

  updateSearch ();
 }

function updateSearch ()
 {
  jQuery.post
   (
    $("#url_for_update").val(),
    $("#search_form").serialize(),
    function(response)
     {
      //$("#placeholder_search_count").html(response);
     },
    'html'
   );
 }

function addRemoveFromWatchlist ()
 {
  origin = $(this).html ();

  if (origin.indexOf ('unchecked') == -1)
   {
    $(this).html (origin.replace ('checked', 'unchecked'));
   }
  else
   {
    $(this).html (origin.replace ('unchecked', 'checked'));
   }

  jQuery.getJSON
   (
    $(this).attr ('rel'),
    function (response)
     {
      $('#' + response.control_id).attr ('rel', response.rel);
      $('#' + response.control_id).html (response.html);

      $('#watchlist-count').html (response.count);
     }
   );
 }

$(document).ready
 (
  function ()
   {
    /* Update on rent/purchase-change */
    $("#dimension").change
     (
      function ()
       {
        if ($("#search_form").size ()) $("#search_form").attr ("action", swapDimensionSubstringValue ($("#search_form").attr ('action'), $("#dimension").val ()));

        if ($("#url_for_json_count").size ()) $("#url_for_json_count").val (swapDimensionSubstringValueIf ($("#url_for_json_count").val ()));
        if ($("#url_for_city_update").size ()) $("#url_for_city_update").val (swapDimensionSubstringValueIf ($("#url_for_city_update").val ()));
        if ($("#url_for_district_update").size ()) $("#url_for_district_update").val (swapDimensionSubstringValueIf ($("#url_for_district_update").val ()));

        if ($("#notification_form").size ()) $("#notification_form").attr ("action", swapDimensionSubstringValue ($("#notification_form").attr ('action'), $("#dimension").val ()));

        if ($("#main-nav a:contains('Startseite')").size ()) $("#main-nav a:contains('Startseite')").attr ("href", swapDimensionSubstringValueIf ($("#main-nav a:contains('Startseite')").attr ('href')));
        if ($("#main-nav a:contains('Detailsuche')").size ()) $("#main-nav a:contains('Detailsuche')").attr ("href", swapDimensionSubstringValueIf ($("#main-nav a:contains('Detailsuche')").attr ('href')));

        jQuery.get
         (
          $('#url_for_dimension_update').val (),
          function (response)
           {
            $("#rent_element").html (response);

            if ($("#url_for_json_count").size ())
             {
              // --> search_options.js
              // Auswahllisten, Checkboxen, Eingabefelder
              $("#rent_element select").change(updateSearchCount);
              $("#rent_element input.checkbox").click(updateSearchCount);
              $("#rent_element input.text").keyup(updateSearchCount);
             }

            $("#rent_switch_button_img").click (RentSwitchButtonEvent);
           }
         );
       }
     );

    // Auswahllisten, Checkboxen, Eingabefelder
    $("#search_form select").change(updateSearch);
    $("#search_form input.checkbox").click(updateSearch);
    $("#search_form input.text").keyup(updateSearch);

    /* Update on final/base-rent-change */
    $("#rent_switch_button_img").click (RentSwitchButtonEvent);

    /* Watchlist Add/Remove */
    $(".watchlist-control").click (addRemoveFromWatchlist);

    $("input, select").mouseover (function () {$(this).toggleClass ('hover')});
    $("input, select").mouseleave (function () {$(this).toggleClass ('hover')});
    $("input, select").focus (function () {$(this).toggleClass ('active')});
    $("input, select").blur (function () {$(this).toggleClass ('active')});
   }
 );

function swapDimensionSubstringValue (origin, newSubStr)
 {
  return origin.replace (origin.match ('miet-immobilien') ? 'miet-immobilien' : 'kauf-immobilien', newSubStr);
 }

function swapDimensionSubstringValueIf (origin)
 {
  if (origin.indexOf ('miet-immobilien') != -1)
   {
    return origin.replace ('miet-immobilien', 'kauf-immobilien');
   }
  else if (origin.indexOf ('kauf-immobilien') != -1)
   {
    return origin.replace ('kauf-immobilien', 'miet-immobilien');
   }
  else
   {
    return origin;
   }
 }

if ((! document.compatMode) || document.compatMode != 'CSS1Compat')
 {
  alert ('quirks: ' + document.compatMode);
 }

