var AD_TRACELOG_HTTP_ROOT = "http://tj.alisoft.com/tj.html";
/**
 * 保存一个切换对象
 * @param  htmlObjId html的id
 * @param  adId    广告位id
 * @param  stayMs  停留的毫秒数
 * follow by shj 
 * @param  openUrl 图片链接 
 * @param  imgUrl  图片url 
 **/
function PollObj( htmlObjId, adId,imgUrl,openUrl, stayMs) {
    this.htmlObjId = htmlObjId;
    this.adId = adId;
    this.stayMs = stayMs;
    
    this.imgUrl = imgUrl;
    this.openUrl = openUrl;
}

/**
 * 切换处理对象
 * @param  newArrPollObj html拖拉对象
 * @param  currentIndex  当前显示的索引id
 * @param  swapImgId  用来做切换处理的图片id, 如果不需要swap的日志的时候，则传个空过来
 **/
function Poll( newArrPollObj, currentIndex, swapImgId, swapMethod) {
    // 里面存储div对象列表
    this.arrPollObj = newArrPollObj;
    // 当前显示的第几个div
    this.currentIndex = currentIndex;
    // 轮询的时间间隔
    this.replaceSencond = 30000;
    // 供我使用的切换的图片的id
    this.swapImgId = swapImgId;
    // 跳转的方法
    this.swapMethod = swapMethod;

    var currentCount = -1;
    var arrPollIndex = [];
    for(var i=0;i<this.arrPollObj.length;i++){
    	arrPollIndex.push(i);
    }
    // 对象切换显示
    this.swap = function() {
        var obj ;
        // 把上一个对象设置成为不显示
        if ( this.currentIndex != -1 ) {
            obj = document.getElementById(this.arrPollObj[this.currentIndex].htmlObjId);
            this.display( obj, false );
        } else {
            obj = document.getElementById(this.arrPollObj[0].htmlObjId);
            this.display( obj, false );
        }
        //设置当前要显示的数据
        this.setCurrentIndex();
        var pollObj = this.getCurrentPollObj();
        obj = document.getElementById(pollObj.htmlObjId);
        this.display( obj, true );
        if ( swapImgId ) {
            if ( isNeedSwapAd() ) {
             //   traceAdLogByType(this.swapImgId, 0, pollObj.adId);
            }
        }
        // 如果有回调的替换方法
        if( this.swapMethod ) {
            // 如果有拖拉对象的停留时间
            if ( pollObj.stayMs ) {
                // alert("this.swapMethod=" + this.swapMethod);
                // alert("second=" + pollObj.stayMs);
                setTimeout( this.swapMethod + "()", pollObj.stayMs );
            }
        }
    };
    this.richMediaSwap = function(){
    	this.getRandomIndex();
    	var currentPoll = this.getCurrentPollObj();
    	window.richImgUrl = currentPoll.imgUrl;
        window.richAdUrl = currentPoll.openUrl;
        window.richAdId = currentPoll.adId;
    };
    
    this.getCurrentPollObj = function() {
        return this.arrPollObj[this.currentIndex];
    };
    
    // 取得完全随即的下一个广告
    this.getRandomIndex = function() {
		var random=Math.random();
		random = random * this.arrPollObj.length;
		var ret = Math.floor(random);
		if ( ret == this.arrPollObj.length) {
			ret = this.arrPollObj.length - 1;
		}
		this.currentIndex = ret;
	};
    
    // 取得当前要显示的对象
    this.setCurrentIndex = function() {
    	if(currentCount>=arrPollIndex.length||currentCount<0){
    		var lastDis;
    		//保证在上一轮最后显示的不会在下一轮已开始就显示，第一轮不作此处理
    		if(currentCount!=-1){
    			lastDis = arrPollIndex[arrPollIndex.length-1];
    		}
    		arrPollIndex.sort(function(x,y){
        		return Math.random()>.5?-1:1;
        	});
    		if(currentCount!=-1){
    			if(lastDis == arrPollIndex[0]){
        			arrPollIndex[0] = arrPollIndex[arrPollIndex.length-1];
        			arrPollIndex[arrPollIndex.length-1] = lastDis;
        		}
    		}
    		currentCount=0;
    	}
		this.currentIndex = arrPollIndex[currentCount];
		currentCount++;
	};

    // 显示某个对象
    this.display = function( obj, needDisplay ) {
        if ( needDisplay ) {
            obj.style.display = "";
        } else {
            obj.style.display = "none";
        }
    };
}

/**
 * 根据指定的type来生成记录日志
 * @param  imgId 图片id
 * @param  type  记录类型： 0 广告切换, 1 广告点击
 * @param  adId  广告id
 **/
function traceAdLogByType(imgId, type, adId) {
    var r=Math.random();
    var linkUrl = AD_TRACELOG_HTTP_ROOT;
    switch ( type ) {
        case 0:
            linkUrl = linkUrl + "?tracelog=show_"+adId+"&signmode=none&rand="+r;  
            break;
        case 1:
            linkUrl = linkUrl + "?tracelog=click_"+adId+"&signmode=none&rand="+r; 
            break;
    }
    // 图片对象
    var obj = document.getElementById(imgId);
    if (obj) {
        obj.src = linkUrl;
    }
}
/**
 * 记录日志
 * @param  imgId 图片id
 * @param  tracelog  日志的参数
 **/
function traceAdLog(imgId, tracelog) {
    var r=Math.random();
    var linkUrl = AD_TRACELOG_HTTP_ROOT;
    linkUrl = linkUrl + "?" + tracelog;
    // 图片对象
    var obj = document.getElementById(imgId);
    if (obj) {
        obj.src = linkUrl;
    }
}
 
var needSwapAd = false;
function setActive() {
  //  alert("active");
    needSwapAd = true;
}
function unsetActive() {
//		alert("unactive");
    needSwapAd = false;
}
function isNeedSwapAd() {
    return needSwapAd;
}
