function setcookie(name,value,days) {
  var expires = "";
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    expires = "; expires="+date.toGMTString();
  }
  document.cookie = name+"="+value+expires+"; path=/";
}  

function eraseCookie(name) {
  setcookie(name, "", -1);
}

function showOverlayForm(url) {
  var bookFrame = Ext.get('book-now-frame');
  
  url = url + '&ifr=1';
  
  if (bookFrame.dom.src != url)
  {
    bookFrame.dom.src = url;
  }
  
  Ext.get('overlay').setVisible(true);
  
  var minus = 120,
      height = 600,
      viewportHeight = Ext.lib.Dom.getViewportHeight(),
      lightboxForm = Ext.get('lightbox-form');
  
  if ((viewportHeight - minus) < height) {
    height = viewportHeight - minus;
    lightboxForm.setHeight(height);
    bookFrame.setHeight(height);
  }
  
  var left = (Ext.lib.Dom.getViewportWidth() / 2) - 282.5;
  var top = ((viewportHeight) / 2) - (height / 2);

  lightboxForm.setLeft(left);
  lightboxForm.setTop(top);
  
  if (Ext.isIE6)
  {
    lightboxForm.dom.style.setExpression("left", "(" + left + " + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px'");
    lightboxForm.dom.style.setExpression("top", "( " + top + " + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px'");
  }
  
  lightboxForm.setStyle('display', 'block');
}

function hideOverlayForm() {
  Ext.get('overlay').setVisible(false);
  var bookFrame = Ext.get('book-now-frame');
  bookFrame.dom.src = bookFrame.dom.src;
  Ext.get('lightbox-form').setStyle('display', 'none');
}

function getTimeDiff(time1, time2) {
  var seconds1 = time1.split(":")[0] * 3600 + time1.split(":")[1] * 60;
  var seconds2 = time2.split(":")[0] * 3600 + time2.split(":")[1] * 60;
  
  return (seconds1 - seconds2);
}

function updatePrices(input)
{
  var bigPrice;
  input = Ext.get(input);
  var form = input.dom.form;
  
  var timeslotSelect = form['values[time_in_day]'];
  
  if (input.dom.name == 'values[date]') {
    timeslotSelect.options.length = 0;
      
    Ext.each(dateChoices[input.getValue()], function(obj) {
      var display = true;
      
      var now = new Date();
      var userFriendly = now.getFullYear() + '-' + (now.getMonth()+1) + '-' + now.getDate();
      
      if (input.getValue() == userFriendly) {
        if (obj.cutoff) {
          if (getTimeDiff(obj.cutoff, now.getHours() + ':' + now.getMinutes()) < 0) {
            display = false;
          }
        }
      }
      
      if (display) {
        timeslotSelect.options[timeslotSelect.options.length] = new Option(obj.from + ' - ' + obj.to, obj.no);
      }
    });
    
    if (timeslotSelect.length === 0) {
      form['values[date]'].remove(form['values[date]'].selectedIndex);
    }
    
    updatePrices(timeslotSelect);
  }
  else if (input.dom.name == 'values[time_in_day]') {  
    form['values[date]'].value;
  }
  
  if ((bigPrice = Ext.get('big-price')))
  {  
    var additional = '';
  
    Ext.select('.option_dropdown').each(function(el) {
      var ident = el.dom.id.replace('values_option_', '');
      
      if (el.getValue() == '1' && pricing[ident][timeslotSelect.value])
      {
        additional += '<div><strong>' + pricing[ident][timeslotSelect.value] + '</strong>' + (descriptions[ident] ? ' - ' + descriptions[ident] : '') + '</div>';
      }
    });
  
    bigPrice.update(additional);
  }
}

function bookNowClickHandler(e, t) {
  e.preventDefault();  
  showOverlayForm(e.getTarget('a').getAttribute('href'));
}

function fontSizeHandler(e, t) {
  e.preventDefault();
  
  var vfontsize = Ext.get('content');
  
  if (!vfontsize)
  {  
    return;
  }
  
  var el = Ext.get(e.getTarget('a'));
  
  var size = 'small';
  
  if (el.hasClass('small-switch')) {
    size = 'small';
  }
  else if (el.hasClass('medium-switch')) {
    size = 'medium';
  }
  else if (el.hasClass('big-switch')) {
    size = 'big';
  }

  var sizes = ['small', 'medium', 'big'];
  
  for (var i = 0; i < sizes.length; i++) {
    vfontsize.removeClass(sizes[i] + 'text');
  }
  vfontsize.addClass(size + 'text');
  
  setcookie('fontsize', size);      
}

Ext.onReady(function() {
  Ext.select('.book_now_opener').on('click', bookNowClickHandler);
  Ext.select('overlay').setVisibilityMode(Ext.Element.DISPLAY);
  Ext.select('.resizer a').on('click', fontSizeHandler);

  Ext.select('.expandable-accordion').each(function(el) {
    new Accordion(el.dom);
  });

  var billing, address, valuesDate;

  if ((billing = Ext.get('billing_address-wrapper')) && (address = Ext.get('values_same_address')))
  {
    billing.setVisibilityMode(Ext.Element.DISPLAY);
    billing.setVisible(!address.dom.checked);

    address.on((Ext.isIE6 ? 'click' : 'change'), function(e, t) {
      billing.setVisible(!e.getTarget().checked);
    });
  }

  if ((valuesDate = Ext.get('values_date')))
  {
    updatePrices(valuesDate);
  }
});

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);