/*
*child:<div id='divGlide'><a href="javascript:;"><img alt="img" src="imgsrc" width="width" height="height" title="title" /></a></div>
*Glide.init(auto, slider, width, height, second, speed, point);
*@param auto type:bolean 自动滑动
*@param slider type:object 容器对象
*@param width type:number 容器宽度
*@param height type:number 容器高度
*@param second type:number 滑动延时
*@param speed type:float 滑动速率 取值在0.05-1之间 当取值是1时 无效果
*@param point type:string left or top
*/
function cleanWhiteSpace(node) { var notWhitespace = /\S/; for (var x = 0; x < node.childNodes.length; x++) { var childNode = node.childNodes[x]; if ((childNode.nodeType == 3) && (!notWhitespace.test(childNode.nodeValue))) { node.removeChild(node.childNodes[x]); x--; } if (childNode.nodeType == 1) { cleanWhiteSpace(childNode); } } }
var Glide = new function() {
    function $id(id) { return document.getElementById(id); };
    this.className = function(slider, num, width, height, point, hideNum) {
        var style = '<style type=text/css>';
        style += '.' + slider + '_picBox{width:' + width + 'px;height:' + height + 'px;margin:0 auto;position:relative;overflow:hidden;font-size:9pt;font-family:arial,simsun;color:#333;}';
        style += '.' + slider + '_picBox .' + slider + '_show_pic{position:absolute;margin:0;padding:0;list-style:none;height:' + height + 'px;width:' + (point == 'left' ? (width * num) : width) + 'px;}';
        style += '.' + slider + '_picBox .' + slider + '_show_pic li{float:left;margin:0;padding:0;border:0;height:' + height + 'px;}';
        style += '.' + slider + '_picBox .' + slider + '_show_pic li img{display:block;border:0;}';
        style += '.' + slider + '_picBox .' + slider + '_func_box{' + (hideNum == true ? 'display:none;' : '') + 'position:absolute;bottom:0;left:0;width:' + width + 'px;padding:0;margin:0;}';
        style += '.' + slider + '_picBox .' + slider + '_func_box li.lititle{position:relative;height:15px;border:0;color:#FFFFFF;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;}';
        style += '.' + slider + '_picBox .' + slider + '_func_box li.libanner{display:block;position:absolute;width:' + width + 'px;height:15px;padding:0;margin:0;border:0;background:#000000;filter:alpha(opacity=30);-moz-opacity:0.3;opacity: 0.3;color:#FFFFFF;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;}';
        style += '.' + slider + '_picBox .' + slider + '_icon_num{' + (hideNum == true ? 'display:none;' : '') + 'position:absolute;bottom:0;right:0;padding:0;margin:0;}';
        style += '.' + slider + '_picBox .' + slider + '_icon_num li{float:left;width:20px;height:15px;padding:0;margin:0;border:0;color:#FFFFFF;list-style:none;text-align:center;cursor:pointer;}';
        style += '.' + slider + '_picBox .' + slider + '_icon_num li.liactive{background:#CE0609;color:#F0F0F0;}';
        style += '</style>';
        document.write(style);
    };
    this.layerGlide = function(auto, oEventCont, oSlider, oBarlival, sSingleSize, second, fSpeed, point) {
        var oSubLi = $id(oEventCont).getElementsByTagName('li');
        var oPicLi = $id(oSlider).getElementsByTagName('img');
        var subliwidth = $id(oSlider).offsetWidth;
        var interval, timeout, oslideRange;
        var time = 1;
        var speed = fSpeed;
        var sum = oSubLi.length;
        var a = 0;
        var delay = second * 1000;
        var apply = function(s, isfloor) {
            return function() {
                oslideRange = Math.abs(parseInt($id(oSlider).style[point]));
                if (isfloor) $id(oSlider).style[point] = -Math.floor(oslideRange + (parseInt(s * sSingleSize) - oslideRange) * speed) + 'px';
                else $id(oSlider).style[point] = -Math.ceil(oslideRange + (parseInt(s * sSingleSize) - oslideRange) * speed) + 'px';
                if (oslideRange == [(sSingleSize * s)]) {
                    clearInterval(interval);
                    a = s;
                }
            }
        };
        for (var i = 0; i < sum; i++) {
            oSubLi[i].onmouseover = (function(i) {
                return function() {
                    for (var c = 0; c < sum; c++) oSubLi[c].className = '';
                    clearTimeout(timeout);
                    clearInterval(interval);
                    oSubLi[i].className = 'liactive';
                    oSubLi[i].title = oPicLi[i].title;
                    oBarlival.innerHTML = oPicLi[i].title;
                    if (Math.abs(parseInt($id(oSlider).style[point])) > [(sSingleSize * i)]) {
                        interval = setInterval(apply(i, true), time);
                        this.onmouseout = function() { if (auto) timeout = setTimeout(autoGlide, delay); };
                    } else if (Math.abs(parseInt($id(oSlider).style[point])) < [(sSingleSize * i)]) {
                        interval = setInterval(apply(i), time);
                        this.onmouseout = function() { if (auto) timeout = setTimeout(autoGlide, delay); };
                    }
                }
            })(i);
        }
        function autoGlide() {
            clearTimeout(timeout);
            clearInterval(interval);
            for (var c = 0; c < sum; c++) oSubLi[c].className = '';
            if (a == parseInt(sum)) {
                a = 0;
                $id(oSlider).removeChild($id(oSlider).lastChild);
                $id(oSlider).style[point] = a;
                if (point == 'left') $id(oSlider).style.width = subliwidth + 'px';
            } else if (a == (parseInt(sum) - 1)) {
                $id(oSlider).appendChild($id(oSlider).firstChild.cloneNode(true));
                if (point == 'left') $id(oSlider).style.width = (subliwidth + sSingleSize) + 'px';
            }
            a++;
            interval = setInterval(apply(a), time);
            timeout = setTimeout(autoGlide, delay);
            oSubLi[a % sum].className = 'liactive';
            oBarlival.innerHTML = oPicLi[a % sum].title;
        }
        if (auto) timeout = setTimeout(autoGlide, delay);
    };
    this.init = function(auto, slider, width, height, second, speed, point, hideNum, hideTitle) {
        cleanWhiteSpace($id(slider));
        var length = $id(slider).childNodes.length;
        if (length == 0) return;
        var show_pic = document.createElement('ul');
        var func_box = document.createElement('ul');
        var func_vli = document.createElement('li');
        var func_ban = document.createElement('li');
        var icon_num = document.createElement('ul');
        show_pic.className = slider + '_show_pic';
        func_box.className = slider + '_func_box';
        func_vli.className = 'lititle';
        func_ban.className = 'libanner';
        icon_num.className = slider + '_icon_num';
        show_pic.id = slider + '_show_pic';
        icon_num.id = slider + '_icon_num';
        func_box.appendChild(func_ban);
        func_box.appendChild(func_vli);
        eval('show_pic.style.' + point + '=0;');
        $id(slider).className = slider + '_picBox';
        for (var i = 0; i < length; i++) {
            var picli = document.createElement('li');
            var numli = document.createElement('li');
            picli.appendChild($id(slider).childNodes[0]);
            numli.innerHTML = i + 1;
            show_pic.appendChild(picli);
            icon_num.appendChild(numli);
            if (i == 0) {
                numli.className = 'liactive';
                func_vli.innerHTML = picli.getElementsByTagName('img')[0].title;
            }
        }
        $id(slider).appendChild(show_pic);
        $id(slider).appendChild(func_box);
        $id(slider).appendChild(icon_num);
        func_vli.style.width = (hideTitle == true ? 0 : (width - length * 20)) + 'px';
        this.className(slider, length, width, height, point, hideNum);
        this.layerGlide(auto, icon_num.id, show_pic.id, func_vli, (point == 'left' ? width : height), second, speed, point);
    }
};
