//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

Drupal.behaviors.unitAttach = function() {

  var header_bg = $('#header').css('background-color');
  var header_text = $('#header').css('color');
  $('#accommodation_legend').css('background-color', header_bg);
  $('#accommodation_legend').css('color', header_text);
  $('.unit_availability_legend').css('background-color', header_bg);
	//LOADING POPUP
	//Click the button event!
	$(".booknow.is_available a:not(.unitAttach-processed)").click(function(){
		if ($.cookie('num_people') == null) {
      var loc = location.pathname;
      loc = loc.replace(Drupal.settings.basePath, "");
      var loadurl = $(this).attr('href');
      $.get(loadurl, "", function(data){
        var searchbox = new Boxy(data, {
            title: "Book this room",
            draggable: true,
            modal: true,
            unloadOnHide: true,
            behaviours: function(c) {
                c.find('.datepicker').datepicker( {
                      showButtonPanel: true,
                      changeMonth: true,
                      changeYear: true,
                      yearRange: "-0:+2",
                      beforeShow: function(input, inst) {
                          $('.ui-datepicker').addClass("ui-datepicker-popup");
                          if($(this).attr("id") == "edit-depart-pop") {
                            $(this).val($("#edit-arrive-pop").val());
                          }
                      }
                });
                },
            afterShow: function() {
                var action = $('#accommodation-unit-block-popup').attr('action') + '?destination=' + loc;
                if(Drupal.settings.fancy_cart == 'true') {
                  $('#accommodation-unit-block-popup').attr('action', action);
                }
            }
        });
      });
		  return false;
		} else {
      if(Drupal.settings.fancy_cart == 'true') {
        show_throbber_after($(this));
        var page = $('#edit-page').val();
        var loadurl = $(this).attr('href');
        $.get(loadurl, { page: page }, function(data){
          fancy_get_cart($(this).attr('id'));
        })
      } else {
        document.location.href = $(this).attr("href");  // this is for IE
      }
    }
		return false;
	}).addClass('unitAttach-processed');
	$(".booknow").not(".is_available").children("a:not(.unitAttach-processed)").click(function(){
        var loadurl = $(this).attr('href');
        $.get(loadurl, "", function(data){
        var searchbox = new Boxy(data, {
          title: "Book this room",
          draggable: true,
          modal: true,
          unloadOnHide: true,
          behaviours: function(c) {
              c.find('.datepicker').datepicker( {
                    showButtonPanel: true,
                    changeMonth: true,
                    changeYear: true,
                    yearRange: "-0:+2",
                    beforeShow: function(input, inst) {
                        $('.ui-datepicker').addClass("ui-datepicker-popup");
                        if($(this).attr("id") == "edit-depart-pop") {
                          $(this).val($("#edit-arrive-pop").val());
                        }
                    }
              });
              c.find('#edit-op-pop').click(function() {
                $('#search_popup h3').append('<br />Checking ...');
                $.cookie('arrive', c.find('#edit-arrive-pop').val(), { path: Drupal.settings.basePath });
                $.cookie('depart', c.find('#edit-depart-pop').val(), { path: Drupal.settings.basePath });
                $.cookie('num_people', c.find('#edit-num-people').val(), { path: Drupal.settings.basePath });
              });
              var base_url = $(location).attr('href').split('?');
              c.find('#accommodation-unit-block-popup').attr('action', base_url[0]);
            return false;
          }
          });
        });
        return false;

	}).addClass('unitAttach-processed');
  $('#edit-arrive:not(.unitAttach-processed)').change(function() {
    var end = new Date($('#edit-arrive').val());
    end.setDate(end.getDate()+1);
    var newd = (end.getMonth() + 1) + '/' + end.getDate() + '/' + end.getFullYear();
    $('#edit-depart').val(newd);
  }).addClass('unitAttach-processed');
}

