/* smartdialog.js v1.0 */
/* (C)t.hira ( http://thira.plavox.info/ ) */
/* Licensed under Creative Commons Attribution 2.1 Japan ( http://creativecommons.org/licenses/by/2.1/jp/ ) */

/*
How to use
--
sdlog.show('message1')
sdlog.show('message2',null,{width:'350',height:'200'})
sdlog.show('message3',{opacity:'0.5',duration:'1.0',backgroundColor:'#fff',clickHideDisabled:1},{top:'20%',left:'20%',width:'350',height:'200'})
--
parameter #1: messages

parameter #2: options for overlay  *ex=default
 -opacity('0.0'-'1.0' ex:'0.7')
 -duration(ex:'1.0')
 -backgroundColor(ex:'#fff')
 -noClickHide('0'or'1' default:'0')

parameter #3: options for dialog
 -top(ex:'40%')
 -left(ex:'50%')
 -width(ex:'300')
 -height(ex:'150')
 -align(ex:'center')
 -padding(ex:'5')
 -border(ex:'2px solid #666')
 -backgroundColor(ex:'#fff')
 -noHideButton('0'or'1' default:'0')
 -nameHideButton(default:'close')
--

*/


var sdlog = Class.create();

sdlog.prototype={
 initialize:function(){
  var objBody = document.getElementsByTagName("body").item(0);

  /* append Overlay*/
  var objOverlay = document.createElement("div");
  objOverlay.setAttribute('id','SOverlay');
  objOverlay.style.display='none';
  document.body.appendChild(objOverlay);
  new Effect.Opacity('SOverlay',{from:0.0,to:0.0,duration:0});

  /* append Dialog*/
  var objDialog = document.createElement("div");
  objDialog.setAttribute('id','SDialog');
  document.body.appendChild(objDialog);

  /* append DialogString*/
  var objDialogString = document.createElement("div");
  objDialogString.setAttribute('id','SDialogString');
  objDialog.appendChild(objDialogString);

  /* append DialogButton*/
  var objDialogButton = document.createElement("div");
  objDialogButton.setAttribute('id','SDialogButton');
  objDialog.appendChild(objDialogButton);
 },

 show:function(msg,overlayOptions,dialogOptions){
  /* null options set */
  overlayOptions=overlayOptions || {};
  dialogOptions=dialogOptions || {};

  /* options set */
  overlayOptions.duration = (overlayOptions.duration!=null) ? overlayOptions.duration : '1';
  overlayOptions.opacity = (overlayOptions.opacity!=null) ? overlayOptions.opacity : '0.7';
  dialogOptions.nameHideButton = (dialogOptions.nameHideButton!=null) ? dialogOptions.nameHideButton : 'close';
  dialogOptions.duration = overlayOptions.duration;

  /* set overlay/dialog */
  this.setOverlay(overlayOptions);
  this.setDialog(msg,dialogOptions);

 },

 setOverlay:function(options){
  var arrayPageSize=getPageSize();
  $("SOverlay").style.position='absolute';
  $("SOverlay").style.top='0px';
  $("SOverlay").style.left='0px';
  $("SOverlay").style.width=arrayPageSize[0]+'px';
  $("SOverlay").style.height=arrayPageSize[1]+'px';
  $('SOverlay').style.backgroundColor = (options.backgroundColor!=null) ? options.backgroundColor :"#fff";
  $('SOverlay').style.zIndex="10";
  $('SOverlay').style.display = "block";
  if(options.noClickHide!='1') {
   $('SOverlay').onclick = function(){ sdlog.hide(options.duration); }
  }
  else{ $('SOverlay').onclick = function(){} }
  
  new Effect.Opacity('SOverlay',{from:0.0,to:options.opacity,duration:options.duration});
  
  /**
   * 2009/07/03 カスタマイズ 開始
   * ダイアログを表示する時に、セレクトボックスを隠す（IE6対策）
   * （レイヤーより上位に表示されるため）
   */
  if (typeof document.documentElement.style.maxHeight == "undefined") {
	  var elems = document.getElementsByTagName("select");
	  for (i = 0; i < elems.length; i++) {
	      elems[i].style.visibility = 'hidden';
	  }
  }
  /**
   * 2009/07/03 カスタマイズ 終了
   */
  
  var arrayPageSize=getPageSize();
  document.body.width = arrayPageSize[2]+'px';
 },

 setDialog:function(msg,options){
  var arrayPageSize=getPageSize();
  
  $('SDialog').style.border = (options.border!=null) ? options.border : '2px solid #666';
  $("SDialog").style.position="absolute";
  
  // 画面中央表示（カスタマイズ）
  this.setWindowCentering(options);
  
  $('SDialog').style.padding = (options.padding!=null) ? options.padding+'px' :"5px";
  $('SDialog').style.backgroundColor = (options.backgroundColor!=null) ? options.backgroundColor :"#fff";
  $('SDialog').style.zIndex="90";
  $('SDialog').style.display = "block";
  $('SDialogString').style.width = (options.width!=null) ? options.width+'px' :"300px";
  $('SDialogString').style.height = (options.height!=null) ? options.height+'px' : "100px";
  $('SDialogString').style.textAlign = (options.textAlign!=null) ? options.textAlign :"center";
  if(msg){ $('SDialogString').innerHTML = msg };
  $('SDialogButton').style.textAlign = (options.textAlign!=null) ? options.textAlign :"center";
  $('SDialogButton').innerHTML = (options.noHideButton!='1') ? '<input type="button" value="'+options.nameHideButton+'" onclick="sdlog.hide(\''+options.duration+'\');" class="button">':"";
 },

 hide: function(duration){
  duration=duration || "0";
  $('SDialogString').innerHTML = "";
  $('SDialogButton').innerHTML = "";
  $('SDialog').style.display = "none";
  new Effect.Opacity('SOverlay',{
   to: 0.0,
   duration: duration,
   afterFinishInternal:function(e){new Effect.Fade('SOverlay',{from:0.0,to:0.0,duration:0});}
   }
  );
  
  /**
   * 2009/07/03 カスタマイズ 開始
   * ダイアログを閉じる時に、セレクトボックスを再表示（IE6対策）
   */
  var elems = document.getElementsByTagName("select");
  for (i = 0; i < elems.length; i++) {
      elems[i].style.visibility = 'visible';
  }
  /**
   * 2009/07/03 カスタマイズ 終了
   */
 },
 
 /**
  * 2009/07/03 カスタマイズ 開始
  * ボタン押下時の画面中央にダイアログ表示
  */
 setWindowCentering: function(options) {
  var arrayPageSize = getPageSize();
  var p = {x:0, y:0};
		
  p.x = document.documentElement.scrollLeft + (arrayPageSize[2] / 2) - (options.width / 2);
  p.y = document.documentElement.scrollTop + (arrayPageSize[3] / 2) - (options.height / 2);

  $('SDialog').style.left = p.x + 'px';
  $('SDialog').style.top = p.y + 'px';

  $("SOverlay").style.width=arrayPageSize[0]+'px';
  $("SOverlay").style.height=arrayPageSize[1]+'px';
 }
 /**
  * 2009/07/03 カスタマイズ 終了
  */
}

function initSDLog() { sdlog=new sdlog(); }
Event.observe(window, 'load', initSDLog, false);


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){

  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {
    xScroll = window.innerWidth + window.scrollMaxX;
    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;
  }

  var windowWidth, windowHeight;

  if (self.innerHeight) { // all except Explorer
    if(document.documentElement.clientWidth){
      windowWidth = document.documentElement.clientWidth;
    } else {
      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 = xScroll;
  } else {
    pageWidth = windowWidth;
  }

  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
  return arrayPageSize;
}

