$(window).load(function() { // use this instead of $().ready() to work around safari issue

  $('a.popup, a.zoom').attr('rel', '#overlay');

  $("a[rel].zoom").overlay({
    onBeforeLoad: function() {
      var wrap = this.getOverlay().find(".contentWrap");
      wrap.html("<img src=\"" + this.getTrigger().attr("href") + "\" alt=\"\">");
    }
  });

  $("a[rel].popup, a.steps").overlay({
    onBeforeLoad: function() {
      var wrap = this.getOverlay().find(".contentWrap");
      var h = this.getTrigger().attr("href");
      wrap.load(h);
    }
  });

  // autocomplete for search bar

  $("input#fld_text_full").autocomplete("/auto_complete");
  $("input#fld_text_full").attr("defaultValue", $("input#fld_text_full").value);

  $("input#fld_text_full").focus(function() {
    var defv = $("input#fld_text_full").attr("defaultValue");
    if (this.value == defv) {
      this.value = '';
    }
  });

  $("input#fld_text_full").blur(function() {
    if (this.value == '') {
      var defv = $("input#fld_text_full").attr("defaultValue");

      this.value = defv;
    }
  });

  // autocomplete for login form

  $("input#email").attr("defaultValue", $("input#email").value);
  $("input#password").attr("defaultValue", $("input#password").value);

  $("input#email").focus(function() {
    var defv = $("input#email").attr("defaultValue");
    if (this.value == defv) {
      this.value = '';
    }
  });

  $("input#email").blur(function() {
    if (this.value == '') {
      var defv = $("input#email").attr("defaultValue");

      this.value = defv;
    }
  });

  $("input#password").focus(function() {
    var defv = $("input#password").attr("defaultValue");
    if (this.value == defv) {
      this.value = '';
    }
  });

  $("input#password").blur(function() {
    if (this.value == '') {
      var defv = $("input#password").attr("defaultValue");

      this.value = defv;
    }
  });


  // add pseudo last-child class
  $('ul li:last-child').addClass("last");

  /*
  $('.box_grid, .onlyicons, .article_grid').each(function() {
    var i = 0;
    $(this).children('.item, li, .article').each(function() {
      if (i % 2 != 0) $(this).addClass("omega_item");
      i++;
    });
  });
     */

  /* make grid items fully clickable */

  $('.box_grid .item').each(function() {
    var self = $(this);
    if (!self.hasClass("non-clickable")) {
      self.addClass("clickable");
      self.click(function() {
        document.location.href = $(this).find('.title a, a.title').attr("href");
      });
    }
  });

  // hide/show extended search

  $('#more_options').click(function() {
    var f = $('#sform');

    if (f.find(".extended").css("display") == "none") {
      f.find('.basic').hide();
      f.find(".extended").slideDown();

      f.attr("action", "/search_full");

    } else {
      f.find(".extended").slideUp();
      f.find('.basic').fadeIn();

      f.attr("action", "/search");

    }

    var l = $(this);
    var t = l.html();
    var a = l.attr("rev");

    l.html(a);
    l.attr("rev", t);

  });

  // remote extended fields on basic search, remove basic fields on extended =)

  $('#sform').submit(function() {
    if ($(this).find(".extended").css("display") != "none") {
      $('#fld_search_industry').remove();
      $('#fld_search_country').remove();

    } else {
      $(this).find(".extended").remove();

    }
  });

  // switch between grid/list view for overview pages (expos, companies, venues, ...)

  $('.view_type .as_list').click(function() {
    $(this).addClass("selected");
    $('.view_type .as_grid').removeClass("selected");

    $('#content .box_grid').addClass('box_list');
    $('#content .box_list .item').addClass('zefix');
  });

  $('.view_type .as_grid').click(function() {
    $(this).addClass("selected");
    $('.view_type .as_list').removeClass("selected");

    $('#content .box_list').removeClass('box_list');
    $('#content .box_list .item').removeClass('zefix');
  });
});

$(document).ready(function() {
  $(".dropdown img.flag").addClass("flagvisibility");

  $(".dropdown dt a").click(function() {
    $(".dropdown dd ul").toggle();
  });

  $(".dropdown dd ul li a").click(function() {
    var text = $(this).html();
    $(".dropdown dt a span").html(text);
    $(".dropdown dd ul").hide();
    $("#result").html("Selected value is: " + getSelectedValue("sample"));
  });

  function getSelectedValue(id) {
    return $("#" + id).find("dt a span.value").html();
  }

  $(document).bind('click', function(e) {
    var $clicked = $(e.target);
    if (! $clicked.parents().hasClass("dropdown"))
      $(".dropdown dd ul").hide();
  });


  $("#flagSwitcher").click(function() {
    $(".dropdown img.flag").toggleClass("flagvisibility");
  });
});
