// Copyright (c) 2007 Bizright Technology Inc
// 
// Window Class にバグがあったのでオーバーライド
//
// VERSION 1.0

  // Destructor
  Window.prototype.destroy = function() {
    this._notify("onDestroy");
    Event.stopObserving(this.topbar, "mousedown", this.eventMouseDown);
    Event.stopObserving(this.bottombar, "mousedown", this.eventMouseDown);
    Event.stopObserving(this.content, "mousedown", this.eventMouseDownContent);
    
    Event.stopObserving(window, "load", this.eventOnLoad);
    Event.stopObserving(window, "resize", this.eventResize);
    Event.stopObserving(window, "scroll", this.eventResize);
    
    Event.stopObserving(this.content, "load", this.options.onload);

    if (this._oldParent) {
      var content = this.getContent();
      var originalContent = null;
      for(var i = 0; i < content.childNodes.length; i++) {
        originalContent = content.childNodes[i];
        if (originalContent.nodeType == 1) 
          break;
        originalContent = null;
      }
      if (originalContent)
        this._oldParent.appendChild(originalContent);
      this._oldParent = null;
    }

    if (this.sizer)
        Event.stopObserving(this.sizer, "mousedown", this.eventMouseDown);

//    if (!this.iefix && this.options.url) 
//      this.content.src = null

     if(this.iefix) 
      Element.remove(this.iefix);

    Element.remove(this.element);
    Windows.unregister(this);      
  }

  Window.prototype.setURL = function(url) {
    // Not an url content, change div to iframe
//    if (!this.iefix && this.options.url)
//      this.content.src = null;

    this.options.url = url;
    var content= "<iframe frameborder='0' name='" + this.getId() + "_content'  id='" + this.getId() + "_content' src='" + url + "' width='" + this.width + "' height='" + this.height + "'> </iframe>";
    $(this.getId() +"_table_content").innerHTML = content;
    
    this.content = $(this.element.id + "_content");
  }

  Window.prototype.setHTMLContent = function(html) {
    // It was an url (iframe), recreate a div content instead of iframe content
    if (this.options.url) {
//      if(!this.iefix)
//	      this.content.src = null;
      this.options.url = null;
      
  	  var content ="<div id=\"" + this.getId() + "_content\" class=\"" + this.options.className + "_content\"> </div>";
      $(this.getId() +"_table_content").innerHTML = content;
      
      this.content = $(this.element.id + "_content");
    }
      
    this.getContent().innerHTML = html;
  }
  
// Modal時のエフェクト消す
  WindowUtilities.enableScreen = function(id) {
    id = id || 'overlay_modal';
    var objOverlay =  $(id);
    if (objOverlay) {
      // hide lightbox and overlay
      if (id == "overlay_modal" && Window.hasEffectLib && Windows.overlayHideEffectOptions)
        new Effect.Fade(objOverlay, Object.extend({from: 0, to:0}, Windows.overlayHideEffectOptions));
      else {
        objOverlay.style.display = 'none';
        objOverlay.parentNode.removeChild(objOverlay);
      }
      
      // make select boxes visible using old value
      if (id != "__invisible__") 
        WindowUtilities._showSelect();
    }
  }
  WindowUtilities._disableScreen = function(className, overlayId, overlayOpacity, contentId) {
    // prep objects
    var objOverlay = $(overlayId);

    var pageSize = WindowUtilities.getPageSize(objOverlay.parentNode);

    // Hide select boxes as they will 'peek' through the image in IE, store old value
    if (contentId && Prototype.Browser.IE) {
      WindowUtilities._hideSelect();
      WindowUtilities._showSelect(contentId);
    }  
  
    // set height of Overlay to take up whole page and show
    objOverlay.style.height = (pageSize.pageHeight + 'px');
    objOverlay.style.display = 'none'; 
    if (overlayId == "overlay_modal" && Window.hasEffectLib && Windows.overlayShowEffectOptions) {
      objOverlay.overlayOpacity = overlayOpacity;
      new Effect.Appear(objOverlay, Object.extend({from: overlayOpacity, to: overlayOpacity}, Windows.overlayShowEffectOptions));
    }
    else
      objOverlay.style.display = "block";
  }
  
Function.prototype.later = function(ms){
  var self = this;
  return function(){
    var args = arguments;
    var thisObject = this;
    var res = {
      complete: false,
      cancel: function(){clearTimeout(PID);},
      notify: function(){clearTimeout(PID);later_func()}
    };
    var later_func = function(){
      self.apply(thisObject,args);
      res.complete = true;
    };
    var PID = setTimeout(later_func,ms);
    return res;
  };
}

Position.offsetParent = function(element) {
	try{
	    if (element.offsetParent) return element.offsetParent;
	    if (element == document.body) return element;
	
	    while ((element = element.parentNode) && element != document.body)
	      if (Element.getStyle(element, 'position') != 'static')
	        return element;
	} catch(e){
		// NOP
	}

    return document.body;
}

