var fotos = true;
var description = true;
var title = true;
var done = true;

function showRoom(id){
	//show ajax thinggie
	show(document.getElementById('waiter'));
	
	//hide old room
	hide(document.getElementById('descripton'));
	hide(document.getElementById('imgList'));
	
	//set both loading vars to false
	fotos = false;
	description = false;
	title = false;
	done = false;
	
	updateFotos(id);
	setTimeout('showDivs('+ id +')', 100);
}

function updateFotos(id){
	//fotos
	makeRequest('/'+ lang +'/rooms?ajax=fotos&id='+ id,
		function() {
			if (http_request.readyState == 4) {
         		if (http_request.status == 200) {
					//update page
					document.getElementById('imgList').innerHTML = http_request.responseText;
					fotos = true;
					showDivs(id);
         		}
         		http_request = false;
			}
		}
	);
}

function updateTitle(id){
	//title
	makeRequest('/'+ lang +'/rooms?ajax=title&id='+ id,
		function() {
			if (http_request.readyState == 4) {
         		if (http_request.status == 200) {
					//update page
					document.getElementById('room.naam.fotos').innerHTML = '<h1><span>'+ http_request.responseText +'</span> foto\'s</h1>';
					document.getElementById('room.naam.info').innerHTML = '<h1><span>'+ http_request.responseText +'</span> info</h1>';
					title = true;
					showDivs(id);
         		}
         		http_request = false;
			}
		}
	);
}

function updateDescription(id){
	//get description
	makeRequest('/'+ lang +'/rooms?ajax=description&id='+ id, 
		function() {
			if (http_request.readyState == 4) {
         		if (http_request.status == 200) {
					//update page
					document.getElementById('descripton').innerHTML = http_request.responseText;
					description = true;
					showDivs(id);
         		}
         		http_request = false;
			}
		}
	);
}

function hide(element){
	element.style.display = 'none';
}

function show(element){
	element.style.display = 'block';
}

function showDivs(id){
	if(description && fotos && title){
		if(!done){
			done = true;
			hide(document.getElementById('waiter'));
			
			show(document.getElementById('descripton'));
			show(document.getElementById('imgList'));
			
			//rereplace shizzle
			replaceHeaders();
			initThumbs();
		}
	}else{
		if(!fotos){
			if(!http_request){
				updateFotos(id);
			}
		}else if(!description){
			if(!http_request){
				updateDescription(id);
			}
		}else if(!title){
			if(!http_request){
				updateTitle(id);
			}
		}
		setTimeout('showDivs('+ id +')', 100);
	}
}

init[init.length] = function() {
		hide(document.getElementById('waiter'));
		hide(document.getElementById('descripton'));
		hide(document.getElementById('imgList'));
		showRoom(firstRoomId);
}

function initThumbs() {
	$$('.img-list img').each(function(img, i) {
		new ReMooz(img, {
			url: img.getParent().href,
			resizeOpacity: 1,
			positionToCenter: true,
			hideSource: false,
			resizeOptions: {
				transition: Fx.Transitions.Back.easeOut
			}
		});
	});
	var myTips = new Tips($$('.toolTipImg'), {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
}

function makeRequest(url, returnFunc) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
        http_request.overrideMimeType('text/xml');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  http_request.onreadystatechange = returnFunc;
  http_request.open('GET', url, true);
  http_request.send(null);
}