﻿/********************************************************************
*本脚本基于xmlhttprequest进行广告读取，点击率核算
*备注：新版该功能和旧版区别是：
*1.采用异步获取机制加快页面下载速度、改善服务器性能
*2.修正了旧版无法实现广告点击率计算的设计缺陷
*3.废除了几乎没用的旧版统计广告在页面显示的次数
*广告设计模块技巧和逻辑原理请参考程序技术文档tiansoft(20080514)
********************************************************************/


function CreateXmlhttp()
{
  var xmlHttp= window.XMLHttpRequest?(new XMLHttpRequest()):(new ActiveXObject("Microsoft.XMLHTTP"));
  return xmlHttp;
}


//发送广告请求信息
function GetAdwordsInfo(TagID,AdsID)
{
  
  var objHttp=CreateXmlhttp();
  
    var url = "/js/AdwordsAjax.aspx?Action=LoadAds&isTitle=0&AdID="+AdsID+"&Time="+Math.random();
    objHttp.open("GET", url, true);    
     objHttp.setRequestHeader("Cache-Control","no-cache"); 
     objHttp.send(null);
     objHttp.onreadystatechange =function(){ 


    if (objHttp.readyState == 4 && objHttp.status==200)
    {
   
      var result=objHttp.responseText;
      
      if(result!="")
      {
   
        document.getElementById(TagID).innerHTML=result;
      }     

    }
 
   }    
 

}

function GetAdsToHtml(TagID,AdsID)
{
    var objHttp=CreateXmlhttp();
  
    var url = "/js/AdwordsAjax.aspx?Action=LoadAds&isTitle=2&AdID="+AdsID+"&Time="+Math.random();
    objHttp.open("GET", url, true);    
    objHttp.setRequestHeader("Cache-Control","no-cache"); 
    objHttp.send(null);
    objHttp.onreadystatechange =function(){ 


    if (objHttp.readyState == 4 && objHttp.status==200)
    {
   
      var result=objHttp.responseText;
     
      if(result=="")
      {
        document.getElementById(TagID).style.display="none";
      }     
      else
      {
        document.getElementById(TagID).innerHTML=result;
      }
    }
 
   }    
}
//发送页面广告点击计数请求－－－本事件调用已集成在后台广告内容里面
function GetRecordCount(AdsID)
{

    var url = "/js/AdwordsAjax.aspx?Action=RecordCount&isTitle=3&AdID="+AdsID+"&RandCache="+Math.random();
  var objHttp=CreateXmlhttp();
    objHttp.open("GET", url, true);    
     objHttp.setRequestHeader("Cache-Control","no-cache");    
     objHttp.send(null); 
     objHttp.onreadystatechange = function(){
     if (objHttp.readyState == 4 && objHttp.status==200)
    {
   
      var result=objHttp.responseText;
      
      if(result=="" || result=="0")
      {
        alert('广告点击率统计系统出错');
      }          
    }
     }
    

}
//设置滚动
function marquee1(height,speed,delay,divBig,divSmall){
	var scrollT;
	var pause = false;
	var ScrollBox = document.getElementById(divBig);
	if(document.getElementById(divSmall).offsetHeight <= height) return;
	ScrollBox.innerHTML += ScrollBox.innerHTML;
	ScrollBox.onmouseover = function(){pause = true}
	ScrollBox.onmouseout = function(){pause = false}
	ScrollBox.scrollTop = 0;
	function start(){
	    scrollT = setInterval(scrolling,speed);
	    if(!pause) ScrollBox.scrollTop += 2;
	}
	function scrolling(){
	    if(ScrollBox.scrollTop % height != 0){
	        ScrollBox.scrollTop += 2;
	        if(ScrollBox.scrollTop >= ScrollBox.scrollHeight/2) ScrollBox.scrollTop = 0;
	    }
		else{
	        clearInterval(scrollT);
	        setTimeout(start,delay);
	    }
	}
	setTimeout(start,delay);
}

///////////////////////////////
function PicFocus(imageContainerID,textContainerID,buttonContainerID,intervarTime){
	    if(typeof PicFocusManager == "undefined")
	    {
		    PicFocusManager =[];
	    }
		this.$ = function (id){return document.getElementById(id)}
		this.index = PicFocusManager.length;
		PicFocusManager[PicFocusManager.length] = this;
		this.imageContainer = this.$(imageContainerID);
		this.textContainer = this.$(textContainerID);
		this.buttonContainer = this.$(buttonContainerID);
		this.firstShow = 0; //默认显示项
		this.interval = (isNaN(intervarTime)?0:intervarTime) || 5000;
		 //切换时间
		this.canAutoPlay = true; //是否可以自动切换
		this.currentPosition = this.firstShow;
		this.timer;
		this.images = [];
		this.texts = [];
		this.buttons = [];
		this.bindEvent = function(){
			var _self = this;
			for(var i=0;i<this.images.length;i++){
				this.images[i].onmouseover = function(){
					_self.stop();
				}
				this.images[i].onmouseout = function(event){
					var willPlay=true;
					if(!event)event=window.event;
					for(var i=0;i<_self.buttons.length;i++){
						if(typeof event.toElement != 'undefined' && event.toElement == _self.buttons[i]){
							willPlay=false;
						}
						
						if(typeof event.relatedTarget != 'undefined' && event.relatedTarget==_self.buttons[i]){
							willPlay=false;
						}
					}
					if(willPlay)
						_self.play();
				}
			}
			for(var i=0;i<this.buttons.length;i++){
				this.buttons[i].onclick = function(){
					_self.focus(this);
				}
			}
		}
		this.play = function(){
			if(this.canAutoPlay){
				this.setFocus(this.currentPosition ++ )
				if(this.currentPosition >= this.images.length)this.currentPosition =0 ;
				this.timer = setTimeout('PicFocusManager[' + this.index + '].play()' , this.interval )
			}
		}
		this.stop = function(){
			clearTimeout( this.timer );
		}
		this.focus = function(button){
			for(var i=0;i<this.buttons.length;i++){
				if(this.buttons[i] == button){
					this.currentPosition = i;
					this.setFocus(this.currentPosition);
					break;
				}
			}
		}
		this.setFocus = function(i){
			try{
				this.imageContainer.filters[0].apply();
				this.imageContainer.filters[0].play();
			}catch(e){}
			for(var j=0;j<this.images.length;j++){
				this.images[j].style.display = (i==j)?"":"none";
			}
			for(var j=0;j<this.texts.length;j++){
				this.texts[j].style.display = (i==j)?"":"none";
			}
			for(var j=0;j<this.buttons.length;j++){
				this.buttons[j].className = (i==j)? this.buttons[j].getAttribute("focusClass") :this.buttons[j].getAttribute("normalClass");
			}
			try{  //滤镜版本
				new ActiveXObject("DXImageTransform.Microsoft.Fade");
				this.imageContainer.filters[0].play();
			}catch(e){}
		}
		this.init = function(){
			if(this.imageContainer && this.imageContainer && this.imageContainer){
				//init
				this.images=this.imageContainer.getElementsByTagName("img");
				if(this.textContainer) this.texts=this.textContainer.getElementsByTagName("label");
				this.buttons=this.buttonContainer.getElementsByTagName("a");
				this.bindEvent();
				for(var i=0;i<this.images.length;i++){
					this.images[i].style.display = "none";
					if(i<this.texts.length) this.texts[i].style.display = "none";
					this.buttons[i].className = this.buttons[i].getAttribute("normalClass");
				}
				this.images[this.firstShow].style.display = "";
				if(this.firstShow<this.texts.length) this.texts[this.firstShow].style.display = "";
				this.buttons[this.firstShow].className = this.buttons[this.firstShow].getAttribute("focusClass");
			}else{
				alert("未提供正确的参数")
			}
		}
	}

