/*
	Simple Window Class 
    (c) 2006 f-shin (f-shin@milkstand.net)
	require dragdrop.js / prototype.js
*/

var WindowObserver = Class.create();
WindowObserver.prototype = {
  element:Object ,
  
  initialize: function(element, observer) {
    this.element   = element;
    this.observer  = observer;
  },
  
  onStart: function(evt , me) {

  },
  
  onEnd: function() {

  },
  
  onDrag: function(){


  
  }
  
}


var WindowManager = {

	windowArray : [] , 
	register : function (window){
		this.windowArray.push(window);

		// for onupdate
		Draggables.addObserver(new WindowObserver(window, undefined));

		Draggables.deactivate = function(){
		var maxZidx = WindowManager.getMaxZindex();
		Draggables.activeDraggable.element.style.zIndex = parseInt(maxZidx , 10) + 1;
		Draggables.activeDraggable = null;
		}

	},
	active:function(window){

	},
	getMaxZindex:function(){
	
		var tmpZ = 100;
		this.windowArray.each(
			function(winObj,index){
				if (winObj.refWin && winObj.refWin.style.zIndex){
					var nowZ = parseInt(winObj.refWin.style.zIndex,10);
					if (tmpZ < nowZ)
					tmpZ = nowZ;
				}
			}
		);
		return tmpZ;
	}

}


var SimpleWindow = Class.create();
SimpleWindow.prototype = {

  id:String,
  title:String,
  body:String,
  btnArray:Array,
  width:Number,
  height:Number,
  evArray:Array,
  template:String,
  txt_val:String,

  initialize: function(id , template) {
	this.id = id;
	this.template = template || "win_body.tt";
	this.title = "";
	this.body  = "";
	this.txt_val = "";
	this.btnArray = [];
	this.width = 200;
	this.height = 100;
	WindowManager.register(this);

  },
  addEventListener : function(name , obj ){
	evArray[name] = obj ;
  },

  onDrug:function(){
	WindowManager.active(this);
  },
  open:function(initX , initY , propObj){

		if (this.refWin){
			this.refWin.style.visibility ="visible";
	   		if (this.evArray && this.evArray['open']) this.evArray['open'].onOpen;
			return this.refWin;
		}

		var html = 
		  Jemplate.process(
		    this.template,this
		  );

		var win = document.createElement("div");

		win.id = this.id;		
		win.style.visibility = "hidden";
		document.body.appendChild(win);

	 	try{
			Position.absolutize(win);
		}catch(e){
		
		}
		
		if ( initX && initY){
			win.style.left = initX + 'px';
			win.style.top = initY + 'px';		
		}
		win.style.opacity = 0;
		win.style.visibility = "visible";
		new Effect.Fade(win, { duration:0.5,from:0.2, to:1.0});

	    win.style.width = this.width;
	    win.style.height = this.height;

	 	win.innerHTML = html;


	 	this.objDraggable = new Draggable(
	 		this.id,
	 		 {revert:false,
	 		  change:this.onDrug.bind(this),
	 		  ghosting:false , 
	 		  starteffect:undefined,
	 		  reverteffect:undefined , endeffect:undefined});

	   if (this.btnArray && this.btnArray.length > 0){
			var btnHtml = "";
			
			this.btnArray.each(
				function(obj){
					if (obj.id){
						var btnEle = $(this.id + "_" + obj.id);
						if (obj.change)
							this.addBtnEvent(btnEle, "click", obj.change.bind(this), false);
					}
				}.bind(this)
			);
	
	   }
		this.refWin = win;
   		if (this.evArray && this.evArray['open']) this.evArray['open'].onOpen;
		return this.refWin;
	   
  },
  close:function(){

	if (this.refWin){
		this.objDraggable.destroy();
		this.refWin.parentNode.removeChild(this.refWin);
		this.refWin = undefined;
	    if (this.evArray && this.evArray['close']) this.evArray['close'].onClose;
	}
  },
  hide:function(){
	if (this.refWin){
		this.refWin.style.visibility = "hidden";
	    if (this.evArray && this.evArray['hide']) this.evArray['hide'].onHide;
	}
  },
  show:function(initX , initY , propObj){
	if (this.refWin){
		this.refWin.style.visibility = "visible";
		 if (this.evArray && this.evArray['show']) this.evArray['show'].onShow;
    }else{
		this.open(initX , initY , propObj);
	}
  },
  
  move:function(x , y){
  
  	if (this.refWin){
		this.refWin.style.left = x + 'px';
		this.refWin.style.top = y + 'px';
	
	}
  
  },
  addBtnEvent : function(element, name, observer, useCapture) {

    if (element.addEventListener) {
      element.addEventListener(name, observer, useCapture);
    } else if (element.attachEvent) {
      element.attachEvent('on' + name, observer);
    }
  }

}

