/**
 * |*** 版权所有,侵权必究 ***|
 * @author 彭伟林
 */

String.prototype.trim = function(){ //自定义一个trim()方法,放到String对象的prototype属性里
    return this.replace(/(^\s*)|(\s*$)/g, ""); //去除所有空格
};

//判断是否为Firefox或IE
var moz = (typeof document.implementation != "undefined") && (typeof document.implementation.createDocument != "undefined");
var ie = (typeof window.ActiveXObject != "undefined");

/**
 * 图片轮换广告对象
 * @param {Integer} width
 * @param {Integer} height
 * @param {Boolean} hasTitle
 * @return {imgNews}
 */
function imgNews(width,height,hasTitle){

    this.xmlDoc;		
    this.titleNodes;	//保存XML里的标题节点的数组
    this.urlNodes;		//保存XML里的URL节点的数组
    this.imgNodes;		//保存XML里的图片路径节点的数组
    this.targetNodes;	//保存XML里的链接目标节点的数组
	
	//后期补上
    this.width=width;
	this.height=height;
	this.hasTitle=hasTitle;	//识别是否有标题显示
	
    this.currentPoint = -1;	//当然的图片指针,
    this.currentTitle;		//当然显示的新闻标题
    this.currentURL;		//当前的链接URL
    this.currentImg;		//当然显示的图片路径
    this.currentTarget = "target";	//当前的链接目标
    
    this.count = 0; //新闻的数量
    this.container; //DIV容器
    this.transTime = 5 * 1000;		//图片切换时间延迟
    this.transTimeOut;				//当然对象的timeout对象
    
    this.startFunciton;		//定义一个图片轮换的闭包.
    	
	/* 保存广告对象各元素的样式. */
	this.titleStyle="";
	this.btnStyle="";
	this.btnContainerStyle="";
	this.imgStyle="";
    
    /**
     * 导入XML文件
     * @param {String} file
     */
    this.importXML = function(file){ 
        if (moz) {
            this.xmlDoc = document.implementation.createDocument("", "doc", null);
            this.xmlDoc.async = false;
        }
        else {
            if (ie) {
                this.xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
                this.xmlDoc.async = false;
                while (this.xmlDoc.readyState != 4) {
                }
            }
        }
        
        this.xmlDoc.load(file); //加载文件
    }
    /**
     * 获取所有新闻的节点及新闻数量
     */
    this.getTotalNewsInfo = function(){ 
        if (this.xmlDoc != null) {
            this.titleNodes = this.xmlDoc.getElementsByTagName("title");
            this.urlNodes = this.xmlDoc.getElementsByTagName("url");
            this.imgNodes = this.xmlDoc.getElementsByTagName("img");
            this.targetNodes = this.xmlDoc.getElementsByTagName("target");
            this.count = this.xmlDoc.getElementsByTagName("imgnews").length;
        }
    }
    /**
     * 初始化广告容器.
     * @param {String} btnConName
     * @param {String} imgName
     * @param {String} titleName
     */
    this.initContainer = function(btnConName, imgName, titleName){
	    if (this.hasTitle) {
			
			var btns = "";
			var btnContainerHead = "<div id=\"" + btnConName + "\" class=\""+this.btnContainerStyle+"\" style=\"display:none;\">";
			
			var btnContainerBottom = "</div>";
			
			btns += btnContainerHead;
			
			for (var i = 0; i < this.count; i++) {
			
				var btn = "<a href=\"javascript:void(0);\" class=\""+this.btnStyle+"\" id=\"btn" + imgName + "" + i + "\">" +
				(i + 1) +
				"</a>";
				if (i % 8 == 0 && i != 0) 
					btns += "<br>";
				btns += btn;
			}
			btns += btnContainerBottom;
			
			this.container.innerHTML += btns;
		}
        this.container.innerHTML += "<img src=\"" +	
	        this.imgNodes[0].firstChild.nodeValue + "\" id=\"" + 
			imgName + "\" class=\""+this.imgStyle+"\" width=\""+this.width+"px\" height=\""+this.height+"px\" style=\"cursor:hand;display:none;filter: revealtrans(duration=2, transition=23)\">";
        
		if(this.hasTitle)
        	this.container.innerHTML += "<div id=\"" + titleName + "\" class=\""+this.titleStyle+"\">" + this.titleNodes[0].firstChild.nodeValue + "</div>";
	}
    /**
     * 设置广告各元素的样式
     * @param {String} titleStyle
     * @param {String} btnStyle
     * @param {String} btnContainerStyle
     * @param {String} imgStyle
     */
	this.setStyle=function(titleStyle,btnStyle,btnContainerStyle,imgStyle){
		this.titleStyle=titleStyle;
		this.btnStyle=btnStyle;
		this.btnContainerStyle=btnContainerStyle;
		this.imgStyle=imgStyle;
	}
	/**
	 * 重新设置图片
	 * @param {String} newImg
	 * @param {String} oldImgName
	 */
    this.setImage = function(newImg, oldImgName){
        var imgElem = document.getElementById(oldImgName);
        imgElem.src = newImg;
    }
    /**
     * 重新设置标题
     * @param {String} newTitle
     * @param {String} titleName
     */
    this.setTitle = function(newTitle, titleName){
	    if (this.hasTitle) {
			
			var titleElem = document.getElementById(titleName);
			//alert(titleElem.innerHTML);
			titleElem.innerHTML = "<a href=\"" + this.urlNodes[this.currentPoint].firstChild.nodeValue + "\" target=\"" + this.targetNodes[this.currentPoint].firstChild.nodeValue + "\">" + newTitle + "</a>";
		}
	}
	/**
	 * 初始化广告对象的事件
	 * @param {String} imgName
	 * @param {String} titleName
	 * @param {Element} obj
	 */
    this.initEvent=function(imgName,titleName,obj){
		if (this.hasTitle) {
			for (var i = 0; i < this.count; i++) {
				var totalBtn = document.getElementById("btn" + imgName + i);
				totalBtn.onmouseover = function(){
					obj.Mover_Event(this, imgName, titleName);
				}
				totalBtn.onmouseout = function(){
					obj.Mout_Event(obj);
				}
			}
		}
		var img=document.getElementById(imgName);
		img.onclick = function(){
			obj.Img_Click(obj);
		};
		img.onmouseout=function(){
			obj.Img_Mout();
		};
		img.onmouseover=function(){
			obj.Img_Mover();
		};
	}
	/**
	 * 重新设置按钮
	 * @param {String} imgName
	 * @param {String} titleName
	 */
    this.setButton = function(imgName,titleName){

        for (var i = 0; i < this.count; i++) {
            var totalBtn = document.getElementById("btn" + imgName + i);
			
            totalBtn.style.backgroundColor = "#333333";
            totalBtn.style.color = "#FFFFFF";
        }
        var btnElem = document.getElementById("btn" + imgName + this.currentPoint);
        
        btnElem.style.backgroundColor = "#FF0000";
        btnElem.style.color = "black";
        
    }
    /**
     * 广告的切换
     * @param {String} imgName
     * @param {String} titleName
     */
    this.transImages = function(imgName, titleName){

        this.currentTitle = this.titleNodes[this.currentPoint].firstChild.nodeValue;
        
        this.currentURL = this.urlNodes[this.currentPoint].firstChild.nodeValue;
        
        this.currentImg = this.imgNodes[this.currentPoint].firstChild.nodeValue;
        
        this.currentTarget = this.targetNodes[this.currentPoint].firstChild.nodeValue;
        
        if (ie) {
            var imgElem = document.getElementById(imgName);
            imgElem.filters.revealtrans.transition = Math.random() * 24;
            imgElem.filters.revealtrans.apply();
            imgElem.filters.revealtrans.play();
        }
		if(this.hasTitle)
	        this.setTitle(this.currentTitle, titleName);
			
		this.setImage(this.currentImg, imgName);
		
		if(this.hasTitle)
			this.setButton(imgName,titleName);
    }
    /**
     * 设置广告切换的时间延迟
     * @param {Integer} ms
     */
    this.setLayTime = function(ms){
        if (ms == "" || ms == null) 
            return;
        this.transTime = ms * 1000;
    }
    /**
     * 设置广告切换所需要用的时间
     * @param {Integer} sec
     * @param {String} imgName
     */
    this.setTransTime = function(sec, imgName){
		if(ie){
			var imgElem = document.getElementById(imgName);
        	imgElem.filters.revealtrans.duration = sec;
        	imgElem.filters.revealtrans.apply();
		}        
    }
    /**
     * 广告初始化,广告开始运行.
     * @param {String} file
     * @param {String} divContainer
     * @param {String} btnConName
     * @param {String} imgName
     * @param {String} titleConName
     * @param {Integer} speed
     * @param {Integer} ms
     * @param {imgNews} obj
     */
    this.transInit = function(file, divContainer, btnConName, imgName, titleConName, speed, ms, obj){
	
        this.container = document.getElementById(divContainer);
                
        this.importXML(file);
        if (this.xmlDoc == null || typeof this.xmlDoc != "object") {
			if(this.hasTitle)
				this.container.innerHTML = "have not Image's News";
			return false;
		}
            
        this.getTotalNewsInfo();
        
        if (this.count > 0) {
            this.initContainer(btnConName, imgName, titleConName);
			
            this.setLayTime(ms);
            this.setTransTime(speed, imgName);
            this.transStart(imgName, btnConName);
			
            this.beginTrans(imgName, titleConName, obj);
			
			this.initEvent(imgName, titleConName,obj);
        }
        else {
			if(this.hasTitle)
            	this.container.innerHTML = "have not Image's News";
        	return false;
		}
		return true;
    }
    /**
     * 广告开始切换
     * @param {String} imgName
     * @param {String} btnConName
     */
    this.transStart = function(imgName, btnConName){
        var imgElem = document.getElementById(imgName);
        imgElem.style.display = "";
		if(this.hasTitle){
	        var contain = document.getElementById(btnConName);
	        contain.style.display = "";
		}
        
    }
    /**
     * 广告切换到下一个
     * @param {String} imgName
     * @param {String} titleName
     * @param {imgNews} obj
     */
    this.beginTrans = function(imgName, titleName,obj){
    
        this.startFunciton = function(){
            obj.beginTrans(imgName, titleName,obj);
        }
		
        this.transNext(imgName, titleName,obj);
		
        this.transTimeOut = window.setTimeout(this.startFunciton, this.transTime);
		
    }
    
	/**
	 * 切换到指定的广告
	 * @param {String} imgName
	 * @param {String} titleName
	 * @param {imgNews} obj
	 */
    this.transNext = function(imgName, titleName,obj){
        if (this.currentPoint >= this.count - 1) {
            this.currentPoint = 0;
        }
        else {
            this.currentPoint++;
        }
        this.transImages(imgName, titleName);
    }
    
    /* -------------------------------------------------- 事件 ------------------------------------------------------- */
    
    this.Img_Click = function(obj){
        if (obj.currentTarget != "_self" || obj.currentTarget != "_parent") {

            window.open(obj.currentURL);
        }
        else {
        location.href = obj.currentURL;
        }
    }
    
    this.Mover_Event = function(obj, imgName, titleName){
        window.clearTimeout(this.transTimeOut)
        this.currentPoint = obj.id.substring(obj.id.length-1);
		
        this.transImages(imgName, titleName);
        obj.style.background = "red";
        obj.style.color = "black";
    }
    
    this.Mout_Event = function(obj){
        obj.transTimeOut = window.setTimeout(obj.startFunciton, obj.transTime, "javascript");
    }
	this.Img_Mover=function(){

		window.clearTimeout(this.transTimeOut);
	}
	this.Img_Mout=function(){
		this.transTimeOut = window.setTimeout(this.startFunciton, this.transTime, "javascript");
	}
    
   // this.transInit(file, divContainer, btnConName, imgName, titleConName, speed, ms, this);
}
