  var chat_timeout;
  var gotham_bold = { src: '/media/gothamrnd-bold.swf', ratios: [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01] };
  var gotham_medium = { src: '/media/gothamrnd-medium.swf', ratios: [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01] };

  sIFR.useStyleCheck = true;
  sIFR.fixWrap = true;
  sIFR.forceWidth  = true;
  sIFR.useDomLoaded = true;
  sIFR.activate(gotham_bold, gotham_medium);

  sIFR.replace(gotham_bold, {
    selector: '.header, .header_small, .header_apartment h2',
    css: ['.sIFR-root { color: #2b2a2a; font-size: 16px; }'],
    forceSingleLine: true,
    sharpness: -400,
    offsetTop: 3,
    wmode: 'transparent'
  });
  sIFR.replace(gotham_medium, {
    selector: '#vorstag h1',
    css: ['.sIFR-root { color: #2b2a2a; }',
          '.red { color: #f23502; }'],
    forceSingleLine: true,
    sharpness: -400,
    offsetTop: 3,
    wmode: 'transparent'
  });

  $(function() {
    $('#menubar img').hover(function() {
        var img = this.src;
        this.src = img.replace(/_off/,'_on');
      }, function() {
        var img = this.src;
        this.src = img.replace(/_on/,'_off');
      });

    if (jQuery.isFunction(jQuery.fn.datePicker)) {
      date  = new Date();
      day   = (date.getDate()<10) ? '0'+date.getDate() : date.getDate();
      month = (date.getMonth()<9) ? '0'+(date.getMonth()+1) : date.getMonth()+1;
      $('.date-pick').datePicker({startDate: day+'/'+month+'/'+date.getFullYear(), endDate: '31/12/'+(date.getFullYear()+5), clickInput: true});
      $('.checkin').bind('dpClosed',
        function(e, selectedDates) {
          var ci = selectedDates[0];
          var co = $('.checkout').dpGetSelected();
          if (ci>=co) {
            ci = new Date(ci);
            $('.checkout').dpSetSelected(ci.addDays(1).asString()).val(ci.asString());
          }
        }
      );
      $('.checkout').bind('dpClosed',
        function(e, selectedDates) {
          var co = selectedDates[0];
          var ci = $('.checkin').dpGetSelected();
          if (co<=ci) {
            co = new Date(co);
            $('.checkin').dpSetSelected(co.addDays(-1).asString()).val(co.asString());
          }
        }
      );
    }
  });

  openChat = function() {
    $.get('/chat?cmd=askname',function(data) {
      $('#window').remove();
      $('body').append('<div id="window"></div>');
      $('#window').html(data);
      $('#window').dialog({autoOpen: true, draggable: true, width: 'auto', height: 'auto', title: 'Customer chat', modal: true, overlay: {opacity: 0.5, background: 'black'} });
    });
  }

  sendName = function() {
    chatname = $('#chat_name').val();
    $.get('/chat?cmd=connect&name='+chatname,function(data) {
      $('#window').dialog('close');
      $('#window').remove();
      $('body').append('<div id="window"></div>');
      $('#window').html(data);
      $('#window').dialog({autoOpen: true, draggable: true, width: 'auto', height: 'auto', title: 'Customer chat', modal: true, overlay: {opacity: 0.5, background: 'black'} });
    });
  }

  updateConnect = function (session) {
    chat_timeout = setTimeout(function() {
      $.get('/chat?cmd=update&session='+session,function(data) {
        if (data) {
          $('#window').dialog('close');
          $('#window').remove();
          $('body').append('<div id="window"></div>');
          $('#window').html(data);
          $('#window').dialog({autoOpen: true, draggable: true, width: 'auto', height: 'auto', title: 'Customer chat', modal: true, overlay: {opacity: 0.5, background: 'black'} });
        } else updateConnect(session);
      });
    },1000);
  }

  updateConversation = function() {
    conversation_timeout = setTimeout(function() {
      $.get('/chat?cmd=getmsgs&session='+current_session,function(data) {
        $('#conversation').html(data);
        if ($("#conversation").attr("scrollTop") > $("#conversation").attr("scrollHeight")-460)
          $("#conversation").attr({ scrollTop: $("#conversation").attr("scrollHeight") });
        updateConversation();
      });
    },1000);
  }

