// JavaScript Document
$ = new Function('x', 'return document.getElementById(x)');
IE = (navigator.appName.indexOf('Microsoft'))==-1?false:true;

function openSubWin(strURL, pixW, pixH) {
	strLocation = strURL;strWHandle = "tasbSubWin";
	strProps = "resizable=no,scrollbars=1,toolbar=no,location=no,directories=no,status=no,menubar=no,width=" + pixW + ",height=" + pixH + ",top=10,left=10";
	window.open(strLocation, strWHandle, strProps);
}

function Page(xhttp, url) {
	this.xhttp = xhttp;
	this.url = url;
	this.text = "";
}

var litebox = {
	coverid: "litebox_cover",
	liteid: "litebox_wrapper",
	textid: "litebox_content",
	
	tstart: "<!--begin litebox content-->",
	tend: "<!--end litebox content-->",
	
	scrollx:0,
	
	xobj: false,
	
	files_loaded: new Array(),
	files_loading: new Array(),
	
	//Files left to load
	files_left: new Array(),
	files_order: new Array(),
	
	start: function() {
		this.make_cover();
		
		var ltext = this.get_litebox_text( document.getElementById(this.textid).innerHTML );
		if(ltext.length > 65) {
			litebox.show();
		}
	},
	
	load: function(url) {
		var found = false;
		
		for( var i=0; i<this.files_loaded.length; i++) {
			
			if(this.files_loaded[i].url == url) {
				found = this.files_loaded[i];
				break;
			}
			
		}
		if(found) {
			this.set_litebox_text(found);
			this.show();
		} else {
			var xhttp = false;
			
			if(window.ActiveXObject) {
				xhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} else if( window.XMLHttpRequest ) {
				xhttp = new XMLHttpRequest();
			}
			
			if(xhttp) {
				this.xobj = new Page(xhttp, url);
				this.xobj.xhttp.onreadystatechange = litebox.handler;
				this.xobj.xhttp.open("GET", url, true);
				this.xobj.xhttp.send(null);
			}
		}
	},
	
	load_all: function() {
		if(this.files_left.length > 0) {
			
			for( var i=0; i<this.files_left.length; i++) {
				var xhttp = false;
				var url = this.files_left[i];
				
				if(window.ActiveXObject) {
					xhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} else if( window.XMLHttpRequest ) {
					xhttp = new XMLHttpRequest();
				}
				
				if(xhttp) {
					xhttp.onreadystatechange = litebox.handle_all;
					xhttp.open("GET",url, true);
					this.files_loading.push( new Page(xhttp,url) );
					
				}
			}
			
			for( var i=0; i<this.files_loading.length; i++) {
				this.files_loading[i].xhttp.send(null);
			}
		} else {
			this.print_all();
		}
	},
	
	add_file: function(url) {
		this.files_left.push(url);
		this.files_order.push(url);
	},
	
	show: function() {
		
		$(this.liteid).style.display = "block";
		this.set_cover_size();
		$(this.coverid).style.display = "block";
		window.scrollTo(0,0);
		
	},
	
	hide: function() {
		window.scrollTo(0,this.scrollx);
		$(this.liteid).style.display = "none";
		$(this.coverid).style.display = "none";
		$(this.textid).innerHTML = "";
	},
	
	make_cover: function() {
		var cdiv = document.createElement("a");
		cdiv.id = this.coverid;
		cdiv.href="javascript:litebox.hide()";
		
		document.getElementsByTagName("body")[0].appendChild(cdiv);
	},
	
	set_cover_size: function() {
		var cdiv = $(this.coverid);
		
		var scrolly = 0;
		var scrollx = 0;
		
		var screeny = 0;
		var screenx = 0;
		
		//scrolling 
		if(document.documentElement.scrollHeight) {
			var scrolly = document.documentElement.scrollHeight;
			var scrollx = document.documentElement.scrollWidth;
			this.scrollx = document.documentElement.scrollTop;
			
		} else if (document.body.scrollHeight) {
			var scrolly = document.body.scrollHeight;
			var scrollx = document.body.scrollWidth;
			this.scrollx = document.body.scrollTop;
		}
		
		//window sizes
		if( document.body.innerHeight) {
			screeny = document.body.innerHeight;
			screenx = document.body.innerWidth;
		}
		else if( document.documentElement.clientHeight) {
			screeny = document.documentElement.clientHeight;
			screenx = document.documentElement.clientWidth;
		}
		
		
		if(scrolly > screeny) {
			screeny = scrolly;
		}
		if(scrollx > screenx) {
			screenx = scrollx;
		}
		var stylecss = "border-width:0px; background-color:#000000; filter: alpha(opacity=30); opacity:.3; display:block; position:absolute; top:0px; left:0px; width:"+screenx+"px; height:"+screeny+"px";
		cdiv.setAttribute("style",stylecss);
		
		cdiv.style.backgroundColor = "#000000";
		cdiv.style.borderWidth = "0px";
		cdiv.style.filter = "alpha(opacity=30)";
		cdiv.style.position = "absolute";
		cdiv.style.zIndex = "50";
		cdiv.style.top = "0px";
		cdiv.style.left = "0px";
		cdiv.style.width = screenx+"px";
		cdiv.style.height = screeny+"px";
		cdiv.style.display = "none";
		
		cdiv.innerHTML="&nbsp;";
		
	},
	
	get_litebox_text: function(html) {
		html = ""+html;
		
		var start = html.indexOf(this.tstart) + this.tstart.length;
		var end = html.indexOf(this.tend);
		
		return html.substring(start,end);
	},
	
	set_litebox_text: function(obj) {
		document.getElementById(litebox.textid).innerHTML = obj.text;
	},
	
	print_all: function() {
		var total = "";
		
		for( var a=0; a<this.files_order.length; a++) {
			for( var b=0; b<this.files_loaded.length; b++) {
				
				if( this.files_order[a] == this.files_loaded[b].url) {
					total += this.files_loaded[b].text;
				}
			}
			
			
		}
		
		document.getElementById(litebox.textid).innerHTML = total;
		this.show();
	},
	
	remove_left: function(url) {
		for( var r=0; r<this.files_left.length; r++) {
			if(this.files_left[r] == url) {
				this.files_left.splice(r,1);
				return;
			}
		}
	},
	
	handle_all: function() {
		for( var j=0; j< litebox.files_loading.length; j++) {
			var xobj = litebox.files_loading[j];
			if(xobj.xhttp.readyState==4 && xobj.xhttp.status==200) {
				
				var text = xobj.xhttp.responseText;
				text = litebox.get_litebox_text(text);
				xobj.text = text;
				
				for( var z=0; z<litebox.files_left.length; z++) {
					if( litebox.files_left[z] == xobj.url) {
					litebox.files_loaded.push(xobj);
					litebox.remove_left(xobj.url);
					break;
					}
				}
				
				if( litebox.files_left<1) {
					litebox.print_all();
				}
			}
		}
	},
	
	handler: function() {
		if(litebox.xobj.xhttp.readyState==4 && litebox.xobj.xhttp.status==200) {			
			//refine text
			var text = litebox.xobj.xhttp.responseText;
			text = litebox.get_litebox_text(text);
			
			//set text to object
			litebox.xobj.text = text;
			//push object to loaded
			litebox.files_loaded.push(litebox.xobj);
			//set text to litebox
			litebox.set_litebox_text(litebox.xobj);
			
			litebox.remove_left(litebox.xobj.url);
			litebox.show();
		}
	}
}
