var WindowUtilities = {  
  // From dragdrop.js
  curOpenedLayer: null,
  backScreen: $('pop_trans'),
  bodyObj: document.body,
  
  getWindowScroll: function(parent) {
    var T, L, W, H;
    parent = parent || document.body;              
    if (parent != document.body) {
      T = parent.scrollTop;
      L = parent.scrollLeft;
      W = parent.scrollWidth;
      H = parent.scrollHeight;
    } 
    else {
      var w = window;
      with (w.document) {
        if (w.document.documentElement && documentElement.scrollTop) {
          T = documentElement.scrollTop;
          L = documentElement.scrollLeft;
        } else if (w.document.body) {
          T = body.scrollTop;
          L = body.scrollLeft;
        }
        if (w.innerWidth) {
          W = w.innerWidth;
          H = w.innerHeight;
        } else if (w.document.documentElement && documentElement.clientWidth) {
          W = documentElement.clientWidth;
          H = documentElement.clientHeight;
        } else {
          W = body.offsetWidth;
          H = body.offsetHeight
        }
      }
    }
    return { top: T, left: L, width: W, height: H };
  }, 
  //
  // getPageSize()
  // Returns array with page width, height and window width, height
  // Core code from - quirksmode.org
  // Edit for Firefox by pHaez
  //
  getPageSize: function(parent){
    parent = parent || document.body;              
    var windowWidth, windowHeight;
    var pageHeight, pageWidth;
    if (parent != document.body) {
      windowWidth = parent.getWidth();
      windowHeight = parent.getHeight();                                
      pageWidth = parent.scrollWidth;
      pageHeight = parent.scrollHeight;                                
    } 
    else {
      var xScroll, yScroll;

      if (window.innerHeight && window.scrollMaxY) {  
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
      } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
      } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
      }


      if (self.innerHeight) {  // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
      } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
      } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
      }  

      // for small pages with total height less then height of the viewport
      if(yScroll < windowHeight){
        pageHeight = windowHeight;
      } else { 
        pageHeight = yScroll;
      }

      // for small pages with total width less then width of the viewport
      if(xScroll < windowWidth){  
        pageWidth = windowWidth;
      } else {
        pageWidth = xScroll;
      }
    }             
    return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
  },

  disableScreen: function(width, height, top, left){
  	WindowUtilities.backScreen.setStyle({width: width+'px', height: height+'px', top: top+'px', left: left+'px'});
  	WindowUtilities.backScreen.show();
  },
  
  enableScreen: function(){
  	WindowUtilities.backScreen.hide();
  },
  
  openLayer: function(layerId){
  	var layer = $(layerId);
  	if(!layer){
  		alert('존재하지 않는 id 입니다'); 
  		return false;
  	}
 	
  	//2. 현재 열린 레이어가 있는지 검사(스크린도)
  	if(WindowUtilities.curOpenedLayer)
  		WindowUtilities.closeLayer();
  	
  	//3. 현재 창크기 계산
  	var scroll = WindowUtilities.getWindowScroll();
  	var top = scroll.top;
  	var left = scroll.left;
  	var width = scroll.width;
  	var height = scroll.height;
  	var x = width/2;
  	var y = height/2;

  	
  	//5. layerId 레이어의 크기와 현재 창크기 계산하여 띄울 중앙 위치  계산 하여 염
  	var layerWidth = layer.getWidth();
  	var layerHeight = layer.getHeight();
  	
  	layer.setStyle({top: (y-(layerHeight/2)+top)+'px', left: (x-(layerWidth/2)+left)+'px'});
	
//	if(!WindowUtilities.backScreen){
//		WindowUtilities.backScreen = $('pop_trans');
	//}

  	WindowUtilities.disableScreen(width, height, top, left);
  	layer.show();
  	
  	//첫번째 input에 포커스 주기
  	var inputs = layer.getElementsBySelector('input', 'textarea');
  	
  	for(var i=0;i<inputs.length;i++){
  		if(inputs[i].tagName == 'input' || inputs[i].tagName == 'INPUT'){
  			if(inputs[i].type == 'text' || inputs[i].type == 'password' ||
  				inputs[i].type == 'TEXT' || inputs[i].type == 'PASSWORD'){
  				inputs[i].activate();
  				break;
  			}
  		}
  		else{
  			if(inputs[i].tagName == 'textarea' || inputs[i].tagName == 'TEXTAREA'){
  				inputs[i].activate();
  				break;
  			}
  		}
  	}
  	
  	//6. curOpenedLayer 변수에 현재 띄워진 layer 저장
  	WindowUtilities.curOpenedLayer = layer;
  },
  
  closeLayer: function(formId){
  	//1. curOpenedLayer 변수가 있으면 거기에 레이어 닫음
  	//2. curOpenedLayer 변수 비움
  	//3. enableScreen
  	if(WindowUtilities.curOpenedLayer)
  		WindowUtilities.curOpenedLayer.hide();
  		
  	WindowUtilities.curOpenedLayer = null;	
  	WindowUtilities.enableScreen();
  	
  	if(formId){
  		$(formId).getElements().invoke('clear');
  	}
  },
  
  refreshPosition: function(){
  	if(!WindowUtilities.curOpenedLayer)
  		return;
  		
  	var scroll = WindowUtilities.getWindowScroll();
  	var top = scroll.top;
  	var left = scroll.left;
  	var width = scroll.width;
  	var height = scroll.height;
  	var x = width/2;
  	var y = height/2;
  	
  	if(WindowUtilities.backScreen){
  		WindowUtilities.backScreen.setStyle({width: width+'px', 
					height: height+'px', top: top+'px', left: left+'px'});
	}
  	
  	var layerWidth = WindowUtilities.curOpenedLayer.getWidth();
  	var layerHeight = WindowUtilities.curOpenedLayer.getHeight();
  	
  	WindowUtilities.curOpenedLayer.setStyle({top: (y-(layerHeight/2)+top)+'px', left: (x-(layerWidth/2)+left)+'px'});
  }
}

Event.observe(window, 'resize', WindowUtilities.refreshPosition);
Event.observe(window, 'scroll', WindowUtilities.refreshPosition);