/**
 *  Base64 encode / decode
 *  http://www.webtoolkit.info/
 */
var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(_1){var _2="";var _3,chr2,chr3,enc1,enc2,enc3,enc4;var i=0;_1=Base64._utf8_encode(_1);while(i<_1.length){_3=_1.charCodeAt(i++);chr2=_1.charCodeAt(i++);chr3=_1.charCodeAt(i++);enc1=_3>>2;enc2=((_3&3)<<4)|(chr2>>4);enc3=((chr2&15)<<2)|(chr3>>6);enc4=chr3&63;if(isNaN(chr2)){enc3=enc4=64;}else{if(isNaN(chr3)){enc4=64;}}_2=_2+this._keyStr.charAt(enc1)+this._keyStr.charAt(enc2)+this._keyStr.charAt(enc3)+this._keyStr.charAt(enc4);}return _2;},decode:function(_5){var _6="";var _7,chr2,chr3;var _8,enc2,enc3,enc4;var i=0;_5=_5.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(i<_5.length){_8=this._keyStr.indexOf(_5.charAt(i++));enc2=this._keyStr.indexOf(_5.charAt(i++));enc3=this._keyStr.indexOf(_5.charAt(i++));enc4=this._keyStr.indexOf(_5.charAt(i++));_7=(_8<<2)|(enc2>>4);chr2=((enc2&15)<<4)|(enc3>>2);chr3=((enc3&3)<<6)|enc4;_6=_6+String.fromCharCode(_7);if(enc3!=64){_6=_6+String.fromCharCode(chr2);}if(enc4!=64){_6=_6+String.fromCharCode(chr3);}}_6=Base64._utf8_decode(_6);return _6;},_utf8_encode:function(_a){_a=_a.replace(/\r\n/g,"\n");var _b="";for(var n=0;n<_a.length;n++){var c=_a.charCodeAt(n);if(c<128){_b+=String.fromCharCode(c);}else{if((c>127)&&(c<2048)){_b+=String.fromCharCode((c>>6)|192);_b+=String.fromCharCode((c&63)|128);}else{_b+=String.fromCharCode((c>>12)|224);_b+=String.fromCharCode(((c>>6)&63)|128);_b+=String.fromCharCode((c&63)|128);}}}return _b;},_utf8_decode:function(_e){var _f="";var i=0;var c=c1=c2=0;while(i<_e.length){c=_e.charCodeAt(i);if(c<128){_f+=String.fromCharCode(c);i++;}else{if((c>191)&&(c<224)){c2=_e.charCodeAt(i+1);_f+=String.fromCharCode(((c&31)<<6)|(c2&63));i+=2;}else{c2=_e.charCodeAt(i+1);c3=_e.charCodeAt(i+2);_f+=String.fromCharCode(((c&15)<<12)|((c2&63)<<6)|(c3&63));i+=3;}}}return _f;}};


if ("undefined" != typeof jQuery) {
  jQuery(function($) {
    $("#deutschlandStandorte h1").after('<form action="" method="post" onsubmit="return false;" id="deutschlandStandortWaehlen"><select name="standort"><option value="">Wählen Sie einen Standort</option><option value="/studios-praxis-deutschland-schleswig-holstein.html">Schleswig-Holstein</option><option value="/studios-praxis-deutschland-hamburg.html">Hamburg</option><option value="/studios-praxis-deutschland-mecklenburg-vorpommern.html">Mecklenburg-Vorpommern</option><option value="/studios-praxis-deutschland-niedersachsen.html">Niedersachsen</option><option value="/studios-praxis-deutschland-bremen.html">Bremen</option><option value="/studios-praxis-deutschland-sachsen-anhalt.html">Sachsen-Anhalt</option><option value="/studios-praxis-deutschland-brandenburg.html">Brandenburg</option><option value="/studios-praxis-deutschland-berlin.html">Berlin</option><option value="/studios-praxis-deutschland-nordrhein-westfalen.html">Nordrhein-Westfalen</option><option value="/studios-praxis-deutschland-hessen.html">Hessen</option><option value="/studios-praxis-deutschland-thueringen.html">Thüringen</option><option value="/studios-praxis-deutschland-sachsen.html">Sachsen</option><option value="/studios-praxis-deutschland-rheinland-pfalz.html">Rheinland-Pfalz</option><option value="/studios-praxis-deutschland-saarland.html">Saarland</option><option value="/studios-praxis-deutschland-baden-wuerttemberg.html">Baden-Württemberg</option><option value="/studios-praxis-deutschland-bayern.html">Bayern</option></select></form>');
    $('form[id=deutschlandStandortWaehlen] select').each(function() {
     var me = $(this);
     var type = me.attr('name').substr(0,1).toUpperCase() + me.attr('name').substr(1);
     var opts = $('select[name=' + type.toLowerCase() + '] option');
     
     var m = '';
     var noneSelectedText = null;
     opts.each(function() {
       var me = $(this);
     
       if ('' == me.attr('value')) {
         noneSelectedText = me.html();
         return true;
       };
       
       var id = 'suche' + type + '_' + Base64.encode(me.attr('value')).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '.');
       m += '<li id="' + id + '"><a href="' + me.attr('value') + '" onclick="return false;">' + me.html() + '</a></li>';
       return true;
     });
     
     m = $('<div id="suche' + type + '" class="ausgewaehlt"><span>' + noneSelectedText + '</span></div><ul id="suche' + type + 'Auswahl" class="auswahl">' + m + '</ul>');
     me.before(m);
    });
    
    
    var sucheStarten = $('<span class="sucheStarten">' + $('input.sucheStarten').attr('value') + '</span>').click(function() {
     $('form#deutschlandStandortWaehlen').submit();
    });
    $('input.sucheStarten').before(sucheStarten).hide();
    
    $('form[id=sucheFormular] select').hide();
    $('.ausgewaehlt').show();
    
    $('.ausgewaehlt').click(function() {
     $('body')[0].menuClicked = true;
     var isThisShown = $('#' + $(this).attr('id') + 'Auswahl:visible').length > 0;
     if (isThisShown) $('#' + $(this).attr('id') + 'Auswahl').hide();
     else {
       $('.auswahl').hide();
       //alert($('#' + $(this).attr('id') + 'Auswahl').height());
       
       if (
        $("#sucheStandort").offset()["top"] + $("#sucheStandortAuswahl").height() >
         $("#boxContent").offset()["top"] + $("#boxContent").height()
       ) {
        $('#' + $(this).attr('id') + 'Auswahl').addClass("bottom");
       }
       
       $('#' + $(this).attr('id') + 'Auswahl').show();
     }
    });
    
    $('.auswahl li').click(function() {
     $('body')[0].menuClicked = true;
     var me = $(this);
     $('#' + me.parent().attr('id').replace(/Auswahl/g, '') + ' span').text($(this).text()).click();
     var type = me.parent().attr('id').replace(/Auswahl|suche/g, '');
     
     var myId = me.attr('id');
     var valueEncoded = myId.substr(myId.indexOf('_') + 1);
     var valueDecoded = Base64.decode(valueEncoded.replace(/-/g, '+').replace(/_/g, '/').replace(/\./g, '='));
     $('#deutschlandStandortWaehlen select[name=' + type.toLowerCase() + ']').val(valueDecoded);
     if ("" != valueDecoded) {
       window.location.href = valueDecoded;
     }
     
     me.parent().hide();
    });
    
    
    $('form#deutschlandStandortWaehlen select').each(function() {
      var me = $(this);
      var type = me.attr('name').substr(0,1).toUpperCase() + me.attr('name').substr(1);
      var val = me.val();
      
      if (0 == val.length) return true;
      var idBeginsWith = 'suche' + type + '_';
      var encodedValue = Base64.encode(me.attr('value')).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '.');
      var id = idBeginsWith + encodedValue;
      
      $("*[id^='" + idBeginsWith + "']").each(function() {
        var me = $(this);
        if (id == me.attr('id')) {
          me.click();
        }  
      });
    });
  });
  
  $('body').click(function() {
    if ('undefined' == typeof this.menuClicked || !this.menuClicked) {
      $('.auswahl').hide();
    };
    
    this.menuClicked = false;
  });
};
