var IE = false ;
if (window.navigator.appName.indexOf("Explorer") !=-1)
{
  IE = true;
}

// Iframe 내에서 호출하여 페이지 높이를 맞춘다.
function cm_paperInit(minHeight)
{
  try
  {
    var timeArray = new Array(200, 500, 1500, 3500, 7000, 12000, 20000);
    //alert("1_cm_paperInit, timeArray.length="+timeArray.length);
    for(var i=0; i < timeArray.length; i++)
      setTimeout('_cm_paperInit(\''+minHeight+'\')', timeArray[i]);

    if(self.name!=null && self.name=="contents")
    {
      top.window.scrollTo(0,0);
    }
  }
  catch (e) {
  }
}

// Iframe 내에서 호출하여 페이지 높이를 맞춘다.
// 최대 하위 3단까지만 리사이징을 하도록 한다.
function _cm_paperInit(minHeight)
{
  try
  {
    //	alert("2_cm_paperInit");
    if(minHeight==null) minHeight = 0;
    if (self.name!=null && self.name!="" && parent!=null
      && parent._cm_resizeIframe!=null)
    {
      parent._cm_resizeIframe(self.name, minHeight);
    }

    if (parent.name!=null && parent.name!=""
      && (parent.name=="contents")
      && parent.parent!=null && parent.parent._cm_resizeIframe!=null && parent.parent._cm_paperInit!=null)
    {
      parent.parent._cm_resizeIframe(parent.name, minHeight);
    }

    if (parent.parent.name!=null && parent.parent.name!=""
      && (parent.parent.name=="contents")
      && parent.parent.parent!=null && parent.parent.parent._cm_resizeIframe!=null && parent.parent.parent._cm_paperInit!=null)
    {
      parent.parent.parent._cm_resizeIframe(parent.parent.name, minHeight);
    }
  }
  catch (e) {
  }
}

// 직접사용은 금함. cm_paperInit()를 사용할것.
function _cm_resizeIframe(name, minHeight)
{
  //alert("cm_resizeIframe, name="+name);
  if(minHeight==null) minHeight = 0;
  if(name==null || name=="") return;

  try
  {
    if (IE)
      var oBody = document.frames(name).document.body;
    else
      var oBody = document.getElementById(name).contentDocument.body;

    var oIFrame = document.getElementById(name);

    var frmWidth  = oBody.scrollWidth;// + (oBody.offsetWidth - oBody.clientWidth);
    var frmHeight = oBody.scrollHeight;// + (oBody.offsetHeight - oBody.clientHeight);

    if(name=="contents")
    {
      if(frmHeight >= 500)
        oIFrame.style.height = frmHeight;
      else
        oIFrame.style.height = 500;

      var oOpacityDiv = document.getElementById("opacityDiv");
      if(oOpacityDiv!=null)
      {
        oOpacityDiv.style.height = parseInt(oIFrame.style.height) + 200;
      }
    }
    else
    {
      oIFrame.style.height = frmHeight;
    }

  }
  catch (e) {
  }
}

// 외부 페이지를 Iframe으로 호출할 경우에 외부페이지에서 호출하기 위한 함수.
function cm_paperInitExternal(name, minHeight)
{
  try
  {
    //alert("cm_paperInitExternal, name="+name);
    var timeArray = new Array(200, 500, 1500, 3500, 7000, 12000, 20000);
    for(var i=0; i < timeArray.length; i++)
    {
      setTimeout('_cm_paperInitExternal(\''+name+'\',\''+minHeight+'\')', timeArray[i]);
    }
  }
  catch (e) {
  }
}

// Iframe 내에서 호출하여 페이지 높이를 맞춘다.
// 최대 하위 3단까지만 리사이징을 하도록 한다.
function _cm_paperInitExternal(name, minHeight)
{
  try
  {
    if(minHeight==null) minHeight = 0;

    if (name!=null && name!='')
    {
      _cm_resizeIframe(name, minHeight);
    }
    if (self.name!=null && self.name!=''
      && (self.name=="contents")
      && parent!=null && parent._cm_resizeIframe!=null)
    {
      parent._cm_resizeIframe(self.name, minHeight);
    }
    if (parent.name!=null && parent.name!=''
      && (parent.name=="contents")
      && parent.parent!=null && parent.parent._cm_resizeIframe!=null)
    {
      parent.parent._cm_resizeIframe(parent.name, minHeight);
    }
  }
  catch (e) {
  }
}


function resizeIfr(obj, minHeight) {
  minHeight = minHeight || 10;
  try {
    var getHeightByElement = function(body) {
      var last = body.lastChild;
      try {
        while (last && last.nodeType != 1 || !last.offsetTop) last = last.previousSibling;
        return last.offsetTop+last.offsetHeight;
      }
      catch(e) {
        return 0;
      }

    }

    var doc = obj.contentDocument || obj.contentWindow.document;
    if (doc.location.href == 'about:blank') {
      obj.style.height = minHeight+'px';
      return;
    }

    //var h = Math.max(doc.body.scrollHeight,getHeightByElement(doc.body));
    //var h = doc.body.scrollHeight;
    if (/MSIE/.test(navigator.userAgent)) {
      var h = doc.body.scrollHeight;
      // ie8에선 ie7과 비교시 약 15px 더 적게 값이 적용됨.
      //if(window.JSON) h+=15;
    }
    else
    {
      var s = doc.body.appendChild(document.createElement('DIV'))
      s.style.clear = 'both';

      var h = s.offsetTop;
      s.parentNode.removeChild(s);
    }

    //if (/MSIE/.test(navigator.userAgent)) h += doc.body.offsetHeight - doc.body.clientHeight;
    if (h < minHeight) h = minHeight;

    obj.style.height = h + 'px';
    if (typeof resizeIfr.check == 'undefined') resizeIfr.check = 0;
    if (typeof obj._check == 'undefined') obj._check = 0;

    //		if (obj._check < 5) {
    //			obj._check++;
    setTimeout(function(){ resizeIfr(obj,minHeight) }, 200); // check 5 times for IE bug
    //		} else {
    //obj._check = 0;
    //		}
  }
  catch (e) {
    //alert(e);
  }
}
function resizeIfrWithoutTimer(obj, minHeight) {
  minHeight = minHeight || 10;
  try {
    var getHeightByElement = function(body) {
      var last = body.lastChild;
      try {
        while (last && last.nodeType != 1 || !last.offsetTop) last = last.previousSibling;
        return last.offsetTop+last.offsetHeight;
      }
      catch(e) {
        return 0;
      }

    }

    var doc = obj.contentDocument || obj.contentWindow.document;
    if (doc.location.href == 'about:blank') {
      obj.style.height = minHeight+'px';
      return;
    }

    //var h = Math.max(doc.body.scrollHeight,getHeightByElement(doc.body));
    //var h = doc.body.scrollHeight;
    if (/MSIE/.test(navigator.userAgent)) {
      var h = doc.body.scrollHeight;
    }
    else {
      var s = doc.body.appendChild(document.createElement('DIV'))
      s.style.clear = 'both';

      var h = s.offsetTop;
      s.parentNode.removeChild(s);
    }

    //if (/MSIE/.test(navigator.userAgent)) h += doc.body.offsetHeight - doc.body.clientHeight;
    if (h < minHeight) h = minHeight;

    obj.style.height = h + 'px';
    if (typeof resizeIfr.check == 'undefined') resizeIfr.check = 0;
    if (typeof obj._check == 'undefined') obj._check = 0;

  }
  catch (e) {
    //alert(e);
  }
}


function exPageResize()
{
  obj = document.getElementById('hiddenRframe');
  obj.action = "http://www.icons.or.kr/js/resize.php";
  obj.target = "scrollResize";
  obj.h.value = document.body.scrollHeight;
  obj.submit();
}
