$(document).ready(function(){
  //var iScrollTop = ($("body", top.document).scrollTop());
  location.hash = "#"; // jQuery history

  $.historyInit(ajaxGetContent);
  if ($.browser.msie && parseInt($.browser.version, 10) < 8){
    //$("body", top.document).scrollTop(iScrollTop);
  }

  $('body').click(function(){
    $('#outer').hide();
  });

  $('#searchNav').autocompleteExhibitors();

  $('#searchNav').tooltip({
    track: true,
    delay: 0,
    showURL: false,
    opacity: 1,
    fixPNG: true,
    showBody: true,
    extraClass: "tooltip-block",
    top: -75,
    left: 15
  });

  $('.searchAll').click(function(){
    var e = jQuery.Event("keyup");
    e.which = 13;
    $('#searchNav').trigger(e);
  });

  $('#formSearchNav').submit(function(){
    return false;
  });



  $('#tree').treeview({
    animated: 'fast',
    persist: 'location',
    unique: true,
    collapsed: true
  });

  $(document).ajaxStart(function(){
    var activeNav = $('body').data('activeNav');
    $("#wait" + activeNav).show();
  });

  $(document).ajaxStop(function(){
    $(".wait").hide();
  });

  $(document).keydown(function(e) {
    //alert(e.keyCode);
    if(e.keyCode == 27) { // ESCAPE Button
      hideDetails();
    }
  });

  $('#shadeBackground').live('click', function(){
    hidePopup();
  });

  $('.notree li').hover(function(){
    $(this).addClass('highlighted');
  },function(){
    $(this).removeClass('highlighted');
  });


  $('.showHide').unbind('click');

  $('.showHide').toggle(function(){
    $(this).children('img').attr({src: 'images/red-minus.gif'});
    $(this).parent().next().slideDown('fast', function() {scrollText();}); // Div zu Bl�ttern
    //$(this).prev().show(); // Div mit Steuerelementen
    $(this).prev().css('visibility', 'visible');
    return false;
  }, function(){
    $(this).children('img').attr({src: 'images/green-plus.gif'});
    $(this).parent().next().slideUp();
    //$(this).prev().hide();
    $(this).prev().css('visibility', 'hidden');
    $(this).prev().children('a').css('visibility', 'inherit');
    return false;
  });

  // Init alle .scroll Elemente
  window.setTimeout("scrollText();", 100);
  $('.scroll').unbind('click');
  $('.scroll').click(function() {
    scrollText($(this));
    return false;
  });

  $('input[type=text]').focus(function(){
    //this.select();
  });

  $('#divSearch').tabs();

  $('#tabsMesseplaner').tabs();

  $('.details-cell .tabs').tabs({
    show : function(event, ui) {
      scrollText();
    }
  });

  $('input[name=searchExhibitors]').focus();

  $('#loginEmail').inputText();
  $('#loginPassword').inputText();

  $('#loginEmail, #loginPassword').keyup(function(e) {
    if(e.which == 13) {
      login();
    }
  });

  $('#butLogin').click(function(){
    login();
  });

  $('#butLogout').click(function(){
    logout();
  });

  $('#butEdit').click(function(){
    ajaxGetContent({nav: 'messe#1'});
  });

  restoreNavigation();

  $("#content").css('display', 'block');

  if ($.browser.msie && parseInt($.browser.version, 10) < 7){
    $('#browserAlert').show();
  }

  $('.scrollElements').scrollElements();
  
  
  // job market
  // for jQuery autocomplete
  monkeyPatchAutocomplete();

  $('.ajaxSubmit').live('click', function(){
    return submitAjaxForm($(this));
  });

  $('a.ajax').live('click', function(){
    return ajaxLink($(this));
  });

  $('.toggleDateDetails').live('click', function(){
    $this = $(this);
    $thisRow = $this.parents('tr');
    $toggable = $thisRow.next().find('td');
    if($toggable.is(':not:visible')) {
      $this = $(this);
      $this.removeClass('dateDetailsOpened');
      $this.addClass('dateDetailsClosed');
      $thisRow.removeClass('selected');
      $toggable.hide();
    } else {
      $this.removeClass('dateDetailsClosed');
      $this.addClass('dateDetailsOpened');
      $thisRow.addClass('selected');
      $toggable.show();
    }
    return false;
  });

  $('.resetAppointmentPrintList').click(function(){
    $('input[name^=print]').removeAttr("checked");
    return false;
  });

  $('.printAppointmentPrintList').click(function(){
    return submitAjaxForm($(this));
  });

  $('.confirmDelete').click(function(){
    confirmDialog(t('txt_confirm_delete'), continueAction, $(this).attr('href'));
    return false;
  });

  $('.hidePopup').live('click', function(){
    return hidePopup();
  });

  $('textarea.wysiwyg').wysiwyg({
    controls : {
      separator00 : {visible : true},
      separator01 : {visible : true},
      separator02 : {visible : false},
      separator03 : {visible : false},
      separator04 : {visible : false},
      insertOrderedList : {visible : true},
      insertUnorderedList : {visible : true},
      undo: {visible : false},
      redo: {visible : false},
      justifyLeft: {visible : true},
      justifyCenter: {visible : true},
      justifyRight: {visible : true},
      justifyFull: {visible : false},
      subscript: {visible : false},
      superscript: {visible : false},
      underline: {visible : false},
      strikeThrough: {visible : false},
      indent: {visible : false},
      outdent: {visible : false},
      createLink: {visible : false},
      insertImage: {visible : false},
      separator05 : {visible : false},
      h1mozilla: {visible : false},
      h2mozilla: {visible : false},
      h3mozilla: {visible : false},
      h1: {visible : false},
      h2: {visible : false},
      h3: {visible : false},
      separator06: {visible : false},
      removeFormat: {visible : false},
      separator07: {visible : false},
      separator08: {visible : false},
      separator09: {visible : false}
    }
  });

  $('.tablesorter').tablesorter({widgets: ['zebra']})
    .tablesorterPager({container: $("#pager"), size: $("#pager").attr('data-size')});

  $('.autocompleteJobmarket').autocomplete({
    source: "index.php?controller=jobMarket&action=autocomplete&ajax=true",
    minLength: 1,
    select: function(event, ui) {
      //$(this).parents('form').find('input[type=submit]').click();
      $(this).val(ui.item.value);
      submitAutocomplete($(this));
    }
  });

  $('.autocompleteJobmarketPostal').autocomplete({
    source: "index.php?controller=jobMarket&action=autocompletePostal&ajax=true",
    minLength: 1,
    select: function(event, ui) {
      $(this).val(ui.item.value);
      submitAutocomplete($(this));
    //$(this).parents('form').find('input[type=submit]').click();
    }
  });

  $('#isallday').change(function(){
    if($(this).attr('checked')) {
      $('#start_time, #end_time').attr('disabled', 'disabled');
    } else {
      $('#start_time, #end_time').removeAttr('disabled');
    }
  });

  $('#advancedSearchToggle').toggle(function(){
    $('#advancedSearch').show();
  }, function(){
    $('#advancedSearch').hide();
  });

  $('.dropDownCheckBox').multiSelect();

  $('.language').change(function(){
    $languages = $('.language:gt(1)');
    if($(this).attr('id') == 'language' && $(this).is(':checked')) {
      $languages.removeAttr('checked');
    }
    if($(this).attr('id') != 'language' && $(this).is(':checked')) {
      $('#language').removeAttr('checked');
    }
  });

  // select printable items
  $('input:checkbox.dcheckbox').checkbox();

});
