DOL = {
  
  api_key: null,
  
  initialize: function (api_key) {
    DOL.api_key = api_key;
    
    DOL.initialize_facebook();
    
    $(document).ready(function () {
      DOL.initialize_fallback_links();
      DOL.initialize_history();
      DOL.initialize_infobubbles();
      DOL.initialize_swap();
      
      $('#help').click(function () {
        var nav;
        
        if ($('#help-nav').length == 0) {
          nav = $('#nav').clone().attr('id', 'help-nav').insertAfter('#nav');
        } else {
          nav = $('#help-nav');
        }
        
        nav.show();
        nav.click(function () { nav.hide(); });
      })
    })
  },
  
  initialize_history: function () {
    DOL.bind_anchors();
    
    $.historyInit(DOL.history_callback);
    
    $(document).bind('close.facebox', function () {
      $.historyLoad('');
    });
  },
  
  initialize_fallback_links: function () {
    $('.imprint a').attr('href', '#!/pages/impressum');
    $('.privacy a').attr('href', '#!/pages/datenschutz');
  },
  
  initialize_facebook: function () {
    FB.init(DOL.api_key, '/xd_receiver.htm', {
      "ifUserConnected": DOL.on_facebook_connected,
      "ifUserNotConnected": DOL.on_facebook_not_connected
    });
  },
  
  initialize_infobubbles: function () {
    $(".bubbleBig a").hover(function() {
      $(this).next("em").stop(true, true).animate({opacity: "show", top: "-110"}, "slow");
    }, function() {
      $(this).next("em").animate({opacity: "hide", top: "-120"}, "fast");
    });
    $(".bubble a").hover(function() {
      $(this).next("em").stop(true, true).animate({opacity: "show", top: "-60"}, "slow");
    }, function() {
      $(this).next("em").animate({opacity: "hide", top: "-70"}, "fast");
    });
    $(".swap span").hover(function() {
      $(this).next("em").stop(true, true).animate({opacity: "show", top: "-60"}, "slow");
    }, function() {
      $(this).next("em").animate({opacity: "hide", top: "-70"}, "fast");
    });
  },
  
  initialize_swap: function () {  
      $('.swap').hover(function () { 
        $(this).addClass('hover');
      }, function () { 
        $(this).removeClass('hover');
      });
  },
  
  on_facebook_connected: function (user_id) {
    // FB.Connect.showPermissionDialog("offline_access");
  },
  
  on_facebook_not_connected: function (user_id) {
    
  },
  update_user_box: function () {
      var user_box = document.getElementById("connect_box");
      user_box.innerHTML =
          '<div id="profile-box">' +
              'Hallo <fb:name uid="loggedinuser" useyou="false" linked="true"></fb:name> ' +
              '<a href="#" onclick="FB.Connect.logout(function() { location.reload(); }); return false;" >(ausloggen)</a>' +
          '</div>';
        FB.XFBML.Host.parseDomTree();
          $.historyLoad('/mail/subscribe');
      },

  history_callback: function (hash) {
    
    // if (hash.match(/\/pages/)) {
    //   FB.Connect.requireSession(function () {
    //     load_deferred();
    //   });
    // } else {
    //   load_deferred();
    // }
    
    if(hash) {
      hash = hash.replace(/^!/, '');
      
      var doctor, params;
      if (m = location.href.match(/doctor(kiss|date|style)/)) {
        doctor = m[0];
        params = 'for=' + doctor;
      }
      
      var clean_hash = hash.replace(/^!\//, '');
      
      var tracking = ['_trackEvent', doctor, hash];
      _gaq.push(tracking);
      
    //  if($.browser.msie) {
     //   hash = encodeURIComponent(hash);
     // }

      $.facebox({ ajax: hash + '?' + params });
    }
    
    DOL.bind_anchors($("#dol-dialog"));
  },
  
  bind_anchors: function (context) {
    if (typeof context == 'undefined')
      context = document;
        
    $("a[rel='history']", context).click(function() {
      var hash = this.href.replace(/^.*#/, '');
      $.historyLoad(hash);
      return false;
    });
  }
  
}
