﻿var width;  //宽
var height;  //高
var pictureCount; //图片数量
var picPath;
var href; //链接地址
var time; //5秒
var blockCount; //分10块移动
var defer; //块延迟基数
var order; // 0(从上到下) 1(从下到上) 2(随机顺序) 3(随机出现012)
var now; //默认显示第一个
var blockHeight;
var remain;
var direct;
var list;
var auto;
function InitFlashBox(configObj) {
    InitConfig(configObj);
    document.write("<div class=\"scroller\" id=\"scroller\" style=\"width:" + width + "px\">");
    document.write("<div class=\"flat\"><a style=\"width:" + width + "px;height:" + height + "px;\" target=\"_blank\" id=\"flat\" href=\"" + href[now] + "\"></a></div>");
    document.write("<div class=\"guide\" id=\"guide\" style=\"margin-top:" + (height - 23) + "px;margin-left:" + (width - 125) + "px\">");
    for (var i = 0; i < pictureCount; i++) document.write("<p onclick=\"changeItem(" + i + ")\">" + parseInt(i + 1) + "</p>");
    document.write("</div>");
    for (var i = 0; i < blockCount; i++) {
        document.write("<div class=\"block\" id=\"item" + i + "\" style=\"height:" + blockHeight + "px\">");
        for (var j = 0; j < pictureCount; j++) {
            document.write("<a href=\"" + href[i] + "\" target=\"_blank\" style=\"background:url(" + picPath[j] + ");height:" + height + "px; width:"+width+"px\"></a>");
        }
        document.write("</div>");
        if (i > 0) Cus$("item" + i).scrollTop = i * blockHeight;
    }
    document.write("</div>");
    for (var i = 0; i < blockCount; i++) list[i] = i;
    auto = setInterval("changeItem()", time);
    changeStyle(now, true);
    if (order > 0) sortList(order);
    Cus$("flat").onmouseover = function() { clearInterval(auto) }
    Cus$("flat").onmouseout = function() { auto = setInterval("changeItem()", time); }
    Cus$("guide").onmouseover = function() { clearInterval(auto) }
}
function InitConfig(configObj) {
    if (configObj != undefined) {
        width = configObj.width == undefined ? 289 : configObj.width;
        height = configObj.width == undefined ? 222 : configObj.height;
        pictureCount = configObj.pictureCount == undefined ? 0 : configObj.pictureCount;
        picPath = configObj.picPath == undefined ? [] : configObj.picPath;
        href = configObj.href == undefined ? [] : configObj.href;
        time = configObj.time == undefined ? 5000 : configObj.time;
        blockCount = 1;
        defer = 50;
        order = configObj.order == undefined ? 3 : configObj.order;
        now = 0;
        if (blockCount <= 0) {
            blockCount = 1;
        }
        blockHeight = Math.floor(height / blockCount);
        remain = new Array(pictureCount);
        list = [];
    }
}
Array.prototype.chaos = function() {
    var randomNumber, temp;
    for (var i = 0; i < this.length; i++) {
        randomNumber = getRandom(0, this.length - 1);
        temp = this[i];
        this[i] = this[randomNumber];
        this[randomNumber] = temp;
    }
}
Array.prototype.total = function() {
    var count = 0;
    for (var i = 0; i < this.length; i++) {
        count += this[i];
    }
    return count;
}
function getRandom(start, end) {
    var number = parseInt(Math.random() * (end - start + 1));
    if (start > 0) number++;
    return number;
}
function sortList(o) {
    switch (o) {
        case 0:
            list.sort();
            break;
        case 1:
            list.sort();
            list.reverse();
            break;
        case 2:
            list.chaos();
            break;
        case 3:
            sortList(getRandom(0, 2));
            break;
    }
}
function Cus$(element) { return document.getElementById(element) }
function Cus$$(parent, element) { return parent.getElementsByTagName(element) }
function offset(i) {
    var step = Math.ceil(remain[i] / 10);
    if (step > 0) {
        if (direct) {
            Cus$("item" + i).scrollTop += step;
        } else {
            Cus$("item" + i).scrollTop -= step;
        }
        remain[i] -= step;
        setTimeout("offset(" + i + ")", 10);
    }
}
function changeStyle(index, type) {
    Cus$$(Cus$("guide"), "p")[index].className = type ? "now" : "";
}
function changeItem(index) {
    if (remain.total() > 0) return;
    changeStyle(now, false);
    if (index == undefined) {
        if (++now >= pictureCount) now = 0;
    } else {
        if (index == now) {
            changeStyle(now, true);
            return false;
        } else {
            now = index;
        }
    }
    changeStyle(now, true);
    Cus$("flat").href = href[now];
    var offsetSize = now * height - Cus$("item0").scrollTop;
    direct = offsetSize > 0 ? 1 : 0;
    if (order > 1) sortList();
    for (var i = 0; i < blockCount; i++) {
        remain[list[i]] = Math.abs(offsetSize);
        setTimeout("offset(" + list[i] + ")", i * defer);
    }
}