﻿var _timer;
var _arrIframes = [];
//var _ARR = new Array();

function GetRootPath() { var root = window.location.toString().split('/'); return root[0] + '//' + root[2] + '/' + root[3] + '/'; }

function ExpCollVerticalOne(divId, replyIframeId) {
    ExpCollVertical(divId, 28,
                         document.all ? $(replyIframeId).Document.body.offsetHeight + 28
                            : $(replyIframeId).contentDocument.body.offsetHeight < 125 ? 125
                            : $(replyIframeId).contentDocument.body.offsetHeight);

}

function ExpCollVertical(id, min, max) {
    clearTimeout(_timer);
    var obj = $(id);
    if (parseInt(obj.style.height) <= min)
        ExpandVertical(obj);
    else
        CollapseVertical(obj, min);
}

function ExpandVertical(obj) {

    if (obj.MinHeight == null) obj.MinHeight = parseInt(obj.offsetHeight);
    obj.style.height = parseInt(obj.offsetHeight) + 'px';
    if (parseInt(obj.style.height) + 50 <= obj.MaxHeight) {
        obj.style.height = (parseInt(obj.style.height) + 50) + 'px';

        _timer = setTimeout(function() { ExpandVertical(obj, obj.MaxHeight) }, 5);
        //  _ARR.push(_timer);
    }
    else {
        obj.style.height = obj.MaxHeight + 'px';
    }
}

function CollapseVertical(obj, min) {
    if (obj.MaxHeight == null) obj.MaxHeight = parseInt(obj.offsetHeight);

    if (obj.style.height == '') obj.style.height = obj.offsetHeight + 'px';

    if (parseInt(obj.style.height) > min) {

        if (parseInt(obj.style.height) - 50 >= min) {
            obj.style.height = (parseInt(obj.style.height) - 50) + 'px'

            _timer = setTimeout(function() { CollapseVertical(obj, min) }, 5);
            // _ARR.push(_timer);
        }
        else {
            obj.style.height = min + 'px';
        }
    }
}

function ExpCollH(id, min, max) {
    clearTimeout(_timer);
    var obj = $(id);
    if (obj.MinWidth == null) obj.MinWidth = min;
    if (obj.MaxWidth == null) obj.MaxWidth = max;

    if (parseInt(obj.style.width) <= min)
        ExpHoriz(obj);
    else
        CollHoriz(obj, min);
}

function ExpHoriz(obj) {

    obj.style.width = parseInt(obj.offsetWidth) + 'px';
    if (parseInt(obj.style.width) + 50 <= obj.MaxWidth) {
        obj.style.width = (parseInt(obj.style.width) + 50) + 'px';

        _timer = setTimeout(function() { ExpHoriz(obj, obj.MaxWidth) }, 5);
        // _arr.push(_timer);
    }
    else {
        obj.style.width = obj.MaxWidth + 'px';
    }
}

function CollHoriz(obj, min) {

    if (obj.style.width == '') obj.style.width = obj.offsetWidth + 'px';

    if (parseInt(obj.style.width) > min) {

        if (parseInt(obj.style.width) - 50 >= min) {
            obj.style.width = (parseInt(obj.style.width) - 50) + 'px'

            _timer = setTimeout(function() { CollHoriz(obj, min) }, 5);
            // _arr.push(_timer);
        }
        else {
            obj.style.width = min + 'px';
        }
    }
}

function OnContentComplete(type, iframeId, max) {
    setTimeout(function() {
        var iframe = document.getElementById(iframeId);
        if (iframe) {

            var doc = document.all ? iframe.Document : iframe.contentDocument;
            var iframeDocBody = doc.body;
            if (iframeDocBody) {

                //  iframe.MaxHeight= document.all ?  iframeDocBody.scrollHeight  :  iframeDocBody.offsetHeight;

                iframe.style.height = document.all ? iframeDocBody.scrollHeight : iframeDocBody.offsetHeight + 'px';
                iframeDocBody.style.fontSize = '11px'; iframeDocBody.style.fontFamily = 'verdana,arial';
                iframe.style.height = document.all ? iframeDocBody.scrollHeight : iframeDocBody.offsetHeight + 30 + 'px';
                // iframe.style.height= document.all ?  iframeDocBody.scrollHeight  :  iframeDocBody.offsetHeight + 30;
                iframe.MaxHeight = parseInt(iframe.style.height);
                iframe.style.height = '1px';
                if (iframe.MaxHeight > max) iframe.MaxHeight = max;
                ExpandVertical(iframe);
                //if( parseInt(iframe.style.height) > max ) iframe.style.height = max+'px';
            }
            if (type == 'i') {
                GoToIframeAnchor(iframeId);
            }
            else if (type == 'r') {
                GoToAnchor();
            }
            MakeLinksPopup(doc);
        }
    }, 0);
}




function Preview(html) {
    var preview;
    preview = window.open(_ROOTPATH + 'Popups/Preview.aspx', 'Preview', 'width=800, height=600, resizable=1, scrollbars=1');
    setTimeout(function() {
        try {
            preview.document.body.innerHTML = html;
        }
        catch (ex) {
            preview.document.body.innerHTML = html;
        }
    }, 1000);
}

function QuickTip(str, W, H) {
    //alert(Root() + 'iframes/qck_tip.aspx?t='+t()+'&tip=' + str , W, H );
    OpenIframe(_ROOTPATH + 'iframes/qck_tip.aspx?t=' + t() + '&tip=' + str, W, H)
}

function BeginProcess(E) {
    var pos = GetMousePosition(E);
    var img = document.createElement('img');
    img.src = _ROOTPATH + 'images/spinner.gif';
    img.style.position = 'absolute';
    img.style.top = pos.y + 20 + 'px';
    img.style.left = pos.x + 'px';
    document.body.appendChild(img);
    return img;
}
function EndProcess(img) {
    document.body.removeChild(img);
}

function AlertMsg(msg, E) {
    var D = document.createElement('div');
    D.style.position = "absolute";
    D.align = "center";
    D.style.color = "black";
    D.style.backgroundImage = "url(" + _ROOTPATH + "images/grad/yellow.png)";
    D.innerHTML = '<img src=' + _ROOTPATH + 'images/icon_info.jpg />';
    D.innerHTML += msg;
    D.style.fontSize = '12px'
    D.style.width = '300px'
    if (E) {
        var pos = GetMousePosition(E);
        D.style.top = pos.y;
        D.style.left = pos.x;
    } else {
        CenterObject(D);
    }

    //    D.style.top = (parent.window.screen.height / 2 + parent.document.body.parentNode.scrollTop - parent.window.screenTop) + 'px'; 
    //    D.style.left = (parent.window.screen.width / 2 + parent.document.body.parentNode.scrollLeft - parent.window.screenLeft 
    //                                         - parseInt(D.style.width) ) +'px'; 
    document.body.appendChild(D);
    setTimeout(function() { document.body.removeChild(D) }, 5000);
}

function Afirm(O, x) {
    if (O.getAttribute('OrigColor') == null) O.setAttribute('OrigColor', O.style.color);
    if (x == null) x = 20;

    if ((x % 2) == 0) O.style.color = 'yellow';
    else O.style.color = 'blue';

    if (x == 0) {
        O.style.color = O.getAttribute('OrigColor');
        return;
    }

    setTimeout(function() { Afirm(O, --x); }, 100);
}

function CenterObject(O) {
    O.style.top = (parseInt(window.screen.height) / 2 + parseInt(document.body.parentNode.scrollTop) -
                        (window.screenTop ? parseInt(window.screenTop) : 100) -
                        parseInt(O.style.height ? O.style.height : 0) / 2) + 'px';
    O.style.left = (parseInt(window.screen.width) / 2 + parseInt(document.body.parentNode.scrollLeft) -
                       (window.screenLeft ? parseInt(window.screenLeft) : parseInt(window.screenX)) -
                        parseInt(O.style.width ? O.style.width : 0) / 2) + 'px';
}


//--Iframe Opener~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function OpenIframe(src, width, height, autoClose) {   //var iframe =document.getElementById('_iframe') ;
    if (_arrIframes.length > 0) CloseIframe();
    iframe = document.createElement('iframe');
    _arrIframes.push(iframe);
    document.body.appendChild(iframe); iframe.style.position = 'absolute'; iframe.style.borderWidth = '0px';

    //          if( iframe.src != "") { 
    //         setTimeout( function(){  OpenIframe( src , width, height   ) }, 500)
    //         return;
    //         } 
    //CloseIframe();  
    //iframe.style.visibility = "visible";
    iframe.src = src;
    if (width) iframe.style.width = width + "px";
    if (height) iframe.style.height = height + "px";

    //document.body.appendChild(iframe);
    CenterObject(iframe);
    MaximizeIframe(iframe, width, height);

    if (autoClose == null || autoClose == true) {
        setTimeout(
                        function() {
                            iframe.focus();   //$('_iframe').focus(); 
                            //iframe.onblur = function(){ setTimeout( function(){MinimizeIframe( iframe );}, 500) ;     } 
                            iframe.onblur = function() {

                                if (document.all) CloseIframe(); //ie
                                else {     //ff            
                                    document.body.onclick = function() {
                                        //  if( iframe.style.visibility != 'hidden' )
                                        CloseIframe();
                                        document.body.onclick = null;
                                    };

                                }
                            }
                        }
                   , 1000);
    }
    //window.history[window.history.length -2] = null;

    return iframe;

}

function CloseIframe() {
    //        $("_iframe").style.visibility = "hidden";
    //        $("_iframe").src = "";      
    setTimeout(function() {
        for (i = 0; i < _arrIframes.length; i++) {
            try {
                document.body.removeChild(_arrIframes[i]);
                _arrIframes.shift();
            } catch (ex) { }

        }
    }, 0);

    //        if( !document.all ){
    //          setTimeout( function(){ 
    //                window.history.back(); //ff offset
    //            }, 1);
    //        } 
}

function MaximizeIframe(iframe, maxWidth, maxHeight, w, h) {
    if (w == null) {
        w = 0;
        //iframe.style.top= iframe.stye
    }
    if (h == null) {
        h = 0;
    }
    if (w <= maxWidth) iframe.style.width = w + "px";
    if (h <= maxHeight) iframe.style.height = h + "px";

    if (w <= maxWidth || h <= maxHeight) {
        setTimeout(
         function() { MaximizeIframe(iframe, maxWidth, maxHeight, w + 50, h + 50); }
         , 10
     );
    }
    else {
        iframe.style.width = maxWidth + "px";
        iframe.style.height = maxHeight + "px";
    }
}

function MinimizeIframe(iframe, w, h) {
    if (w == null) w = parseInt(iframe.style.width);
    if (h == null) h = parseInt(iframe.style.height);

    if (w > 0) iframe.style.width = w + "px";
    if (h > 0) iframe.style.height = h + "px";

    if (w > 0 || h > 0) {
        setTimeout(
        function() { MinimizeIframe(iframe, w - 50, h - 50); }
        , 10
        );
    }
    else {
        CloseIframe();
    }
}

function DoAlternateItemImg(obj) {
    obj.onerror = null;
    obj.src = _DefaultItemImagePath;
}
function DoAlternateAvatar(obj) {
    obj.onerror = null;
    obj.src = _DefaultAvatarPath;
}

function DDL_SelectValue(ddl, value) {
    ddl = document.getElementById(ddl);
    for (i = 0; i < ddl.options.length; i++) {
        if (value == ddl.options[i].value) {
            ddl.selectedIndex = i;
            return;
        }
    }
}

function OpenSendLink(link, from) {
    OpenIframe(_ROOTPATH + 'iframes/sendlink.aspx?link=' + link + '&from=' + from, 380, 180);
}
function OpenInviteWin() {
    OpenIframe(_ROOTPATH + 'iframes/invite.aspx', 390, 160);
}


function GetMousePosition(ev) {
    if (ev.pageX || ev.pageY) {
        return { x: ev.pageX, y: ev.pageY };
    }
    return {
        x: ev.clientX + document.body.parentNode.scrollLeft - document.body.clientLeft,
        y: ev.clientY + document.body.parentNode.scrollTop - document.body.clientTop
    };
}

function CreateDiv(E, str) {
    var odiv;
    odiv = $("_div");
    odiv.innerHTML = str;
    odiv.style.backgroundColor = "yellow";
    odiv.style.position = "absolute";
    odiv.style.top = E.clientY; odiv.style.left = E.clientX;
    alert(odiv.style.backgroundPositionX);
}

function CreateIframe(url) {
    var F = document.createElement("iframe");
    F.src = url;
    F.style.position = "absolute";
    F.onblur = function() { EL("_div").innerHTML = ''; }
    return F;
}