
// ID of current window (count)
var jswin_window_id = 0;

// Number of open windows
var jswin_win_open  = 0;

function JsWin(title, width, height, x, y, icon_url){
	
	
	//Status
	this.has_icon = (icon_url && (typeof icon_url != 'undefined' ));
	
	//Has the window been shown yet?
	this.has_shown = false;
	
	this.dimensions = {x:width , y :height};
	
	//DOM elements
	this.outer = document.createElement('div');
	this.titlebar = document.createElement('div');
	this.toolbar = document.createElement('div');
	this.content = document.createElement('div');
	this.footer = null;
	this.titlespan = document.createElement('b');
	
	this.id = jswin_window_id++;
	this.outer.setAttribute('id', 'window_'+this.id);
	this.outer.jswin_owner = this;
	
	//Style the window
	if (width != -1) this.outer.style.width=width+'px';
	this.outer.style.position = 'absolute';
	this.outer.style.display = 'none';
	this.outer.style.zindex = 2;
	this.outer.style.top=y+'px';
	this.outer.style.left = x+'px';
	this.outer.className = 'default_window';
	
	this.titlebar.className = 'titlebar';
	//this.titlebar.style.cursor = 'move';
	this.toolbar.className = 'toolbar';
	
	var span = document.createElement('span');
	
	var a 	= document.createElement('a');
	a.id 	= this.id;
	a.setAttribute('href', '#');
	//a.setAttribute('onclick', 'alert($("window_'+this.id+'").jswin_owner); $("window_'+this.id+'").jswin_owner.onclose(); return false;');
	
	a.onclick = function() {
		$("window_"+this.id).jswin_owner.onclose();
		return false;
	};
	
	a.innerHTML = '<img style="border: 1px solid #999;" src="/images/new/embed-side-close.gif" alt="Close window" />';
	span.appendChild(a);
	
	this.titlebar.appendChild(span);
	
	if (this.has_icon)
		this.titlespan.innerHTML = '<img src="'+icon_url+'" border="0" align="texttop" /> '+title;
	else
		this.titlespan.innerHTML = title;
	this.titlebar.appendChild(this.titlespan);
	
	var div = document.createElement('div');
	div.className = 'titlebarwrap';
	div.appendChild(this.titlebar);
	
	this.outer.appendChild(div);
	this.outer.appendChild(this.toolbar);
	
	this.content.className = 'wincontent';
	if (height != -1) this.content.style.height = height+'px';
	this.content.style.width = (width-6)+'px';
	this.content.style.overflow = 'auto';
	
	
	//add to document
	document.body.appendChild(this.outer);
	
	//For dom drag
	//DragManager.init(this.titlebar, this.outer, false);
	
	
	this.showcontents = function(){
		this.outer.appendChild(this.content);
	}
	
	this.hidecontents = function(){
		this.outer.removeChild(this.content);
	}
	
	this.show = function(){
		jswin_win_open++;

		//Center: need sperate code for IE (arg!)
		if (MS.browser == BR_IE){
			windowHeight 	= document.documentElement.clientHeight;
			windowWidth 	= document.documentElement.clientWidth;
		} else {
			windowHeight 	= window.innerHeight;
			windowWidth 	= window.innerWidth;
		}
		
		if ($('fade')) {
			UI.showFade();
		}
		
		if (!this.has_shown) {
			this.oncreate();
			this.has_shown = true;
		}
		
		this.outer.style.display = 'block';
		
		if (this.dimensions.y != -1)
			this.outer.style.top= ((windowHeight/2) -(this.outer.offsetHeight)/2)+'px';
		
		this.outer.style.left = ((windowWidth/2) -(this.outer.offsetWidth)/2)+'px';	
		
		this.onshow();
	}
	
	this.hide = function(){
		jswin_win_open--;
		
		if ((jswin_win_open ==0 ) && $('fade')) {
			UI.hideFade();
		}
		
		this.outer.style.display = 'none';
		this.onhide();
	}
	
	this.destroy = function(){
		this.ondestroy();
		document.body.removeChild(this.outer);
	}
	
	this.oncreate = function(){}
	
	this.ongetfocus = function(){ /*this.outer.style.background = '#acf'*/}
	
	this.onlosefocus = function(){ /*this.outer.style.background = '#eee5ff'*/}
	
	this.ondestroy = function(){};
	
	this.onclose = function(){this.hide();}
	
	this.onshow = function(){}
	
	this.onhide = function(){}
	
	this.changeTitle = function(title){
		this.titlespan.innerHTML = title;
	}
	
	//Display the window
	this.showcontents();
	
}