var currentType;
var currentIndex;
var isFullSize = false;
var preloadComplete = false;
var maxWidth = 600;
var maxHeight = 800;
var preloadedImages = new Array();
// var gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/;
var isMSIE = /*@cc_on!@*/false;

/* Tip advice (text is passed as parameter) */
function show_message(event,message) {
	var text = '<table cellpadding="0" cellspacing="0"><tr><td style="text-align: justify">';
	text += message;
	text += '</td></tr></table>';
	show('float_baloon',text,event);
}

/* Show ratechange message */
function show_advice(event,ratechange,netprice) {
	var text = '<table cellpadding="0" cellspacing="0" class="form_hotel_info_advice">';
	if (ratechange) text += '<tr><td>' + mess_ratechange + '</td></tr>';
	if (netprice) text += '<tr><td>' + mess_netprice + '</td></tr>';
	text += '</table>';
	show('float_baloon',text,event);
}

function setImage(imageOrImageName) {
	
	if (!preloadComplete) return false;
	
	var image = (typeof imageOrImageName == 'string') ? document.getElementById(imageOrImageName) : imageOrImageName;
	var changed = false;
		
	if (arguments.length > 1) {
		changed = (currentType != arguments[1]);
		currentType = arguments[1];
	}
	
	if (arguments.length > 2) {
		changed = (changed || (currentIndex != arguments[2]));
		currentIndex = arguments[2];
	}
	
	/* Remove image */
	isFullSize = false;
	image.style.display = (image.style.display == '' && !changed) ? 'none' : '';
	if (!image.style.display) resizeImage(image);
	return true;
}

function resizeImage(image) {
	
	var sourceImage = preloadedImages[currentType][currentIndex];
	
	if (sourceImage.complete) {
		var XresizeFactor = (sourceImage.width > maxWidth) ? maxWidth / sourceImage.width : 1;
		var YresizeFactor = (sourceImage.height > maxHeight) ? maxHeight / sourceImage.height : 1;
		var resizeFactor = ((XresizeFactor < YresizeFactor) ? XresizeFactor : YresizeFactor);
		var enlarge = (resizeFactor < 1 && !isFullSize);
		var reduce = (isFullSize && (sourceImage.width > maxWidth || sourceImage.height > maxHeight));
		var newWidth = (!isFullSize) ? parseInt((sourceImage.width * resizeFactor),10) + 'px' : 'auto';
		var newHeight = (!isFullSize) ? parseInt((sourceImage.height * resizeFactor),10) + 'px' : 'auto';
		
		isFullSize = !isFullSize;
		image.src = sourceImage.src;
		image.style.width = newWidth;
		image.style.height = newHeight;
		image.style.cursor = (enlarge || reduce) ? 'pointer' : 'default';
		image.title = (enlarge) ? msgEnlarge : ((reduce) ? msgReduce : '');
		image.style.display = '';
	}	else {
		currentType = '';
		currentIndex = '';
	}
	return;
}

function focusImage(idImage,type,index,forceFocus,event) {
	if (!preloadComplete)	 return false;
	
	var image = document.getElementById(idImage);
	setImage(image,type,index);
	if (image.style.display == '' || forceFocus) image.focus();
	return true;
}

function sb(event) {
	if (!preloadComplete && !isMSIE) show('float_baloon',msgNotLoaded,event); // IE7 doesn't allow DOM modify untill page is complete
	return true;
}

function hb() {
	return hide('float_baloon');
}

function ghrs_showRoomDescriptions(id,url){
	var div_desc = document.getElementById(id+'_description');
	if(div_desc) ghrs_showDescription(id);
	else{
		document.getElementsByTagName('body')[0].style.cursor = 'wait';
		document.getElementById(id+'_link').style.display = 'none';
		ghrs_makeRequest(url + '&param[room_id_ajax]=' + id,'ghrs_displayRoomDescription',id);
	}
}

function ghrs_displayRoomDescription(http_request,id){
	if (http_request.readyState == 4) {
		// If the state is ready
		if (http_request.status == 200) {
			/* If the request has been executed correctly */
			var substring_after = http_request.responseText.indexOf("<result>");
			var substring_before = http_request.responseText.lastIndexOf("</result>");
			if(substring_after>0 && substring_before>0){
				var result = http_request.responseText.substring(substring_after+8,substring_before);
				var attributes = {'id':id + '_description', 'style':'display:none;'};
				var div_desc = ghrs_createElementWithAttribute('div',attributes);
				var div = document.getElementById(id);
				div.appendChild(div_desc);
				div_desc = document.getElementById(id+'_description');
				div_desc.innerHTML = result; /* + '[<span class="link" style"float:right!important;" onClick="ghrs_hideDescription(\'' + id + '\');">close</span>]';*/
				ghrs_showDescription(id);
				document.getElementsByTagName('body')[0].style.cursor = 'default';
			}
			else{
				document.getElementsByTagName('body')[0].style.cursor = 'default';
				document.getElementById(id+'_link').style.display = '';
			}
		}
	}
}


function ghrs_showDescription(id){
	document.getElementById(id+'_description').style.display = '';
	document.getElementById(id+'_link').style.display = 'none';
}

function ghrs_hideDescription(id){
	document.getElementById(id+'_description').style.display = 'none';
	document.getElementById(id+'_link').style.display = '';
}

function ghrs_bookingConditionsSH(id){
	var contitions = document.getElementById('booking_conditions_' + id);
	var button = document.getElementById('conditions_button_' + id);
	if(contitions.style.display=='none'){
		contitions.style.display='';
		button.src = String(button.src).replace('arw_opn.gif','arw_cls.gif');
	}
	else{
		contitions.style.display='none';
		button.src = String(button.src).replace('arw_cls.gif','arw_opn.gif');
	}
}

