//显示广告
/*
arrAD，广告数组，包含ID、Type、Title、Color、Path、Url、Width、Height、AreaNoList、Weight
iLength，该位置广告的个数
iShowType，广告的显示形式，1为刷新换广告，2为上下滚动，3为左右滚动，4为轮显，......
strAreaNo，用户所在的地区，如果AreaNoList不为空的话，有限显示strAreaNo对应的地区，如果显示的数量不够的话，再从其他广告选择
广告链接地址：TgAdvClick.aspx?id={ID}&u={Url}，Flash也要遵循这个规则
Type：1.文字，2.图片，3.Flash，4.图片+文字
Title：广告文字，如果是图片的话要加Alt
Color：文字颜色
Path：图片、Flash链接的地址
Url：点击链接的地址
Width：图片、Flash的宽度
Height：图片、Flash的高度
AreaNoList：该地区的人才可以看到广告，如果显示的数量不够的话，再从其他广告选择。格式：000100110001,000100120001
Weight：广告权重，权重越高，排序越前，并且当arrAD的数量大于iLength时，就按权重随机选择显示，权重越大，出现的几率就越高
*/
var arrADObjID = new Array();

function ShowAdv(arrAD,iLength,iShowType,strAreaNo)
{
    iLength = iLength < 1 ? 1 : iLength;
    iShowType = iShowType < 1 ? 1 : iShowType;
    
    var arrShowAD = new Array();
    if(arrAD.length <= iLength)
    {
        arrShowAD = arrAD;
    }
    else
    {
        var arrAreaAD = GetAreaAD(arrAD,strAreaNo);
        
        if(arrAreaAD.length > iLength)
        {
            arrShowAD = GetRandomAD(arrAreaAD,iLength);
        }
        else
        {
            arrShowAD = arrAreaAD;
            for(var i = 0; i < arrShowAD.length; i++)
            {
                RemoveObj(arrAD,arrShowAD[i]);
            }
            var arrTmp = GetRandomAD(arrAD,iLength - arrShowAD.length);
            for(var i = 0; i < arrTmp.length; i++)
            {
                arrShowAD.push(arrTmp[i]);
            }
        }
    }
	
	arrShowAD.sort(
		function(a,b)
		{
			return b.Weight - a.Weight;
		}
	);
	
	
	var strHtml = "";
	for(var i =0; i < arrShowAD.length; i++)
	{
		var obj = arrShowAD[i];
		var strTitle = decodeURIComponent(obj.Title);
		var strUrl = "";
		if(obj.Type == "3")
		{
		    strUrl = "http://tgadv.tgnet.cn/TgAdvClick.aspx?id=" + obj.ID + escape("&jumpto=" + decodeURIComponent(obj.Url));
		}
		else
		{
		    strUrl = "http://tgadv.tgnet.cn/TgAdvClick.aspx?id=" + obj.ID + "&jumpto=" + escape(decodeURIComponent(obj.Url));
		}
		
		var strPath = decodeURIComponent(obj.Path);
		if(strPath.indexOf("http://") == -1)
		{
		    strPath = "http://img.tgimg.cn" + strPath;
		}
		var strColor = decodeURIComponent(obj.Color);
		if(strColor.length > 0)
		{
		    strColor = " style = 'color:" + strColor + "' ";
		}
		var iWidth = obj.Width;
		var iHeight = obj.Height;
		var size = "";
		if(iWidth != "" && iWidth != "0") size += " width = '" + iWidth + "'";
		if(iHeight != "" && iHeight != "0") size += " height ='" + iHeight + "' ";
		var objID = "div_" + obj.ID;
		arrADObjID.push(objID);
		switch(obj.Type)
		{
			//文字
			case "1":
				strHtml += "<p><a id='" + objID + "' href='" + strUrl + "' target='_blank' title='" + strTitle + "' " + strColor + ">" + strTitle + "</a></p>";
				break;
			//图片
			case "2":
				strHtml += "<div><a href='" + strUrl + "' target='_blank' title='" + strTitle + "'><img id='" + objID + "' src='"+ strPath +"' alt='"+ strTitle +"' " + size + " /></a></div>";
				break;
			//Flash
			case "3":
				strHtml += "<div id='" + objID + "'><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' " + size + " align='middle'>";
				strHtml += "<param name='allowScriptAccess' value='sameDomain' /><param name='bgcolor' value='#ffffff' /><param name='wmode' value='transparent' />";
				strHtml += "<param name='movie' value='" + strPath + "' /><param name='quality' value='high' />";
				strHtml += "<param name='FlashVars' value='u=" + strUrl + "' />";
				strHtml += "<embed src='" + strPath + "' FlashVars='u=" + strUrl + "' quality='high' bgcolor='#ffffff' " + size + " align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' wmode='transparent' /></object></div>";
				break;
			//图片+文字
			case "4":
				strHtml += "<div><a href='" + strUrl + "' target='_blank' title='" + strTitle + "'><img id='" + objID + "' src='"+ strPath +"' alt='"+ strTitle +"'" + size + strColor + "/>" + strTitle + "</a></div>";
				break;
		}
	}
    switch(iShowType){
		case 1 :
		    break;
		case 2 :
		    var strScrollID = "txtScroll";
		    if(arrShowAD.length > 0) strScrollID += arrShowAD[0].ID;
			strHtml = "<div id='" + strScrollID + "'>" + strHtml + "</div><script type='text/javascript'>txtScroll('" + strScrollID + "')</script>";
		    break;
		case 3 :
		    break;
		case 4 :
			var links = [];
			var srcs = [];
			var titles = [];
			var ids = [];
			var width = "";
			var height = "";
			for(var i =0; i < arrShowAD.length; i++){
				var obj = arrShowAD[i];
				var strUrl = "";
		        if(obj.Type == "3")
		        {
		            strUrl = "http://tgadv.tgnet.cn/TgAdvClick.aspx?id=" + obj.ID + escape("&jumpto=" + decodeURIComponent(obj.Url));
		        }
		        else
		        {
		            strUrl = "http://tgadv.tgnet.cn/TgAdvClick.aspx?id=" + obj.ID + "&jumpto=" + escape(decodeURIComponent(obj.Url));
		        }
				links.push(strUrl);
				var strPath = decodeURIComponent(obj.Path);
		        if(strPath.indexOf("http://") == -1)
		        {
		            strPath = "http://img.tgimg.cn" + strPath;
		        }
				srcs.push(strPath);
				var strTitle = decodeURIComponent(obj.Title);
				titles.push(strTitle);
				ids.push("div_" + obj.ID);
				width = obj.Width;
				height = obj.Height;
			}
			strHtml = "<script type='text/javascript'>";
			if(width != "" && width != "0")strHtml += "width = '" + width + "';";
			if(height != "" && height != "0")strHtml += "height = '"+ height +"';";
			strHtml += "links = '"+ links.join("|") +"';";
			strHtml += "srcs = '"+ srcs.join("|") +"';";
			strHtml += "titles = '"+ titles.join("|") +"';";
			strHtml += "ids = '"+ ids.join("|") +"';";
			strHtml += "</"+"script>";
		    break;
	}
	document.write(strHtml);
}

function GetAreaAD(arrAD,strAreaNo)
{
    if(strAreaNo.length == 0) return arrAD;
    var arrAreaAD = new Array();
    for(var i = 0; i < arrAD.length ; i++)
    {
        var obj = arrAD[i];
        if(obj.AreaNoList.length == 0)
        {
            arrAreaAD.push(obj);
        }
        else
        {
            var areaList = decodeURIComponent(obj.AreaNoList).split(",");
            for(var j = 0; j < areaList.length; j++)
            {
                if(strAreaNo.indexOf(areaList[j]) != -1 )
                {
                    arrAreaAD.push(obj);
                    break;
                }
            }
        }
    }
    return arrAreaAD;
}

function GetRandomAD(arrAD,iLength)
{
    if(arrAD.length > iLength * 2)
    {
        var arrShowAD = new Array();
        while(arrShowAD.length < iLength)
        {
            var obj = GetRandomObj(arrAD);
            
            if(obj)
            {
                arrShowAD.push(obj);
                RemoveObj(arrAD,obj);
            }
        }
        return arrShowAD;
    }
    else
    {
        while(arrAD.length > iLength)
        {
            var obj = GetRandomObj(arrAD);
            if(obj)
            {
                RemoveObj(arrAD,obj);
            }
        }
        return arrAD;
    }
}

function GetRandomObj(arrAD)
{
    var iWeight = 0;
    for(var i = 0; i < arrAD.length; i++)
    {
        iWeight += parseInt(arrAD[i].Weight);
    }
    var iRand = Math.random() * iWeight;
    iWeight = 0;
    for(var i = 0; i < arrAD.length; i++)
    {
        iWeight += parseInt(arrAD[i].Weight);
        
        if( iWeight > iRand)
        {
            return arrAD[i];
        }
    }
    return null;
}

function RemoveObj(arr,obj)
{
    var i = 0;
    for(; i < arr.length ; i++)
    {
        if(arr[i].ID == obj.ID)
        {
            break;
        }
    }
    arr.splice(i,1);
}

function isShow(obj){
    if(!Brower.WebKit){
        if(getOffsetTop(obj) - document.documentElement.scrollTop < document.documentElement.clientHeight){
	        return true;
        }
    }
    else {
        if(getOffsetTop(obj) - document.body.scrollTop < document.documentElement.clientHeight){
	        return true;
        }
    }
    return false;
}


function ADStat()
{
    
	var arr = [];
	var auto;
	var speed = 5000;
	var funAuto;
	if(!arrADObjID instanceof Array) return;
	for(var i = 0; i< arrADObjID.length; i++){
		arr.push(document.getElementById(arrADObjID[i]));
	}
	
	for(var i=0; i<arr.length; i++){
		arr[i].isShow = false;
		arr[i].isMouseover = false;
		arr[i].isShowPost = false;
		arr[i].isMouseoverPost = false;
	}
	for(var j = 0; j < arr.length; j++){
		arr[j].onmouseover = function(){
			if(!this.isMouseover)
				this.isMouseover = true;
		}
	}
    getShowElement();

	
	if(Brower.ie){
		document.documentElement.onscroll = getShowElement;
	}
	else if(Brower.opera){
		funAuto = setInterval(getShowElement,10000);
	}
	else{
		document.onscroll = getShowElement;
	}
	
	auto = setInterval(function(){
		getObjShow();
		clear();
	},speed);
	
	
	
	
	function getShowElement(){
		for(var k = 0; k<arr.length; k++){
			if(isShow(arr[k])){
				arr[k].isShow = true;
			}
		}
	}
	
	function getObjShow(){
	    var strShow = "";
	    var strMouseover = "";
		for(var i = 0; i<arr.length; i++){
			if(arr[i].isShow && !arr[i].isShowPost){
			    if(strShow.length > 0) strShow += ",";
			    strShow += arr[i].id.replace("div_","");
			    arr[i].isShowPost = true;
			}
			if(arr[i].isMouseover && !arr[i].isMouseoverPost)
			{
			    if(strMouseover.length > 0) strMouseover += ",";
			    strMouseover += arr[i].id.replace("div_","");
			    arr[i].isMouseoverPost = true;
			}
		}
		Post(strShow,strMouseover);
	}
	
	function checkAllPost(){
		var allPost = true;
		for(var i = 0; i<arr.length; i++){
			if(!arr[i].isMouseoverPost || !arr[i].isShowPost){
				allPost = false;
				break;
			}
		}
		return allPost;
	}
	
	function clear(){
		if(checkAllPost()){
			clearInterval(auto);
			clearInterval(funAuto);
			if(Brower.ie){
		        document.documentElement.onscroll = null;
	        }
	        else {
		        document.onscroll = null;
	        }
			return;
		}
	}
	
	function Post(strShow,strMouseover)
	{
	    if(strShow.length > 0 || strMouseover.length > 0)
		{
		    var strParam = "op=tgadvstat&show=" + strShow + "&mouse=" + strMouseover;
		    var url = "/System/AjaxService.aspx";
		    tgAjax.request({
                url : url,
                params : strParam,
                method : "post" 
            })
		    //$.get(url,{"op":"tgadvstat","show":strShow,"mouse":strMouseover});
		}
	}
}
