$(document).ready(function(){
  init();
});	

function init() {
  $('#panel').css('display', 'none');
  $('#toggle_extended').click(function() {
    $('#panel').css('display') == 'block' ? $('#up').click() : $('#down').click();
  });
  $("#down").click(function() {
    if ($('#panel').css('display')!='block') {
      $("#panel").slideDown("slow", scrollTo);
      $('#btnSearchSimple').attr('className', 'input_button');
      $('#btnSearchComplex').attr('className', 'input_button_default');
    }
  });

  $("#up").click(function() {
    if ($('#panel').css('display')!='none') {
      scrollToTop(function() { $("#panel").slideUp("slow")} );
      $('#btnSearchSimple').attr('className', 'input_button_default');
      $('#btnSearchComplex').attr('className', 'input_button');
    }
  }); 

  field = $('#edtSearch');
  field.bind({
    blur: decorateEmptySearchField,
    focus: cleanEmptySearchField
  });
  //set initial field value
  field.trigger('blur');

  $('input:checkbox').bind('focus', function() {
    this.blur();
  });

  var fbSettings = {
    'overlayOpacity'    : 0.5,
    'height'            : 470,
    'width'             : 660,
    'showCloseButton'   : false,
    'hideOnOverlayClick': false,
    'hideOnContentClick': false,
    'classInner'        : 'dialog'
  };
  $('a#sendmessage').fancybox(fbSettings);
  $('#edtZIP').keypress(function(event) {
    if ($('#rbZIPTownZIP').attr('checked') != 'checked') {
      $('#rbZIPTownZIP').attr('checked', 'checked');
    }
  });
  $('#edtTown').keypress(function(event) {
    if ($('#rbZIPTownTown').attr('checked') != 'checked') {
      $('#rbZIPTownTown').attr('checked', 'checked');
    }
  });
}

function decorateEmptySearchField(event) {
  if (this.value=='') {
    this.value='z.B.: Ort, PLZ, Service, Haarfarbe, Körbchengröße, Gewicht usw.';
    $(this).addClass('inactive');
  }
}

function cleanEmptySearchField(event) {
  if (this.value=='z.B.: Ort, PLZ, Service, Haarfarbe, Körbchengröße, Gewicht usw.') {
    this.value='';
    $(this).removeClass('inactive');
  }
}

function scrollTo(event, callback) {
  elem = $(this);
  maxSize = $(window).height();
  elemTop = elem.offset().top;
  elemHeight = elem.height();
  targetOffset=elemTop;
  if (elemHeight<maxSize) {
    targetOffset = elemTop - Math.round((maxSize - elemHeight) / 2);
  }
  animateScroll(targetOffset, callback);
}

function scrollToTop(callback) {
  animateScroll(0, callback);
}

function animateScroll(position, callback) {
  $('html').animate({scrollTop : position}, 250, 'swing', callback);
}

