﻿var sgtajax = {
    // qstr: query str
    sgtrequest : function(pageurl, qstr)
    {
        new Ajax.Request(pageurl,
            {
                parameters: qstr,
                method : 'get',
                //onComplete : function(response){if(response.responseText == 'ok') { alert(message); } else{ alert(response.responseText); }}
                //onComplete : function(response){ alert(response.responseText); },
                onFailure:sgtajax.errFunc,
                onSuccess:sgtajax.successfun
            }
        );
    },
    errorfun : function(t) {
        alert('发生错误： ' + t.status + ' -- ' + t.statusText);
        //alert('发生错误： ' + t.statusText);
    },
    successfun : function(t) {
        alert(t.responseText);
    },
    sgtupdate : function(objid, dataurl, paras, onSuccessFun)
    {
        var success  = Prototype.emptyFunction;
        if (onSuccessFun)
        {
            success = onSuccessFun;
        }
        try
		{
            new Ajax.Updater(objid, dataurl,
                {
                    asynchronous:true,
                    evalScripts:true,
                    parameters : paras,
                    onSuccess : success
                }
            );
		}
		catch (ex)
		{
		    alert(ex.message);
		}
    },
    savemsgtitle : function(recid, newtitle)
    {
        var anewtitle = encodeURI(newtitle);
        new Ajax.Request('/ajaxphp/ajax.php', { parameters : 'recid='+recid+'&t='+anewtitle, method:'get',onSuccess:function(response) {sgtajax.savemsgtitlecallback(response,recid, newtitle);} } );
    },
    savemsgtitlecallback : function(res,recid, newtitle)
    {
        if (res.responseText == 'ok')
        {
            $('href'+recid).innerText = newtitle;
            cancel(recid);
        }
        else
        {
            alert('保存失败:' + res.responseText);
        }
    },
    getuserfriendlist : function(objid, listid, page)
    {
        try
		{
		    $(objid).innerText = "正在加载数据，请稍候";
            new Ajax.Updater(objid, '/ajaxphp/friends.php?lid='+listid+'&page='+page,
                {
                    asynchronous:true,
                    evalScripts:true,
                    parameters : null
                }
            );
		}
		catch (ex)
		{
		    alert(ex.message);
		}
    },
    getuserphotolist : function(objid, page)
    {
        try
		{
		    $(objid).innerText = "正在加载数据，请稍候";
            new Ajax.Updater(objid, '/ajaxphp/photos.php?page='+page,
                {
                    asynchronous:true,
                    evalScripts:true,
                    parameters : null
                }
            );
		}
		catch (ex)
		{
		    alert(ex.message);
		}
    },
    getuserarticllist : function(objid, page)
    {
        try
		{
		    $(objid).innerText = "正在加载数据，请稍候";
            new Ajax.Updater(objid, '/ajaxphp/articlelist.php?page='+page,
                {
                    asynchronous:true,
                    evalScripts:true,
                    parameters : null
                }
            );
		}
		catch (ex)
		{
		    alert(ex.message);
		}
    },
    getusermusiclist : function(objid, t, page)
    {
        try
		{
		    $(objid).innerText = "正在加载数据，请稍候";
            new Ajax.Updater(objid, '/ajaxphp/music.php?lt='+t+'&page='+page,
                {
                    asynchronous:true,
                    evalScripts:true,
                    parameters : null
                }
            );
		}
		catch (ex)
		{
		    alert(ex.message);
		}
    },
    // t: 1-加为好友，2-成为粉丝
    adduser : function(t, tusername, tnickname)
    {
        sgtajax.sgtrequest('/ajaxphp/useropt.php?t='+t+'&u='+tusername+'&n='+encodeURI(tnickname));
    },
    // 收藏歌曲
    addfavsong : function(songid)
    {
        sgtajax.sgtrequest('/ajaxphp/useropt.php?t=3', 'sid='+songid);
    },
    comment_list : function(p,aid)
    {
		var url = '/ajaxphp/comment_list.php?aid='+aid+'&p='+p;
		var myAjax = new Ajax.Updater(
			{success:'comments_lists'},
			url,
			{asynchronous:true,method:'post', evalscripts:true}
		);
	},
	comComment : function(p)
    {
		var url = '/ajaxphp/comComments.php?p='+p;
		var myAjax = new Ajax.Updater(
			{success:'comentsList'},
			url,
			{asynchronous:true,method:'post', evalscripts:true}
		);
	},
	notesList : function(p)
    {
		var url = '/ajaxphp/notes.php?p='+p;
		var myAjax = new Ajax.Updater(
			{success:'notesList'},
			url,
			{asynchronous:true,method:'post', evalscripts:true}
		);
	},
	// 歌曲投票
    songvote : function(pkid,songid)
    {
        var votecode = $('votecode').value;
        if (votecode.length < 4)
        {
            alert('请输入验证码');
            $('votecode').focus();
            return;
        }
        sgtajax.sgtrequest('/ajaxphp/pksongvote.php?sid='+songid+'&pkid='+pkid+'&code='+votecode);
    },
    // 发表建议
    addsuggest: function(content)
    {
        //alert(content);
    	new Ajax.Request('/ajaxphp/suggestopt.php',
            {
                parameters: 'con='+content,
                method : 'post',
                onFailure:sgtajax.errorfun,
                onSuccess:sgtajax.addsuggestsucc
            }
       );
    },
    addsuggestsucc: function(response)
    {

    	if (response.responseText == 'ok')
    	{
    	    gocompage(1);
    	    $('con').value = '';
    	}
    	else if (response.responseText == 'user')
    	{
    	    alert('请登录留言');
    	    window.location.href='http://www.'+domain+'/login.php?url=http://www.'+domain+'/suggest.php';
    	}
    	else
    	{
    		alert('对不起，留言失败，请检查留言内容是否妥当。');
    	}
    },
    // 获取留言
    getsuggest : function(divid,page)
    {
    	try
		{
		    $(divid).innerHTML = "<div class='listA'>正在加载数据，请稍候</div>";

            new Ajax.Updater(divid, '/ajaxphp/suggest.php?page='+page,
                {
                    asynchronous:true,
                    evalScripts:true,
                    parameters : null
                }
            );
		}
		catch (ex)
		{
		    alert(ex.message);
		}
    },
    // 周主打星推荐自己
    selfrec : function()
    {
        sgtajax.sgtrequest('/ajaxphp/selrec.php');
    },
    // t:4 投票，songid
    votesong : function(songid)
    {
        sgtajax.sgtrequest('/ajaxphp/useropt.php?t=4&sid='+songid);
    },
    getlistnumsong : function(objid, listnum)
    {
        try
		{
		    $(objid).innerText = "正在加载数据，请稍候";
            new Ajax.Updater(objid, '/ajaxphp/yclistsong.php?n='+listnum,
                {
                    asynchronous:true,
                    evalScripts:false,
                    parameters : null
                }
            );
		}
		catch (ex)
		{
		    alert(ex.message);
		}
    },
    // 给选手投票
    voteuser : function(pronum, prouserid)
    {
        var pageurl = '/ajaxphp/useropt.php?t=6&pronum='+pronum+'&uid='+prouserid;
        //sgtajax.sgtrequest(pageurl);
        new Ajax.Request(pageurl,
            {
                parameters: null,
                method : 'get',
                onFailure:sgtajax.errFunc,
                onSuccess: function(t) { sgtajax.voteusercallback(t, prouserid); }
            }
        );
    },
    voteusercallback : function (res, prouserid)
    {
        var rtext = res.responseText;
        if (rtext == -1)
        {
            alert('对不起，您刚刚给ta投过票了，请稍候再来，谢谢。');
        }
        else if (rtext == 0)
        {
            alert('对不起，投票出现错误。');
        }
        else if (rtext == 1)
        {
            alert('投票成功');
            //var num = parseInt($('vote_'+prouserid).innerText) + 1;
            //$('vote_'+prouserid).innerText = num;
        }
        else
        {
            alert(rtext);
        }
    }
};