/*
	Simple Window Class 
    (c) 2006 f-shin (f-shin@milkstand.net) & fujikawa@paperboy.co.jp
	require dragdrop.js / prototype.js
*/

var WindowObserver = Class.create();
WindowObserver.prototype = {
  initialize: function(element, observer) {
    this.element   = $(element);
    this.observer  = observer;
  },
  
  onStart: function() {

  },
  
  onEnd: 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){

		this.activeWindow = window;

	},
	getMaxZindex:function(){
		var tmpZ = 0;
		this.windowArray.each(
			function(winObj,index){
				if (winObj.refWin && tmpZ < winObj.refWin.style.zIndex)
					tmpZ = winObj.refWin.style.zIndex;
			}
		);
		return tmpZ;
	}

}


var SimpleWindow = Class.create();
SimpleWindow.prototype = {

  id:String,
  template:String,
  title:String,
  body:String,
  btnArray:Array,
  width:Number,
  height:Number,
  evArray:Array,

  initialize: function(id , template) {
	this.id = id;
	this.template = template
	this.title = "";
	this.body  = "";
	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(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(
		    'win_body.tt',this
		  );

		var win = document.createElement("div");

		win.id = this.id;		

	 	document.body.appendChild(win);
		Position.absolutize(win);
	 	win.innerHTML = html;

	    win.style.width = this.width;
	    win.style.height = this.height;

		if (propObj &&  propObj["draggable"] == true){
			this.objDraggable = new Draggable(this.id, {revert:false,change:this.onDrug.bind(this),ghosting:false , starteffect:undefined , reverteffect:undefined , endeffect:undefined});

		}else{

	 		Event.observe(document , 'click' , this.close.bind(this) , false);	//画面内クリックで閉じる
			
		}


	   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){

		Event.stopObserving(document , 'click' , this.close.bind(this) , false);
//		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(){
	if (this.refWin){
		this.refWin.style.visibility = "visible";
		 if (this.evArray && this.evArray['show']) this.evArray['show'].onShow;
    }else{
		this.open();
	}
  },
/*  getWinTmpl: function(){
	//private
	var dlgTmpl = '<div class="pop" id ="##ID##_dlg"><form style="margin:0px" id="##ID##_dlgForm">';
	dlgTmpl += '<table border="0" cellspacing="0" cellpadding="0">';
	dlgTmpl += '<tr>';
	dlgTmpl += '<td height="199" class="msg">';
	dlgTmpl += '<div class="bar" id="##ID##_dlgBar">';
	dlgTmpl += '<table><tr><th><span id ="##ID##_dlgTitle"></span></th><td><span class="close"></span></td></tr></table></div>';
	dlgTmpl += '<div class="body" id="##ID##_dlgBody"></div>';
	dlgTmpl += '</td></tr>';
	dlgTmpl += '<tr><td class="button" align="center"><div id="##ID##_dlgBtn">&nbsp;</div></td></tr>';
	dlgTmpl += '</table></form></div>';
	return dlgTmpl;
  }, */
  addBtnEvent : function(element, name, observer, useCapture) {

    if (element.addEventListener) {
      element.addEventListener(name, observer, useCapture);
    } else if (element.attachEvent) {
      element.attachEvent('on' + name, observer);
    }
  }

}

