﻿

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof (window._onload) == 'object') {
        window._onload.add(func);
    }
    else {
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    }
}

function getElementsByClass(searchClass, node, tag) {
    var classElements = new Array();
    if (node == null)
        node = document;
    if (tag == null)
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}


min_width = 0;
max_width = 192;
width_in_time = 30;
in_time = 5000;

//---------------- flash轮换 顶部 -------------------

function indexFlash() {
    if (!document.getElementById("flash_num")) return false;
    var focus_box = document.getElementById("flash_num");
    var lis = focus_box.getElementsByTagName("li");
    var focus_pic = document.getElementById("flash_pic");
    var imgs = focus_pic.getElementsByTagName("img");
    
    for (var i = 0; i < lis.length; i++) {
        lis[i].onmouseover = function() { autokey = true; };
        lis[i].onmouseout = function() { autokey = false; };
        get_firstChild(lis[i]).onclick = function() {
            var getc = getElementsByClass("current", focus_box, "li");
            var beforeID = document.getElementById("c_before");
            if (getc[0].style.width != "192px" || getc[0] == this.parentNode || beforeID != null) { return false; }
            getc[0].setAttribute("id", "c_before");
            widthElement("c_before", min_width, width_in_time);
            removeClass(getc[0], "current");
            addClass(this.parentNode, "current")
            var getc = getElementsByClass("current", focus_box, "li");
            getc[0].setAttribute("id", "c_now");
            widthElement("c_now", max_width, width_in_time);

            theChange(this.parentNode);
        }
        function theChange(obj) {
            for (var i = 0; i < lis.length; i++) {
                if (lis[i] == obj) { imgs[i].style.display = "block"; }
                else { imgs[i].style.display = "none"; }
            }
        }
    }
}

addLoadEvent(indexFlash);


var settime = setInterval('flashAuto()', in_time);
var autokey = false;

function flashAuto() {
    if (autokey) { return false; }

    if (!document.getElementById("flash_num")) return false;
    var focus_box = document.getElementById("flash_num");
    var lis = focus_box.getElementsByTagName("li");
    var focus_pic = document.getElementById("flash_pic");
    var imgs = focus_pic.getElementsByTagName("img");
    li_active();
    var getc = getElementsByClass("current", focus_box, "li");
    theChange(getc[0]);
    function theChange(obj) {
        for (var i = 0; i < lis.length; i++) {
            if (lis[i] == obj) { imgs[i].style.display = "block"; }
            else { imgs[i].style.display = "none"; }
        }
    }
    function li_active() {
        var getc = getElementsByClass("current", focus_box, "li");
        var nowID = document.getElementById("c_now");
        if (getc[0] == get_lastChild(focus_box) && getc[0].style.width == "192px" && nowID == null) {
            removeClass(getc[0], "current");
            addClass(get_firstChild(focus_box), "current")
            get_firstChild(focus_box).style.width = "0px";
            get_firstChild(focus_box).setAttribute("id", "c_now");
            get_lastChild(focus_box).setAttribute("id", "c_before");
            widthElement("c_before", min_width, width_in_time);
            widthElement("c_now", max_width, width_in_time);
            return false;
        }
        if (document.getElementById("c_before") == null) {
            getc[0].setAttribute("id", "c_before");
            widthElement("c_before", min_width, width_in_time);
            removeClass(getc[0], "current");
            addClass(get_nextSibling(getc[0]), "current")
            var getc = getElementsByClass("current", focus_box, "li");
            getc[0].setAttribute("id", "c_now");
            widthElement("c_now", max_width, width_in_time);
        }
    }

}


//---------------- flash轮换 社区焦点 -------------------

function indexFocus() {
    if (!document.getElementById("focus_num")) return false;
    var focus_box = document.getElementById("focus_num");
    var lis = focus_box.getElementsByTagName("li");
    var focus_pic = document.getElementById("focus_pic");
    var imgs = focus_pic.getElementsByTagName("img");
    for (var i = 0; i < lis.length; i++) {
        lis[i].onmouseover = function() { autokey2 = true; };
        lis[i].onmouseout = function() { autokey2 = false; };
        get_firstChild(lis[i]).onclick = function() {
            var getc = getElementsByClass("current", focus_box, "li");
            var beforeID = document.getElementById("c_before");
            if (getc[0].style.width != "192px" || getc[0] == this.parentNode || beforeID != null) { return false; }
            getc[0].setAttribute("id", "c_before");
            widthElement("c_before", min_width, width_in_time);
            removeClass(getc[0], "current");
            addClass(this.parentNode, "current")
            var getc = getElementsByClass("current", focus_box, "li");
            getc[0].setAttribute("id", "c_now");
            widthElement("c_now", max_width, width_in_time);

            theChange(this.parentNode);
        }
        function theChange(obj) {
            for (var i = 0; i < lis.length; i++) {
                if (lis[i] == obj) { imgs[i].style.display = "block"; }
                else { imgs[i].style.display = "none"; }
            }
        }
    }
}

//addLoadEvent(indexFocus);


var settime = setInterval('focusAuto()', in_time);
var autokey2 = false;

function focusAuto() {
    if (autokey2) { return false; }

    if (!document.getElementById("focus_num")) return false;
    var focus_box = document.getElementById("focus_num");
    var lis = focus_box.getElementsByTagName("li");
    var focus_pic = document.getElementById("focus_pic");
    var imgs = focus_pic.getElementsByTagName("img");
    li_active();
    var getc = getElementsByClass("current", focus_box, "li");
    theChange(getc[0]);
    function theChange(obj) {
        for (var i = 0; i < lis.length; i++) {
            if (lis[i] == obj) { imgs[i].style.display = "block"; }
            else { imgs[i].style.display = "none"; }
        }
    }
    function li_active() {
        var getc = getElementsByClass("current", focus_box, "li");
        var nowID = document.getElementById("c_now");
        if (getc[0] == get_lastChild(focus_box) && getc[0].style.width == "192px" && nowID == null) {
            removeClass(getc[0], "current");
            addClass(get_firstChild(focus_box), "current")
            get_firstChild(focus_box).style.width = "0px";
            get_firstChild(focus_box).setAttribute("id", "c_now");
            get_lastChild(focus_box).setAttribute("id", "c_before");
            widthElement("c_before", min_width, width_in_time);
            widthElement("c_now", max_width, width_in_time);
            return false;
        }
        if (document.getElementById("c_before") == null) {
            getc[0].setAttribute("id", "c_before");
            widthElement("c_before", min_width, width_in_time);
            removeClass(getc[0], "current");
            addClass(get_nextSibling(getc[0]), "current")
            var getc = getElementsByClass("current", focus_box, "li");
            getc[0].setAttribute("id", "c_now");
            widthElement("c_now", max_width, width_in_time);
        }
    }

}


function addClass(element, value) {
    if (!element.className) {
        element.className = value;
    } else {
        newClassName = element.className;
        newClassName += " ";
        newClassName += value;
        element.className = newClassName;
    }
}

function removeClass(element, value) {
    var removedClass = element.className;
    var pattern = new RegExp("(^| )" + value + "( |$)");
    removedClass = removedClass.replace(pattern, "$1");
    removedClass = removedClass.replace(/ $/, "");
    element.className = removedClass;
    return true;
}
function get_firstChild(n) {
    var y = n.firstChild;
    while (y != null && y.nodeType != 1) {
        y = y.nextSibling;
    }
    return y;
}

function get_lastChild(n) {
    var y = n.lastChild;
    while (y != null && y.nodeType != 1) {
        y = y.previousSibling;
    }
    return y;
}

function get_nextSibling(n) {
    var y = n.nextSibling;
    while (y != null && y.nodeType != 1) {
        y = y.nextSibling;
    }
    return y;
}

function get_previousSibling(n) {
    var y = n.previousSibling;
    while (y != null && y.nodeType != 1) {
        y = y.previousSibling;
    }
    return y;
}


function widthElement(elementID, final_width, interval) {
    if (!document.getElementById) return false;
    if (!document.getElementById(elementID)) return false;
    var elem = document.getElementById(elementID);
    if (elem.movement) {
        clearTimeout(elem.movement);
    }
    if (!elem.style.width) {
        elem.style.width = "0px";
    }
    var xpos = parseInt(elem.style.width);
    if (xpos == final_width) {
        if (!document.getElementById("c_before")) return false;
        if (!document.getElementById("c_now")) return false;
        var c_before = document.getElementById("c_before");
        c_before.setAttribute("id", "");
        var c_now = document.getElementById("c_now");
        c_now.setAttribute("id", "");
        return true;
    }
    if (xpos < final_width) {
        var dist = Math.ceil((final_width - xpos) / 2);
        xpos = xpos + dist;
    }
    if (xpos > final_width) {
        var dist = Math.ceil((xpos - final_width) / 2);
        xpos = xpos - dist;
    }
    elem.style.width = xpos + "px";
    var repeat = "widthElement('" + elementID + "'," + final_width + "," + interval + ")";
    elem.movement = setTimeout(repeat, interval);
}



//--------------------------------- 热点车型 -----------------------------------

function fnTopList() {
    var obj = document.getElementById("topList");
    var arrName = getElementsByClass("topName", obj, "div");
    var arrParam = getElementsByClass("topParam", obj, "div");
    for (var i = 0; i < arrName.length; i++) {
        arrName[i].index = i;
        arrName[i].onmouseover = function() {

            for (var j = 0; j < arrName.length; j++) {
                arrName[j].style.display = 'block';
                arrParam[j].style.display = 'none';
                arrName[j].style.fontWeight = 'normal';
            }
            this.style.display = 'block';
            this.style.fontWeight = 'bold';
            arrParam[this.index].style.display = 'block';
        }
    }
}

addLoadEvent(fnTopList);





//---------------------------------滑动标签---------------------------------

function initTags(tagid, listid) {
    var Tags = document.getElementById(tagid).getElementsByTagName("li");
    var TagsTb = document.getElementById(listid).getElementsByTagName("table");
    var len = Tags.length;
    var flag = 0; //修改默认值
    for (i = 0; i < len; i++) {
        Tags[i].value = i;
        Tags[i].onclick = function() { changeTags(tagid, listid, this.value) };
        TagsTb[i].style.display = "none";
    }
    Tags[flag].className = "sel";
    TagsTb[flag].style.display = "";
}

function changeTags(tagid, listid, v) {
    var Tags = document.getElementById(tagid).getElementsByTagName("li");
    var TagsTb = document.getElementById(listid).getElementsByTagName("table");
    var len = Tags.length;
    for (var i = 0; i < len; i++) {
        if (v == i) {
            Tags[v].className = "sel";
            TagsTb[v].style.display = "";
        }
        else {
            Tags[i].className = "";
            TagsTb[i].style.display = "none";
        }
    }
}


//---------------------------------滑动标签(头条)---------------------------------
function initTopTags(tagid, listid) {
    var Tags = document.getElementById(tagid).getElementsByTagName("li");
    var TagsTb = document.getElementById(listid).getElementsByTagName("ul");
    var len = Tags.length;
    var flag = 0; //修改默认值
    for (i = 0; i < len; i++) {
        Tags[i].value = i;
        Tags[i].onmouseover = function() { changeTopTags(tagid, listid, this.value) };
        TagsTb[i].style.display = "none";
    }
    Tags[flag].className = "selected";
    TagsTb[flag].style.display = "";
}

function changeTopTags(tagid, listid, v) {
    var Tags = document.getElementById(tagid).getElementsByTagName("li");
    var TagsTb = document.getElementById(listid).getElementsByTagName("ul");
    var len = Tags.length;
    for (var i = 0; i < len; i++) {
        if (v == i) {
            Tags[v].className = "selected";
            TagsTb[v].style.display = "";
        }
        else {
            Tags[i].className = "";
            TagsTb[i].style.display = "none";
        }
    }
}


//--------------------------------------------缓慢下落的广告-------------------------------------------

var intervalId = null;
function slideMove(id, nStayTime, sState, nMaxHth, nMinHth) {
    this.stayTime = nStayTime * 1000 || 3000;
    this.maxHeigth = nMaxHth || 330;
    this.minHeigth = nMinHth || 2.5;
    this.state = sState || "down";
    var obj = document.getElementById(id);
    if (intervalId != null) window.clearInterval(intervalId);
    function openBox() {
        var h = obj.offsetHeight;
        obj.style.height = ((this.state == "down") ? (h + 5) : (h - 5)) + "px";
        if (obj.offsetHeight > this.maxHeigth) {
            window.clearInterval(intervalId);
            intervalId = window.setInterval(closeBox, this.stayTime);
        }
        if (obj.offsetHeight < this.minHeigth) {
            window.clearInterval(intervalId);
            obj.style.display = "none";
        }
    }
    function closeBox() {
        slideMove(id, this.stayTime, "up", nMaxHth, nMinHth);
    }
    intervalId = window.setInterval(openBox, 5);
}

//--------------------------------------------固定广告位图片切换-------------------------------------------
var bStart = 0;
var nows = new Array();
nows[0] = nows[1] = nows[2] = nows[3] = nows[4] = nows[5] = nows[6] = nows[7] = nows[8] = nows[9] = 0;
var maxs = new Array();
var nexts = new Array();
var imgchangeTime = 3000;

function valueMax(n) {
    //alert("n="+n);
    for (var i = 0; i < n; i++) {
        maxs[i] = document.getElementById("box" + i).getElementsByTagName("img").length;
        //alert("maxs[" + i + "]");
    }
//    maxs[0] = document.getElementById("box0").getElementsByTagName("img").length;
//    maxs[1] = document.getElementById("box1").getElementsByTagName("img").length;
//    maxs[2] = document.getElementById("box2").getElementsByTagName("img").length;
//    maxs[3] = document.getElementById("box3").getElementsByTagName("img").length;
//    maxs[4] = document.getElementById("box4").getElementsByTagName("img").length;
//    maxs[5] = document.getElementById("box5").getElementsByTagName("img").length;
//    maxs[6] = document.getElementById("box6").getElementsByTagName("img").length;
//    maxs[7] = document.getElementById("box7").getElementsByTagName("img").length;
//    maxs[8] = document.getElementById("box8").getElementsByTagName("img").length;
//    maxs[9] = document.getElementById("box9").getElementsByTagName("img").length;
}

function changeimg(n) {
    //alert("n=" + n);
    //alert(maxs[0] + "," + maxs[1] + "," + maxs[2] + "," + maxs[3] + "," + maxs[4] + "," + maxs[5] + "," + maxs[6] + "," + maxs[7] + "," + maxs[8]);
    for (var i = 0; i < n; i++) {
        if (maxs[i] == 1) { continue; }
        nexts[i] = nows[i] + 1;
        if (nexts[i] == maxs[i]) {
            nows[i] = maxs[i] - 1;
            nexts[i] = 0;
        }
        //alert("max" + i + ":" + maxs[i] + "/now" + i + ":" + nows[i] + "/next" + i + ":" + nexts[i]);
    }
    if (bStart == 0) {
        bStart = 1;
        setTimeout('changeimg('+n+')', imgchangeTime);
        return;
    }
    else {
        //alert("next4_" + nexts[4] + " now4_" + nows[4] + " max4_" + maxs[4]);
        for (var j = 0; j < n; j++) {
            if (maxs[j] == 1) { continue; }
            document.getElementById("img_" + j + "_" + nexts[j]).style.display = "";
            document.getElementById("img_" + j + "_" + nows[j]).style.display = "none";
            if (nows[j] == maxs[j] - 1) {
                nows[j] = 0;
                //alert("now = max-1 / " + j);
            }
            else {
                nows[j] = nows[j] + 1;
            }
        }
        //alert("now " + nows[0]);
    }
    setTimeout('changeimg('+n+')', imgchangeTime);
}


