/**
 *
 * @param config                configuracion del objeto
 * @param config.form           Id del formulario
 * @param config.input          Id del input que activara el arbol y en dode se pondra el nombre de la zona
 * @param config.codigo         Id de la variable del formulario en donde se pondra el codigo
 * @param config.residente      Id del input para comprovar si es residente
 */
Arbol = function(config) {

    this.crearArbol = function(url) {

        $.ajax({
            type: "GET",
            url: url,
            dataType: "html",
            success: function(msg) {
                var div = document.getElementById('arbol_div');
                if (!div) {
                    div = document.createElement('div');
                    div.id = 'arbol_div';
                    div.style.position = 'absolute';
                    div.style.backgroundColor = '#FFF';
                    div.style.border = '1px solid #000';
                    div.style.textAlign = 'left';
                    div.style.display = 'none';
                    document.body.appendChild(div);
                }
                div.style.display = 'none';
                div.innerHTML = msg;
                $("#arbol").treeview({
                    collapsed: true,
                    animated: false,
                    unique:true
                });
            }
        });

    };

    this.mostrarArbol = function() {
        $('#arbol_div').show();
        var text = document.getElementById(config.input);
        var arb = document.getElementById('arbol_div');
        var dim = Zapatec.Utils.getElementOffset(text);
        arb.style.top = dim.top + dim.height + 'px';
        arb.style.left = dim.left + 'px';
        arb.style.width = dim.width + 'px';
        arb.style.zIndex = '100';
        arb.style.display = 'block';
        arb.isOut = false;
        document.onclick = function(e) {
            var arb = document.getElementById('arbol_div');
            if (!arb.isOut) arb.isOut = true;
            else {
                var dim = Zapatec.Utils.getElementOffset(arb);
                //arb.style.display = 'none';
                var pos = mousePos(e);
                if (!clickInDiv(dim, pos)) {
                    arb.style.display = 'none';
                    if (isIE6()) {
                        var iframe = document.getElementById('iframeShimArbol');
                        if (iframe) iframe.style.display = 'none';
                    }
                }
            }
        }
        var dim_arb = Zapatec.Utils.getElementOffset(arb);
        if (isIE6()) {
            var iframe = document.getElementById("iframeShimArbol");
            if (!iframe) {
                iframe = document.createElement('iframe');
                iframe.id = 'IframeShimArbol';
                document.body.appendChild(iframe);
            }
            iframe.style.position = 'absolute';
            iframe.style.top = dim.top + dim.height + 'px';
            iframe.style.left = dim.left + 'px';
            iframe.style.width = dim.width + 'px';
            iframe.style.height = dim_arb.height + 'px';
            iframe.style.zIndex = '0';
            iframe.style.display = 'block';
            $(".expandable").click(reajustaIframeShim);
        }
    };

    this.ocultarArbol = function(){
        $('#arbol_div').hide();
    }

    reajustaIframeShim = function() {
        var text = document.getElementById(config.input);
        var arb = document.getElementById('arbol_div');
        var dim = Zapatec.Utils.getElementOffset(text);
        var dim_arb = Zapatec.Utils.getElementOffset(arb);
        var iframe = document.getElementById("iframeShimArbol");
        if (isIE6()) {
            iframe.style.position = 'absolute';
            iframe.style.top = dim.top + dim.height + 'px';
            iframe.style.left = dim.left + 'px';
            iframe.style.width = dim.width + 'px';
            iframe.style.height = dim_arb.height + 'px';
            iframe.style.zIndex = '0';
            iframe.style.display = 'block';
        }
    };

    clickInDiv = function(dim, pos) {
        return (pos.x > dim.left && pos.x < dim.left + dim.width && pos.y > dim.top && pos.y < dim.top + dim.height)
    };

    mousePos = function(e) {
        var isIE = document.all ? true : false;
        if (isIE) { // grab the x-y pos.s if browser is IE
            tempX = event.clientX + document.body.scrollLeft
            tempY = event.clientY + document.body.scrollTop
        } else {  // grab the x-y pos.s if browser is NS
            tempX = e.pageX
            tempY = e.pageY
        }
        var obj = new Object;
        obj.x = tempX;
        obj.y = tempY;
        return obj;
    }
            ,
            isIE6 = function() {
                return (navigator.appVersion.indexOf('MSIE 6') != -1);
            }
}
