//-------------------------------------------------------------
// Functions for library added
//-------------------------------------------------------------
function library(iCid, iGid, iType)
{
    // iCid: 内容 ID
    // iGid: 组 ID
    // iType: 1是知识，2是智囊
    var hContents = {
        url     : 'http://www.studentsspace.net/m/?m=library&t=dialog&act=add_lib',
        options : {method: 'get'}
    };
    var hParams = {
        width       : 350,
        className   : 'alphacube',
        okLabel     : '确定',
        cancelLabel : '取消',
        onOk: function() {
            infoAddLibrary(iCid, iGid, iType);
            return true;
        }   //end function
    };
    Dialog.confirm(hContents, hParams);
}   //end function
function infoAddLibrary(iCid, iGid, iType)
{
    var iLCid = document.getElementById('selectLibCate').value;
    var sContent = '<div id="windowOpen" style="width:200px;padding-top:20px;' + (!document.all ? 'padding-left:25px;' : '') + 'text-align:center"><img src="http://www.studentsspace.net/m/ui/images/icon/icon_loadeditor.gif" /></div>';
    var hParams = {
        width       : 250,
        height      : 100,
        className   : 'alphacube',
        showProgress: true
    };
    Dialog.info(sContent, hParams);
    dealAddLibrary(iLCid, iCid, iGid, iType);
}   //end function
function dealAddLibrary(iLCid, iCid, iGid, iType)
{
    var sUrl = 'http://www.studentsspace.net/m/?m=service&t=ajax_library&act=add_to_library&lcid=${lcid}&cid=${cid}&gid=${gid}&type=${type}';
    var xmlHttp = XmlHttp.create();
    var loaded = 0;
    xmlHttp.open('GET', sUrl.replace('${lcid}', iLCid).replace('${cid}', iCid).replace('${gid}', iGid).replace('${type}', iType), true);
    xmlHttp.onreadystatechange = function() {
        if (4 == xmlHttp.readyState) {
            if (0 == loaded) {
                var sText, sReturn = _getTrimStr(xmlHttp.responseText);
                if ('OKY' == sReturn.substr(0, 3)) {
                    //TODO...
                } else if ('EXIST' == sReturn.substr(0, 5)) {
                    //TODO...
                } else {
                    //TODO...
                }   //end if
                sUrl = 'http://www.studentsspace.net/m/?m=library&id=${id}';
                document.location.href = sUrl.replace('${id}', iLCid);
                //Dialog.closeInfo();
            }   //end if
        }   //end if
    }   //end if
    xmlHttp.send(null);
    return void(0);
}   //end function
//-------------------------------------------------------------
// Functions for library moved
//-------------------------------------------------------------
function moveLibrary(iLid, iOLCid)
{
    if (0 == iLid) {
        iSelectedNumber = _getCheckNumber();
        if (iSelectedNumber <= 0) {
            _alert('至少选择一条记录\n');//至少选择一条记录
            return;
        }   //end if
    }   //end if
    var hContents = {
        url     : ('http://www.studentsspace.net/m/?m=library&t=dialog&act=cp_lib&olcid=${olcid}').replace('${olcid}', iOLCid),
        options : {method: 'get'}
    };
    var hParams = {
        width       : 350,
        className   : 'alphacube',
        okLabel     : '移动',
        cancelLabel : '复制',
        onOk: function() {
            infoMoveLibrary('mv', iLid);
            return true;
        },   //end function
        onCancel: function() {
            infoMoveLibrary('cp', iLid);
            return true;
        }   //end function
    };
    Dialog.confirm(hContents, hParams);
}   //end function
function infoMoveLibrary(sCmd, iLid)
{
    var iLCid = document.getElementById('selectLibCate').value;
    var sContent = '<div id="windowOpen" style="width:200px;padding-top:20px;' + (!document.all ? 'padding-left:25px;' : '') + 'text-align:center"><img src="http://www.studentsspace.net/m/ui/images/icon/icon_loadeditor.gif" /></div>';
    var hParams = {
        width       : 250,
        height      : 100,
        className   : 'alphacube',
        showProgress: true
    };
    Dialog.info(sContent, hParams);
    dealMoveLibrary(sCmd, iLCid, iLid);
}   //end function
function dealMoveLibrary(sCmd, iLCid, iLid)
{
    var bIsMulti = (0 == parseInt(iLid));
    var cEl = document.frmLibrary.elements;
    var aLids = [], sLids = '0';
    if (bIsMulti) {
        for (var i = 0; i < cEl.length; i++) {
            if ('checkbox' == cEl[i].type && 'library_ids[]' == cEl[i].name)
                aLids.push(cEl[i].value);
        }   //end for
        sLids = aLids.join('|');
    }   //end if
    var sUrl = 'http://www.studentsspace.net/m/?m=service&t=ajax_library&act=move_to&cmd=${cmd}&lcid=${lcid}&lid=${lid}&lids=${lids}';
    var xmlHttp = XmlHttp.create();
    var loaded = 0;
    xmlHttp.open('GET', sUrl.replace('${cmd}', sCmd).replace('${lcid}', iLCid).replace('${lid}', iLid).replace('${lids}', sLids), true);
    xmlHttp.onreadystatechange = function() {
        if (4 == xmlHttp.readyState) {
            if (0 == loaded) {
                var sText, sReturn = _getTrimStr(xmlHttp.responseText);
                if ('OKY' == sReturn.substr(0, 3)) {
                    //TODO...
                } else if ('EXIST' == sReturn.substr(0, 5)) {
                    //TODO...
                } else {
                    //TODO...
                }   //end if
                sUrl = 'http://www.studentsspace.net/m/?m=library&id=${id}';
                document.location.href = sUrl.replace('${id}', iLCid);
                //Dialog.closeInfo();
            }   //end if
        }   //end if
    }   //end if
    xmlHttp.send(null);
    return void(0);
}   //end function
