﻿  //fix bug that the flash refreshes after hide and show content in firefox and safari
  //use visiblitiy instead of setting display to fix 
  //hide lower bar as setting visibility to hidden only hide the main content but not remove it from layout
  Window.prototype.minimize = function() {
    if (this.resizing)
      return;
    
    var r2 = $(this.getId() + "_row2");
    var r3 = $(this.getId() + "_row3");
    
    if (!this.minimized) {
      this.minimized = true;

      var dh = r2.getDimensions().height;
      this.r2Height = dh;
      var h  = this.element.getHeight() - dh;

      if (this.useLeft && this.useTop && Window.hasEffectLib && Effect.ResizeWindow) {
        new Effect.ResizeWindow(this, null, null, null, this.height -dh, {duration: Window.resizeEffectDuration});
      } else  {
        this.height -= dh;
        this.element.setStyle({height: h + "px"});
        r2.setStyle({visibility:'hidden'});
        r3.hide();
//        r2.hide();
      }

      if (! this.useTop) {
        var bottom = parseFloat(this.element.getStyle('bottom'));
        this.element.setStyle({bottom: (bottom + dh) + 'px'});
      }
    } 
    else {      
      this.minimized = false;
      
      var dh = this.r2Height;
      this.r2Height = null;
      if (this.useLeft && this.useTop && Window.hasEffectLib && Effect.ResizeWindow) {
        new Effect.ResizeWindow(this, null, null, null, this.height + dh, {duration: Window.resizeEffectDuration});
      }
      else {
        var h  = this.element.getHeight() + dh;
        this.height += dh;
        this.element.setStyle({height: h + "px"})
        r2.setStyle({visibility:'visible'});
        r3.show();
//        r2.show();
      }
      if (! this.useTop) {
        var bottom = parseFloat(this.element.getStyle('bottom'));
        this.element.setStyle({bottom: (bottom - dh) + 'px'});
      }
      this.toFront();
    }
    this._notify("onMinimize");
    
    // Store new location/size if need be
    this._saveCookie()
  }

  // fixed draw wire line area after using visibility for content instead of setting display to none
  Window.prototype.z2z_superChart_createWiredElement = Window.prototype._createWiredElement;
  Window.prototype._createWiredElement = function() {
  	var result = this.z2z_superChart_createWiredElement();
    var r1 = $(this.getId() + "_row1");
    var r2 = $(this.getId() + "_row2");
    var r3 = $(this.getId() + "_row3");
  	if (this.minimized)
  	{
	    var dh = r1.getDimensions().height;
	  	this.wiredElement.setStyle({height: dh +"px"});
	}
	else if (!this.storedLocation)
	{
	    var dh = r1.getDimensions().height+r2.getDimensions().height+r3.getDimensions().height;
	  	this.wiredElement.setStyle({height: dh +"px"});
	}
  	return result;
  }

  // Minimizes a window with its id
    Windows.minimize = function(id, event) {
    var win = this.getWindow(id)
/*    if (win && win.visible)
      win.minimize();*/
	if (win && win.visible)
    {
    	if (win.isMaximized())
    	{
    		var max_btn = $(id+"_maximize");
    		if (max_btn)
    		{
//	    		max_btn.style.visibility = "visible";
	    	}
    		win.maximize();
		}
		else if (!win.isMinimized())
		{
    		var min_btn = $(id+"_minimize");
    		if (min_btn)
    		{
//	    		min_btn.style.visibility = "hidden";
	    	}
    		win.minimize();
		}
	}
    Event.stop(event);
  }
  
  // Maximizes a window with its id
    Windows.maximize = function(id, event) {
    var win = this.getWindow(id)
/*    if (win && win.visible)
      win.maximize();*/
	if (win && win.visible)
	{
    	if (win.isMinimized())
    	{
    		var min_btn = $(id+"_minimize");
    		if (min_btn)
    		{
//	    		min_btn.style.visibility = "visible";
	    	}
    		win.minimize();
		}
		else if (!win.isMaximized())
		{
    		var max_btn = $(id+"_maximize");
    		if (max_btn)
    		{
//	    		max_btn.style.visibility = "hidden";
	    	}
    		win.maximize();
		}
	}
    Event.stop(event);
  }

//fix doctype and ie problem
  WindowUtilities.z2z_superChart_getWindowScroll = WindowUtilities.getWindowScroll;
  WindowUtilities.getWindowScroll = function()
  {
  	var result = WindowUtilities.z2z_superChart_getWindowScroll();
	if (window.ie)
	{
		if (document.documentElement && document.documentElement.scrollTop)
		{
			result.top = document.documentElement.scrollTop;
			result.left = document.documentElement.scrollLeft;
		}
		else if (document.body)
		{
			result.top = document.body.scrollTop;
			result.left = document.body.scrollLeft;
		}
	}
  	return result;
  }

//fix firefox size calculation problem
  WindowUtilities.z2z_superChart_getPageSize = WindowUtilities.getPageSize;
  WindowUtilities.getPageSize = function()
  {
  	result = WindowUtilities.z2z_superChart_getPageSize();
  	if (!window.ie && !window.khtml)
  	{
		if (document.body && document.body.clientWidth)
		{
			result.windowWidth = document.body.clientWidth;
			result.windowHeight = document.body.clientHeight;
		}
	}
  	return result;
  }
