var blockFixedParams = {};

function updateBlockFixedParams() {
	blockFixedParams.jqEndOfContent = $("#end_of_content");
	blockFixedParams.jqBlockCtr = $("#block_fixed");
	blockFixedParams.blockOffset = $("#block_fixed_offset").offset();
	
	// height is incorrect until we set position: absolute
	blockFixedParams.rightCtrHeight = blockFixedParams.jqBlockCtr.css({position: 'absolute'}).height()+12;
	blockFixedParams.jqBlockCtr.css({position: ''});
}

$(document).ready(function() {
	updateBlockFixedParams();
	
	$(window).scroll(function(event) {
		var scrollTop = $(document).scrollTop();
		var windowHeight = $(window).height();
		var pixelsAboveMap = blockFixedParams.jqEndOfContent.offset().top - scrollTop;
		
		if (windowHeight < blockFixedParams.rightCtrHeight-12) return false;
		
		if (scrollTop > blockFixedParams.blockOffset.top-12) {
			// the moving block won't fit entirely on the screen above the end of content (above the hotels map)
			if (blockFixedParams.rightCtrHeight > pixelsAboveMap) {
				if (blockFixedParams.jqEndOfContent.offset().top-blockFixedParams.rightCtrHeight < blockFixedParams.blockOffset.top) {
					blockFixedParams.jqBlockCtr.css({position: '', top: '', zIndex: ''});
				}
				else {
					blockFixedParams.jqBlockCtr.css({
						position: 'absolute',
						top: (blockFixedParams.jqEndOfContent.offset().top-blockFixedParams.rightCtrHeight+$('.spacer2:first').height())+'px'
					});
				}
			} else {
				if ($.browser.msie && $.browser.version < 7) {
					blockFixedParams.jqBlockCtr.css({
						position: 'absolute',
						top: (scrollTop+12)+'px'
					});
				} else {
					blockFixedParams.jqBlockCtr.css({
						position: 'fixed',
						top: '12px'
					});
				}
			}
		} else {
			blockFixedParams.jqBlockCtr.css({position: '', top: '', zIndex: ''});
		}
	});
});

var globalAjaxRequests = [];

function ajaxFilterSubmit(o) {
	if (!o) o = {};

 	while (globalAjaxRequests.length > 0) {
 		var req = globalAjaxRequests.shift();
 		// abort previous request if abort method is supported and if it is not finished already
 		if (typeof req.oReq.abort != 'undefined' && req.oReq.readyState != 4) {
			req.oReq.abort();
	 		req.oLoading.hide();
	 	} 
 	}
	
	var oLoading = new StuffedLoading({
		html: (o.loadingImage+o.loadingText),
		width: '300px',
		padding: '25px'
	});
	oLoading.show();

	var jqResultsForm = $("#results_form");
	var jqFilterForm = $("#filter_form");
	var data = getFormValues(jqFilterForm.get(0));
	
	var resultsData = getFormValues(jqResultsForm.get(0)) || {};
	
	// combining values from two forms
	for (key in resultsData) {
		data[key] = resultsData[key];	
	}
	
	data.sub_action = 'hotels_col';
	
	if (o.filterApplied || o.historyUpdate) data.filter_applied = 1;
	if (o.removeFilter) data.remove_filter = 1;
	if (o.page) data.page = o.page;
	if (o.r_hotel_id) data.r_hotel_id = o.r_hotel_id;
	if (o.hash_id) data.hash_id = o.hash_id;
	if (o.historyUpdate) data.history_update = 1;
  if (o.areaFilter) data.area_visible_hotels = map.af.getVisibleMarkersIds().toString();
  
 	var oReq = $.ajax({
 		type: 'post',
 		url: jqResultsForm.attr('action'),
 		data: data,
    dataType: 'json',
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			oLoading.hide();
			if (XMLHttpRequest.responseText) $.prompt(XMLHttpRequest.responseText);
		},
		success: function (data, textStatus) {
			oLoading.html(o.loadingImage+o.updatingText);
			
			setTimeout(function() {
				$("#block_fixed").html(data.filter);
				updateBlockFixedParams();
				$("#hotels_list").html(data.hotels_list);
				$("#above_map").html(data.above_map);
				
				var jqShowing = $("#showing_map").parents("tr:first");
				if (data.showing_map) {
					$("#showing_map").html(data.showing_map);
					jqShowing.show();
				} else {
					jqShowing.hide();
				}
				
	      // refresh google map
        updateMarkers({hotels: data.gmap_hotels});
        
        if (o.areaFilter) {
          map.af.remove();
          map.af.setup(map);
          map.af.show();
        }
	    	
	    	if (data.page) currentPage = data.page;
	    	if (data.hash_id && !o.historyUpdate) $.history.add(data.hash_id);
        if (data.filter_applied) filterApplied = true;
        
        var scrollId = "#above_map";
	    	if (data.r_hotel_id && $("#h"+data.r_hotel_id).length > 0) {
					scrollId = "#h"+data.r_hotel_id;
				}

				$.scrollTo(scrollId, {
					duration: 1000, 
					easing: 'swing',
					onAfter: function() { oLoading.hide()	}
				});
			}, 0);
		}
 	});
 	
 	globalAjaxRequests.push({oReq: oReq, oLoading: oLoading});
 	
	return false;
}