﻿var avar_active = null;
var temp = null;
var chk = true;
function check(e) {
    var target = (e && e.target) || (event && event.srcElement);
    if (avar_active != null) {
        var obj2 = document.getElementById(avar_active.replace('_div', ''));
        var obj = document.getElementById(avar_active);
        if (obj) {
            if (avar_active.replace('_div','') == target.id) {
                checkParent(target) ? obj.style.display = 'none' : null;
                if (!chk) {
                    target == obj2 ? obj.style.display = 'block' : null;
                    scrollmenu(obj.id);
                }
            }
            else {
                checkParent(target) ? obj.style.display = 'none' : null;
                chk = true;
            }
        }
    }
}
function checkParent(t) {
    while (t.parentNode) {
        if (t == document.getElementById(avar_active)) {
            return false
        }
        t = t.parentNode
    }
    return true
}

function show_avapanel(panel, ownreid,u_name,u_avatar) 
{
    try {
        if (temp == null) { avar_active = panel.id + "_div"; }
        var t = panel.id + '_div';
        var obj = document.getElementById(panel.id + "_div");

        if (t == avar_active) {
            temp = "";
            if (chk) {
                obj.style.display = 'block';
                scrollmenu(avar_active);
                chk = false;
                if (obj.innerHTML == '') {
                    obj.innerHTML = '<img src="/images/upload_progress.gif" width=\"28\" height=\"8\"/>';
                    AnhsoServices.SubAvaMenu.BuildSubMenu(ownreid,u_name,u_avatar,getMenuComplete);
                }
            }
            else {
                obj.style.display = 'none';
                chk = true;
            }
        }
        else {
            document.getElementById(avar_active).style.display = 'none';

            avar_active = panel.id + "_div";
            document.getElementById(avar_active).style.display = 'block';
            scrollmenu(avar_active);
            chk = false;
            if (obj.innerHTML == '') {
                obj.innerHTML = '<img src="/images/upload_progress.gif" width=\"28\" height=\"8\"/>';
                AnhsoServices.SubAvaMenu.BuildSubMenu(ownreid,u_name,u_avatar,getMenuComplete);
            }
        }
    }
    catch (ex) { chk = false; }
}

function getMenuComplete(result) {
    document.getElementById(avar_active).innerHTML = result;
}

function scrollmenu(elem) {
    var divobj = document.getElementById(elem);
    var scrollBottom = innerSize("h");

    var pheight = document.documentElement.offsetHeight || document.body.offsetHeight;
    var ratioBottom = getScrollXY('y') + scrollBottom;
    var ratio = (ratioBottom - divobj.offsetTop);
    if (ratio < divobj.offsetHeight) {
        var j = divobj.offsetHeight - ratio;
        for (i = 0; i < j; i++) {
            setTimeout(function() {  window.scrollBy(0, 1) }, 100);
        }

    }

}
function getScrollXY(pos) {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        scrOfY = window.pageYOffset; scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft;
    }
    if (pos == "x") {
        return scrOfX;
    }
    else {
        return scrOfY;
    }

}

function innerSize(pos) {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        myWidth = window.innerWidth; myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        myWidth = document.body.clientWidth; myHeight = document.body.clientHeight;
    }
    if (pos == "w") {
        return myWidth
    }
    else return myHeight;
}
var hoving = false;
function avahover(ava)
  {
  
    var popitem = document.getElementById(ava.id + "_pop");
    var popitem_mn = document.getElementById(ava.id + "_pop_div");
    //alert(getposOffset(ava,'top'));
	popitem.style.top = getposOffset(ava,'top') - 4 + "px";
	popitem.style.left = getposOffset(ava,'left') - 2 +"px" ;
	popitem_mn.style.top = getposOffset(ava,'top') +55 +"px";
	popitem_mn.style.left = getposOffset(ava,'left')  + "px";
	popitem.style.display="inline";
  }
   function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
   }

function ahide(ava) 
{
    var popitem = document.getElementById(ava.id + "_pop");
    popitem.style.display = "none";
}

function menugotopage(url) {
    document.location.href = url;
}
function contactchange(n,s,a,id)
{
   var bg = document.getElementById('contactchangebg');
   var pop = document.getElementById('contactchangepop');
   if (bg && pop) {
       bg.style.width = innerSize('w') - 10 + "px";
       bg.style.height = innerSize('h') + "px";
       bg.style.top = getScrollXY('y') + "px";
       bg.style.display = "block";
       pop.style.top = getScrollXY('y') + 100 + 'px';

       pop.style.left = "40%";
       pop.style.display = "block";
       getFormChangeContent(n,s,a,id);
   }
}

function contactchange_hide()
{
    var bg = document.getElementById('contactchangebg');
    var pop= document.getElementById('contactchangepop');
    bg.style.display="none";
    pop.style.display="none";
}

function handle()
{
      
       var pop= document.getElementById('contactchangepop');
       var bg = document.getElementById('contactchangebg');
       if(bg && pop)
       {
           bg.style.width = innerSize('w') - 10 + "px";
           bg.style.height = innerSize('h') + "px";
           bg.style.top = getScrollXY('y') + "px";
           pop.style.top = getScrollXY('y') + 100 + 'px';
           pop.style.left = "40%";
       }
   }

   function getFormChangeContent(n,s,a,id)
   {
       var bg = document.getElementById('PopChangeContent');
       if (bg) 
       {
           bg.innerHTML = a;
       }
       AnhsoServices.SubAvaMenu.ChangeFrienOrFarmilyStatus(n,s,a,id,ChangeFrienOrFarmilyStatusComplete);
   }

   function ChangeFrienOrFarmilyStatusComplete(result) {
       var bg = document.getElementById('PopChangeContent');
       if (bg) {
           bg.innerHTML = result;
       }
   }
   
   
   
   


window.onresize = handle;
window.onscroll = handle;