﻿var fade_vec = new Array();
var fade_timer;

var fade_st = 0;
var fade_open_timer;
var fade_close_timer;
var open_st = 0;
var close_st = 0;

var timer1;

var popup_vec = new Array();

function getLeft(elt) {
    return getAbsPos(elt, "Left");
}

function getRight(elt) {
    return getAbsPos(elt, "Right");
}

function getTop(elt) {
    return getAbsPos(elt, "Top");
}

function getBottom(elt) {
    return getAbsPos(elt, "Bottom");
}

function getAbsPos(elt, which) {
    var pos = 0;
    while (elt != null) {
        pos += elt["offset" + which];
        elt = elt.offsetParent;
    }
    return pos;
}

function get_width(elt) {
    //return elt.style.width;        
    return elt.offsetWidth;
    //return elt.scrollWidth; 
}
function get_height(elt) {
    //return elt.style.height;
    return elt.offsetHeight;
    //return elt.scrollHeight; 
}
function getElt(elt_id) {
    if (document.getElementById)
        return document.getElementById(elt_id);
    else
        return document[elt_id];
}

function xmlhttpPost(strURL, callback) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            callback(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(null);
}

function responseAjax(str) {
    //alert(str);
}

function goLink(link, name, val) {
    var url = "PopupPage.aspx?act=set_cur&name=" + name + "&val=" + val;
    xmlhttpPost(url, responseAjax);
    window.location.href = link;
}

function setRoot(name, val) {
    var url = "PopupPage.aspx?act=set_root&name=" + name + "&val=" + val;
    xmlhttpPost(url, responseAjax);
}

function openPopup(parent_id, child_id, lev, hor, heb, cw1, csw1, fade, slide) {
    clearTimeout(timer1);
    var cw = parseInt(cw1);
    var csw = parseInt(csw1);
    var parent = getElt(parent_id);
    var child = getElt(child_id);
    //alert(parent+","+child+","+lev);
    child.style.display = "block";
    child.style.height = "auto";
    var i;
    if (popup_vec[lev] != null)
        if (popup_vec[lev] == parent)
        return;
    if (popup_vec[lev] == child)
        return;
    closePopup(lev, fade, slide);
    //popup_vec[lev]=child;
    popup_vec.push(child);
    //child.style.visibility = 'visible';
    if (fade == 'True') {
        //fade_set(child,100,1000);

        child.style.visibility = 'visible';
        doSlide(child);
        //$(child).slideDown('slow');

        //$(child).fadeIn('slow');
        //child.style.opacity=0/100;
        //$(child).fadeTo('1500', '0.0');            
        //$(child).css('opacity',0).fadeTo(1500, 1.0);
        //$(child).css('opacity',0);
        //$(child).fadeTo(1500, 1.0);            
        //            child.style.opacity=0/100;
        //            child.style.filter = "alpha(opacity=" + (0) + ")";            
        //            $(child).fadeTo('1500', '1.0');
        //opt_set(child,0);
    }
    else if (slide == 'True') {
        //$(child).stop();
        //$(child).slideUp('fast');        
        //$(child).hide();        
        //$(child).slideDown('slow');        
        //$(child).slideToggle('slow');
        //$(child).stop().hide().slideDown('slow');
        $(child).stop().hide().slideDown('normal');
        child.style.visibility = 'visible';
    }
    else {
        child.style.visibility = 'visible';
    }
    if (lev < 1 && hor == 'True') {
        if (heb == 'True') {
            //child.style.left=getLeft(parent)+get_width(parent)-getWidth(child);
            if (cw < 0)
                child.style.left = getLeft(parent) + (get_width(parent) - csw)+'px';
            else
                child.style.left = getLeft(parent) + (cw - csw) + 'px';
            child.style.top = getTop(parent) + get_height(parent) + 'px';
        }
        else {
            child.style.left = getLeft(parent) + 'px';
            child.style.top = getTop(parent) + get_height(parent) + 'px';
        }
    }
    else {
        if (heb == 'True') {
            child.style.left = getLeft(parent) - csw + 'px';
            child.style.top = getTop(parent) + 'px';
        }
        else {
            child.style.left = getLeft(parent) + get_width(child) + 'px';
            child.style.top = getTop(parent) + 'px';
        }
    }
}
function closePopup(lev, fade, slide) {
    var opt;
    var i;
    var elm;
    if (fade == 'True') {
        for (i = lev; i < popup_vec.length; i++) {
            elm = popup_vec[i];
            //fade_set(elm,0,1000);                

            //child.style.visibility = 'visible';
            //doSlide(elm);

            //                if ($(elm)==null)
            //                    $(elm).slideUp('slow');

            //popup_vec[i].style.visibility = 'hidden';
            //opt_set(elm,0);


            // Start animation
            //    $("#go").click(function(){
            //      $(".block").animate({left: '+=100px'}, 2000);
            //    });

            //    // Stop animation when button is clicked
            //    $("#stop").click(function(){
            //      $(".block").stop();
            //    });

            //    // Start animation in the opposite direction
            //    $("#back").click(function(){
            //      $(".block").animate({left: '-=100px'}, 2000);
            //    });

            popup_vec.splice(i, 1);
            i--;
        }
    }
    else if (slide == 'True') {
        for (i = lev; i < popup_vec.length; i++) {
            elm = popup_vec[i];
            //$(elm).stop();
            //$(elm).show();
            //$(elm).slideUp('slow');
            //$(elm).slideToggle('slow');
            //popup_vec[i].style.visibility = 'hidden';
            //$(elm).stop().slideUp('slow');
            $(elm).stop().hide();
            popup_vec.splice(i, 1);
            i--;
        }
    }
    else {
        for (i = lev; i < popup_vec.length; i++) {
            popup_vec[i].style.visibility = 'hidden';
            popup_vec.splice(i, 1);
            i--;
        }
    }

}
function outPopup(lev) {
    timer1 = setTimeout("closePopup(" + lev + ");", 1000);
}
function opt_set(elm, opt) {
    //        elm.style.opacity=(opt)/100;
    //        elm.style.filter = "alpha(opacity=" + (opt) + ")";    
    if (opt > 0)
        elm.style.visibility = 'visible';
    else
        elm.style.visibility = 'hidden';

    //elm.style.height=opt+'%';
    elm.style.height = cur.height * opt / 100;
    elm.style.opacity = (100) / 100;
    elm.style.filter = "alpha(opacity=" + (100) + ")";
}
function opt_get(elm) {
    var opt;
    if (elm.style.visibility == 'hidden')
        opt = 0;
    else {
        opt = elm.style.opacity;
        if (opt == null) opt = 0;
    }
    return opt;
}
function fade_set(elm, to, time) {
    var i, cur;
    for (i = 0; i < fade_vec.length; i++) {
        cur = fade_vec[i];
        if (cur.elm == elm) {
            cur.to = to;
            cur.step = (cur.to - cur.cnt) / (time / 50);
            fade_vec[i] = cur;
            return;
        }
    }
    cur = new Object();
    cur.elm = elm;
    cur.cnt = opt_get(elm);
    cur.to = to;
    cur.step = (cur.to - cur.cnt) / (time / 50);

    cur.height = elm.offsetHeight;

    fade_vec.push(cur);
    if (fade_vec.length == 1)
        fade_timer = setTimeout("fade_go()", 50);
}
function fade_go() {
    clearTimeout(fade_timer);
    var i;
    for (i = 0; i < fade_vec.length; i++) {
        cur = fade_vec[i];

        if (cur.to > cur.cnt) {
            cur.cnt += cur.step;
            if (cur.cnt > cur.to) cur.cnt = cur.to;
            opt_set(cur.elm, cur.cnt);
            fade_vec[i] = cur;
        }
        else if (cur.to < cur.cnt) {
            cur.cnt += cur.step;
            if (cur.cnt < cur.to) cur.cnt = cur.to;
            opt_set(cur.elm, cur.cnt);
            fade_vec[i] = cur;
        }
        else {
            //                fade_vec.splice(i,1);
            //                i--;
        }
    }
    if (fade_vec.length > 0)
        fade_timer = setTimeout("fade_go()", 50);
}
//--------------------------
//    function doSlide(id){ 
//        timeToSlide = 50; // in milliseconds 
//        obj = document.getElementById(id); 

function doSlide(obj) {
    timeToSlide = 50; // in milliseconds 
    if (obj.style.display == "none") { // if it's allready hidden we slide it down        
        obj.style.visibility = "hidden";
        obj.style.display = "block";
        height = obj.offsetHeight;
        obj.style.height = "0px";
        obj.style.visibility = "visible";
        slideDown(obj, 0, height, Math.ceil(height / timeToSlide));
    } else {
        slideUp(obj, Math.ceil(obj.offsetHeight / timeToSlide), obj.offsetHeight);
    }
}
function slideDown(obj, offset, full, px) {
    if (offset < full) {
        obj.style.height = offset + "px";
        offset = offset + px;
        setTimeout((function() { slideDown(obj, offset, full, px); }), 1);
    } else {
        obj.style.height = full + "px"; //If the data inside is updated on runtime you can use auto instead... 
    }
}
function slideUp(obj, px, full) {
    if ((obj.offsetHeight - px) > 0) {
        obj.style.height = obj.offsetHeight - px + "px";
        setTimeout((function() { slideUp(obj, px, full); }), 1);
    } else {
        obj.style.height = full + "px"; // we reset the height if we were to slide it back down 
        obj.style.display = 'none';
    }
}    

