// JavaScript Document

var WYS = {};

WYS.version = 'Wys Img 1.0';

WYS.container = ''; //容器
WYS.BackGround = '';
WYS.ScrollingTime = 5000; //间隔时间
WYS.MoreTarget = "_blank" //更多的打开方式
WYS.TitleTarget = "" //标题的打开方式
WYS.ImgHeight = 290; //图片高度
WYS.Default = "images/error.jpg"; //默认图片
WYS.ImgSrc = new Array();  //图片地址数组
WYS.ImgTitle = new Array(); //图片显示名称数组
WYS.ImgAllTitle = new Array(); //图片全称数组
WYS.ImgLink = new Array(); //信息链接
WYS.ImgMores = new Array(); //更多链接

WYS.ScrollImg = new Array();
WYS.ListImg = new Array();
WYS.Current = 0;
WYS.ImgTitleShow = "";
WYS.ImgMore = "";
WYS.RotateId = "";
WYS.PreTopPosition = 33;
WYS.PrePositionTop = 0;
WYS.MovePosition = 0;
WYS.MoveInterval = "";
WYS.ListTopPosition = 0;
WYS.ListPositionTop = 0;
WYS.Pre = 0;
WYS.ImgObj = new Array();

WYS.$ = function(id, doc){
    var doc = doc || document;
    return doc.getElementById(id);
};

WYS.event = {
    add : function(el, event, listener) {
        if (el.addEventListener){
            el.addEventListener(event, listener, false);
        } else if (el.attachEvent){
            el.attachEvent('on' + event, listener);
        }
    },
    remove : function(el, event, listener) {
        if (el.removeEventListener){
            el.removeEventListener(event, listener, false);
        } else if (el.detachEvent){
            el.detachEvent('on' + event, listener);
        }
    },
    input : function(el, func) {
        this.add(el, 'keyup', function(e) {
            if (!e.ctrlKey && !e.shiftKey && !e.altKey && (e.keyCode < 16 || e.keyCode > 18)) {
                func(e);
                return false;
            }
        });
    },
    ctrl : function(el, key, func) {
        this.add(el, 'keydown', function(e) {
            if (e.ctrlKey && e.keyCode == key.toUpperCase().charCodeAt(0) && !e.shiftKey && !e.altKey) {
                func(e);
                return false;
            }
        });
    }
};

WYS.show = function(config){
	WYS.container = config.container;
	WYS.BackGround = config.BackGround;
	WYS.ScrollingTime = config.ScrollingTime;
	WYS.MoreTarget = config.MoreTarget;
	WYS.TitleTarget = config.TitleTarget;
	WYS.Default = config.Default;
	WYS.ImgSrc = config.ImgSrc;
	WYS.ImgTitle = config.ImgTitle;
	WYS.ImgAllTitle = config.ImgAllTitle;
	WYS.ImgLink = config.ImgLink;
	WYS.ImgHeight = config.ImgHeight;
	WYS.ImgMores = config.ImgMore;
	WYS.create(config.container);
	WYS.event.add("window","load",WYS.initialize());
	}
	
WYS.create = function(c){
	var con,pic,sc,span,img;
	con = WYS.creatNode("div","scrolling");
	con.style.background = WYS.BackGround;
	pic = WYS.creatNode("div","pic");
	sc = WYS.creatNode("div","ScrolingImg");
	for(i=0;i<WYS.ImgSrc.length;i++){
	span = WYS.creatNode("span","");
	span.innerHTML = "<img src='"+WYS.ImgSrc[i]+"' id='ScrollImg"+i+"' onerror='WYS.NoImg(this)'/>";
	sc.appendChild(span);
	}
	pic.appendChild(sc);
	con.appendChild(pic);
	WYS.$(c).appendChild(con);
	
	pic = WYS.creatNode("div","link");
	span = WYS.creatNode("span","Title");
	pic.appendChild(span);
	span = WYS.creatNode("span","More");
	pic.appendChild(span);
	con.appendChild(pic);
	
	sc = WYS.creatNode("div","list");
	pic = WYS.creatNode("div","listTop");
	sc.appendChild(pic);
	pic = WYS.creatNode("div","listPic");
	for(i=0;i<WYS.ImgSrc.length;i++){
	span = WYS.creatNode("span","");
	span.innerHTML = "<img src=\""+WYS.ImgSrc[i]+"\" id=\"ImgList"+i+"\" onerror=\"WYS.NoImg(this)\" onmouseover=\"WYS.cut("+i+")\"/>";
	pic.appendChild(span);
	}
	sc.appendChild(pic);
	con.appendChild(sc);
	}
	
WYS.creatNode = function(type,id) {
	var newelement;
	newelement = document.createElement(type);
	newelement.setAttribute("id",id);
	return newelement;
	}

WYS.move = function()
    {  
		if(WYS.Pre-WYS.ListPositionTop>5) {
				WYS.Pre -=5;
			    WYS.$("ScrolingImg").style.left= "-"+WYS.Pre+"px";
			    WYS.MoveInterval = setTimeout("WYS.move()",1); 
			    } 
	    if(0<=WYS.Pre-WYS.ListPositionTop&&WYS.Pre-WYS.ListPositionTop<=5) {
			  WYS.Pre = WYS.ListPositionTop;
			  WYS.$("ScrolingImg").style.left= "-"+WYS.Pre+"px";
				} 
		if(WYS.ListPositionTop-WYS.Pre>5) {
			  WYS.Pre +=5;
			  WYS.$("ScrolingImg").style.left= "-"+WYS.Pre+"px";
			  WYS.MoveInterval = setTimeout("WYS.move()",1);
			  }
		if(0<=WYS.ListPositionTop-WYS.Pre&&WYS.ListPositionTop-WYS.Pre<=5) {
			   WYS.Pre = WYS.ListPositionTop;
			   WYS.$("ScrolingImg").style.left= "-"+WYS.Pre+"px";
			   } 
	};

WYS.rotate = function()
   {  
      if(WYS.Current<WYS.ImgSrc.length){
	 WYS.ImgTitleShow.innerHTML = "<a href=\""+WYS.ImgLink[WYS.Current]+"\" target=\""+WYS.TitleTarget+"\" title=\""+WYS.ImgAllTitle[WYS.Current]+"\" style=\"font-family:\'宋体\';\">"+WYS.ImgTitle[WYS.Current]+"</a>";
	  WYS.ImgMore.innerHTML = "<a href=\""+WYS.ImgMores[WYS.Current]+"\" target=\""+WYS.MoreTarget+"\" title=\""+WYS.ImgAllTitle[WYS.Current]+"\" style=\"font-family:\'宋体\';\">更多>></a>";  
	  for(i=0;i<WYS.ImgSrc.length;i++){
		  WYS.ListImg[i].parentNode.className = "";
		  }
	  WYS.ListImg[WYS.Current].parentNode.className = "imgOn";
	  WYS.ListTopPosition = WYS.Current*72+33;
	  WYS.ListPositionTop = WYS.Current*WYS.ImgHeight;
	  WYS.Pre = WYS.PrePositionTop;
      WYS.move();
	   WYS.$("listTop").style.backgroundPosition= WYS.ListTopPosition+"px 0px";
	   WYS.PreTopPosition = WYS.ListTopPosition;
       WYS.PrePositionTop = WYS.ListPositionTop;
	   WYS.Current++;
	   WYS.RotateId = setTimeout("WYS.cut()",WYS.ScrollingTime);
	  } else if(WYS.Current>=WYS.ImgSrc.length) {WYS.cut(0);}
	}

WYS.initialize = function() 
    {
	  WYS.ImgTitleShow = WYS.$("Title");
	  WYS.ImgMore = WYS.$("More");
	  for(j=0;j<WYS.ImgSrc.length;j++) {
		  WYS.ScrollImg[j] = WYS.$("ScrollImg"+j); 
		  WYS.ListImg[j] = WYS.$("ImgList"+j);
	  }
	  WYS.ImgTitleShow.innerHTML = "<a href=\""+WYS.ImgLink[0]+"\" target=\""+WYS.TitleTarget+"\" title=\""+WYS.ImgAllTitle[0]+"\" style=\"font-family:\'宋体\';\">"+WYS.ImgTitle[0]+"</a>";
	  WYS.ImgMore.innerHTML = "<a href=\""+WYS.ImgMores[0]+"\" target=\""+WYS.MoreTarget+"\" title=\""+WYS.ImgAllTitle[0]+"\" style=\"font-family:\'宋体\';\">更多>></a>";
	   WYS.ListImg[0].parentNode.className = "imgOn";
	   WYS.$("listTop").style.backgroundPosition= "33px 0px";
	  WYS.Current = 1;
	  WYS.RotateId = setTimeout("WYS.cut()",WYS.ScrollingTime);
	}
	
WYS.cut = function (Curr) {
	clearTimeout(WYS.MoveInterval);
	clearTimeout(WYS.RotateId);
	if(Curr!=null) {
		  WYS.Current=Curr;
		}
	WYS.rotate();
	}
	
WYS.NoImg = function(obj){   
        obj.src=WYS.Default;   
    }
