//
// カスタムコントロール 「おっきくする／もとにもどす」の切り替え
//
function FullScreenControl() {}

FullScreenControl.prototype = new GControl();

// 初期化
FullScreenControl.prototype.initialize = function(map) {
  this.map = map;
  this.is_fullscreen = false;
  this.beforepos = new Object();

  this.button = document.createElement("input");
  this.button.setAttribute("type", "button");
  this.button.setAttribute("value", "おっきくする(X)");
  GEvent.addDomListener(this.button, "click", function(){this.parent.toggle_fullscreen()});
  this.button.parent = this;

  this.map.getContainer().appendChild(this.button);

  // 角丸
  this.rc1 = document.createElement("img");
  this.rc1.setAttribute("src", "./misc/rc1.gif");
  this.rc1.style.position = "absolute";
  this.rc1.style.top = "-10px";
  this.rc1.style.left = "-10px";
  this.rc1.parent = this;
  this.map.getContainer().appendChild(this.rc1);

  this.rc2 = document.createElement("img");
  this.rc2.setAttribute("src", "./misc/rc2.gif");
  this.rc2.style.position = "absolute";
  this.rc2.style.top = "-10px";
  this.rc2.style.right = "-10px";
  this.rc2.parent = this;
  this.map.getContainer().appendChild(this.rc2);

  this.rc3 = document.createElement("img");
  this.rc3.setAttribute("src", "./misc/rc3.gif");
  this.rc3.style.position = "absolute";
  this.rc3.style.bottom = "-10px";
  this.rc3.style.left = "-10px";
  this.rc3.parent = this;
  this.map.getContainer().appendChild(this.rc3);

  this.rc4 = document.createElement("img");
  this.rc4.setAttribute("src", "./misc/rc4.gif");
  this.rc4.style.position = "absolute";
  this.rc4.style.bottom = "-10px";
  this.rc4.style.right = "-10px";
  this.rc4.parent = this;
  this.map.getContainer().appendChild(this.rc4);

  // 角丸IE対応
  this.spacer1 = document.createElement("img");
  this.spacer1.setAttribute("src", "./misc/spacer.gif");
  this.spacer1.style.position = "absolute";
  this.spacer1.style.width = "1px";
  this.spacer1.style.height = "20px";
  this.spacer1.style.top = "-10px";
  this.spacer1.style.right = "-11px";
  this.spacer1.parent = this;
  this.map.getContainer().appendChild(this.spacer1);

  this.spacer2 = document.createElement("img");
  this.spacer2.setAttribute("src", "./misc/spacer.gif");
  this.spacer2.style.position = "absolute";
  this.spacer2.style.width = "1px";
  this.spacer2.style.height = "20px";
  this.spacer2.style.bottom = "-10px";
  this.spacer2.style.right = "-11px";
  this.spacer2.parent = this;
  this.map.getContainer().appendChild(this.spacer2);

  document.getElementById("map").style.border = "10px solid #000000";

  return this.button;
}

// コントロール位置を返す関数
FullScreenControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(5, 5));
}

// 「おっきくする／もとにもどす」を切り替える
FullScreenControl.prototype.toggle_fullscreen = function() {
  this.is_fullscreen = !this.is_fullscreen;
  var container = this.map.getContainer();

  if(this.is_fullscreen){
    // おっきくする前の状態をバックアップ
    this.beforemargin = document.body.style.margin;
    this.beforepos.position = container.style.position;
    this.beforepos.top      = container.style.top;
    this.beforepos.left     = container.style.left;
    this.beforepos.width    = container.style.width;
    this.beforepos.height   = container.style.height;
    this.beforepos.border   = container.style.border;
    if(window.screenX!=undefined)
    {
      this.beforepos.screenX = window.screenX;
      this.beforepos.screenY = window.screenY;
    }
    else
    {
      this.beforepos.screenX = 0;
      this.beforepos.screenY = 0;
    }
    if(window.outerWidth!=undefined)
    {
      this.beforepos.screenW = window.outerWidth;
      this.beforepos.screenH = window.outerHeight;
    }
    else
    {
      if(window.screen.availWidth<1024)
      {
        this.beforepos.screenW = window.screen.availWidth;
      }
      else
      {
        this.beforepos.screenW = 1024;
      }
      if(window.screen.availHeight<900)
      {
        this.beforepos.screenH = window.screen.availHeight;
      }
      else
      {
        this.beforepos.screenH = 900;
      }
    }

    this.button.value = "もとにもどす(X)"; // ボタンの文字

    var header_elem = document.getElementById("header");
    if(header_elem){
        header_elem.style.display = "none";
    }
    var footer_elem = document.getElementById("footer");
    if(footer_elem){
        footer_elem.style.display = "none";
    }
//    window.moveTo(0, 0);
//    window.resizeTo(window.screen.availWidth, window.screen.availHeight);

    this.rc1.style.visibility = "hidden";
    this.rc1.style.top = "0px";
    this.rc1.style.left = "0px";
    this.rc2.style.visibility = "hidden";
    this.rc2.style.top = "0px";
    this.rc2.style.right = "0px";
    this.rc3.style.visibility = "hidden";
    this.rc3.style.bottom = "0px";
    this.rc3.style.left = "0px";
    this.rc4.style.visibility = "hidden";
    this.rc4.style.bottom = "0px";
    this.rc4.style.right = "0px";
    this.spacer1.style.visibility = "hidden";
    this.spacer1.style.top = "0px";
    this.spacer1.style.right = "0px";
    this.spacer2.style.visibility = "hidden";
    this.spacer2.style.bottom = "0px";
    this.spacer2.style.right = "0px";

    document.body.style.margin = "0";
    container.style.border = "0px solid #000000";
    container.style.position = "absolute";
    container.style.top = 0;
    container.style.left = 0;

    var width = this.getWindowWidth();
    if(width==0) {container.style.width = "100%";}
    else{container.style.width = width+"px";}

    var height = this.getWindowHeight();
    if(height==0) {container.style.height = "100%";}
    else{container.style.height = height+"px";}
  }else{
    this.button.value = "おっきくする(X)";

    var header_elem = document.getElementById("header");
    if(header_elem){
        header_elem.style.display = "block";
    }
    var footer_elem = document.getElementById("footer");
    if(footer_elem){
        footer_elem.style.display = "block";
    }
//    window.resizeTo(this.beforepos.screenW, this.beforepos.screenH);
//    window.moveTo(this.beforepos.screenX, this.beforepos.screenY);

    this.rc1.style.visibility = "visible";
    this.rc1.style.top = "-10px";
    this.rc1.style.left = "-10px";
    this.rc2.style.visibility = "visible";
    this.rc2.style.top = "-10px";
    this.rc2.style.right = "-10px";
    this.rc3.style.visibility = "visible";
    this.rc3.style.bottom = "-10px";
    this.rc3.style.left = "-10px";
    this.rc4.style.visibility = "visible";
    this.rc4.style.bottom = "-10px";
    this.rc4.style.right = "-10px";
    this.spacer1.style.visibility = "visible";
    this.spacer1.style.top = "-10px";
    this.spacer1.style.right = "-11px";
    this.spacer2.style.visibility = "visible";
    this.spacer2.style.bottom = "-10px";
    this.spacer2.style.right = "-11px";

    document.body.style.margin = this.beforemargin;
    container.style.border = this.beforepos.border;
    container.style.position = this.beforepos.position;
    container.style.top = this.beforepos.top;
    container.style.left = this.beforepos.left;
    container.style.width = this.beforepos.width;
    container.style.height = this.beforepos.height;
  }

  this.map.checkResize();
  this.button.blur();
}

FullScreenControl.prototype.getWindowHeight = function() {
  if (window.self && self.innerHeight) {
    return self.innerHeight;
  }
  
  if (document.documentElement && document.documentElement.clientHeight) {
    return document.documentElement.clientHeight;
  }
  
  if( document.body.clientHeight ) {
    return document.body.clientHeight;
  }
  
  return 0;
}

FullScreenControl.prototype.getWindowWidth = function() {
  if (window.self && self.innerWidth) {
    return self.innerWidth;
  }
  
  if (document.documentElement && document.documentElement.clientWidth) {
    return document.documentElement.clientWidth;
  }
  
  if( document.body.clientWidth ) {
    return document.body.clientWidth;
  }
  
  return 0;
}

