var Wndw = Class.create();

Wndw.prototype = {
	
	wdwid:null, 
	
	parentDom:null,
	
	drag_flg: false,
	
	// img dir
	img_dir : "/img/calendar_icon/",
	
	initialize: function( params ){
		
		var wndwWidth = params.w;
		var wndwHeight = params.h;
		var wndwX = params.x;
		var wndwY = params.y;
		
		this.wdwid = new Date().getTime();
		
		this.parentDom = document.body;
		
		var wdw = document.createElement('DIV');
			wdw.id = "wdw"+this.wdwid;
			wdw.style.position = "absolute";
			wdw.style.left = wndwX + "px";
			wdw.style.top = wndwY + "px";
			wdw.style.backgroundColor = "#ffffff";
			wdw.style.border = "1px solid #eeeeee";
			//wdw.style.width = wndwWidth+"px";
			//wdw.style.height = wndwHeight+"px";
		
		this.parentDom.appendChild( wdw );
		
		var header = document.createElement('DIV');
			header.id = "header"+this.wdwid;
			header.style.position = "relative";
			header.style.top = "0px";
			header.style.backgroundColor = "#666666";
			header.style.padding = "3px";
			header.style.color = "#ffffff";
			header.style.fontSize = "10px";
			header.style.fontWeight = "bold";
			header.style.textAlign = "right";
			
			var html = "<table border='0' cellpadding='0' cellspacing='0'><tr>";
				html+= "<a id='closeBtn"+this.wdwid+"' style='cursor:pointer;'>";
				html+= "<img src='"+this.img_dir+"close.gif'></a>";
				html+= "<td id='header_title"+this.wdwid+"' style='font-size:10px;font-weight:bold;color:#ffffff;'></td>";
				html+= "<td align='right' style='font-size:10px;font-weight:bold;color:#ffffff;'>";
				html+= "</td></tr></table>";
			header.innerHTML = html;
			
		$('wdw'+this.wdwid).appendChild( header );
		
		var html = document.createElement('DIV');
			html.id = "detail"+this.wdwid;
			html.style.top = "30px";
			html.style.backgroundColor = "#ffffff";
			//html.style.width = "170px";
			//html.style.height = wndwHeight+"px";
			
		$('wdw'+this.wdwid).appendChild( html );
		
		var contain = document.createElement('DIV');
			contain.id = "contain"+this.wdwid;
			contain.style.position = "static";
			//contain.style.width = "170px";
			//contain.style.height = "100%";
			contain.style.overflow = "auto";
			
		$('detail'+this.wdwid).appendChild( contain );
		
		Event.observe( $('closeBtn'+this.wdwid), 'click', this.closeWindow.bind(this) , false );
		Event.observe( document, 'selectstart', this.cantSelect, false );
		
		Event.observe( $('header'+this.wdwid), 'mousedown', this.mousedown.bind(this), false );
		Event.observe( document, 'mouseup', this.mouseup.bind(this), false );
		Event.observe( document, 'mousemove', this.mousemove.bind(this), false );
	},
	
	cantSelect: function(){
		return false;
	},
	
	setContents: function( element ){
		
		$( 'contain'+this.wdwid ).appendChild(element);
	},
	
	setInnerHTML: function( html ){
		
		$( 'contain'+this.wdwid ).innerHTML = html;
	},
	
	setTitle: function( t ){
		
		$( 'header_title'+this.wdwid ).innerHTML = "&nbsp;"+t;
	},
	
	closeWindow: function(){
		
		Event.stopObserving( $( 'header'+this.wdwid ), 'mousedown', this.mousedown.bind(this), false );
		Event.stopObserving( document, 'mouseup', this.mouseup.bind(this), false );
		Event.stopObserving( document, 'mousemove', this.mousemove.bind(this), false );
		Event.stopObserving( document, 'selectstart', this.cantSelect, false );
		
		this.parentDom.removeChild( $("wdw"+this.wdwid) );
	},
	
	setPos: function( x, y ){
		
		$('wdw'+this.wdwid).style.top = x + "px";
		$('wdw'+this.wdwid).style.left = y + "px";
	},
	
	
	mousedown: function( e ){
		
		this.drag_flg = true;
		$('wdw'+this.wdwid).offsetX =  e.clientX - $('wdw'+this.wdwid).style.left.slice( 0, -2 );
		$('wdw'+this.wdwid).offsetY =  e.clientY - $('wdw'+this.wdwid).style.top.slice( 0, -2 );
	},
		
	mouseup: function( e ){
		
		this.drag_flg = false;
	},
	
	mousemove: function( e ){
		
		if ( this.drag_flg )
		{
			$('wdw'+this.wdwid).style.left = e.clientX - $('wdw'+this.wdwid).offsetX;
			$('wdw'+this.wdwid).style.top = e.clientY - $('wdw'+this.wdwid).offsetY;
		}
	}
}