/************************************************/
/******************ROOM GALLERY******************/
/************************************************/
	var NEXT_IMAGE;
	var IMAGE_STACK;
	
	var InStack = function(imgURL) {
		for (var i=0; i < IMAGE_STACK.length; i++) {
			if (IMAGE_STACK[i] == imgURL) {
				return true;
				break;
			}
		}
		return false;
	}
	
	var PreloadImages = function() {
		IMAGE_STACK = new Array();
		$("#roomGallery a").each(function() {
			IMAGE_STACK.push($(this).attr('href'));
		});
		while (0 < IMAGE_STACK.length) {
			$("<img />").attr('src',IMAGE_STACK[0]).load(
				IMAGE_STACK.shift()
			);
		}
	}
	
	var fadeImgOut = function() {
		$("#roomImage").fadeOut('slow',changeImage);
	}
	
	var fadeImgIn = function() {
		if (InStack(NEXT_IMAGE)) {
			$("<img />").attr('src',NEXT_IMAGE).load(function() {
				$("#roomImage").fadeIn('slow');
			});
		} else {
			$("#roomImage").fadeIn('slow');
		}
	}
	
	var changeImage = function() {
		$("#roomImage").attr('src',NEXT_IMAGE);
		fadeImgIn();
	}
	
	var roomGalleryBindings = function() {
		$("#roomGallery a").bind('click',function(e) {
			e.preventDefault();
			NEXT_IMAGE = $(this).attr('href');
			fadeImgOut();
		});
		
		$("#modalWindow").bind('click',function(e) {
			var isModalObject = false;
			var targ = $(e.target);
			for (var i = 0; i < targ.parents().length; i ++) {
				if (i > 0) {
					targ = targ.parent()
				}
				if (targ.attr('id') == 'innerContainer' || targ.attr('id') == 'roomGallery') {
					isModalObject = true;
					break;
				} else if (targ.attr('id') == 'roomContainer') {
					break;
				}
			}
			if (!isModalObject) {
					closeModal();
			}
		});
	}

/************************************************/
/*****************MODAL WINDOW*******************/
/************************************************/
	var closeModal = function() {
		$("#IE_imgBG").hide();
		$("#modalWindow").hide();
		$("#modalBackground").hide();
		$("#modalClose").empty();
		$("#modalWindow").empty();
		//$("#modalWindow").attr('src','');
	}
	
	var centerOnScreen = function (element) {
		var modWin = element;
		var obj = element;
		obj.css('top', $(window).height()/2-modWin.height()/2);
		obj.css('left', $(window).width()/2-modWin.width()/2);
	}
	
	var showModal = function() {
		PreloadImages();
		setModal();
		roomGalleryBindings();
		$('#modalWindow').after('<a href="#" id="modalClose"><img src="/images/button_icon_x.gif" alt="close this window" /></a>');
		$("#modalBackground").bind('click',function() {
			closeModal();
		});
	}
	
	var setModal = function () {
		$("#modalBackground").width($(document).width());
		$("#modalBackground").height($(document).height());
		centerOnScreen($("#modalWindow"));
		$("#modalWindow").css("opacity",0)
			.show()
			.animate({'opacity':1},'slow',function() {
				var pos = $("#modalWindow").offset();
				$("#modalClose").empty()
				.append('<img src="/images/button_icon_x.gif" alt="close this window" />')
				.css({'top':pos.top + 'px','right':(pos.left + 5) + 'px'})
				.fadeIn('slow')
				.bind('click',function(e) {
					e.preventDefault();
					closeModal();
				});
				$("#IE_imgBG").show();
			});
		$("#IE_imgBG").css( 'top', $("#sideLeft").offset().top + 'px' );
		$("#IE_imgBG").css( 'left', $("#sideLeft").offset().left + 'px' );
	}
	
	var clickFunction = function(url) {
		$("#modalBackground").show();
		$("#modalBackground").fadeTo("fast", 0.66);
		url = url + "index.htm?rand=" + Math.random() + " #roomContainer";
		$("#modalWindow").load(url,showModal);
		//$("#modalWindow").attr('src',url);
	}
	
	$(window).bind('resize', function() {
		if( $("#modalWindow").is(":visible") ) {
			setModal();
		}
	});
	
	$(window).unload(function () {
		closeModal();
	});
	
	$(document).ready(function() {
		
		$('#modalWindow').before('<div id="modalBackground"></div>');
		$('#modalWindow').before('<div id="IE_imgBG"></div>');
		
		closeModal();
		
		$('.roomColumn a').bind('click',function(e) {
			e.preventDefault();
			clickFunction($(this).attr('href'));
		});
		
		$('.slide .inner a').bind('click',function(e) {
			e.preventDefault();
			clickFunction($(this).attr('href'));
		});
		
	});
