﻿


function InitControlPopup(obj, objScript) {
    this.objID = obj;    
    this.imgPathZoomOut = "/wpresources/Bamboo.SPGridView/Popup/ZoomOut.gif";
    this.imgpathZoomIn = "/wpresources/Bamboo.SPGridView/Popup/ZoomIn.gif";
    var pathASPX = "/_Layouts/Bamboo.SPGridView/Bamboo.GridView.AddItems.aspx";
    var Position = { Left: function() { return (screen.width) ? (screen.width - 700) / 2 + document.body.scrollLeft : 0 }, Top: function() { return (screen.height) ? (screen.height - 650) / 2 + document.body.scrollTop : 0 } };
    var String = { Empty: "", IsNullOrEmpty: function(str) { if (!str || str.length == 0) return true; return false; }, Trim: function(stringToTrim) { return stringToTrim.replace(/^\s+|\s+$/g, ""); }, LeftTrim: function(stringToTrim) { return stringToTrim.replace(/^\s+/, ""); }, RightTrim: function(stringToTrim) { return stringToTrim.replace(/\s+$/, ""); }, TrimAll: function(sString) { while (sString.substring(0, 1) == ' ') { sString = sString.substring(1, sString.length); } while (sString.substring(sString.length - 1, sString.length) == ' ') { sString = sString.substring(0, sString.length - 1); } return sString; } };

    function ShowForIFrame(workOn, idWP) {     
        var idIFrame = obj + "_iframeID";
        var objIFrame = document.getElementById(idIFrame);
        if (objIFrame == null)
            return;

        objIFrame.style.width = "100%";
        objIFrame.style.height = "100%";

        var makeframe = document.createElement("iframe");
        makeframe.setAttribute("id", "PopIframe");
        makeframe.setAttribute("class", "PopIframe");
        makeframe.setAttribute("name", "PopIframe");
        makeframe.setAttribute("frameborder", "0");
        makeframe.setAttribute("src", pathASPX + "?WorkOn=" + workOn + "&IdWebpart=" + idWP);
        makeframe.style.width = "100%";
        makeframe.style.height = "100%";
        makeframe.setAttribute("scrolling", "yes");
        objIFrame.innerHTML = "";
        objIFrame.appendChild(makeframe);        
       
    }
    
    this.CloseLoading = function() {        
        parent.document.getElementById(this.objID + "_idLoading").innerText = "";
    }

    this.ClosePopup = function(isRecalculator) {
        if (isRecalculator && objScript) {
            objScript.RefreshValueTotals(null, null);
            objScript.Calculator();
        }
        var ifr = parent.document.getElementById("PopIframe");
        ifr.parentNode.removeChild(ifr);
        parent.document.getElementById(this.objID + "_imgCloseMeetTeamCalendar").click();
    }

    function CreateXMLRequest(url, idWebpart) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                var value = xmlhttp.responseText;
                if (!String.IsNullOrEmpty(value)) {
                    var myObject = eval('(' + value + ')');
                    ShowForIFrame(myObject.workOn, myObject.idwp);
                }
            }
        };
        var bambooSPGridViewForAddNewItem = "&bambooSPGridViewForAddNewItem=" + idWebpart;
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        xmlhttp.setRequestHeader("Accept-Encoding", "gzip, deflate");
        xmlhttp.send(bambooSPGridViewForAddNewItem);
    }

    this.Showpopup = function(workOn, idWebpart) {
        document.body.scroll = "no";
        enableMoving = true;         
        var urlTmp = window.location.href;
        CreateXMLRequest(urlTmp, idWebpart);
        var panel = document.getElementById(this.objID + "_dagdropbodyid");
        if (panel != null) {
            panel.style.display = "block";
            panel.style.zIndex = "1505";


            panel.style.left = Position.Left();
            panel.style.top = Position.Top();
            panel.style.width = 700;
            panel.style.height = 500;

        }
        var divBg = document.getElementById(this.objID + "_divBackGround");
        if (divBg != null) {

            divBg.style.display = "block";
            divBg.style.zIndex = "1504";
            divBg.style.top = document.body.scrollTop;
            divBg.style.left = document.body.scrollLeft;
            divBg.style.width = window.screen.width; //(document.body.scrollWidth > window.screen.width ? document.body.scrollWidth : window.screen.width - 24);
            divBg.style.height = screen.height;
        }
    }

    this.SetClosePopup = function(obj, htmlBgDiv, hiddenField, imgControl, zoomOut)//binhan
    {
        enableMoving = true;
        document.body.scroll = "auto";
        obj = document.getElementById(this.objID + "_" + obj);
        htmlBgDiv = document.getElementById(this.objID + "_" + htmlBgDiv);
        var statusZoom = document.getElementById(this.objID + "_" + hiddenField);
        if (statusZoom != null) {

            statusZoom.value = "min";
            if (obj != null) {               
                obj.style.left = Position.Left();
                obj.style.top = Position.Top();
                obj.style.width = 700;
                obj.style.height = 500;

            }
            var objImg = document.getElementById(this.objID + "_" + imgControl);
            if (objImg != null) {
                objImg.src = this.imgPathZoomOut;
                objImg.title = zoomOut;
            }
        }

        if (htmlBgDiv != null) {
            if (htmlBgDiv.style.display == 'none') {
                htmlBgDiv.style.display = "block";
            }
            else {
                htmlBgDiv.style.display = "none";
            }
        }
        if (obj != null) {
            if (obj.style.display == 'block') {
                obj.style.display = 'none';
                obj.style.zIndex = '-1';
            }
            else {
                obj.style.display = 'block';
                obj.style.zIndex = '100';
            }

            if (obj.style.display == 'block') {
            
                var reSizeObj = { Top: function() { var wint; var h = 300; if (screen.width) wint = (screen.height - h) / 2; else wint = 0; return wint; }, Left: function() { var winl; var w = 500; if (screen.width) winl = (screen.width - w) / 2; else winl = 0; return winl; } };

                obj.style.top = reSizeObj.Top();
                obj.style.left = reSizeObj.Left();
            }
        }
    }

    this.CheckZoomPopupMeeting = function(event, obj, objDiv, statusZoom, zoomIn, zoomOut) //default = min
    {      
        var div = document.getElementById(this.objID + '_' + objDiv);
        var hiddenfield = document.getElementById(this.objID + '_' + statusZoom);
        if (hiddenfield.value == "min") {
            div.style.width = (window.screen.width - 25);
            div.style.height = (window.screen.height - 220);
            div.style.top = 10 + document.body.scrollTop;
            div.style.left = 10 + document.body.scrollLeft;
            obj.src = this.imgpathZoomIn;
            obj.title = zoomIn;
            hiddenfield.value = "max";              
            enableMoving = false;
        }
        else if (hiddenfield.value == "max") {
            if (div != null) {

                div.style.left = Position.Left();
                div.style.top = Position.Top();
                div.style.width = 700;
                div.style.height = 500;

            }
            obj.src = this.imgPathZoomOut;
            obj.title = zoomOut;
            hiddenfield.value = "min";             
            enableMoving = true;
        }
    }

    this.MakeSureForMoving = function(needSure) {
        if (needSure) {
            document.getElementById("dagdropbodyIframeid").style.zIndex = 1505;
            document.getElementById("dagdropbodyIframeid").style.display = 'block';
        } else {
            document.getElementById("dagdropbodyIframeid").style.zIndex = -1;
            document.getElementById("dagdropbodyIframeid").style.display = 'none';
        }
    }
}

var enableMoving = true;
var bambooIsMakeMoveable = false;
function BambooDragAndDrop() {

    var dragObject = null;
    var dragObjectTmp = null;
    var mouseOffset = null;
    var ObjectMovingTmp = { X: 0, Y: 0, Style: null };
    BambooDragAndDrop.prototype.MouseType = { hand: "hand", move: "move", auto: "auto" };

    this.InitObject = function(obj) {
        if (typeof (obj) == 'string') { obj = document.getElementById(obj); }
        dragObjectTmp = obj;
        document.onmousemove = MouseMove;
        document.onmouseup = MouseUp;
        MakeDraggable(obj);
    }

    this.MakeMoveable = function(mouseType) {
        if (!dragObjectTmp) {
            alert('You must call InitObject method before!');
            return;
        }
        if (mouseType != "") {
            dragObjectTmp.onmouseover = function(e) {
                dragObjectTmp.style.cursor = mouseType;
            }
        }
        dragObjectTmp.onmousedown = function() {
            bambooIsMakeMoveable = true;
        }
    }

    function GetMouseOffset(target, ev) {
        ev = ev || window.event;

        var docPos = GetPosition(target);
        var mousePos = MouseCoords(ev);
        return { x: mousePos.x - docPos.x, y: mousePos.y - docPos.y };
    }

    function GetPosition(e) {
        var left = 0;
        var top = 0;

        while (e.offsetParent) {
            left += e.offsetLeft;
            top += e.offsetTop;
            e = e.offsetParent;
        }

        left += e.offsetLeft;
        top += e.offsetTop;

        return { x: left, y: top };
    }
    function MouseMove(ev) {
        if (!bambooIsMakeMoveable || !enableMoving) {           
            return;
        }
        ev = ev || window.event;
        var mousePos = MouseCoords(ev);

        if (dragObject) {
            if (objControlPopup)
                objControlPopup.MakeSureForMoving(true);  
                          
            dragObject.style.position = 'absolute';
            dragObject.style.zIndex = 1505;
            ObjectMovingTmp.Y = dragObject.style.top = mousePos.y - mouseOffset.y;
            ObjectMovingTmp.X = dragObject.style.left = mousePos.x - mouseOffset.x;
            ObjectMovingTmp.Style = dragObject.style;
            return false;
        }    
    }


    function MouseUp() {
        if (objControlPopup)
            objControlPopup.MakeSureForMoving(false);       
        dragObject = null;
        dragObjectTmp = null;
        bambooIsMakeMoveable = false;
    }

    function MakeDraggable(item) {
        if (!item) return;
        item.onmousedown = function(ev) {
            dragObjectTmp = dragObject = this;
            mouseOffset = GetMouseOffset(this, ev);
            return false;
        }
    }

    function MouseCoords(ev) {
        if (ev.pageX || ev.pageY) {
            return { x: ev.pageX, y: ev.pageY };
        }
        return {
            x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
            y: ev.clientY + document.body.scrollTop - document.body.clientTop
        };
    }
}


