function $tag(tag) {
	return document.getElementsByTagName(tag).item(0);
}

function $tags(tag) {
	return document.getElementsByTagName(tag);
}

var Win = {
	ajaxPage: null,

	openAjax: function() {
		var ajax;
		try {
			ajax = new XMLHttpRequest();
		} catch(ee) {
			try {
				ajax = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					ajax = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(E) {
					ajax = false;
				}
			}
		}
		return ajax;
	},
	
	show: function(page) {
		if(document.getElementById('divBg') != null) return;
		this.ajaxPage = page;
		var divBg = document.createElement('div');
		var pgSize = this.getPageSize();
		divBg.style.height = pgSize[1]+'px';
		divBg.style.width = pgSize[0]+'px';
		divBg.setAttribute('id', 'divBg');
		$tag('body').appendChild(divBg);
		this.setBackground(1);
	},
	
	setBackground: function(n) {
		var divBg = document.getElementById('divBg');
		if(n >= 70){
			divBg.onclick = this.remove;
			this.createContent();
			return;
		}
		divBg.style.MozOpacity = n/100;
		divBg.style.opacity = n/100;
		divBg.style.filter = "alpha(opacity="+n+")";
		n = n+20;
		setTimeout('Win.setBackground('+n+')', 1);
	},
		
	remove: function() {
		if(document.getElementById('divBg')!=null) $tag('body').removeChild(document.getElementById('divBg'));
		if(document.getElementById('divContent2')!=null) $tag('body').removeChild(document.getElementById('divContent2'));
		if(document.getElementById('_loading')!=null) $tag('body').removeChild(document.getElementById('_loading'));
	},
	
	createContent: function() {
		this.showLoading();
		var ajax = this.openAjax();
		ajax.open('get', this.ajaxPage, true);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					if(document.getElementById('divBg')==null) return;
					$tag('body').removeChild(document.getElementById('_loading'));
					var divContent2 = document.createElement('div');
					divContent2.setAttribute('id', 'divContent2');
					var html = ajax.responseText;
					html=html.replace(/\+/g," ");
					html=unescape(html);
					divContent2.innerHTML = html;
					divContent2.style.width = '0px';
					divContent2.style.height = '10px';
					$tag('body').appendChild(divContent2);
					Win.setSize(800, 600);
				} else {
					this.remove();
				}
			} else {
				if(document.getElementById('divBg')==null) return;
			}
		}
		ajax.send(null);
	},
	
	setSize: function(w, h) {
		if(!document.getElementById('divContent2')) return;
		var div = document.getElementById('divContent2');
		var he = div.style.height.split('px')[0];
		var wi = div.style.width.split('px')[0];
		if((Math.abs((w)-wi)<2)&&(Math.abs((h)-he)<2)) return;
		if(Math.abs((w)-wi)>2) {
			div.style.width = (wi - (wi-(w))/3)+"px";
		} else {
			div.style.height = (he - (he-(h))/3)+"px";
		}
		setTimeout('Win.setSize('+w+', '+h+')', 80);
	},
	
	showLoading: function() {
		var img = document.createElement('img');
		img.setAttribute('id', '_loading');
		with(img.style) {
			position = 'absolute';
			left = '50%';
			top = '50%';
			marginLeft = '-30px';
			marginTop = '-30px';
			zIndex = 99;
		}
		img.src = 'imgs/loading-mac.gif';
		$tag('body').appendChild(img);
	},
	
	getPageSize: function(){
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}
};
