﻿$(document).ready(function() {
    $(".boxMesage").bind("click", function() {
        doMessageBox(0, '', false);
    });
    innitava();
})

$.extend({
    getUrlVars: function() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name) {
        return $.getUrlVars()[name];
    }
});

function innitava() {
    $('div.ava_div').hover(function() { avahover(this) }, function() {
        ahide(this);
    });
    $('div.ava_popup').hover(function(){
      if(avar_active){clearTimeout(toutclock)}
     },function(){
    });
    
}
function hide_avamenu(ava){
      $(ava).fadeOut();
      $('#'+ava.id.split('_')[0]+'_pop').hide();
     avar_active=null;
   
}


function doMessageBox(type, content, isShow) {
    if (isShow) {
        $(".boxMesage").animate({top: "0px"}, { duration: 500})
        if (type == 0)//error
		{
            $(".boxMesage").addClass("boxMesage_error");
			 
			$("#msgPage").html('').html(content);
			
        }
		else if (type == 1)//info
		{
            $(".boxMesage").addClass("boxMesage_info");
			$(document).scrollTo(".boxMesage", { offset: -100 });
			$("#msgPage").html('').html(content);
		}
    }
    else {
        $(".boxMesage").removeClass("boxMesage_error").removeClass("boxMesage_info").animate({top: "-353px"}, { duration: 500, queue: false });
    }
}

 $(".boxMesage")
    .click(function () {
        $(".boxMesage").fadeOut("slow");
        return false;
    });

var avar_active = null;
var temp = null;
var chk = true;
var toutclock = null;

function show_avapanel(panel, ownerid, u_name, u_avatar) {
       if(avar_active){ 
           var cur_active = avar_active ;
           hide_avamenu(document.getElementById(avar_active));
         if(cur_active == panel.id +'_div' ) return
       
       }
       avar_active=panel.id + "_div";
       var obj = document.getElementById(avar_active);
        
          $(obj).fadeIn();
          scrollmenu(avar_active);
          if(obj.innerHTML == '')
            {
               obj.innerHTML = '<img src="/images/upload_progress.gif" width=\"28\" height=\"8\"/>';
                    $.ajax({
                        type: "POST",
                        url: "/farmservice/userservice.asmx/BuildSubMenu",
                        data: "{'ownerid': '" + ownerid + "','u_name':'" + u_name + "','u_avatar':'" + u_avatar + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(msg) { getMenuComplete(msg); }
                    });
            }
         
       
}       

function getMenuComplete(result) {
    document.getElementById(avar_active).innerHTML = result.d;
}

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");
    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) {
    if (avar_active) {
        if(avar_active.split('_')[0]== ava.id)  {
            return 
            }
    }
    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) {
    $.ajax({
        type: "GET",
        url: "/farm/AvatarMenuFunction.aspx",
        contentType: "application/html; charset=utf-8",
        data: "name=" + n + "&status=" + s + "&avatar=" + a + "&ownerid=" + id,
        dataType: "html",
        success: function(msg) {
            $("#PopChangeContent").html(msg);
        }
    });    
}

function ChangeFrienOrFarmilyStatusComplete(result) {
    var bg = document.getElementById('PopChangeContent');
    if (bg) {
        bg.innerHTML = result;
    }
}

function SendRequest(whoAccept) {
    var whoRequest = $("#txturf").val();
    var request = $("#txtRequestMsg").val();
    $("#rdiMoreFam").is(":checked") ? type = 2 : type = 1;
    $.ajax({
        type: "POST",
        data: "{whoRequest:" + whoRequest + ",whoAccept:" + whoAccept + ",type:" + type + ",request:'" + request + "'}",
        contentType: "application/json; charset=utf-8",
        url: "/farmservice/friendservice.asmx/sendRequestToAddFriend",
        dataType: "json",
        success: function(data) {
            data = eval('(' + data.d + ')');
            if (data == 1) {
                contactchange_hide();
                doMessageBox(0, "Đã gửi thành công lời mời kết bạn", true);
            }
            else
                alert("Lỗi trong quá trình gửi lời mời kết bạn");
        }
    });
    
}

function removeSuget(object, friendID) {
    // removeSuggestMakeFriend(int userID,int friendID)
    if (confirm("Bạn chắc chắn muốn từ chối người bạn này ?")) {
        panel = $(object).parent().parent(); //.parents(".FriendMayBeKnowItem");
        userID = $("#txturf").val();
        $(panel).fadeTo(1000, 1).fadeTo(1000, 0.2, function() {
            $.ajax({
                type: "POST",
                data: "{userID:" + userID + ",friendID:" + friendID + "}",
                contentType: "application/json; charset=utf-8",
                url: "/farmservice/SamService.asmx/removeSuggestMakeFriend",
                dataType: "json",
                success: function(data) {
                    data = eval('(' + data.d + ')');
                    if (data == 1) {
                        $(panel).remove();
                        $("#totalFriend").text(parseInt($("#totalFriend").text())-1);
                        init();
                    }
                    else {
                        alert("Bạn không có quyền xóa.");
                        $(panel).fadeTo(1000, 1);
                    }
                }
            })
        })
    }
}

window.onresize = handle;
window.onscroll = handle;
