var iframeContainer;

function quicksearchSubmit(form, url, param) {	
	var div, loadingImg, closeButton, iframe, date_from, date_to;
	var params = $H();
	
	if($('unknown_date_'+param).checked == false) {
		date_from = $('date_from_'+param).getValue();
		date_to = getDateTo(date_from, $('nights_'+param).getValue());
	
		params.set('date_from', date_from);
		params.set('date_to', date_to);
	}
	params.set('searchtext', $('searchtext_'+param).getValue());
	params.set('number_adult[]', $('number_adult_'+param).getValue());
	
	params.each(function(e) {
		if(!e.value) {
			params.unset(e.key);
		}
	});
	
	if(!params.get('date_from')) {
		params.unset('date_to');
		params.set('date_unknown', '1');
	}
		
	url = url + '&reset=1&doSearch=1&waiter=1&' + params.toQueryString();
	
	if($('iframeTomas')) {
		iframe = $('iframeTomas');
		iframe.writeAttribute({ 'src': url });
		showIframe();
	}
	else {
		
		var height = document.viewport.getHeight() - 50;
	
		iframeContainer = new Element('div', { 'id': 'iframeTomasContainer', 'class': 'quicksearch', 'style': 'height: ' + height + 'px;' });
		iframe = new Element('iframe', { 'src': url, 'id': 'iframeTomas', 'class': 'tomas', 'style': 'height: ' + height + 'px;', 'marginheight': '0', 'marginwidth': '0', 'frameborder': '0', 'scrolling': 'auto' });
		closeButton = '<a class="iframeTomasCloseButton" href="javascript:void(0);" onclick="hideIframe()"><img src="templates/rooms/modules/portlet_quicksearch/img/close.png" alt="Close" /></a>';
		
		iframeContainer.insert(closeButton);
		iframeContainer.insert(iframe);
		
		showIframe();
	}
}

function toggleUnknownDate(value, param) {
	if(value.checked) {
		// disable input field and hide calendar icon
		$('date_from_'+param).disable();
		$$('div#portlet_quicksearch_'+param+' img.tcalIcon').each(function(e) {
			e.hide();
		});
	}
	else {
		// enable input field and show calendar icon
		$('date_from_'+param).enable();
		$$('img.tcalIcon').each(function(e) {
			e.show();
		});
	}
}

function showIframe() {
	// new instance of prototype-overlay
	popup = new Overlay();
	popup.show(iframeContainer, undefined, { 'afterhide': removeIframe });
	
	// lookfs if the esc key is pressed and closes the current Popup if yes.
	window.onkeydown = function(e) {
		if (!e) {
			e = window.event;
		}
		if (e.keyCode == 27) {
			hideIframe();
		}
	}
}

function hideIframe() {
	popup.hide(iframeContainer);
}

function removeIframe() {
	iframeContainer.remove();
}

function getDateTo(date_from, nights) {
	var parts = date_from.split('.');
	var day, month, year;
	
	day = parseInt(parts[0], 10);
	month = parseInt(parts[1], 10);
	year = parseInt(parts[2], 10);
	
	nights = parseInt(nights, 10);
	
	var date_to = new Date(year, month-1, day);
	var timestamp = parseInt(date_to.getTime(), 10);
	
	var date_to = new Date((timestamp + (nights * 86400 * 1000)));
	
	day = date_to.getDate();
	month = parseInt(date_to.getMonth(), 10)+1;
	year = date_to.getFullYear();
	
	return formatNumber(day) + "." + formatNumber(month) + "." + year;
}

function formatNumber(number) {
	if (number < 10) {
		number = "0" + number;
	}
	
	return number;
}
