﻿//Variables globals coordenades de mouse
var MousePosX = 0;
var MousePosY = 0;

$(document).mousemove(function(e) {
    window.mouseXPos = e.pageX;
    window.mouseYPos = e.pageY;
});

//Variables globals de drag
var DragItemTId = null;
var DragItemData = null;
var DragItemType = null;

$("#DivHelp").ready(function() {
    $('body').mousemove(function(e) {
        MousePosX = e.pageX;
        MousePosY = e.pageY;
    }); 
});

function MostrarStatusGlobal(Msg) {
    var ObjModal = $find("ModalPnlStatus");
    var DivPnl = document.getElementById($$("DivPnlStatus").attr("id"));
    var ObjMsg = document.getElementById("StatusMsg");    
    if (ObjModal && DivPnl && ObjMsg) {
        ObjMsg.innerHTML = Msg;
        if (Msg.length != 0) DivPnl.style.width = (15 * Msg.length) + "px";
        DivPnl.style.top = "-25px";
        DivPnl.style.left = -($(DivPnl).width() / 2) + "px";
        $(DivPnl).css({ opacity: 0.0 });
        ObjModal.show();
        $(DivPnl).animate({
            opacity: "1",
            top: "+=25px"
        }, 150);
    }
}

function AmagarStatusGlobal() {
    var ObjModal = $find("ModalPnlStatus");
    var DivPnl = document.getElementById($$("DivPnlStatus").attr("id"));
    var ObjMsg = document.getElementById("StatusMsg");
    if (ObjModal && DivPnl && ObjMsg) {
        $(DivPnl).animate({
            opacity: "0",
            top: "+=25px"
        }, 150, function() {
            ObjModal.hide();
            ObjMsg.innerHTML = "";
        });
    }
}

function MostrarStatus(Msg, TdId, ModalId) {
    var StatusMsg = document.getElementById(TdId);
    StatusMsg.innerHTML = Msg;
    $find(ModalId).show();
}

function AmagarStatus(TdId, ModalId) {
    var StatusMsg = document.getElementById(TdId);
    StatusMsg.innerHTML = "";
    $find(ModalId).hide();
}

function MostrarStatusSimple(Msg, FCall) {
    var Div = document.getElementById("DivStatusSimple");
    var TdMsg = document.getElementById("StatusSimpleMsg");
    if (Div && TdMsg) {
        TdMsg.innerHTML = Msg;
        Div.style.display = "block";
        Div.style.top = ($(window).height() / 2) - ($(Div).height() / 2) - 25 + "px";
        Div.style.left = ($(window).width() / 2) - ($(Div).width() / 2) + "px";
        $(Div).css({ opacity: 0.0 });
        $(Div).animate({
            opacity: "1",
            top: "+=25px"
        }, 150, function() {
            if (typeof FCall == "function") FCall();
        });
    }
}

function AmagarStatusSimple(FCall) {
    var Div = document.getElementById("DivStatusSimple");
    if (Div) {
        $(Div).animate({
            opacity: "0",
            top: "+=25px"
        }, 150, function() {
            Div.style.display = "none";
            if (typeof FCall == "function") FCall();
        });
    }
}

function MostrarMsg(Titol, Msg) {
    var ObjModal = $find("ModalPnlMsg");
    var DivPnl = document.getElementById($$("DivPnlMsg").attr("id"));
    var ObjTitol = document.getElementById("PnlMsgTitol");
    var ObjMsg = document.getElementById("PnlMsgMsg");
    var ObjBtOK = document.getElementById($$("BtOKPnlMsg").attr("id"));
    if (ObjModal && DivPnl && ObjTitol && ObjMsg && ObjBtOK) {
        ObjTitol.innerHTML = Titol;
        ObjMsg.innerHTML = Msg;
        ObjBtOK.onclick = "";
        ObjBtOK.style.display = "none";
        ObjModal.show();

        DivPnl.style.top = "-25px";
        DivPnl.style.left = -($(DivPnl).width() / 2) + "px";
        $(DivPnl).css({ opacity: 0.0 });
        ObjModal.show();
        $(DivPnl).animate({
            opacity: "1",
            top: "+=25px"
        }, 150);
        
    }
}

function AmagarMsg() {
    var ObjModal = $find("ModalPnlMsg");
    var DivPnl = document.getElementById($$("DivPnlMsg").attr("id"));
    if (ObjModal && DivPnl) {
        $(DivPnl).animate({
            opacity: "0",
            top: "+=25px"
        }, 150, function() {
            ObjModal.hide();
        });
    }
}

function MostrarCon(Titol, Msg, CadFuncio) {
    var ObjModal = $find("ModalPnlMsg");
    var DivPnl = document.getElementById($$("DivPnlMsg").attr("id"));
    var ObjTitol = document.getElementById("PnlMsgTitol");
    var ObjMsg = document.getElementById("PnlMsgMsg");
    var ObjBtOK = document.getElementById($$("BtOKPnlMsg").attr("id"));
    if (ObjModal && DivPnl && ObjTitol && ObjMsg && ObjBtOK) {
        ObjTitol.innerHTML = Titol;
        ObjMsg.innerHTML = Msg;
        ObjBtOK.onclick = function() { eval(CadFuncio); }
        ObjBtOK.style.display = "block";
        ObjModal.show();

        DivPnl.style.top = "-25px";
        DivPnl.style.left = -($(DivPnl).width() / 2) + "px";
        $(DivPnl).css({ opacity: 0.0 });
        ObjModal.show();
        $(DivPnl).animate({
            opacity: "1",
            top: "+=25px"
        }, 150);
        
    }
}

function AmagarCon() {
    var ObjModal = $find("ModalPnlMsg");
    var DivPnl = document.getElementById($$("DivPnlMsg").attr("id"));
    var ObjTitol = document.getElementById("PnlMsgTitol");
    var ObjMsg = document.getElementById("PnlMsgMsg");
    var ObjBtOK = document.getElementById($$("BtOKPnlMsg").attr("id"));
    if (ObjModal && DivPnl && ObjTitol && ObjMsg && ObjBtOK) {
        $(DivPnl).animate({
            opacity: "0",
            top: "+=25px"
        }, 150, function() {
            ObjModal.hide();
            ObjTitol.innerHTML = "";
            ObjMsg.innerHTML = "";
            ObjBtOK.setAttribute('onclick', "");
            ObjBtOK.style.display = "none";
        });
    }
}

function $$(id, context) {
    var el = $("#" + id, context);
    if (el.length < 1)
        el = $("[id$=_" + id + "]", context);
    return el;
}

function BuscarPos(Obj) {
    var PosLeft = PosTop = 0;
    if (Obj.offsetParent) {
        do {
            PosLeft += Obj.offsetLeft;
            PosTop += Obj.offsetTop;
        } while (Obj = Obj.offsetParent);
        return [PosLeft, PosTop];
    }
}

function ObtenirPos(Item) {
    var left = 0;
    var top = 0;
    while (Item.offsetParent) {
        left += Item.offsetLeft;
        top += Item.offsetTop;
        Item = Item.offsetParent;
    }
    left += Item.offsetLeft;
    top += Item.offsetTop;
    return { x: left, y: top };
}

function CapMouseCoords(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
    };
}

function SetFocus(CId) {
    if ($$(CId)) {

        var ObjDiv = document.createElement("div");
        ObjDiv.style.border = "dashed 2px #FF0000";
        ObjDiv.style.backgroundColor = "transparent";
        ObjDiv.style.position = "absolute";
        ObjDiv.style.display = "none";
        ObjDiv.style.width = $$(CId).width() + 10 + "px";
        ObjDiv.style.height = $$(CId).height() + 10 + "px";
        ObjDiv.style.top = $$(CId).offset().top - 5 + "px";
        ObjDiv.style.left = $$(CId).offset().left - 5 + "px";
        ObjDiv.id = "DivFocusControl";
        $("body").append(ObjDiv);
        $$(CId).focus();
        $(ObjDiv).fadeIn(200, function() {
            $(document).oneTime(2500, "hide", function() {
                $(ObjDiv).fadeOut(200, function() {
                    $("body").remove(ObjDiv);
                });
            });
        });

    }
}

function ConvertirString(Valor) {
    if (Valor) {
        return Valor.toString();
    } else {
        return "";
    }
}
