﻿var t,h,w;
var loginuserid   = '';
var loginusername = '';
$(document).ready(function(){

    t = (document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop);
    h = (document.body.clientHeight > document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight;
    w = $('body').width();
    
    // 加载登录状态
    $.ajax({
        url: 'login.aspx?action=getstate',
        type: 'POST',
        dataType: 'json',
        timeout: 100000,
        success: function(dat){
            if(dat.userid * 1 <= 0){
                $('#login').css('background','').html('<div style="padding:10px 0 5px 0;">已经有了帐号？ <a id="logined" href="javascript:;">请登录</a></div><div>或者 <a id="register" href="javascript:;">免费注册</a></div>');
                if (location.href.indexOf('myblog.aspx') > 0)
                    setWindow('用户登录', 400, 300, 'login.aspx?action=login', true);
            }else{
                $('#login').css('background','#FFFFFF');
                var _html = '<div style=" padding-right:5px;">';
                _html += '<div class="loginavg"><img src="' + (dat.avatar == '' ? 'images/default.gif' : dat.avatar) + '" /></div>';
                _html += '<div class="loginico0"><a href="user-' + dat.userid + '.html">别人眼中我的花园</a></div>';
                _html += '<div class="loginico1"><a href="myblog.aspx">控制面板</a> <a href="myblog.aspx?a1=f">收藏夹</a></div>';
                _html += '<div class="loginico2"><a href="myblog.aspx?a1=m">短消息<b' + (dat.message != '0' ? ' class="newmsg"' :'') + '>(' + dat.message + ')</b></a> &nbsp; <a id="loginout" href="javascript:;">退出</a></div>';
                _html += '<div class="loginico3"><a id="loginusername" href="myblog.aspx?a1=i" class="loginokuser">资料修改</a> &nbsp; ';
                _html += '</div>';
                _html += '</div>';
                $('#login').html(_html);
                $('#loginout').click(function(){ loginOut(); });
                $('#uid').val(dat.userid);
                $('#uname').val(dat.username);
                loginuserid = dat.userid;
                loginusername = dat.username;
            }
            // 加载登录框
            $('#logined').click(function(){
                setWindow('用户登录', 400, 300, 'login.aspx?action=login');
            });
            // 加载注册框
            $('#register').click(function(){
                setWindow('注册新用户', 400, 350, 'login.aspx?action=register');
            });
        },
        error:function(){
            //
        }
    });
    
    
//    $('#commentbutton').click(function(){
//        if($('#commentcontent').val() == ''){
//            alert('请先输入评论内容！');
//            return;
//        }
//        if($('#commentcontent').val().length > 200){
//            alert('评论内容不能多于200字！');
//            return;
//        }
//    });


});

// 退出登录
function loginOut(){
    $.ajax({
        url: 'login.aspx?action=loginout',
        type: 'POST',
        dataType: 'text',
        timeout: 100000,
        success: function(dat){
            $('#login').html('<div style="padding-top:5px;">已经有了帐号？ <a id="logined" href="javascript:;">请登录</a></div><div>或者 <a id="register" href="javascript:;">免费注册</a></div>');
            // 加载登录框
            $('#logined').click(function(){
                setWindow('用户登录', 400, 300, 'login.aspx?action=login');
            });
            // 加载注册框
            $('#register').click(function(){
                setWindow('注册新用户', 400, 350, 'login.aspx?action=register');
            });
            if (location.href.indexOf('myblog.aspx') > 0)
                setWindow('用户登录', 400, 300, 'login.aspx?action=login', true);
        },
        error:function(){
            //
        }
    });
}

// 弹出模拟窗口
function setWindow(_title, _w, _h, _url, noclose){
	if(noclose == 'undefined') noclose = true;
    var _border = 5;
    $('body').css('overflow', 'hidden');
    $('html').css('overflow', 'hidden');
    $('<div id="winbg" style="width:' + $('body').width() + 'px;height:' + $('body').height() + 'px;">&nbsp;</div>').prependTo("body");
    $('<div id="popupbox"></div>').prependTo("body");
    $("#popupbox").css({'top': ((((h - _h - _border * 2) / 2) < 0 ? 0 : ((h - _h - _border * 2) / 2)) + getTop())+ 'px', 'left':((w - _w - _border * 2) / 2) + 'px', 'width': _w + 'px','height':_h + 'px'});
    var closediv = '<div class="closediv">' + (noclose ? '' : '<a href="javascript:closeWindows();">关闭</a>') + '<b id="popuptitle">' + _title + '：</b></div>';
    $("#popupbox").html(closediv + '<div class="iframedic"><iframe id="subiframe" src="" border="0" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" width="100%" height="' + (_h - (40 + _border * 2)) + '"></iframe></div>').show();
    $('#subiframe').attr('src', _url);
}


// 刷新本页
function reLoad(){
    location.href = location.href;
}

// 重设窗口大小
function reWindowSize(_title, _w, _h){
    $('#popuptitle').html(_title);
    $("#popupbox").css({'top': (((h - _h - 4) / 2) < 0 ? 0 : ((h - _h - 4) / 2)) + 'px', 'left':((w - _w - 4) / 2) + 'px', 'width': _w + 'px','height':_h + 'px'});
    $('#subiframe').attr('height', _h - 44);    
}

// 关闭模拟窗口
function closeWindows()
{
    $('#popupbox').remove();
    $('#winbg').remove();
    $('body').css('overflow', 'auto');
    $('html').css('overflow', 'auto');
} 

function getTop()    {return (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop);}
function getHeight() {return ((document.body.clientHeight > document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight);}
function getWidth()  {return ((document.documentElement.clientWidth == 0)?document.body.clientWidth:document.documentElement.clientWidth);}


function addFavorites(id, table){
    if(loginuserid == ''){
        alert('只有登录用户可以使用此功能！');
        return;
    }else {
        $.ajax({
            url: 'login.aspx?action=favorites&tid=' + id + '&table=' + table,
            type: 'GET',
            dataType: 'text',
            timeout: 100000,
            success: function(dat){
                if(dat == 't')
                    alert('您已经收藏过这个花园！');
                else if(dat == '2')
                    alert("收藏成功！");
            },
            error:function(){
                //
            }
        }); 
    }
}
function addFriend(){
    if(loginusername == $('#thisusername').val()){
        alert("不能加自己为好友！");
        return;
    }
    if(loginusername == ''){
        alert("请先登录才能使用这个功能！");
        return;
    }
    $.ajax({
        url: 'user.aspx?action=addfriend&username=' + $('#thisusername').val(),
        type: 'GET',
        dataType: 'text',
        timeout: 100000,
        success: function(dat){
            if(dat == "l")
                alert("您还没有登录！");
            else if(dat == 'y')
                alert('该用户已经是您的好友了！');
            else if(dat == 't')
                alert("好友添加成功！");
            else
                alert("好友添加失败！");
        },
        error:function(){
            alert("服务器返回错误！");
        }
    }); 
}
function sendMessage(){

}
