﻿window.onload = function() {
    var $ = function(oid) {
        if (typeof (oid) == "string")
            return document.getElementById(oid);
        return oid;
    }
    //      var ps = new ScrollPanel("paks", "city_panel", "copy_list", "previous", "next", 432);
    //      ps.setAutoScroll(3);

    var ad = new ScrollPanel("ad_scroll", "listbox", "copy_box", "ad_prev", "ad_next", 478);
    var subnode = $("actor").childNodes;

    var adnum = $("ad_num");
    var count = 0;

    var ad_numlist = new Array();
    for (var i = 0; i < subnode.length; i++) {

        if (subnode[i].nodeType != "1")
            continue;
        count++;
        var node = document.createElement("div");
        node.id = "ad_scroll_" + count;
        ad_numlist[count - 1] = node.id;
        if (count == 1) {
            node.className = "cur";
            node.style.marginLeft = "0px";
        }
        node.value = count + "";
        node.innerHTML = "&nbsp;";
        node.onclick = function() {
           
            ad.adjustSync(this.value);
        }
        adnum.appendChild(node);
    }

    ad.setNumList(ad_numlist);
    ad.setAutoScroll(2);
    var numlist = $("numlist");
    var length = 13 + 13 + 17 * count; //计算数字列表的总宽度
    length = Math.ceil((485 - length) / 2); //数字列表处于中间
    numlist.style.left = length + "px";
    var cs = function() { ad.cancel = true; ad.clearAutoScroll(); }
    var as = function() { ad.cancel = false; ad.setAutoScroll(); }
    if (window.addEventListener) {
        numlist.addEventListener("mouseover", cs, false);
        numlist.addEventListener("mouseout", as, false);
    }
    else {
        numlist.attachEvent("onmouseover", cs);
        numlist.attachEvent("onmouseout", as);
    }
}

//改写原型，改写后的方法只适应于首页的顶部滚动，对其他滚动不适应
ScrollPanel.prototype.rightScroll = function() {
   
    if (this.lock)
        return;

    this.isPositiveDirection = true; //座标轴负方向
    this.adjustSync();
    this.distance = this.pageLength;
    this.isVertical = false;
    this.lock = true;
    this.clearAutoScroll();
    this.scrollData();
}
ScrollPanel.prototype.leftScroll = function() {
    if (this.lock)
        return;
    this.isPositiveDirection = false; //座标轴负方向
    this.adjustSync();
    this.distance = -this.pageLength;
    this.isVertical = false;
    this.lock = true;
    this.clearAutoScroll();
    this.scrollData();
}
ScrollPanel.prototype.adjustSync = function() {

    if (this.lock)
        return;
    if (this.boxID != "ad_scroll")
        return;
    if (this.numlist == null || this.numlist.length == 0) {
        alert("has no any picture!");
        return;
    }
    var id = "";
    if (arguments.length == 1) {//click event.
        id = arguments[0];

        this.$(this.boxID).scrollLeft = (parseInt(id) - 1) * this.pageLength;
       
    }
    else {
        var curid = 0;
        if (this.isPositiveDirection) {
            curid = Math.ceil(this.$(this.boxID).scrollLeft / this.pageLength) % this.numlist.length + 1;
            curid = (curid + 1) % this.numlist.length;
        }
        else
            curid = Math.ceil(this.$(this.boxID).scrollLeft / this.pageLength) % this.numlist.length;
        if (curid == 0)
            curid = this.numlist.length;
        id = curid + "";
    }
    for (var i = 0; i < this.numlist.length; i++) {
        if ("ad_scroll_" + id == this.numlist[i])
            this.$(this.numlist[i]).className = "cur";
        else
            this.$(this.numlist[i]).className = "";
    }
}
ScrollPanel.prototype.setNumList = function(numlist) {
    this.numlist = numlist;
}
