/*  inline_browser.js, version 1.0
*
*  (c)2008- s.fujikawa Some Rights Reserved
*
*/

var movatwi_inline = Class.create();
movatwi_inline.prototype = {
  myWin: Object,
  myName: String,
  lastX: Number,
  lastY: Number,
  require: function(libraryName) {
    var el=document.createElement('script');
    el.type='text/javascript';
    el.src=libraryName;
	el.charset="UTF-8";
	document.body.appendChild(el);
  },
  requireCSS: function(cssName){
    var d = document;
    // head

    var head = d.getElementsByTagName('HEAD')[0];
    if (!head) return;

	var ele=document.createElement('link');
	ele.rel='stylesheet';
	ele.href=cssName;
	ele.type='text/css';
	head.appendChild(ele);
  },
  initialize: function(){
  
  	this.requireCSS(MOVATWI_DOMAIN + '/js/mtw_inline.css');
  	/*
	try{	
		eval(MOVATWI_MODULE).call(this);
	}catch(e){
		this.call();
	}  
	*/                
  },
  call:function(){

		var d = document;
		var viewObj = new Object();
		viewObj.title = 'title';
		viewObj.body = 'body';
	   	this.view(viewObj);
 },
  view:function(viewObj){

	var d = document;
	var serial = (new Date()).getTime() ;
	this.myName = MOVATWI_MODULE + '_body' + serial;

	this.myWin = new SimpleWindow(this.myName , MOVATWI_MODULE + '_body.tt');
	this.myWin.title =viewObj.title;
	this.myWin.txt_val = this.selectText();

	this.myWin.btnArray = [
	  {id:"btn1",src:"/img/btn_close.gif",width:"55",height:"13",value:"close",change:this.win_close.bind(this)}
    ];
	this.myWin.btnArray2 = [
	  {id:"btn2",src:"/img/btn_close.gif",width:"55",height:"13",value:"close",change:this.win_close.bind(this)}
    ];
	
	winWidth = d.body.clientWidth;

	this.lastX = viewObj.x || 0;
	this.lastY = viewObj.y || d.body.scrollTop || d.documentElement.scrollTop || d.scrollY || 0;
		
	var tmpX , tmpY;
	tmpX = this.lastX;
	tmpY = this.lastY;
	
	this.myWin.show(tmpX , tmpY , { element:viewObj.element });
// 	this.myWin.addBtnEvent($('btn1'), "click", this.win_close.bind(this), false);

	
  },
  win_close:function (event){

  	this.myWin.close();

	if (eval(MOVATWI_MODULE).onClose)
		eval(MOVATWI_MODULE).onClose(this);
  	
  	
    if (event.preventDefault) {
      event.preventDefault();
      event.stopPropagation();
    } else {
      event.returnValue = false;
      event.cancelBubble = true;
    }
	return false;
  },
  selectText:function(){

		var d = document;

		if (window.getSelection){
			var range = window.getSelection();
			return range.toString();
		}else if (d.selection){

			if (d.selection && d.selection.type != "Control") {
			    // TextRange
			    var textrange = d.selection.createRange();
			    return textrange.text;
			} else {
			    // ControlRangeCollection
			    //var ctrlcollection = d.selection.createRange();
			    //return ctrlcollection.text;
			    return "";
			}  
		}

		return "";
  
  }

};


