var agt = navigator.userAgent.toLowerCase();
var isFF = (agt.indexOf("firefox") != -1);
var jq;
My.loadJs("js/min.js?tim=" + (+new Date), function(){
    jq = jQuery.noConflict();
});
//if(showLogin()){
//	My.loadJs("http://fav.2345.com/my_2345.php?tim="+(+new Date));
//}
function changePage(Url){
   // My.loadJs(Url+"&tim=" + (+new Date));
	//My.loadJs("action.asp?action=getSortList&page=2&tim=" + (+new Date), myfav.initSort);
	setTimeout(function(){
		My.loadJs(Url);
	}, 200);	
	//myfav.clickSort('32')
}
//user
function ifloginf(){
	if(soojs_id==0){parent.User_login(); return false};
/*	if(parent.document.getElementById("log2").style.display=="none")
	{parent.User_login(); return false};*/
	return true;
}
//检察添加收藏
function c_add_fav(){
    var tObj = getObj('vTitle');
    var vLen = tObj.value.cnSize();
    if (vLen < 1 || vLen > 20) {
        alert("标题请在1-20个字节以内..");
        tObj.focus();
        return false;
    }
    var uObj = getObj('vUrl');
    var vLen = uObj.value.cnSize();
    if (vLen < 11) {
        alert("链接请在11个字节以上..");
        uObj.focus();
        return false;
    }
    return true;
}

//关闭对话框
function closeDiv()
{   
	
	if(isFF){location.hash="";}
	parent.initURL=0;
	parent.mySet(true);
}
var fNum = 255; //总字数
//换算中文字长
String.prototype.cnSize = function(){
    var arr = this.match(/[^\x00-\xff]/ig);
    return this.length + (arr == null ? 0 : arr.length);
}
//取出部分文字
String.prototype.sub_cnStr = function(num, mode, vvStr){
    if (!/\d+/.test(num)) 
        return (this);
    var str = this.substr(0, num);
    if (!mode) 
        return str;
    var strNum = str.cnSize();
    if (strNum < num) {
        return this;
    }
    else {
        var n = strNum - str.length;
        num = num - parseInt(n / 2);
        return this.substr(0, num) + vvStr;
    }
}
//判断非法字符
function noStr(vStr){
    if (/[\<\>]/i.vStr) 
        return true;
    else 
        return false;
}

//还原html
function HtmlToStr(vStr){
    vStr = vStr.replace(/&amp;/g, "&");
    vStr = vStr.replace(/&quot;/g, '"');
    vStr = vStr.replace(/&#039;/g, "'");
    vStr = vStr.replace(/&lt;/g, "<");
    vStr = vStr.replace(/&gt;/g, ">");
    return vStr;
}

//还原url
function urlToStr(vStr){
    vStr = vStr.replace(/%3C/g, "\<");
    vStr = vStr.replace(/%3E/g, "\>");
    vStr = vStr.replace(/%27/g, "'");
    vStr = vStr.replace(/%22/g, '"');
    vStr = vStr.replace(/\\/g, '/');
    return vStr;
}


//获取通用对象
function getObj(id){
    if (document.getElementById) {
        return document.getElementById(id);
    }
    else 
        if (document.all) {
            return document.all[id];
        }
        else 
            if (document.layers) {
                return document.layers[id];
            }
}





//复制内容至剪切板
function copyToMemory(vStr, vMsg){
    window.clipboardData.setData("Text", vStr);
    alert(vMsg);
    return true;
}



//检察函数
function c_add_sort(){
    var vSort = getObj('vSort');
    var vLen = vSort.value.cnSize();
    if (vLen < 1 || vLen > 12) {
        alert("分类请在1-12个字节以内..");
        vSort.focus();
        return false;
    }
    return true;
}


//链接扩展命令
function ext_cmd(){
    var cmd = location.href.split("#");
    if (cmd.length > 1) {
        if (cmd[1] == "mf") 
            show_move_fav();
    }
    return true;
}

function wopen(){
    window.open('export.php', '', 'top=50,left=200,height=300, width=450, toolbar =no, menubar=no, scrollbars=auto, resizable=yes, location=no, status=no')
}



myfav = {
    selectedSort: 'all',
    sortList: [],
    favList: [],
    sortTotalNum: 0,
    sortUnknowNum: 0,
    sortCYNum: 0,
    isLoaded: true,
    isSearch: false,
    nowTab: 1,
	nowPage: 1,
    xhr: null,
    $: function(id){
        if (document.getElementById) {
            return document.getElementById(id);
        }
        else 
            if (document.all) {
                return document.all[id];
            }
            else 
                if (document.layers) {
                    return document.layers[id];
                }
    },
    show: function(id){
        myfav.$(id).style.display = 'block';
    },
    hide: function(id){
        myfav.$(id).style.display = 'none';
    },
    loadJs: function(js, callback){
        My.loadJs(js, callback);
    },
    //设置分类列表
    setSortList: function(arr){
        myfav.sortList = arr;
    },
    //设置收藏列表
    setFavList: function(arr){
        myfav.favList = arr;
    },
    //id获取收藏
    getFavById: function(id){
        var fav = myfav.favList;
        if (fav.length == 0) 
            return false;
        for (var i = 0; i < fav.length; i++) {
            if (fav[i][0] == id) {
                return fav[i];
            }
        }
        return false;
    },
    //id获取分类
    getSortById: function(id){
        var sort = myfav.sortList;
        if (sort.length == 0) 
            return false;
        for (var i = 0; i < sort.length; i++) {
            if (sort[i][0] == id) {
                return sort[i];
            }
        }
        return false;
    },
    //显示添加分类
    showAddSort: function(){
		if(!ifloginf()) return false;
        myfav.$('add_sort_font').innerHTML = '添加分类';
        myfav.$('vSort').value = '';
        myfav.$('add_sort_cmd').value = 'add_sort';
        myfav.$('add_sort_id').value = '';
        myfav.showDiv('addNewSort','vSort');
    },
    //显示添加收藏
    showAddFav: function(){
		if(!ifloginf()) return false;
        var selectSort = (isNaN(myfav.selectedSort)) ? 0 : Number(myfav.selectedSort);
        myfav.createSelect('favSort', myfav.sortList, selectSort);
        myfav.$('fav_operation_title').innerHTML = '新增网址';
        myfav.$('vTitle').value = '';
        myfav.$('vUrl').value = 'http://';
        myfav.$('add_fav_cmd').value = 'add_fav';
        myfav.$('add_fav_id').value = '';
        myfav.showDiv('addFav','vTitle');
    },
    //显示编辑收藏
    showEditFav: function(favid){
        var fav = myfav.getFavById(favid);
		
        if (!fav) {
            myfav.msg('没有这个收藏！');
        }
        myfav.createSelect('favSort', myfav.sortList, fav[1]);
        myfav.$('fav_operation_title').innerHTML = '修改网址';
        myfav.$('vTitle').value = fav[3];
        myfav.$('vUrl').value = fav[2];
        myfav.$('add_fav_cmd').value = 'edit_fav';
        myfav.$('add_fav_id').value = fav[1] + '|' + fav[0];
        myfav.showDiv('addFav');
    },
    //显示编辑分类
    showEditSort: function(sortid, sortvalue){
        myfav.$('add_sort_font').innerHTML = '修改分类';
        myfav.$('vSort').value = sortvalue;
        myfav.$('add_sort_cmd').value = 'edit_sort';
        myfav.$('add_sort_id').value = sortid;
        myfav.showDiv('addNewSort');
    },
    //删除分类
    delSort: function(vKey){
        var st = myfav.$('st' + vKey).innerHTML;
        if (st > 0) {
            alert("删除分类前请转移该分类下的 " + st + " 收藏。");
            return false;
        }
        else {
            if (confirm('确认删除分类？')) {
                var delform = myfav.$('delconfirm');
                myfav.$('delconfirm_id').value = vKey;
                myfav.$('delconfirm_cmd').value = 'del_sort';
                delform.submit();
            }
        }
        return false;
        
    },
    //删除收藏
    delFav: function(vKey){
    	
        if (confirm('确认删除这个收藏？')) {
            var delform = myfav.$('delconfirm');
            if (myfav.selectedSort == 'cy') {
            
                myfav.$('delconfirm_id').value = vKey;
                myfav.$('delconfirm_cmd').value = 'del_cy_fav';
            }
            else {
                myfav.$('delconfirm_id').value = vKey;
                myfav.$('delconfirm_cmd').value = 'del_fav';
            }
            
            delform.submit();
        }
        
        return false;
        
    },
    //创建下拉菜单
    createSelect: function(selectId, sArray, selectVal, isMoveFav){
        var sObj = myfav.$(selectId);
        sObj.options.length = 0; //清空列表
        var i = 0;
        if (isMoveFav) {
            sObj.options[i] = new Option('移动到', 0);
            if (selectVal == null || selectVal == 0) 
                sObj.options[i].selected = true;
        }
        else {
            sObj.options[i] = new Option('请选择分类', 0);
            if (selectVal == null || selectVal == 0) 
                sObj.options[i].selected = true;
        }
        i = 1;
        for (key in sArray) {
            sObj.options[i] = new Option(HtmlToStr(sArray[key][1]), sArray[key][0]);
            if (sArray[key][0] == selectVal) 
                sObj.options[i].selected = true;
            i++;
        }
        return true;
    },
    //显示div
    showDiv: function(nDiv){
        var w, h, ow, oh;
        var dbg = myfav.$("divBg");
        w = document.body.clientWidth + 20;
        h = document.body.clientHeight + 20;
        dbg.style.width = w + 'px';
        dbg.style.height = h + 'px';
        dbg.style.display = "block";
        
        var onDiv = myfav.$(nDiv);
        var pw = parseInt(onDiv.style.width.substr(0, (onDiv.style.width.length - 2))); //ff
        if (isNaN(pw)) {
            pw = 0;
        }
        onDiv.style.left = (w - pw) / 2 + "px";
        onDiv.style.top = 10 + "px";
        onDiv.style.display = "block";
        onDiv.disabled = false;
        //		scroll(0,0);
        var s = arguments.length;
        if (s == 2) 
            myfav.$(arguments[1]).focus();
        return false;
    },
    //隐藏div
    hideDiv: function(nDiv){
        var s = arguments.length;
        //不关闭背景
        if (s != 2) 
            myfav.$("divBg").style.display = "none";
        var onDiv = myfav.$(nDiv);
        onDiv.style.display = "none";
        onDiv.disabled = true;
        return true;
    },
    //分类列表初始化
    initSort: function(){
        if (!myfav.sortList) 
            return;
        var list = myfav.sortList;
        var str = '<a href="javascript:;" id="sort_all" name="all" onclick="myfav.clickSort(\'all\')">全部收藏(' + myfav.sortTotalNum + ')</a>';
        str += '<a href="javascript:;" id="sort_unknow" name="unknow" onclick="myfav.clickSort(\'unknow\')">未分类(' + myfav.sortUnknowNum + ')</a>';
        str += '<a href="javascript:;" id="sort_cy" name="cy" onclick="myfav.clickSort(\'cy\')">常用网址(' + myfav.sortCYNum + ')</a>';
        var control_str_0 = '<span class="favs-tl" style="display:none;" id="ct';
        var control_str_1 = '" ><input title="编辑" class="edit01" name="" type="button" value="" onclick="myfav.showEditSort(';
        var control_str_2 = '\')" /><input title="删除" class="del01" name="" type="button" value="" onclick="myfav.delSort(';
        var control_str_3 = ')" /></span>';
        
        for (var i = 0; i < list.length; i++) {
            str += '<a href="javascript:;" id="sort_' +
            list[i][0] +
            '" onclick="myfav.clickSort(\'' +
            list[i][0] +
            '\')" onmouseover="myfav.show(\'ct' +
            list[i][0] +
            '\')" onmouseout="myfav.hide(\'ct' +
            list[i][0] +
            '\')">' +
            control_str_0 +
            list[i][0] +
            control_str_1 +
            list[i][0] +
            ',\'' +
            list[i][1] +
            control_str_2 +
            list[i][0] +
            control_str_3 +
            list[i][1].sub_cnStr(8) +
            '(<span id="st' +
            list[i][0] +
            '">' +
            list[i][2] +
            '</span>)</a>';
        }
		
        myfav.createSelect('favMoveSelect', list, 0, true);
        myfav.html('sortList', str);
        myfav.selectSort(myfav.selectedSort);
    },
    //收藏列表初始化
    initFav: function(){
    	//重置全选
		myfav.$("favSelectAll").checked= false;
        if (!myfav.favList) 
            return;
        var list = myfav.favList;
        var str = '';
        if (list.length < 1) {
            if (myfav.isSearch) {
                str = '<li>没有搜索到符合条件的收藏！</li>'
            }
            else {
                str = '<li>该分类下还没有收藏，请手动添加！</li>'
            }
        }
        else {
            for (var i = 0; i < list.length; i++) {
                var fav = list[i];
                str += '<li id="fav' + fav[0] + '" onmouseover="myfav.favMouseover(' +
                fav[0] +
                ');" onmouseout="myfav.favMouseout(' +
                fav[0] +
                ');" ><i>' +
                fav[4] +
                '</i><input id="check' +
                fav[0] +
                '" onclick="myfav.oncheckFavBox(this);" name="box[]" type="checkbox" value="' +
                fav[1] +
                '|' +
                fav[0] +
                '" /><a href="' +
                fav[2] +
                '" target="_blank">' +
                fav[3].sub_cnStr(30) +
                '</a><span id="ct__' +
                fav[0] +
                '" style="display:none;">';
                if (myfav.selectedSort != 'cy') {
                    str += '<a href="javascript:void(0);" onclick="myfav.showEditFav(\'' + fav[0] + '\')" >修改</a>&nbsp;';
                    str += '<a href="javascript:void(0);" onclick="myfav.delFav(\'' + fav[1] + '|' + fav[0] + '\');">删除</a>&nbsp;';
                    
                }
                else {
                    str += '<a href="javascript:void(0);" onclick="myfav.delFav(\'' + fav[0] + '\');">删除</a>&nbsp;';
                }
                
                if (myfav.selectedSort == 'all' && !myfav.isSearch) {
                    str += '<a href="javascript:void(1);" onclick="myfav.favMoveOrder(\'' + fav[0] + '\',\'up\')">上移</a> <a href="javascript:void(0);" onclick="myfav.favMoveOrder(\'' + fav[0] + '\',\'down\')">下移</a>';
                }
                str += '</span></li>';
            }
        }
        myfav.html('fav_list', str);
        
    },
    favMouseover: function(id){
        myfav.$('fav' + id).className = 'favsa';
        myfav.$('ct__' + id).style.display = '';
    },
    favMouseout: function(id){
        if (myfav.$('check' + id).checked == false) {
            myfav.$('fav' + id).className = '';
        }
        myfav.$('ct__' + id).style.display = 'none';
    },
    favSelectAll: function(fName){
        var ff = myfav.$(fName);
        var sB = myfav.$("favSelectAll");
        var cR = "";
        var liClass = '';
        if (sB.checked == true) {
            liClass = 'favsa';
            cR = true;
        }
        else {
            liClass = '';
            cR = false;
        }
        for (var i = 1; i < ff.elements.length; i++) {
            if (ff.elements[i].type == "checkbox") {
                ff.elements[i].parentNode.className = liClass;
                ff.elements[i].checked = cR;
            }
        }
        return true;
    },
    oncheckFavBox: function(checkb){
        var ff = myfav.$('mmForm');
        var sB = myfav.$("favSelectAll");
        if (checkb.checked == false) {
            sB.checked = false;
        }
        else {
            var num = 0, j = 0;
            for (var i = 1; i < ff.elements.length; i++) {
                if (ff.elements[i].type == "checkbox") {
                    num++;
                    if (ff.elements[i].checked == true) {
                        j++;
                    }
                }
            }
            if (num == j) {
                sB.checked = true;
            }
        }
        
    },
    clickSort: function(id){
    
        myfav.selectSort(id);
    },
    //选择分类的事件处理
    selectSort: function(id){
        if (id == '') 
            return;
        if (myfav.selectedSort == '') 
            myfav.selectedSort = 'all';
        //		alert(myfav.selectedSort);
        if (myfav.$('sort_' + myfav.selectedSort)) 
            myfav.$('sort_' + myfav.selectedSort).className = '';
        myfav.selectedSort = id;
        myfav.$('sort_' + myfav.selectedSort).className = 'feil_acti';
        //		var selectItem = myfav.$(id);
        //		var newItem = document.createElement('b');
        //		var newchild = selectItem.cloneNode(true);
        //		newItem.appendChild(newchild);
        //		selectItem.parentNode.replaceChild(newItem,selectItem);
        //		alert("http://fav.2345.com/my_2345_v2010.php?t="+id+"&tim="+(+new Date));
        myfav.isLoaded = false;
        myfav.isSearch = false;
        myfav.$('favSelectAll').checked = false;
        var getUrl = 'action.asp?action=getTypeList&';
        getUrl += "tim=" + (+new Date);
        if (Number(id)) {
            getUrl += '&id=' + id;
        }
        else 
            if (id == 'cy') {
                getUrl += '&id=cy';
            }
            else 
                if (id == 'unknow') {
                    getUrl += '&id=unknow';
                }
        setTimeout(function(){
            My.loadJs(getUrl);
        }, 200);
		//document.getElementById("aaaa").value=getUrl
    },
    //移动分类
    moveFav: function(vObj){
        if (vObj.value == 0) 
            return false;
        if (myfav.selectedSort == 'cy') {
            alert('常用分类不可移动！');
            return;
        }
        var fn = myfav.getSelectedFavNum("mmForm");
        if (fn <= 0) {
            alert("请选择您要移动的收藏..");
            myfav.createSelect('favMoveSelect', myfav.sortList, 0, true);
            return false;
        }
        var targetSort = myfav.getSortById(vObj.value);
        if (confirm("是否要将这 " + fn + " 个收藏移动到 " + targetSort[1] + " 中？")) {
            var mmform = myfav.$('mmForm');
            myfav.$('msid').value = vObj.value;
            myfav.$('all_cmd').value = 'move_fav';
			myfav.$('msids').value = 'move';
            mmform.submit();
        }
        myfav.$('favMoveSelect').value = 0;
		myfav.$('favMoveSelect').blur();
        return true;
    },
    deleteAll: function(){
        var fn = myfav.getSelectedFavNum("mmForm");
        if (fn <= 0) {
            alert("请选择您要删除的收藏..");
            return false;
        }
        if (myfav.selectedSort == 'cy') {
            alert('常用分类不可批量删除！');
            return;
        }
        //设置删除命令
        if (confirm("是否要将这 " + fn + " 个收藏删除？")) {
            var mmform = myfav.$('mmForm');
            myfav.$('msid').value = '';
            myfav.$('all_cmd').value = 'delall';
            mmform.submit();
        }
        return true;
    },
    switchTab: function(tabid, noreload){
		
        if (tabid == myfav.nowTab) 
            return;
        myfav.$('tab' + tabid).style.display = '';
        myfav.$('tab_li_' + myfav.nowTab).className = '';
        myfav.$('tab_li_' + tabid).className = 'f_acti';
        myfav.$('tab' + myfav.nowTab).style.display = 'none';
        myfav.nowTab = tabid;
		if(tabid == 1 && !noreload){
			 myfav.reloadSort();
		}
    },
    //返回有多少个多选框被选中
    getSelectedFavNum: function(fName){
        var ff = myfav.$(fName);
        var j = 0;
        for (var i = 0; i < ff.elements.length; i++) {
            if (ff.elements[i].type == "checkbox") {
                if (ff.elements[i].checked == true && ff.elements[i].id != "favSelectAll") {
                    j++;
                }
            }
        }
        return j;
    },
    //搜索
    search: function(me){
		if(!ifloginf()) return false;
		myfav.switchTab(1, true);
        var keyword = myfav.$('search_keyword').value;
        var sid = myfav.selectedSort;
        if (keyword == '' || keyword == '请输入标题') 
            return;
        var getUrl = 'action.asp?action=getTypeList';
        if (Number(sid)) {
            getUrl += '&id=' + sid;
        }
		
		keyword = myfav.DBC2SBC(keyword);
		myfav.$('search_keyword').value = keyword;
        getUrl += "&vKey=" + keyword;
        My.loadJs(getUrl);
        me.blur();
    },
    addEvent: function(target, fn){
    
    },
    html: function(id, str){
        (myfav.$(id)).innerHTML = str;
    },
    msg: function(m){
        var msgDiv = myfav.$('show_msg');
        myfav.html('show_msg', m);
        jq("#show_msg").show("slow");
        setTimeout(function(){
            jq("#show_msg").fadeOut("slow");
        }, 2000);
    },
    setPage: function(str){
        myfav.html('thePage', str);
    },
    getData: function(url, callback){
        var _callBack = arguments[1] ||
        function(){
        };
        var _url = url;
        alert(_url);
        if (myfav.xhr == null) 
            myfav.initXhr();
        if (myfav.xhr) {
            myfav.xhr.abort();
            myfav.xhr.onreadystatechange = _callBack;
            myfav.xhr.open("GET", _url, true);
            myfav.xhr.send(null);
        }
    },
    initXhr: function(){
        if (window.XMLHttpRequest) {
            try {
                myfav.xhr = new XMLHttpRequest();
            } 
            catch (e) {
                myfav.xhr = false;
            }
        }
        else 
            if (window.ActiveXObject) {
                try {
                    myfav.xhr = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                    try {
                        myfav.xhr = new ActiveXObject("Microsoft.XMLHTTP");
                    } 
                    catch (e) {
                        myfav.xhr = false;
                    }
                }
            }
    },
    favMoveOrder: function(id, op){
        var favmoveupdown = myfav.$('favmoveupdown');
        myfav.$('favmoveupdown_id').value = id;
        myfav.$('favmoveupdown_op').value = op;
        favmoveupdown.submit();
        return false;
    },
    reloadSort: function(){
        var selected_str = '';
        if (myfav.selectedSort != 'all') {
            selected_str = '&id=' + myfav.selectedSort;
        }
        My.loadJs("action.asp?action=getSortList" + selected_str + "&tim=" + (+new Date), myfav.initSort);
    },
    checkMKNum: function(vObj, fNum, myevent){
        var nowNum = fNum - vObj.value.cnSize();
        if (nowNum < 0) {
            nowNum = 0;
			var event = myevent || window.event;
			var  keycode=event.keyCode;
			if(keycode != 8 && keycode != 13)
            alert("该内容已经达到最大字数限制..\n您的文字将在 " + fNum + " 个字节后被截取..");
			var text = vObj.value.sub_cnStr(fNum);
			vObj.value = text;
            return false;
        }
    },
    searchEnter: function(myevent){
        var event = myevent || window.event;
        if (event.keyCode == 13) {
            var button = document.getElementById("search_button"); //bsubmit 为botton按钮的id 
            button.click();
            return false;
        }
    },
	//全角转半角
    DBC2SBC: function(str){
        var result = "";
        for (var i = 0; i < str.length; i++) {
            code = str.charCodeAt(i);//获取当前字符的unicode编码
            if (code >= 65281 && code <= 65373)//在这个unicode编码范围中的是所有的英文字母已经各种字符
            {
                var d = str.charCodeAt(i) - 65248;
                result += String.fromCharCode(d);//把全角字符的unicode编码转换为对应半角字符的unicode码
            }
            else 
                if (code == 12288)//空格
                {
                    var d = str.charCodeAt(i) - 12288 + 32;
                    result += String.fromCharCode(d);
                }
                else {
                    result += str.charAt(i);
                }
        }
        return result;
    }
}
if (typeof(HTMLElement) != "undefined") //给firefox定义contains()方法，ie下不起作用
{
    HTMLElement.prototype.contains = function(obj){
        while (obj != null && typeof(obj.tagName) != "undefind") {
            if (obj == this) 
                return true;
            obj = obj.parentNode;
        }
        return false;
    };
}

My.loadJs("action.asp?action=getSortList&tim=" + (+new Date), myfav.initSort);
//chkLogin2();
//parent.allCount('mysiteweb3');
myfav.$('favSelectAll').focus();
