var STRELLIDSEQ = 0;
var lbox = null;

function documentScroll(){
	if (document.body && document.body.scrollTop)
		scrollTop = document.body.scrollTop;
	else
		scrollTop = document.getElementsByTagName('html').item(0).scrollTop;
	if (document.body && document.body.scrollLeft)
		scrollLeft = document.body.scrollLeft;
	else
		scrollLeft = document.getElementsByTagName('html').item(0).scrollLeft;
	return {left: scrollLeft, top: scrollTop};
}

function startResizeAnimation(sizeto, window, inc, delay, onfinish){
	var wnd = document.getElementById(window);
	if (typeof onfinish == 'function' && !wnd.onResizeAnimationonFinish){
		wnd.onResizeAnimationonFinish = onfinish;
	}
	var setRefresh = false;
	if (wnd.clientHeight < sizeto.height){
		wnd.style.height = ((wnd.clientHeight + inc >= sizeto.height)?sizeto.height:(wnd.clientHeight + inc)) + 'px';
		setRefresh = true;
	} else{
		if (wnd.clientWidth < sizeto.width){
			wnd.style.width = ((wnd.clientWidth + inc >= sizeto.width)?sizeto.width:(wnd.clientWidth + inc)) + 'px';
			setRefresh = true;
		} else{
			var onfinish = wnd.onResizeAnimationonFinish;
			wnd.onResizeAnimationonFinish = null;
			if (typeof onfinish == 'function')
				onfinish(wnd);
		}
	}
	var scroll = documentScroll();
	wnd.style.left = (document.getElementsByTagName('html').item(0).clientWidth - wnd.clientWidth) / 2 +scroll.left + 'px';
	wnd.style.top = (document.getElementsByTagName('html').item(0).clientHeight - wnd.clientHeight) / 2 + scroll.top + 'px';
	if (setRefresh)
		setTimeout('startResizeAnimation({width:'+sizeto.width+', height: '+sizeto.height+'}, \''+window+'\', '+inc+', '+delay+');', delay);
}

var _strellTextBox = function(){
	var body = document.getElementsByTagName('body').item(0);
	this.shadow = document.createElement('div');
	this.shadow.style.width = body.clientWidth + 'px';
	this.shadow.style.height = body.clientHeight + 'px';
	this.shadow.style.background = 'black';
	this.shadow.style.display = 'none';
	
	this.shadow.style.position = 'absolute';
	this.shadow.style.left = '0px';
	this.shadow.style.top = '0px';
	this.shadow.style.zIndex = '10000';
	this.shadow.id = 'strell_textbox_shadow_id'+(++STRELLIDSEQ);
	body.appendChild(this.shadow);
	
	this.window = document.createElement('div');
	this.window.style.display = 'none';
	this.window.style.position = 'absolute';
	this.window.style.top = '10px';
	this.window.style.left = '100px';
	this.window.style.height = '100px';
	this.window.style.width = '100px';
	this.window.style.background = 'white';
	this.window.id = 'strell_textbox_window_id'+(++STRELLIDSEQ);
	this.window.style.zIndex = '10001';
	this.window.instance = this;
	this.shadow.instance = this;
	if (navigator.userAgent.indexOf('MSIE') != -1){
		var frame = document.createElement('iframe');
		frame.className = 'ieIframeFix';
		this.shadow.appendChild(frame);
		this.frame = frame;
		frame = null;
	}
	body.appendChild(this.window);
	setOpacity(this.shadow.id, 0);
	setOpacity(this.window.id, 0);
	this.shadow.onmouseup = function (){
		this.instance.releaseWindow();
	}
	if (typeof this.frame != 'undefined'){
		this.frame.onmouseup = this.shadow.onmouseup; 
		this.frame = null;
	}
	this.visible = function(){
		return this.window.style.display == '';
	}
	this.setContentStatic = function(text, width, height, onopen, onclose){
		if (typeof width != 'undefined' && typeof height != 'undefined'){
			this.window.style.width = width + 'px';
			this.window.style.height = height + 'px';
			this.window.container.style.width = this.window.clientWidth + 'px';
			this.window.container.style.height = this.window.clientHeight + 'px';
			var scroll = documentScroll();
			this.window.style.left = (document.getElementsByTagName('html').item(0).clientWidth - this.window.clientWidth) / 2 + scroll.left + 'px';
			this.window.style.top = (document.getElementsByTagName('html').item(0).clientHeight - this.window.clientHeight) / 2 + scroll.top + 'px';
		}
		this.window.container.innerHTML = text;
		this.window.btnClose.style.left = this.window.clientWidth - 16  - 5 + 'px';
		this.window.container.appendChild(this.window.btnClose);
		if (typeof onclose == 'function')
			this.onclose = onclose;
		if (typeof onopen == 'function')
			onopen();
	}
	this.setContent = function(text, width, height, onopen, onclose){
		if (navigator.userAgent.indexOf('MSIE') != -1){
			this.setContentStatic(text, width, height, onopen, onclose);
			return;
		}
		this.onclose = onclose;
		this.window.container.textToSet = text;
		this.window.container.btnClose = this.window.btnClose;
		this.window.container.sizeto = (typeof width != 'undefined' && typeof height != 'undefined')?{"width": width, "height": height}:null;
		this.window.onOpenCallBack = (typeof onopen != 'undefined')?onopen:null;
		opcityTo(this.window.container.id, 0, -0.05, 40, function(id){
			if (document.getElementById(id).sizeto != null){
				startResizeAnimation(document.getElementById(id).sizeto, document.getElementById(id).parentNode.id, 20, 1, function(wnd){
					wnd.container.innerHTML = wnd.container.textToSet;
					wnd.container.btnClose.style.left = wnd.clientWidth - 16  - 5 + 'px';
					wnd.container.style.width = wnd.clientWidth + 'px';
					wnd.container.style.height = wnd.clientHeight + 'px';
					wnd.container.appendChild(wnd.container.btnClose);
					opcityTo(wnd.container.id, 1, 0.05, 40, function(id){
						if (document.getElementById(id).parentNode.onOpenCallBack)
							document.getElementById(id).parentNode.onOpenCallBack();
					}, wnd.id);
				});
			}
			else{
				document.getElementById(id).innerHTML = document.getElementById(id).textToSet;
				document.getElementById(id).appendChild(document.getElementById(id).btnClose);
				opcityTo(id, 1, 0.05, 40, function(){});
			}
		}, this.window.container.id);
	}
	this.appendContent = function(wnd, content){
		if (this.shadow.style.display != '' || this.window.style.display != '')	
			return;
		var div = document.createElement('div');
		div.style.position = 'relative';
		div.style.width = wnd.clientWidth + 'px';
		div.style.height = wnd.clientHeight + 'px';
		div.id = 'strell_textbox_window_rel_id'+(++STRELLIDSEQ);
		div.innerHTML = content;
		div.style.overflow = 'hidden';
		wnd.appendChild(div);
		wnd.container = div;
		
		var btnClose = document.createElement('div');
		btnClose.style.width = '16px';
		btnClose.style.height = '16px';
		btnClose.style.left = wnd.clientWidth - 16  - 5 + 'px';
		btnClose.style.top = '5px';
		btnClose.style.position = 'absolute';
		btnClose.style.zIndex = '2';
		btnClose.style.background = 'url(/images/textLightBox/btnClose.gif) left top no-repeat';
		btnClose.style.cursor = 'pointer';
		btnClose.instance = wnd.instance;
		wnd.btnClose = btnClose;
		btnClose.onmouseover = function(){
			this.style.background = 'url(/images/textLightBox/btnCloseHover.gif) left top no-repeat';
		}
		btnClose.onmouseout = function(){
			this.style.background = 'url(/images/textLightBox/btnClose.gif) left top no-repeat';
		}
		btnClose.onclick = function(){
			if (typeof this.instance.shadow.onmouseup == 'function')
				this.instance.shadow.onmouseup();
		}
		div.appendChild(btnClose);
	}
	this.animateWindow = function(onopen){
		this.window.style.width = '1px';
		this.window.style.height = '1px';
		var scroll = documentScroll();
		this.window.style.left = (document.getElementsByTagName('html').item(0).clientWidth - this.window.clientWidth) / 2 + scroll.left + 'px';
		this.window.style.top = (document.getElementsByTagName('html').item(0).clientHeight - this.window.clientHeight) / 2 + scroll.top + 'px';
		setOpacity(this.window.id, 0);
		this.window.style.display = '';
		this.window.onopenWindow = onopen;
		startResizeAnimation({height: this.wndHeight, width: this.wndWidth}, this.window.id, 20, 1, function(wnd){
			wnd.instance.appendContent(wnd, wnd.instance.content);			
			if (typeof wnd.onopenWindow == 'function')
				wnd.onopenWindow(wnd);
		});
		opcityTo(this.window.id, 1, 0.05, 40, function(){});
	}
	this.showStatic = function(content, width, height, onopen, onclose){
		this.shadow.style.width = body.clientWidth + 'px';
		this.shadow.style.height = body.clientHeight + 'px';
		this.shadow.style.display = '';
		setOpacity(this.shadow.id, 0.4);
		var scroll = documentScroll();
		this.window.style.display = '';
		setOpacity(this.window.id, 1);
		this.window.style.width = width + 'px';
		this.window.style.height = height + 'px';
		this.window.style.left = (document.getElementsByTagName('html').item(0).clientWidth - this.window.clientWidth) / 2 + scroll.left + 'px';
		this.window.style.top = (document.getElementsByTagName('html').item(0).clientHeight - this.window.clientHeight) / 2 + scroll.top + 'px';
		
		
		this.appendContent(this.window, content);
		this.onclose = onclose;
		if (typeof onopen == 'function')
			onopen();
	}
	
	this.showWindow = function(content, width, height, onopen, onclose){
		if (navigator.userAgent.indexOf('MSIE') != -1){
			this.showStatic(content, width, height, onopen, onclose);
			return;
		}
		this.content = content;
		this.wndWidth = width;
		this.wndHeight = height;
		this.onclose = onclose;
		var scroll = documentScroll();
		//this.shadow.style.left = scroll.left + 'px';
		//this.shadow.style.top = scroll.top + 'px';
		this.shadow.style.width = body.clientWidth + 'px';
		this.shadow.style.height = body.clientHeight + 'px';
		var id = this.shadow.id;
		document.getElementById(id).style.display = '';
		this.shadow.onopenWindow = typeof onopen == 'function'?onopen:null;
		opcityTo(id, 0.4, 0.1, 40, function(id){
			document.getElementById(id).instance.animateWindow(document.getElementById(id).onopenWindow);
		}, id);	
	}
	this.releaseWindow = function(){
		if (navigator.userAgent.indexOf('MSIE') != -1){
			setOpacity(this.window.id, 0);
			this.window.style.display = 'none';
			setOpacity(this.shadow.id, 0);
			this.shadow.style.display = 'none';
			if (typeof this.onclose == 'function')
				this.onclose();			
			for (tmp = this.window.firstChild; tmp != null;){
				var old = tmp;
				tmp = old.nextSibling;
				this.window.removeChild(old);
			}			
			return;
		}
		opcityTo(this.window.id, 0, -0.2, 40, function(idwindow, id){
			var wnd = document.getElementById(idwindow);
			wnd.style.display = 'none';
			wnd.instance.releaseShadow();
		}, this.window.id);
	}
	this.releaseShadow = function(){
		var id = this.shadow.id;
		opcityTo(id, 0, -0.1, 40, function(id){
			var shadow = document.getElementById(id);
			shadow.style.display = 'none';
			if (typeof shadow.instance.onclose == 'function')
				shadow.instance.onclose();
			var wnd = shadow.instance.window;
			for (tmp = wnd.firstChild; tmp != null;){
				var old = tmp;
				tmp = old.nextSibling;
				wnd.removeChild(old);
			}
		}, id);	
	}
	this.unload = function(){
		/*this.shadow.onopenWindow = null;
		this.window.onopenWindow = null;
		if (this.window.container){
			this.window.container.textToSet = null;
			this.window.container.sizeto = null;
			if (this.window.container.btnClose)
				this.window.container.btnClose.instance = null;
			this.window.container.btnClose = null;
		}
		this.window.container = null;
		this.window.onOpenCallBack = null;
		if (this.window.btnClose != null){
			this.widnow.btnClose.onclick = null;
			this.widnow.btnClose.onmouseout = null;
			this.widnow.btnClose.onmouseover = null;
			this.window.btnClose.instance = null;
		}
		this.window.btnClose = null;
		this.window.instance = null;
		this.shadow.onmouseup = null;
		this.shadow.instance = null;	
		this.window.onResizeAnimationonFinish = null;
		this.window = null;
		this.shadow = null;*/
		
		
	}
}


