// Window Object


var highZ = 900;

function Window()
{
// Start Object
//-------------------
	this.id 					= '';
	this.theclass				= 'popupwindow';
	this.display			= '';
	this.title				= 'Draggable Window';
	this.loading			= false;
  this.x		= false;
	this.y 		= false;
	this.inner;	
	this.bottom_color = '#fff';
	
	var nodes = {};
	
	var theid = ''; // use with event
	
	this.update = function(what,value,where) {
		if (!$(nodes[what])) return false;		
		var update_node = $(nodes[what]);
		if (!value.length) value = [value];
		var value = Builder.node('DIV',{},value);
		var value = value.firstChild();
		if (!where) where = 'replace';
		if (where == 'replace') {
			update_node.innerHTML = '';
			update_node.appendChild(value);
		}
		else if (where == 'last') {
			update_node.appendChild(value);
		}
		else if (where == 'first') {
			Element.insertFirst(value,update_node);
		}		
	}
	
	this.show_loading = function()
	{
		var loading							= document.createElement('DIV');
		var loading_img					= document.createElement('IMG');
		var loading_text				= document.createTextNode(' loading... please be patient');
				loading_img.src			= jsconfig.loading_image;
				loading.appendChild(loading_img);				
				loading.appendChild(loading_text);
		return loading;
	}
	
	this.clear = function()
	{
		this.inner.innerHTML = '';
	}
	
	this.create = function()
	{
		var outer 							= document.createElement('DIV');
		var title								= document.createElement('DIV');
		var window_controls			= document.createElement('DIV');		
		var closewin			      = document.createElement('DIV');
		var shrink							= document.createElement('DIV');
		var padding							= document.createElement('DIV');
		var inner								= document.createElement('DIV');
		var saved			 					= document.createElement('DIV');
		var resize		 					= document.createElement('DIV');
		
		theid = this.id;
	
		if (this.overflow) this.theclass += ' overflow_visable';
	
				saved.innerHTML 						= '<p style="text-align:center;font-size:3em;">Saved</p>';						
				saved.style.display 				= 'none';
				saved.style.backgroundColor = '#fff';
				saved.id 										=	this.id+'_saved'; 
		
				outer.id 						= this.id;
				outer.className			= this.theclass;
				outer.style.display = this.display;
				outer.style.zIndex	= highZ++;

			  title_text              = Builder.node('SPAN',{id:this.id+'_title_text'},[this.title]);		
				title.className					= 'title dragme';
				title.id								= this.id+'_title';
				title.appendChild(title_text);
				title.onclick						= function() { $(outer.id).style.zIndex = highZ++; }
				
				closewin.title				= 'Close';
				closewin.onclick = function() {$(outer.id).style.display = "none";};				
				closewin.className		= 'control_item close';
				closewin.appendChild(document.createTextNode('X'));

				padding.id							= this.id+'_padding';
				padding.className				= 'padding relative popupwindow_border';

				inner.id				= this.id+'_inner';
				inner.className = 'popupwindow_inner';
				if(this.loading == true) inner.appendChild(this.show_loading());

				shrink.onclick = function() { 
					new Effect.toggle($(inner.id),'blind'); 
					if (inner.style.display == 'none')
					{
						shrink.innerHTML = '-';
						shrink.title = 'Shrink';
					}
					else 
					{
						shrink.innerHTML = '+';
						shrink.title = 'Expand';
					}
				};
				shrink.title				= 'Shrink';
				shrink.className		= 'control_item shrink';
				shrink.appendChild(document.createTextNode('-'));
				
				window_controls.className = 'popupwindow_controls';

	this.inner = inner;
	nodes.title_text  = title_text; 		
	nodes.inner = inner;
	
	padding.appendChild(inner);
	padding.appendChild(saved);
	window_controls.appendChild(shrink);		
	window_controls.appendChild(closewin);
	title.appendChild(window_controls);
	outer.appendChild(title);
	outer.appendChild(padding);
	
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

	if (this.x != false) outer.style.left=this.x;
	else outer.style.left = myWidth/2-200+scrOfX;
	if (this.y != false) outer.style.top=this.y;
	else outer.style.top = myHeight/4+scrOfY;	
	window.document.body.appendChild(outer);
	

	Nifty("div#"+title.id,"top");
		
	new Draggable(this.id,{handle:'dragme'});
	xAddEventListener($(outer.id),'mousedown',bring_to_top,false);			// show note on focus
	}
		
	var bring_to_top = function()
	{
		$(theid).style.zIndex = highZ++;
	}

//-------------------	
// End Object
}