﻿var popup = function()
{
    var dragObj = new Object();
    var myPopup;
    dragObj.zIndex = 0;
    var _disabled;
    function pop()
    {
    }
    
    pop.prototype.show = function(event, id)
    {
        myPopup = document.getElementById(id);
        myPopup.style.zIndex = 998;
        positionMe();
        var f = new fade.fader(event, id);
        f.fadeIn();
        _disabled = document.createElement("div");
        _disabled.style.left = 0;
        _disabled.style.top = 0;
        _disabled.style.width = (_scrollWidth()) + "px";
        _disabled.style.height = (_scrollHeight()) + "px";
        _disabled.style.opacity = 0.6;
        _disabled.style.filter = "alpha(opacity=60)";
        _disabled.style.zIndex = 9;
        _disabled.style.position = "absolute";
        _disabled.style.background = "#2b2b2b";
        _disabled.setAttribute('id','_disabled');
        document.body.appendChild(_disabled);
        
        //fadeInOut(id,1)
    }
    
    pop.prototype.hide = function(id)
    {
        var f = new fade.fader(null, id);
        f.fadeOut();
        myPopup.style.zIndex = 0;
        document.body.removeChild(_disabled);
        //fadeInOut(id,-1)
    }
        
    pop.prototype.dragStart = function(event, id) {

      var el;
      var x, y;

      // If an element id was given, find it. Otherwise use the element being
      // clicked on.

      if (id)
        dragObj.elNode = getElement(id);
      else {
        if (isIE)
          dragObj.elNode = window.event.srcElement;
        if (isNS)
          dragObj.elNode = event.target;

        // If this is a text node, use its parent element.

        if (dragObj.elNode.nodeType == 3)
          dragObj.elNode = dragObj.elNode.parentNode;
      }

      // Get cursor position with respect to the page.

      if (isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
          + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
          + document.body.scrollTop;
      }
      if (isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
      }

      // Save starting positions of cursor and element.

      dragObj.cursorStartX = x;
      dragObj.cursorStartY = y;
      dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 0);
      dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  0);

      if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
      if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

      // Update element's z-index.

      dragObj.elNode.style.zIndex = myPopup.style.zIndex + 1;//++dragObj.zIndex;

      // Capture mousemove and mouseup events on the page.

      if (isIE) {
        document.attachEvent("onmousemove", dragGo);
        document.attachEvent("onmouseup",   dragStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
      }
      if (isNS) {
        document.addEventListener("mousemove", dragGo,   true);
        document.addEventListener("mouseup",   dragStop, true);
        event.preventDefault();
      }
    }

    pop.prototype.resizeStart = function(event, id) {

      var el;
      var x, y;

      // If an element id was given, find it. Otherwise use the element being
      // clicked on.

      if (id)
        dragObj.elNode = getElement(id);
      else {
        if (isIE)
          dragObj.elNode = window.event.srcElement;
        if (isNS)
          dragObj.elNode = event.target;

        // If this is a text node, use its parent element.

        if (dragObj.elNode.nodeType == 3)
          dragObj.elNode = dragObj.elNode.parentNode;
      }

      // Get cursor position with respect to the page.

      if (isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
          + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
          + document.body.scrollTop;
      }
      if (isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
      }

      // Save starting positions of cursor and element.

      dragObj.cursorStartX = x;
      dragObj.cursorStartY = y;
      dragObj.elStartWidth  = parseInt(dragObj.elNode.style.width, 10);
      dragObj.elStartHeight   = parseInt(dragObj.elNode.style.height,  10);

      if (isNaN(dragObj.elStartWidth)) dragObj.elStartWidth = getElementWidth(id);
      if (isNaN(dragObj.elStartHeight))  dragObj.elStartHeight  = getElementHeight(id);

      // Update element's z-index.

      dragObj.elNode.style.zIndex = myPopup.style.zIndex + 1;//++dragObj.zIndex;

      // Capture mousemove and mouseup events on the page.

      if (isIE) {
        document.attachEvent("onmousemove", resizeGo);
        document.attachEvent("onmouseup",   resizeStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
      }
      if (isNS) {
        document.addEventListener("mousemove", resizeGo,   true);
        document.addEventListener("mouseup",   resizeStop, true);
        event.preventDefault();
      }
    }
    
    function positionMe() {

        var elm = myPopup;
	    // popup width and height
	    var tpWd = (ie4||ie5)? elm.clientWidth: elm.offsetWidth;
	    var tpHt = (ie4||ie5)? elm.clientHeight: elm.offsetHeight;

        if (tpWd == 0)
            tpWd = parseInt(elm.style.width);
        if (tpHt == 0)
            tpHt = parseInt(elm.style.height);
            
   	    // check mouse position against tip and window dimensions
	    // and position 
	    elm.style.left = (Math.ceil((_winWidth()-tpWd)/2) + _scrollX()) + "px";
	    elm.style.top = (Math.ceil((_winHeight()-tpHt)/2) + _scrollY()) + "px";
    }
    
    function dragGo(event) {

      var x, y;

      // Get cursor position with respect to the page.

      if (isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
          + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
          + document.body.scrollTop;
      }
      if (isNS) {
        x = event.clientX + _scrollX();
        y = event.clientY + _scrollY();
      }

      // Move drag element by the same amount the cursor has moved.

      dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
      dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

      if (isIE) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
      }
      if (isNS)
        event.preventDefault();
    }

    dragStop = function(event) {
      // Stop capturing mousemove and mouseup events.
      if (isIE) {
        document.detachEvent("onmousemove", dragGo);
        document.detachEvent("onmouseup",   dragStop);
      }
      if (isNS) {
        document.removeEventListener("mousemove", dragGo,   true);
        document.removeEventListener("mouseup",   dragStop, true);
      }
    }
    
    function resizeGo(event) {

      var x, y;

      // Get cursor position with respect to the page.

      if (isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
          + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
          + document.body.scrollTop;
      }
      if (isNS) {
        x = event.clientX + _scrollX();
        y = event.clientY + _scrollY();
      }

      // Move drag element by the same amount the cursor has moved.

      dragObj.elNode.style.width = (dragObj.elStartWidth + x - dragObj.cursorStartX) + "px";
      dragObj.elNode.style.height  = (dragObj.elStartHeight  + y - dragObj.cursorStartY) + "px";

      if (isIE) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
      }
      if (isNS)
        event.preventDefault();
    }

    resizeStop = function(event) {
      // Stop capturing mousemove and mouseup events.
      if (isIE) {
        document.detachEvent("onmousemove", resizeGo);
        document.detachEvent("onmouseup",   resizeStop);
      }
      if (isNS) {
        document.removeEventListener("mousemove", resizeGo,   true);
        document.removeEventListener("mouseup",   resizeStop, true);
      }
    }    
    return {pop:pop}
}();

var _popup = new popup.pop();

