//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

Drupal.unitAttach = function() {
  $('#unit-availability-block')
    .append('<input type="hidden" name="unit" value="1" />')
    .each(function () {
      var elt = new Drupal.unit(this);
    });
  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);
  $('fieldset.collapsible legend a').css('color', 'white');
  Drupal.searchcriteria();
	//LOADING POPUP
	//Click the button event!
	$(".booknow.is_available a").click(function(){
		if ($.cookie('num_people') == null) {
			//centering with css
			var eid = $(this).parent().attr("id");
			centerPopup(eid);
			//load popup
			loadPopup();
			var link = $(this).attr("href");
			$("#search_popup #accommodation-unit-block-popup").attr("action",link);
			return false;
		}
		document.location.href = $(this).attr("href");	// this is for IE
		return false;
	});
	$(".booknow").not(".is_available").children("a").click(function(){
		//centering with css
		var eid = $(this).parent().attr("id");
		centerPopup(eid);
		//load popup
		loadPopup();
		var link = window.location.href;
		$("#search_popup #accommodation-unit-block-popup").attr("action",link);
		$("input #edit-op-pop").val("Change Dates");
		return false;
	});
	//CLOSING POPUP
	//Click the x event!
	$("#searchPopupClose").click(function(){
		disablePopup();
		return false;
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	$('#search_popup input[@name="op_pop"]').unbind("click").click(function(){
		var base_path = $('#search_popup_base_path').val();
		var options = { path: base_path, expires: 0 };
		var arrive = $("#search_popup #edit-arrive-pop").val();
		var depart = $("#search_popup #edit-depart-pop").val();
		var num_people = $("#search_popup #edit-num-people").val();
		$.cookie('num_people', num_people, options);
		$.cookie('arrive', arrive, options);
		$.cookie('depart', depart, options);
		Drupal.searchcriteria();
		$("#accommodation-unit-block-popup").css('display', 'none');
		$("#please_wait").html("<p>Checking availability</p><p><b>Please wait ...</b></p>");
	});
}

Drupal.searchcriteria = function() {
/**
 * Check if we have some search days in a cookie. If so, display it
 */
  var num_people = $.cookie('num_people');
  var depart = $.cookie('depart');
  var arrive = $.cookie('arrive');
  if (num_people != null) {
  	$('.search_values').html('Arrival: ' + arrive + '<br />Departure: ' + depart + '<br />Number of people: ' + num_people);	
  }
  $('#search_popup input[@name="op_pop"]').val("Book it");
}
Drupal.unit = function(form) {
  Drupal.redirectFormSubmit($(form).attr('action'), form, this);
}

/**
 * Handler for the form redirection submission.
 */
Drupal.unit.prototype.onsubmit = function () {
  // Insert progressbar.
  // Insert progressbar and stretch to take the same space.
  this.progress = new Drupal.progressBar('ajaxeditprogress');
  this.progress.setProgress(-1, 'Fetching results');
  var el = this.progress.element;
  $(el).css({
    width: '250px',
    height: '15px',
    paddingTop: '10px',
	paddingBottom:'20px'
  });
  $('div.availability_search_results')
    .html('')
    .append(el)
    .fadeIn('slow');
}

/**
 * Handler for the form redirection completion.
 */
Drupal.unit.prototype.oncomplete = function (data) {
  //$(this.progress.element).remove();
  //this.progress = null;
  $('div.availability_search_results')
    .hide()
    .html(data)
    .end()
    .fadeIn('slow');
  Drupal.searchcriteria();
}

/**
 * Handler for the form redirection error.
 */
Drupal.unit.prototype.onerror = function (error) {
  alert(error);
  $(this.progress.element).fadeOut('slow', function() {
      $(this).remove();
    });
  this.progress = null;
}

if (Drupal.jsEnabled) {
  $(document).ready(Drupal.unitAttach);
}

/**
 * All the function for date selection popup in case cookie values are not set
 */

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#search_popup").fadeIn("slow");
		popupStatus = 1;
	}
}
//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#search_popup").fadeOut("slow");
		popupStatus = 0;
	}
}
//centering popup
function centerPopup(eid){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#search_popup").height();
	var popupWidth = $("#search_popup").width();
	var pos = findPos(document.getElementById(eid));
	//centering
	$("#search_popup").css({
		"position": "absolute",
		"top": (pos[1] - popupHeight) + "px",
		"left": (windowWidth/2-popupWidth/2) + "px"
	});
	//only need force for IE6

	$("#backgroundPopup").css({
		"height": windowHeight
	});
	document.getElementById('search_popup').scrollIntoView(true);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}
