/**********************
* jQuery¿Í °°ÀÌ ½á¾ß ÇÔ
***********************/

//----------------------------
// ÇöÀç È­¸éÀÇ top positionÀ» °è»êÇÑ´Ù.
//----------------------------
function getOffsetTop(inputObj)
{
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null)
  {
    if(inputObj.tagName!='HTML')
    {
      returnValue += inputObj.offsetTop;
    }
  }
  return returnValue;
}

//----------------------------
// ÇöÀç È­¸éÀÇ left positionÀ» °è»êÇÑ´Ù.
//----------------------------
function getOffsetLeft(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null)
  {
    if(inputObj.tagName!='HTML')
    {
      returnValue += inputObj.offsetLeft;
    }
  }
  return returnValue;
}

//----------------------------
// ÇöÀç È­¸éÀÇ ScrollµÈ Á¤µµ¸¦ ¾ò´Â´Ù.
//----------------------------
function getPageScrollTop()
{
  var yScrolltop;
  var xScrollleft;
  if (self.pageYOffset || self.pageXOffset) {
    yScrolltop = self.pageYOffset;
    xScrollleft = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){     // Explorer 6 Strict
    yScrolltop = document.documentElement.scrollTop;
    xScrollleft = document.documentElement.scrollLeft;
  } else if (document.body) {// all other Explorers
    yScrolltop = document.body.scrollTop;
    xScrollleft = document.body.scrollLeft;
  }
  return {x:xScrollleft, y:yScrolltop};
}

//--------------------------------
// ÆäÀÌÁöÀÇ Å©±â¸¦ ¾ò´Â´Ù.
//--------------------------------
function getPageSize()
{
  var de = document.documentElement;
  var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
  return {w:w, h:h};
}

//--------------------------------
// ObjectÀÇ À§Ä¡¸¦ Áß¾Ó¿¡ À§Ä¡½ÃÅ²´Ù.
//--------------------------------
function center(objId)
{
  var jQObj = $("#"+objId);
  var pagesize = getPageSize();
  var arrayPageScroll = getPageScrollTop();
  var topPos = (arrayPageScroll.y + (pagesize.h - jQObj.height())/2);
  jQObj.css("left", (arrayPageScroll.x + (pagesize.w - jQObj.width())/2));
  jQObj.css("top", ((topPos < 0)?0:topPos));
}

//--------------------------------
// Object¸¦ Áß¾Ó¿¡ À§Ä¡½ÃÅ³ Position.
//--------------------------------
function centerPos(objId)
{
  var jQObj = $("#"+objId);
  var pagesize = getPageSize();
  var arrayPageScroll = getPageScrollTop();
  var topPos = (arrayPageScroll.y + (pagesize.h - jQObj.height())/2);
  var returnObj = {x:0,y:0};
  returnObj.x = (arrayPageScroll.x + (pagesize.w - jQObj.width())/2);
  returnObj.y = ((topPos < 0)?0:topPos);
  return returnObj;
}


//--------------------------------
// ObjectÀÇ ³ôÀÌ¸¦ Á¶Á¤ÇÑ´Ù.
//--------------------------------
function resizeHeightObj(objId, size, withObjId, reduceOffsetHeight)
{
  var jQueryObj = $("#" + objId);
  var height = jQueryObj.height();
  if(size < 0 && height <= 50)
  {
    return;
  }
  var resize = height + size;
  if(resize < 50) resize = 50;
  jQueryObj.height(resize);
  if(withObjId != null)
  {
    $("#" + withObjId).height(resize - reduceOffsetHeight);
  }
}

//--------------------------------
// Tooltip Layer º¸ÀÌ±â
//--------------------------------
function showTooltip(htmlStr, obj, xPos, yPos)
{
  if($("#tooltipLayer").length == 0)
  {
    //var tooltip = document.createElement("<div></div>"); <--- Firefox ¿¡·¯
    $("<div></div>").attr("id", "tooltipLayer").css({position:"absolute", display:"none", zIndex:"1"}).prependTo("body");
  }
  
  $("#tooltipLayer").html(htmlStr);
  
  if(!xPos) xPos = $(obj).offset().left;
  if(!yPos) yPos = $(obj).offset().top + $(obj).height();
  $("#tooltipLayer").css("left", xPos).css("top", yPos).show();
}

//--------------------------------
// Tooltip Layer ´Ý±â
//--------------------------------
function hideTooltip()
{
  $("#tooltipLayer").empty().hide();
}

/**********************************
* ÀÌ¹ÌÁöºä¾î¸¦ ÅëÇÑ ¿øº» ÀÌ¹ÌÁö º¸±â 
* 
*/
function showImageViewer(imagePath, startPosLeft, startPosTop, startWidth, startHeight)
{
  var pageSize = getPageSize();
  var pageScroll = getPageScrollTop();
  var viewer = null;
  if($("#imageViewer").length == 0)
  {
    viewer = $("<div></div>").attr("id", "imageViewer")
                             .css({position:"absolute", border:"1px solid #C3D3EF", padding:"2px", backgroundColor:"#E3EEFF"})
                             .prependTo("body");
  }
  else
  {
    viewer = $("#imageViewer").empty();
  }

  $("<div></div>").attr("id", "imageViewerHeader")
                  .css({width:"100%", height:22, cursor:"move", color:"#C3D3EF", paddingLeft:"10px", fontWeight:"bold", overflow:"hidden"})
                  .mousedown(function(event){ objStartMove(event, $("#imageViewer").get(0)); })
                  .text("Image Viewer")
                  .prependTo(viewer);
  
  if(startPosTop == null)
  {
    startPosTop = (pageScroll.y + 10);
  }
  
  if(startPosLeft == null)
  {
    startPosLeft = (pageSize.w/2);
  }
  
  if(startWidth == null)
  {
    startWidth = 0;
  }
  
  if(startHeight == null)
  {
    startHeight = 10;
  }
  
  var img = $("<img />").attr("id", "imageViewerImg")
                        .attr("src", imagePath)
                        .attr("alt", "Å¬¸¯ÇÏ¸é ÀÌ¹ÌÁö Ã¢ÀÌ ´ÝÈü´Ï´Ù.")
                        .css({cursor:"POINTER"})
                        .click(function(){ $("#imageViewer").remove(); });
  
  viewer.css({top:startPosTop, left:startPosLeft, width:startWidth, height:startHeight, opacity: 0.1})
        .append(img);
  
  var imgWidth = img.width();
  var imgHeight = img.height();
  if(imgWidth > 800)
  {
    imgHeight = Math.round(800*imgHeight/imgWidth);
    imgWidth = 800;
    
    $("#imageViewerImg").css({width:imgWidth, height:imgHeight});
  }
  /*
  if(imgWidth > 800 || imgHeight > 600)
  {
    if((imgWidth - 800) >= (imgHeight - 600))
    {
      imgHeight = Math.round(800*imgHeight/imgWidth);
      imgWidth = 800;
    }
    else
    {
      imgWidth = Math.round(600*imgWidth/imgHeight);
      imgHeight = 600;
    }
    
    $("#imageViewerImg").css({width:imgWidth, height:imgHeight});
  }
   */
  
  viewer.animate({ 
                   top: (pageScroll.y + (pageSize.h - imgHeight)/2), 
                   left: (pageScroll.x + (pageSize.w - imgWidth)/2),
                   width: imgWidth,
                   height: imgHeight + 22,
                   opacity: 1
                 }, 700 );
}


/**********************************
* Object DragAndDrop Move 
* common.js¿Í ÇÔ²² »ç¿ëÇØ¾ßÇÔ

<»ç¿ë ¿¹>
  $(document).ready(function(){
    initEvents();
  });
  
  <div id="menuBox" style="left:200px;top:100px;position:absolute;width:500px;border:2px groove #669;">
    <div style="height:22px;background-color:#669;cursor:move;" onmousedown="objStartMove(event, document.getElementById('menuBox'))"></div>
    <div>....</div>
  </div>

***********************************/

////////////////////////////  Object Move Start ////////////////////////////////////////
var opera = navigator.userAgent.toLowerCase().indexOf('opera')>=0?true:false;
var moveObj;
var isObjMoving = false;
var orgZIndex;
function initEvents()
{
  document.body.onselectstart = cancelSelectionEvent;
  document.body.ondragstart = cancelEvent;
  document.body.onmousemove = objMoving;
  document.body.onmouseup = objStopMoving;
}

function cancelSelectionEvent(e)
{
  if(document.all)e = event;

  if (e.target) source = e.target;
  else if (e.srcElement) source = e.srcElement;

  if (source.nodeType == 3) // defeat Safari bug
    source = source.parentNode;

  // DragÁß Select ¹æÁö
  if(moveObj != null) return false;
  else return true;
}

function cancelEvent()
{
  return false;
}

function objStartMove(e, obj)
{
  if(document.all)
  {
    e = event;
  }

  moveObj = obj;
  orgZIndex = obj.style.zIndex;
  obj.style.zIndex = 1000;
  
  obj.adjustX = e.clientX - getOffsetLeft(obj);
  obj.adjustY = e.clientY - getOffsetTop(obj) + document.documentElement.scrollTop;
  obj.moving = true;
}


function objMoving(e)
{
  if(document.all) e = event;

  if(document.all && e.button!=1 && !opera)
  {
    objStopMoving();
    return;
  }

  if(moveObj && moveObj.moving && !isObjMoving)
  {
    isObjMoving = true;
    moveObj.style.filter = 'alpha(opacity=70)';
    moveObj.style.opacity = '0.7';

  moveObj.style.marginLeft = "0px";
    var left = e.clientX - moveObj.adjustX;
    if(left > 0)
//    if(left > availLeftStart &&  (left + moveObj.offsetWidth) < availLeftEnd)
    {
      moveObj.style.left = left + 'px';
    }

    var top = e.clientY - moveObj.adjustY + document.documentElement.scrollTop;
    if(top > 0)
//    if(top > availTop)
    {
      moveObj.style.top = top + 'px';
    }
    isObjMoving = false;
  }
  
}

function objStopMoving()
{
  if(moveObj != null && moveObj.moving == true)
  {
    moveObj.style.zIndex = orgZIndex;
    moveObj.style.filter = null;
    moveObj.style.opacity = null;
    moveObj.moving = false;
    moveObj = null;
  }
}
////////////////////////////  Object Move End ////////////////////////////////////////



/**********************************
* fullscreen 
* common.js¿Í ÇÔ²² »ç¿ëÇØ¾ßÇÔ

<»ç¿ë ¿¹>

  function clickEvent(evt)
  {
    var str = "<div id='fsBox'><span id='fsCloser' tabindex='1'>x</span><br /><textarea id='fullTA'>Å×½ºÆ®³»¿ë</textarea></div>";
    var fs = showFullScreen(str);
    $("#fsCloser").attr("title", "´Ý±â").attr("alt", "´Ý±â").attr("tabindex", "1")
      .click(function(e){
        closeFullScreen(e);
      })
      .keydown(function(e){
        if(e.which == 13) //enter key
        {
          closeFullScreen(e);
        }
      });
    $("#fullTA").width(fs.w -40).height(fs.h -50).css("padding", "5px").click(stopEventBubble);
  }

***********************************/
var fullSCID = "fullSC";
var _oldDim = null;
var _oldVp = null;

function showFullScreen(fullScreenBodyHtml)
{
  _oldDim = {w:$("body").width(), h:$("body").height()};
  _oldVp = getPageScrollTop();

  if($("#" + fullSCID).length == 0)
  {
    $("<div></div>").attr("id", fullSCID).appendTo("body").click(stopEventBubble);
  }

  if(document.documentElement)
  {
    document.documentElement.style.overflow = 'hidden';
  }
  else
  {
    $(document).css("overflow", "hidden");
  }
  window.scrollTo(0,0);
  $("body").width(0).height(0);

  $("#fullSC").width("100%").height("100%").empty().html(fullScreenBodyHtml).show();

  return {w:$("#" + fullSCID).width(), h:$("#" + fullSCID).height()};
}

function closeFullScreen(evt)
{
  stopEventBubble(evt);
  $("body").width(_oldDim.w).height(_oldDim.h);
  $("#" + fullSCID).empty().hide();
  if(document.documentElement)
  {
    document.documentElement.style.overflow = 'auto';
  }
  else
  {
    $(document).css("overflow", "auto");
  }
  window.scrollTo(_oldVp.x, _oldVp.y);
  _oldDim = _oldVp = null;
}

var fsContId = null;
function defaultShowFSTextarea(taId)
{
  fsContId = taId;
  var str = "<div id='fsBox'><div id='fsHeader'><span id='fsCloser' tabindex='1'>x</span></div><textarea id='fullTA' tabindex='2'></textarea><div id='fsFooter'><input type='button' value='´Ý±â' alt='´Ý±â' title='´Ý±â' tabindex='3' onclick='defaultCloseFSTextarea(event)' /></div></div>";
  var fs = showFullScreen(str);
  $("#fsCloser").attr("title", "ÀüÃ¼È­¸é´Ý±â").attr("alt", "ÀüÃ¼È­¸é´Ý±â").attr("tabindex", "1")
    .click(function(e){
      defaultCloseFSTextarea(e);
    })
    .keydown(function(e){
      if(e.which == 13) //enter key
      {
        defaultCloseFSTextarea(e);
      }
    }).focus();
  $("#fullTA").width(fs.w -30).height(fs.h -70).click(stopEventBubble).val($("#"+taId).val());
}

function defaultCloseFSTextarea(e)
{
  $("#"+fsContId).val($("#fullTA").val());
  closeFullScreen(e);
  fsContId = null;
}

/***** end fullscreen ******/
function movPlay()
{
	document.getElementById("movplay").value="on";
	document.getElementById("movplay").style.display="block";
	document.getElementById("movplay").play();
}
function movStop()
{
	document.getElementById("movplay").stop();
	document.getElementById("movplay").style.display="none";
}
function movPause()
{
	document.getElementById("movplay").pause();
}
