﻿function Positioner(panelDragId, positionId, positionTypeId, isRepositionedId, windowScrollOffsetId,
    isShownId) {

    //Setup Properties
    this.panelBehaviorId = "";
    this.positionId = "";
    this.positionTypeId = "";
    this.isRepositionedId = "";
    this.windowScrollOffsetId = "";
    this.isShowId = "";
    
    this.panelBehaviorId = panelDragId;
    this.positionId = positionId;
    this.positionTypeId = positionTypeId;
    this.isRepositionedId = isRepositionedId;
    this.windowScrollOffsetId = windowScrollOffsetId;
    this.isShowId = isShownId;

    var iPosit = this;

    this.SetPosition = function(iPosit, x, y) {

        try {
            var cPanel = $('#' + iPosit.panelBehaviorId);
            //Need to make it visible before we can set offset
            if ($('#' + iPosit.isShowId) != null) {
                var hfShown = $('#' + iPosit.isShowId);
                //alert($('#' + iPosit.isShowId).attr("id"));
                if (parseInt(hfShown.attr("value")) == 1)
                    $('#' + iPosit.panelBehaviorId).parent().show();
                else
                    $('#' + iPosit.panelBehaviorId).parent().hide();
            } else
                $('#' + iPosit.panelBehaviorId).parent().hide();

            // set the position of the panel manually with the retrieve value
            if (y != null) {
                cPanel.css("top", y);
                cPanel.parent().css("top", y);
            }

            cPanel.parent().css("left", x);
            //cPanel.parent().offset({ top: parseInt(y), left: parseInt(x) });
            cPanel.css("left", x);
            //cPanel.offset({ top: parseInt(y), left: parseInt(x) });

            var hfPos = $('#' + iPosit.positionId).attr("value", x.toString() + ';' + y.toString());
        } catch (e) {
        }
    };

    this.SetPositionCenterRight = function(iPosit) {
        var cPanel = $('#' + iPosit.panelBehaviorId);

        //Get the DragPanel width and height
        var pWidth = parseInt(cPanel.parent().css("width").replace('px', ''));
        var pHeight = parseInt(cPanel.parent().css("height").replace('px', ''));

        //Get the Screen Width and Height
        var sWidth = $(window).width();
        var sHeight = $(window).height();

        //Calculate the New Top and Left
        var newLeft = sWidth - pWidth;
        var newTop = (sHeight / 2) - (pHeight / 2);

        iPosit.SetPosition(iPosit, newLeft, newTop);
    }

    this.SetPositionCenterLeft = function(iPosit) {       
        //Get the DragPanel height
        var cPanel = $('#' + iPosit.panelBehaviorId);
        var pHeight = parseInt(cPanel.parent().css("height").replace('px', ''));

        //Get the Screen Height
        var sHeight = $(window).height();
        
        //Calculate the New Top and Left
        var newTop = (sHeight / 2) - (pHeight / 2);

        iPosit.SetPosition(iPosit, 0, newTop);
    };

    this.SetPositionBottomRight = function(iPosit) {       
        //Get the DragPanel width and height
        var cPanel = $('#' + iPosit.panelBehaviorId);
        var pWidth = parseInt(cPanel.parent().css("width").replace('px', ''));
        var pHeight = parseInt(cPanel.parent().css("height").replace('px', ''));

        //Get the Screen Width and Height
        var sWidth = $(window).width();
        var sHeight = $(window).height();
        
        //Calculate the New Top and Left
        var newLeft = sWidth - pWidth;
        var newTop = sHeight - pHeight;

        iPosit.SetPosition(iPosit, newLeft, newTop);
    };

    this.SetPositionBottomLeft = function(iPosit) {
          //Get the DragPanel height
        var cPanel = $('#' + iPosit.panelBehaviorId);
        var pHeight = parseInt(cPanel.parent().css("height").replace('px', ''));

        //Get the Screen Height
        var sHeight = $(window).height();
        
        //Calculate the New Top
        var newTop = sHeight - pHeight;

        iPosit.SetPosition(iPosit, 0, newTop);
    };

    this.SetPositionTopRight = function(iPosit) {
         //Get the DragPanel width
        var cPanel = $('#' + iPosit.panelBehaviorId);
        var pWidth = parseInt(cPanel.parent().css("width").replace('px', ''));

        //Get the Screen Width
        var sWidth = $(window).width();
        
        //Calculate the new left
        var newLeft = sWidth - pWidth;

        iPosit.SetPosition(iPosit, newLeft, 0);
    };

    this.SetPositionTopLeft = function(iPosit) {
    iPosit.SetPosition(iPosit, 0, 0);
    };

    this.SetPositionCenter = function(iPosit) {
        //Get the DragPanel width and height
        var cPanel = $('#' + iPosit.panelBehaviorId);
        var pWidth = parseInt(cPanel.parent().css("width").replace('px', ''));
        var pHeight = parseInt(cPanel.parent().css("height").replace('px', ''));

        //Get the Screen Width and Height
        var sWidth = $(window).width();
        var sHeight = $(window).height();

        //Calculate the New Top and Left
        var newLeft = (sWidth / 2) - (pWidth / 2);
        var newTop = (sHeight / 2) - (pHeight / 2);

        iPosit.SetPosition(iPosit, newLeft, newTop);
    };

    this.SetPositionExact = function(iPosit) {
        var hfPos = $('#' + iPosit.positionId);
        if (hfPos.attr("value") != '0') {
            var temp = new Array();
            temp = hfPos.attr("value").split(';');
            iPosit.SetPosition(iPosit, temp[0], temp[1]);
        }
    };

    this.SavePanelPosition = function(iPosit) {
        var elem = $('#' + iPosit.panelBehaviorId);
        var elem1 = $('#' + iPosit.positionId);
        //var pnlOffset = elem.offset();

        elem1.attr("value", elem.css("left").replace('px', '') + ';' + elem.css("top").replace('px', ''));

        // store the value in the hidden field
        var wElem = $('#' + iPosit.windowScrollOffsetId);
        wElem.attr("value", getScrollXY());

        var isRepositioned = $('#' + iPosit.isRepositionedId);
        isRepositioned.attr("value", 'y');
    };

    this.OnBodyScroll = function() {
        var elem = $('#' + iPosit.positionId);
        if (elem.attr("value") != null) {
            if (elem.attr("value") != "0") {
                //get the original position of the panel
                var oTemp = new Array();
                oTemp = elem.attr("value").split(';');

                //get the new window offset
                var temp = new Array();
                temp = getScrollXY().split(';');

                //get the old window offset
                var wElem = $('#' + iPosit.windowScrollOffsetId);
                var wTemp = new Array();
                wTemp = wElem.attr("value").split(';');

                var winX, winY;
                winX = parseInt(temp[0]) - parseInt(wTemp[0]);
                winY = parseInt(temp[1]) - parseInt(wTemp[1]);

                var newX, newY;
                newX = parseInt(oTemp[0]) - winX;
                newY = parseInt(oTemp[1]);

                // set the position of the panel manually with the retrieve value
                var cPanel = $('#' + iPosit.panelBehaviorId);

                if (cPanel != null) {
                    iPosit.SetPosition(iPosit, newX, newY);

                    //finally update the position in the HiddenField for PostBacks
                    elem.attr("value", newX.toString() + ';' + newY.toString());
                    //set the new scrollbar position
                    wElem.attr("value", temp[0].toString() + ';' +
                                temp[1].toString());
                }
            }
        }
    };
    
    //Add the function for Control Scrolling
    //This prevents automatic Browser Horizontal Scrolling
    if (window.addEventListener) {
        window.addEventListener("scroll", iPosit.OnBodyScroll, false);
    } else {
        window.attachEvent("scroll", iPosit.OnBodyScroll);
    }
    
    //Setup Methods
    this.PageLoad = function() {
        var panelId = iPosit.panelBehaviorId;

        // call the savePanelPosition when the panel is moved
        if ($('#' + panelId) != null) {
            $('#' + panelId).draggable({
                stop: function(event, ui) { iPosit.SavePanelPosition(iPosit); },
                drag: function(event, ui) {
                    var pPanel = ui.helper.parent();
                    pPanel.css("left", ui.position.left);
                    pPanel.css("top", ui.position.top);
                }
            });

            //Check if the Panel has been repositioned manually by user.
            var hfType = $('#' + iPosit.positionTypeId);

            var Repositioned = $('#' + iPosit.isRepositionedId);
            if (Repositioned.attr("value") == 'y') {
                hfType.attr("value", 'Exact');
            }

            var cPanel = $('#' + iPosit.panelBehaviorId);
            if (cPanel.parent().attr("id") != null) {

                //Check the Position Type of the box
                var posType = hfType.attr("value");

                switch (posType) {
                    case 'TopLeft': iPosit.SetPositionTopLeft(iPosit);
                        break;
                    case 'TopRight': iPosit.SetPositionTopRight(iPosit);
                        break;
                    case 'Center': iPosit.SetPositionCenter(iPosit);
                        break;
                    case 'BottomLeft': iPosit.SetPositionBottomLeft(iPosit);
                        break;
                    case 'BottomRight': iPosit.SetPositionBottomRight(iPosit);
                        break;
                    case 'CenterLeft': iPosit.SetPositionCenterLeft(iPosit);
                        break;
                    case 'CenterRight': iPosit.SetPositionCenterRight(iPosit);
                        break;
                    default: iPosit.SetPositionExact(iPosit);
                        break;
                }
            }
        }
    };

    this.SetShown = function(isShown, callback) {
        $('#' + iPosit.isShowId).attr("value", isShown);

        if (isShown == '1')
            $('#' + iPosit.panelBehaviorId).parent().show();
        else
            $('#' + iPosit.panelBehaviorId).parent().hide();

        if (callback != null)
            callback(isShown == '1');
    };

    Sys.Application.add_load(iPosit.PageLoad);
}      
        
function getScrollXY() 
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  
  return scrOfX + ';' + scrOfY;
}   
