﻿var stmnGAP1 = 10; // 위쪽 여백
var stmnGAP2 = 140; // 스크롤시 떨어지는 거리
var stmnBASE = 100; //시작위치 
var stmnActivateSpeed = 500; //내려오는속도
var stmnScrollSpeed = 20; //스크롤속도

var stmnTimer;
var objID;
var obj_leftMargin;
var obj_topMargin;
var isStop

function GetLeftPosition(what, offsettype) {
    var totaloffset = (offsettype == "left") ? what.offsetLeft : what.offsetTop;
    var parentEl = what.offsetParent; // element의 parent element가 있을 수 있으므로 계산해 주어야 한다.
    while (parentEl != null) {
        totaloffset = (offsettype == "left") ? totaloffset + parentEl.offsetLeft : totaloffset + parentEl.offsetTop;
        parentEl = parentEl.offsetParent;
    }
    return totaloffset;
  }


function StopStaticMenu(obj) {
    if (true == obj.checked) {
        isStop = "Y";
        ts_setCookie("SkyStop", "Y", 10);

        clearTimeout(stmnTimer);
    }
    else {
        isStop = "N";
        ts_deleteCookie("SkyStop");

        stayTopLeft();
    }
}

function JS_SKY_FloatTopDiv(ID, leftMargin, topMargin) {
  try {
    var startX = 900, startY = 100;
    var ns = (navigator.appName.indexOf("Netscape") != -1);
    var d = document;

    var obj = document.getElementById(ID);
    startX = GetLeftPosition(obj, "left") + leftMargin;

    function ml(id) {
      var el = d.getElementById ? d.getElementById(id) : d.all ? d.all[id] : d.layers[id];
      if (d.layers) el.style = el;
      el.sP = function(x, y) { this.style.left = x + 'px'; this.style.top = y + 'px'; };
      el.x = startX;
      el.y = startY;
      return el;
    }

    window.showDiv = function(isShow) {
      if (isShow == true) {
        ftlObj.style.visibility = "visible";
        ftlObj.style.display = "block"
      } else if (isShow == false) {
        ftlObj.style.visibility = "hidden";
        ftlObj.style.display = "none"
      }
    }

    window.stayTopLeft = function() {
      var stmnStartPoint, stmnEndPoint, stmnRefreshTimer;
      var pY = ns ? pageYOffset : document.documentElement.scrollTop;

      ftlObj.x = GetLeftPosition(obj, "left") + leftMargin;

      stmnStartPoint = parseInt(ftlObj.y, 10);
      stmnEndPoint = pY + stmnGAP2;

      if (stmnEndPoint < stmnGAP1)
        stmnEndPoint = stmnGAP1;

      stmnRefreshTimer = stmnActivateSpeed;

      if (stmnStartPoint != stmnEndPoint) {
        stmnScrollAmount = Math.ceil(Math.abs(stmnEndPoint - stmnStartPoint) / 15);
        ftlObj.y = parseInt(ftlObj.y, 10) + ((stmnEndPoint < stmnStartPoint) ? -stmnScrollAmount : stmnScrollAmount);
        stmnRefreshTimer = stmnScrollSpeed;
      }
      else {
        if ("Y" == isStop) {
          clearTimeout(stmnTimer);
          return;
        }
      }

      if (ftlObj.x >= 0 && ftlObj.y >= 0) {
        ftlObj.sP(ftlObj.x, ftlObj.y);
      }

      stmnTimer = setTimeout("stayTopLeft()", stmnRefreshTimer);
    }

    isStop = ts_getCookie("SkyStop", "")

    if ("Y" == isStop) {
      chkbox = document.getElementById("chk_StopStaticMenu");
      chkbox.checked = true;
    }

    ftlObj = ml("divStayTopLeft");
    stayTopLeft();
    showDiv(true);
  }
  catch (ex) {
  }
}

  function GetRightPosition(what) {
    var totaloffset = what.offsetLeft + what.offsetWidth;
    var parentEl = what.offsetParent; // element의 parent element가 있을 수 있으므로 계산해 주어야 한다.
    while (parentEl != null) {
      totaloffset = totaloffset + parentEl.offsetLeft;
      parentEl = parentEl.offsetParent;
    }
    return totaloffset;
  }

  // ex) fn_initFloatBanner('xx', 0, 0, 300, 10, 140)
  // targetID             : float banner가 위치하기 위한 인접 element
  // activateSpeed        : 내려오는속도
  // scrollSpeed          : 스크롤속도
  // scrollMoveDistance   : 스크롤 이동시 banner 이동 거리
  // endTargetID          : float banner의 넘지 말아야할 element
  // endTargetGap         : endTarget element와 flaot banner간의 거리
  
  var v_sourceID = "divStayTopLeft";
  var v_endTargetID = "";
  var v_endTargetGap;
  function fn_initFloatBanner(targetID, leftMargin, topMargin, activateSpeed, scrollSpeed, scrollMoveDistance, endTargetID, endTargetGap) {
    var ns = (navigator.appName.indexOf("Netscape") != -1);
    var d = document;

    var obj = document.getElementById(targetID);
    var startX = GetRightPosition(obj) + leftMargin;
    var startY = ns ? pageYOffset : document.documentElement.scrollTop;
    if (startY < GetLeftPosition(obj, "top")) {
      startY = GetLeftPosition(obj, "top") + topMargin;
    }

    function ml(id) {
      var el = d.getElementById ? d.getElementById(id) : d.all ? d.all[id] : d.layers[id];
      if (d.layers) el.style = el;
      el.sP = function(x, y) { this.style.left = x + 'px'; this.style.top = y + 'px'; };
      el.x = startX;
      el.y = startY;
      return el;
    }

    window.showDiv = function(isShow) {
      if (isShow == true) {
        ftlObj.style.visibility = "visible";
        ftlObj.style.display = "block"
      } else if (isShow == false) {
        ftlObj.style.visibility = "hidden";
        ftlObj.style.display = "none"
      }
    }

    window.stayTopLeft = function() {
      var stmnStartPoint, stmnEndPoint, stmnRefreshTimer;
      var pY = ns ? pageYOffset : document.documentElement.scrollTop;
      if (pY < GetLeftPosition(obj, "top")) {
        pY = GetLeftPosition(obj, "top") + topMargin;
      }

      ftlObj.x = GetRightPosition(obj) + leftMargin;

      stmnStartPoint = parseInt(ftlObj.y, 10);
      stmnEndPoint = pY + scrollMoveDistance;

      if (stmnEndPoint < topMargin)
        stmnEndPoint = topMargin;

      stmnRefreshTimer = activateSpeed;

      if (stmnStartPoint != stmnEndPoint) {
        if ("Y" != isStop) {
          if (undefined != endTargetID) {
            s_height = ftlObj.y + document.getElementById(v_sourceID).clientHeight;
            e_height = GetLeftPosition(document.getElementById(v_endTargetID), "top") - (undefined != v_endTargetGap ? v_endTargetGap : 0);

            if (s_height < e_height || stmnStartPoint > stmnEndPoint) {
              stmnScrollAmount = Math.ceil(Math.abs(stmnEndPoint - stmnStartPoint) / 15);
              ftlObj.y = parseInt(ftlObj.y, 10) + ((stmnEndPoint < stmnStartPoint) ? -stmnScrollAmount : stmnScrollAmount);
            }
          }
          else {
            stmnScrollAmount = Math.ceil(Math.abs(stmnEndPoint - stmnStartPoint) / 15);
            ftlObj.y = parseInt(ftlObj.y, 10) + ((stmnEndPoint < stmnStartPoint) ? -stmnScrollAmount : stmnScrollAmount);
          }
        }
        stmnRefreshTimer = scrollSpeed;
      }

      if (ftlObj.x >= 0 && ftlObj.y >= 0)
        ftlObj.sP(ftlObj.x, ftlObj.y);

      stmnTimer = setTimeout("stayTopLeft()", stmnRefreshTimer);
    }

//    isStop = ts_getCookie("SkyStop", "")

//    if ("Y" == isStop) {
//      chkbox = document.getElementById("chk_StopStaticMenu");
//      chkbox.checked = true;
//    }

    ftlObj = ml(v_sourceID);
    if (undefined != endTargetID)
      v_endTargetID = endTargetID;
    if (undefined != endTargetGap)
      v_endTargetGap = endTargetGap;
    stayTopLeft();
    showDiv(true);
  }