﻿// JavaScript Document
//申请Sou对象
if (typeof (Sou) == "undefined")
    Sou = {};
//判断document加载方法
(function () {
    var ie = !!(window.attachEvent && !window.opera);
    var wk = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525);
    var fn = [];
    var run = function () { for (var i = 0; i < fn.length; i++) fn[i](); };
    var d = document;
    d.ready = function (f) {
        if (!ie && !wk && d.addEventListener)
            return d.addEventListener('DOMContentLoaded', f, false);
        if (fn.push(f) > 1) return;
        if (ie)
            (function () {
                try { d.documentElement.doScroll('left'); run(); }
                catch (err) { setTimeout(arguments.callee, 0); }
            })();
        else if (wk)
            var t = setInterval(function () {
                if (/^(loaded|complete)$/.test(d.readyState))
                    clearInterval(t), run();
            }, 0);
    };
})();
//ajax
if (typeof (Sou.Ajax) == "undefined")
    Sou.Ajax = {};
Sou.Ajax.lastScript;
Sou.Ajax.h = document.getElementsByTagName("head")[0];
Sou.Ajax.loadScript = function (url) {
    var f = document.createElement("script");
    var d = new Date().getTime();
    f.type = "text/javascript";
    f.id = d;
    f.src = url + '&d=' + d;
    Sou.Ajax.h.appendChild(f);
    if (Sou.Ajax.lastScript && Sou.Ajax.g(Sou.Ajax.lastScript))
        Sou.Ajax.g(Sou.Ajax.lastScript).parentNode.removeChild(Sou.Ajax.g(Sou.Ajax.lastScript));
    Sou.Ajax.lastScript = d;
}
Sou.Ajax.loadScript = function (url, isRemove) {
    var f = document.createElement("script");
    var d = new Date().getTime();
    f.type = "text/javascript";
    f.id = d;
    f.src = url + '&d=' + d;
    Sou.Ajax.h.appendChild(f);
    if (Sou.Ajax.lastScript && Sou.Ajax.g(Sou.Ajax.lastScript) && isRemove)
        Sou.Ajax.g(Sou.Ajax.lastScript).parentNode.removeChild(Sou.Ajax.g(Sou.Ajax.lastScript));
    Sou.Ajax.lastScript = d;
}
Sou.Ajax.g = function (x) {
    return document.getElementById(x)
};
//高级搜索显示层
function showlayout(n) {
    document.getElementById("layout" + n).style.display = "block";
}
function closelayout(n) {
    document.getElementById("layout" + n).style.display = "none";
}
//Autocomplete
if (typeof (Sou.Autocomplate) == "undefined")
    Sou.Autocomplate = {};
Sou.Autocomplate.tipNum = 0;
Sou.Autocomplate.AddAutocomplate = function (obj) {
    var keyword = obj.value;
    var tip = document.getElementById("autocomplateTip");
    var timeOut;
    var isIE = navigator.userAgent.indexOf("MSIE") != -1 && !window.opera;
    function getData(e) {
        e = e || window.event;
        if (e.keyCode != 38 && e.keyCode != 40) {
            tipNum = 0;
            try {
                Sou.Ajax.loadScript("Controls/AutoComplateQuery.ashx?q=" + escape(obj.value));
            } catch (e) {
            }
        }
    }
    function move(e) {
        e = e || window.event;
        if (e.keyCode == 13) {
            if (isIE) {
                e.returnValue = false
            }
            else {
                e.preventDefault()
            }
            document.getElementById("souform").submit();
        }
        if (e.keyCode == 38 || e.keyCode == 40) {
            if (tip.style.display != "none") {
                var max = tip.getElementsByTagName("li").length;
                if (e.keyCode == 38) {
                    tipNum--;
                    if (tipNum <= 0)
                        tipNum = max;
                }
                if (e.keyCode == 40) {
                    tipNum++;
                    if (tipNum > max)
                        tipNum = 1;
                }
                var current = tip.getElementsByTagName("a")[tipNum - 1];
                for (var i = 0; i < max; i++) {
                    tip.getElementsByTagName("a")[i].style.backgroundColor = "";
                }
                current.style.backgroundColor = "#e7f0f9"
                obj.value = current.title;
            }
        }
    }
    obj.onkeyup = getData;
    obj.onkeydown = move;
};
Sou.Autocomplate.summitform = function (keyword) {
    document.getElementById("q").value = keyword;
    document.getElementById("souform").submit();
}
Sou.Autocomplate.bindAutocomplate = function (json) {
    var tip = document.getElementById("autocomplateTip");
    if (json.length > 0) {
        var arrKey = json.split(',');
        var strHtml = "<ul>";
        for (var i = 0; i < arrKey.length; i++) {
            var id = "div" + (i + 1);
            var keyword = document.getElementById("q").value;
            var show = arrKey[i].indexOf(keyword) > -1 ? keyword + "<strong>" + arrKey[i].substring(keyword.length) + "</strong>" : arrKey[i];
            strHtml += "<li><a href=\"javascript:void(0);\" target=\"_self\" onclick=\"Sou.Autocomplate.summitform('" + arrKey[i] + "')\" title=\"" + arrKey[i] + "\">" + show + "</a></li>";
        }
        strHtml += "</ul>";
        tip.innerHTML = strHtml;
        tip.style.display = "block";
    }
    else {
        tip.style.display = "none";
    }
}
//社区高级搜索联想词
Sou.Autocomplate.ClubAutocomplate = function (obj) {
    var keyword = obj.value;
    var tip = document.getElementById("clubAutocomplateTip");
    var timeOut;
    var isIE = navigator.userAgent.indexOf("MSIE") != -1 && !window.opera;
    function getData(e) {
        e = e || window.event;
        if (e.keyCode != 38 && e.keyCode != 40) {
            tipNum = 0;
            try {
                Sou.Ajax.loadScript("Controls/AutoComplateClub.ashx?q=" + escape(obj.value));
            } catch (e) {
            }
        }
    }
    function move(e) {
        e = e || window.event;
        if (e.keyCode == 13) {
            if (isIE) {
                e.returnValue = false
            }
            else {
                e.preventDefault()
            }
            obj.value = current.title;
            tip.style.display = "none";
        }
        if (e.keyCode == 38 || e.keyCode == 40) {
            if (tip.style.display != "none") {
                var max = tip.getElementsByTagName("li").length;
                if (e.keyCode == 38) {
                    tipNum--;
                    if (tipNum <= 0)
                        tipNum = max;
                }
                if (e.keyCode == 40) {
                    tipNum++;
                    if (tipNum > max)
                        tipNum = 1;
                }
                var current = tip.getElementsByTagName("a")[tipNum - 1];
                for (var i = 0; i < max; i++) {
                    tip.getElementsByTagName("a")[i].style.backgroundColor = "";
                }
                current.style.backgroundColor = "#e7f0f9"
                obj.value = current.title;
            }
        }
    }
    obj.onkeyup = getData;
    obj.onkeydown = move;
};
Sou.Autocomplate.myonclick = function (wordname, word, tipname) {
    document.getElementById(wordname).value = word;
    document.getElementById(tipname).style.display = "none";
}
Sou.Autocomplate.bindclubAutocomplate = function (json) {
    var tip = document.getElementById("clubAutocomplateTip");
    if (json.length > 0) {
        var max = tip.getElementsByTagName("li").length;
        var arrKey = json.split(',');
        var strHtml = "<ul>";
        for (var i = 0; i < arrKey.length; i++) {
            var id = "div" + (i + 1);
            var keyword = document.getElementById("q").value;
            var show = arrKey[i].indexOf(keyword) > -1 ? keyword + "<strong>" + arrKey[i].substring(keyword.length) + "</strong>" : arrKey[i];
            strHtml += "<li><a href=\"javascript:void(0);\" target=\"_self\" onclick=\"Sou.Autocomplate.myonclick('tbclub',this.title,'clubAutocomplateTip')\" title=\"" + arrKey[i] + "\">" + show + "</a></li>";
        }
        strHtml += "</ul>";
        tip.innerHTML = strHtml;
        tip.style.display = "block";
    }
    else {
        tip.style.display = "none";
    }
}
//社区用户联想词
Sou.Autocomplate.MemberAutocomplate = function (obj) {
    var keyword = obj.value;
    var tip = document.getElementById("memberAutocomplateTip");
    var timeOut;
    var isIE = navigator.userAgent.indexOf("MSIE") != -1 && !window.opera;
    function getData(e) {
        e = e || window.event;
        if (e.keyCode != 38 && e.keyCode != 40) {
            tipNum = 0;
            try {
                Sou.Ajax.loadScript("Controls/AutoComplateMember.ashx?q=" + escape(obj.value));
            } catch (e) {
            }
        }
    }
    function move(e) {
        e = e || window.event;
        if (e.keyCode == 13) {
            if (isIE) {
                e.returnValue = false
            }
            else {
                e.preventDefault()
            }
            obj.value = current.title;
            tip.style.display = "none";
        }
        if (e.keyCode == 38 || e.keyCode == 40) {
            if (tip.style.display != "none") {
                var max = tip.getElementsByTagName("li").length;
                if (e.keyCode == 38) {
                    tipNum--;
                    if (tipNum <= 0)
                        tipNum = max;
                }
                if (e.keyCode == 40) {
                    tipNum++;
                    if (tipNum > max)
                        tipNum = 1;
                }
                var current = tip.getElementsByTagName("a")[tipNum - 1];
                for (var i = 0; i < max; i++) {
                    tip.getElementsByTagName("a")[i].style.backgroundColor = "";
                }
                current.style.backgroundColor = "#e7f0f9"
                obj.value = current.title;
            }
        }
    }
    obj.onkeyup = getData;
    obj.onkeydown = move;
};
Sou.Autocomplate.bindmemberAutocomplate = function (json) {
    var tip = document.getElementById("memberAutocomplateTip");
    if (json.length > 0) {
        var max = tip.getElementsByTagName("li").length;
        var arrKey = json.split(',');
        var strHtml = "<ul>";
        for (var i = 0; i < arrKey.length; i++) {
            var id = "div" + (i + 1);
            var keyword = document.getElementById("q").value;
            var show = arrKey[i].indexOf(keyword) > -1 ? keyword + "<strong>" + arrKey[i].substring(keyword.length) + "</strong>" : arrKey[i];
            strHtml += "<li><a href=\"javascript:void(0);\" target=\"_self\" onclick=\"Sou.Autocomplate.myonclick('tbmember',this.title,'memberAutocomplateTip')\" title=\""+arrKey[i]+"\">" + show + "</a></li>";
        }
        strHtml += "</ul>";
        tip.innerHTML = strHtml;
        tip.style.display = "block";
    }
    else {
        tip.style.display = "none";
    }
}
Sou.Autocomplate.OnFocusSelect = function (obj) {
    function onfocusselect() {
        obj.select();
    }
    obj.onfocus = onfocusselect;
}
Sou.Autocomplate.OnFocusClear = function (obj, text) {
    function onfocusclear() {
        if (obj.value == text) {
            obj.value = "";
            obj.style.color = "";
        }
    }
    function onblurclear() {
        if (obj.value == "") {
            obj.value = text;
            obj.style.color = "#999999";
        }
    }
    obj.onfocus = onfocusclear;
    obj.onblur = onblurclear;
}
//关闭
window.onload = function () {
    document.onclick = function (e) {
        e = e || window.event;
        var dom = e.srcElement || e.target;

        var objclub = document.getElementById("clubAutocomplateTip");
        var objmember = document.getElementById("memberAutocomplateTip");
        var objword = document.getElementById("autocomplateTip");
        if (objclub != null) {
            if (dom.id != "clubAutocomplateTip" && document.getElementById("clubAutocomplateTip").style.display == "block") {
                document.getElementById("clubAutocomplateTip").style.display = "none";
            }
        }
        if (objmember != null) {
            if (dom.id != "memberAutocomplateTip" && document.getElementById("memberAutocomplateTip").style.display == "block") {
                document.getElementById("memberAutocomplateTip").style.display = "none";
            }
        }
        if (objword != null) {
            if (dom.id != "autocomplateTip" && document.getElementById("autocomplateTip").style.display == "block") {
                document.getElementById("autocomplateTip").style.display = "none";
            }
        }
    };
};
//左侧工具栏
if (typeof (Sou.LeftBar) == "undefined")
    Sou.LeftBar = {};
Sou.LeftBar.init = function () {
    function showLeftButton() {
        document.getElementById("toggle").style.display = "block";
    }
    function hideLeftButton() {
        document.getElementById("toggle").style.display = "none";
    }
    function showLeftBar() {
        var max = 0;
        var left = document.getElementById("mainContainer");
        this.TimeProcess = setInterval(function () {
            if (parseInt(left.style.marginLeft) >= max)
                clearInterval(TimeProcess);
            left.style.marginLeft = (parseInt(left.style.marginLeft) + 10) + "px";
        }, 1);
        document.getElementById("toggleicon").className = "";
    }
    function hideLeftBar() {
        var min = -150;
        var left = document.getElementById("mainContainer");
        this.TimeProcess = setInterval(function () {
            if (parseInt(left.style.marginLeft) <= min)
                clearInterval(TimeProcess);
            left.style.marginLeft = (parseInt(left.style.marginLeft) - 10) + "px";
        }, 1);
        document.getElementById("toggleicon").className = "close";
    }
    function showHideLeftBar() {
        var left = document.getElementById("mainContainer");
        if (parseInt(left.style.marginLeft) >= 0)
            hideLeftBar();
        else {
            showLeftBar();
        }
    }
    var obj = document.getElementById("toggle");
    if (obj != null) {
        obj.onclick = showHideLeftBar;
    }
    var objmain = document.getElementById("main_left");
    if (objmain != null) {
        objmain.onmouseover = showLeftButton;
        objmain.onmouseout = hideLeftButton;
    }
}
//社区帖子回复数
if (typeof (Sou.Club) == "undefined")
    Sou.Club = {};
Sou.Club.Request = function (str) {
    Sou.Ajax.loadScript("Controls/ClubReply.ashx?topicids=" + escape(str));
}
Sou.Club.Response = function (json) {
    for (var i in json) {
        var obj = document.getElementById("replaytip" + i);
        if (obj != null)
            obj.innerHTML = json[i];
    }
}
//文章搜索相关资讯和经销商魔盒
if (typeof (Sou.Dealer) == "undefined")
    Sou.Dealer = {};
Sou.Dealer.writeDealerData = function (data) {
    var obj = document.getElementById("dealerlist");
    if (obj != null)
        obj.innerHTML = data;
}
Sou.Dealer.writeMarketData = function (data) {
    var obj = document.getElementById("marketlist");
    if (obj != null)
        obj.innerHTML = data;
}
//加载完成
document.ready(function () {
    Sou.Autocomplate.AddAutocomplate(document.getElementById("q"));
    var clubtext = document.getElementById("tbclub");
    if (clubtext != null) {
        Sou.Autocomplate.ClubAutocomplate(clubtext);
        Sou.Autocomplate.OnFocusClear(clubtext, "输入论坛名称或论坛id");
    }
    var membertext = document.getElementById("tbmember");
    if (membertext != null) {
        Sou.Autocomplate.MemberAutocomplate(membertext);
        Sou.Autocomplate.OnFocusClear(membertext, "输入发帖人名称或id");
    }
    Sou.LeftBar.init();
});
