/*       imgWidth：图片长，默认275px
        imgHeight：图片宽，默认215px
         numAlpha：数字导航透明度，0-1之间，默认为1，不透明
         numColor：数字导航颜色，默认为白色#fff
       numBGColor：数字导航背景颜色，默认为黑色#000
    numFocusColor：获取焦点数字导航颜色，默认为白色#fff
  numFocusBGColor：获取焦点数字导航背景颜色，默认为红色#f00
      numPosition：导航居左还是右，默认居右
   imgFilterIndex：仅IE5..5浏览器有用，指定要使用的滤镜，提供11种滤镜，值为0~10的数字，未指定则随机一个滤镜
            delay：自动播放间隔，单位ms，默认8000ms
     tlLineHeight：标题行高，默认25px
       tlFontSize：标题字体大小，默认13px
          tlColor：标题颜色，默认黑色#000
     tlFontWeight：标题样式，默认normal
     */
var libImages = function (cfg) {
    this.initProperty = function (cfg) {
        var attrs = 'imgWidth,imgHeight,numAlpha,numColor,numBGColor,numFocusColor,numFocusBGColor,imgFilterIndex,delay,tlFontSize,tlColor,tlLineHeight,tlFontWeight,numPosition'.split(',')
       , values = '275,215,1,#fff,transparent,#fff,transparent,rnd,8000,13,#fff,25,normal,right'.split(','), rxInt = /^\d+$/;
        for (var i = 0; i < attrs.length; i++) this[attrs[i]] = typeof (cfg[attrs[i]]) != 'undefined' ? cfg[attrs[i]] : (rxInt.test(values[i]) ? parseFloat(values[i]) : values[i]);
    }
    this.rnd = function () { return Math.floor(Math.random() * this.filters.length); }
    this.$ = function (Id) { return document.getElementById(Id); }
    this.initProperty(cfg ? cfg : {});
    if (cfg.numPosition != 'left' && cfg.numPosition != 'right') cfg.numPosition = 'right';
    this.instanceIndex = libImages.instance++
    this.isIE = !!document.all;
    this.IEVertion = -1;
    if (this.isIE) this.IEVersion = parseFloat(/msie ([\d\.]+)/i.exec(navigator.userAgent)[1]);
    this.filters = this.isIE && this.IEVersion > 5.4 ? ("progid:DXImageTransform.Microsoft.Pixelate(,enabled=false,duration=1,maxSquare=25);"
            + "progid:DXImageTransform.Microsoft.Fade(duration=1,overlap=0);"
            + "progid:DXImageTransform.Microsoft.GradientWipe(duration=1,gradientSize=0.25,motion=forward );"
            + "progid:DXImageTransform.Microsoft.Stretch(duration=1,stretchStyle=PUSH);"
            + "progid:DXImageTransform.Microsoft.Wheel(duration=1,spokes=16);"
            + "progid:DXImageTransform.Microsoft.RandomDissolve(duration=1);"
            + "progid:DXImageTransform.Microsoft.Spiral(duration=1,gridSizeX=50,gridSizeY=50);"
            + "progid:DXImageTransform.Microsoft.Slide(duration=1,bands=1,slideStyle=SWAP);"
            + "progid:DXImageTransform.Microsoft.RadialWipe(duration=1,wipeStyle=CLOCK);"
            + "progid:DXImageTransform.Microsoft.Blinds(Bands=10,direction=up);"
            + "progid:DXImageTransform.Microsoft.Checkerboard(Direction=right,SquaresX=2,SquaresY=2)").split(';') : false;
    if (this.filters && this.imgFilterIndex != 'rnd' && this.imgFilterIndex >= this.filters.length) this.imgFilterIndex = 'rnd';
    /*
    arr为json数组对象，如[{u:'链接',tl:'标题',img:'图片路径'},{u:'链接',tl:'标题',img:'图片路径'}.....]
    */
    this.chrImage = function (a, arrIndex) {
        var as = this.$('dvNum' + this.instanceIndex).getElementsByTagName('a'), me = this;
        for (var i = 0; i < as.length; i++) as[i].className = '';
        if (a) {this.nowIndex=arrIndex; clearInterval(this.timer); a.className = 'numfocus'; this.timer = setInterval(function () { me.chrImage(); }, this.delay); }
        else as[this.nowIndex].className = 'numfocus';
        if (this.filters) { this.$('imgObj' + this.instanceIndex).style.filter = this.filters[this.imgFilterIndex == 'rnd' ? this.rnd() : this.imgFilterIndex]; this.$('imgObj' + this.instanceIndex).filters[0].apply() }
        if (this.arr[this.nowIndex].u) this.$('imgHref' + this.instanceIndex).href = this.$('wordHref' + this.instanceIndex).href = this.arr[this.nowIndex].u;
        if (this.arr[this.nowIndex].tl || this.arr[this.nowIndex].entl) this.$('wordHref' + this.instanceIndex).innerHTML = this.arr[this.nowIndex].tl + (this.arr[this.nowIndex].entl ? '<font>' + this.arr[this.nowIndex].entl + '</font>' : '');
        this.$('imgObj' + this.instanceIndex).src = this.arr[this.nowIndex].img;
        this.$('imgObj' + this.instanceIndex).alt = this.arr[this.nowIndex].tl ? this.arr[this.nowIndex].tl : this.arr[this.nowIndex].entl ? this.arr[this.nowIndex].entl : '';
        if (this.dtID && this.arr[this.nowIndex].dt) this.$(this.dtID).innerHTML = (this.arr[this.nowIndex].tl?'　　':'') + '<a href="' + this.arr[this.nowIndex].u + '" target="_blank">' + this.arr[this.nowIndex].dt + '</a>';
        if (this.filters) this.$('imgObj' + this.instanceIndex).filters[0].play();
        this.nowIndex++;
        if (this.nowIndex >= this.arr.length) this.nowIndex = 0;
        return false;
    }
    this.init = function (arr, dtID) {
        if (typeof (arr) == 'undefined' || arr.length == 0) { alert('未指定数据源或者数据源为空！'); return false; }
        this.arr = arr;
        this.nowIndex = 1;
        this.dtID = dtID;
        document.write('<style type="text/css">\
	    #imgContainer' + this.instanceIndex + '{position:relative;width:' + this.imgWidth + 'px;margin:0px auto;}\
	    #imgContainer' + this.instanceIndex + ' .imgObj{height:' + this.imgHeight + 'px;width:' + this.imgWidth + 'px;display:block;border:none;}\
        #imgContainer' + this.instanceIndex + ' .alphabg{height:40px;filter:alpha(opacity:50);opacity:.5;background:#000;position:absolute;bottom:0px;width:100%;}\
	    #imgContainer' + this.instanceIndex + ' .newstl{height:35px;padding-left:5px;width:100%;line-height:16px;overflow:hidden;position:absolute;bottom:0px;overflow:hidden;}\
        #imgContainer' + this.instanceIndex + ' .newstl a{color:yellow}\
        #imgContainer' + this.instanceIndex + ' .newstl a font{' + (arr[0].tl == '' ? '' : 'color:#fff;') + 'display:block;;font-size:10px;font-family: Arial,Helvetica,sans-serif;}\
	    #imgContainer' + this.instanceIndex + ' .num{position:absolute;bottom:' + (40 + 2) + 'px;' + cfg.numPosition + ':2px;font-weight:bold;font-size:13px;height:15px;}\
	    #imgContainer' + this.instanceIndex + ' .num div{float:' + cfg.numPosition + ';}\
	    #imgContainer' + this.instanceIndex + ' .num div a{float:left;width:15px;color:' + this.numColor + ';background:' + this.numBGColor + ';margin-left:5px;text-align:center;text-decoration:none;line-height:15px;filter:alpha(opacity:' + (this.numAlpha * 100) + ');opacity:' + this.numAlpha + ';outline:none;hideFocus:expression(this.hideFocus=true)}\
	    #imgContainer' + this.instanceIndex + ' .num div a.numfocus{background:' + this.numFocusBGColor + ';color:' + this.numFocusColor + ';border:solid 1px #fff;}\
	</style>');
        document.write('<div id="imgContainer' + this.instanceIndex + '" class="imgContainer">\
	    <a href="' + arr[0].u + '" target="_blank" id="imgHref' + this.instanceIndex + '"><img alt="' + (arr[0].tl ? arr[0].tl : arr[0].entl ? arr[0].entl : '') + '" class="imgObj" id="imgObj' + this.instanceIndex + '" src="' + arr[0].img + '" /></a>\
        <div class="alphabg"></div>\
        <div class="newstl"><a href="' + arr[0].u + '" target="_blank" id="wordHref' + this.instanceIndex + '" style="font-size:' + this.tlFontSize + 'px;">' + arr[0].tl + '<font>' + arr[0].entl + '</font></a></div>\
	    <div class="num"><div id="dvNum' + this.instanceIndex + '"></div></div>\
        </div>');
        var html = '<div>', pNode = this.$('dvNum' + this.instanceIndex), a, me = this;
        for (var i = 0; i < arr.length; i++) {
            a = document.createElement('a');
            if (i == 0) a.className = 'numfocus';
            a.innerHTML = i + 1; a.href = 'javascript:void(0)'; a.setAttribute('idx', i);
            if (arr.length > 1) a.onclick = function () { me.chrImage(this, parseInt(this.getAttribute('idx', 10))); }
            pNode.appendChild(a);
        }
        if (arr.length > 1) this.timer = setInterval(function () { me.chrImage(); }, this.delay);
    }
}
libImages.instance = 0;
