var showad = true;
var Toppx = 60;//上端位置
var winWidth; //窗口宽度
var PageWidth;//由SessionMgr输出，在此声明防止已生成静态页报错
//var MinScreenW = 0; //显示广告的最小屏幕宽度象素 
function scall() {
    //获取窗口宽度
    if (window.innerWidth) {
        winWidth = window.innerWidth;
    }
    else if ((document.body) && (document.body.clientWidth)) {
        winWidth = document.body.clientWidth;
    }
    /*nasty hack to deal with doctype swith in IE*/
    //通过深入Document内部对body进行检测，获取窗口大小
    if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
        winHeight = document.documentElement.clientHeight;
        winWidth = document.documentElement.clientWidth;
    }
    if (winWidth < AdDivW * 2) {
        document.getElementById("Javascript.LeftDiv").style.display = "none";
        document.getElementById("Javascript.RightDiv").style.display = "none";
        return;
    }
    if (!showad) { return; }
    if (winWidth > PageWidth + 2 * AdDivW) {
        Borderpx = ((winWidth - PageWidth) / 2 - AdDivW) / 2;
    }
    else {
        Borderpx = 0
    }
    if (l == "top" || r == "top") {
        document.getElementById("Javascript.LeftDiv").style.display = "";
        document.getElementById("Javascript.LeftDiv").style.top = document.body.scrollTop + Toppx;
        document.getElementById("Javascript.LeftDiv").style.left = document.body.scrollLeft + Borderpx;
        document.getElementById("Javascript.RightDiv").style.display = "";
        document.getElementById("Javascript.RightDiv").style.top = document.body.scrollTop + Toppx;
        document.getElementById("Javascript.RightDiv").style.left = document.body.scrollLeft + document.body.clientWidth - document.getElementById("Javascript.RightDiv").offsetWidth - Borderpx;
    }
    else {
        document.getElementById("Javascript.LeftDiv").style.display = "";
        document.getElementById("Javascript.LeftDiv").style.top = document.body.scrollTop + (document.documentElement.offsetHeight - AdDivH - Toppx);
        document.getElementById("Javascript.LeftDiv").style.left = document.body.scrollLeft + Borderpx;
        document.getElementById("Javascript.RightDiv").style.display = "";
        document.getElementById("Javascript.RightDiv").style.top = document.body.scrollTop + (document.documentElement.offsetHeight - AdDivH - Toppx);
        document.getElementById("Javascript.RightDiv").style.left = document.body.scrollLeft + document.body.clientWidth - document.getElementById("Javascript.RightDiv").offsetWidth - Borderpx;
    }
}

function hidead() {
    showad = false;
    document.getElementById("Javascript.LeftDiv").style.display = "none";
    document.getElementById("Javascript.RightDiv").style.display = "none";
}

//---------------------------------------------------------------------------------------------------------生成静态页面相关方法  李延杰	2010.2.8
//不需要传入参数的初始化js参数
var isDissertation = false;//是否专题页面
var flag = false;//是否浮动广告
var left = "";//左侧对联广告代码
var right = "";//右侧对联广告代码
var isShowPopAD = true;//是否显示弹出广告
var width = 0;
var height = 60;

//需要在具体调用时传入值的一些参数
var thisStructID;//栏目StructID
var thisCategoryType;//目录类型

//获取本页面的的一些参数信息
function GatThisPammers()
{
	var isShowADInThisPage = true;
	if(thisStructID != 0)
	{//StructID不为0，二级或三级页面
		if(thisCategoryType == 7)
		{//专题
			isDissertation = true;
			isShowPopAD = true;
		}
	}

	if(thisStructID == 0)
	{//是Default页
		isShowPopAD = true;
	}
	else
	{//二、三级页面
		if( isDefaultADOnly && !isDissertation)
		{//只在首页显示广告，并且当前不是专题
			isShowADInThisPage = false;
		}
	}

	return isShowADInThisPage;
}

//获取本页面的的广告数组列表
function GetThisArrayDT()
{
	if(isDissertation)
	{//专题
		dtArrayThis = RemoveArrayRow(dtArrayThis,thisStructID);
	}
	else
	{//其他页面
		dtArrayThis = RemoveArrayRow(dtArrayThis,0);
	}
	if(isDissertation && (dtArrayThis.length == 0) && !isDefaultADOnly)
	{//专题未设置广告，且全部页面显示广告
		dtArrayThis = dtArrayAll;
		dtArrayThis = RemoveArrayRow(dtArrayThis,0);
	}
}
//移除数组中不符合条件的行
function RemoveArrayRow(dtArrayTemp, valueTemp)
{
	for(var i=0;i<dtArrayTemp.length;i++)
	{
		if( dtArrayTemp[i][12] != valueTemp )
		{
			dtArrayTemp.splice(i,1);
			dtArrayTemp = RemoveArrayRow(dtArrayTemp, valueTemp);
		}
	}
	return dtArrayTemp;
}
//获取单条广告详细html
function GetADDetail(drArray)
{
	var detail = "";
	switch(parseInt(drArray[3]))//遍历广告类型 Type
	{
			//图片广告
		case 1:
			if(drArray[4] != "")
			{//有链接
				detail += "<a href='";
				detail += drArray[4];//Link
				detail += "'target=_blank><img src=\"../..";
				detail += drArray[2]+"\"";//ADFile
				//detail += "\" alt=\"";
				//detail += drArray[5]+"\"";//Word
				detail += " border=\"0\" width=\"";
				detail += drArray[7];//Width
				detail += "\"></a>";
			}
			else
			{//无链接
				detail += "<img src=\"../..";
				detail += drArray[2]+"\"";//ADFile
				//detail += "\" alt=\"";
				//detail += drArray[5]+"\"";//Word
				detail += " border=\"0\" width=\"";
				detail += drArray[7];//Width
				detail += "\">";
			}
			break;
			//Flash广告
		case 2:
			detail += "<EMBED src='../..";
			detail += drArray[2];//ADFile
			detail += "' quality=high  WIDTH='";
			detail += drArray[7];//Width
			detail += "' HEIGHT='";
			detail += drArray[8];//Height
			detail += "' TYPE='application/x-shockwave-flash'></EMBED>";
			break;
			//文字广告
		case 0:
			if(drArray[4] != "")
			{//有链接
				detail += "<a href='";
				detail += drArray[4];//Link
				detail += "' target=_blank>";
				detail += drArray[5];//Word
				detail += "</a>";
			}
			else
			{//无链接
				detail += drArray[5];//Word
			}
			break;
	}
	return detail;
}
//获取列表中所有广告html代码
function GetADHtmlStr()
{
	var result = "";
	for(var i=0;i<dtArrayThis.length;i++)
	{//遍历广告列表
		switch(parseInt(dtArrayThis[i][11]))
		{//遍历广告显示类型 DisplayType
				//浮动广告
			case 5:
				result += '<div id=\"flyads\" onmouseover=stopme(\"flyads\"); onmouseout=movechip(\"flyads\"); style=\"height:49px;left:178px;position:absolute;top:1237px;width:70px; z-index:1000\">';
				//result += '<div id=\"flyadsinner\" style=\"display:none;\"><a href=\"javascript:;\" onclick=\"hideflayad();return false;\" style=\"color:red;text-decoration:none;font-size:12px;\">'+closeStr+'</a></div>'; //浮动广告不显示关闭按钮
				result += '<div id=\"flyadsinner\" style=\"visibility:hidden;\"><a href=\"javascript:;\" onclick=\"hideflayad();return false;\" style=\"color:red;text-decoration:none;font-size:12px;\">'+closeChar+'</a></div>';
				result += GetADDetail(dtArrayThis[i]);
				result += '</div>';
				result += '<script language=javascript>flyads();</scr'+'ipt>';
				break;
				//对联左侧顶部
			case 1:
				if(parseInt(dtArrayThis[i][13]) == 0)
				{//如果关闭按钮CloseBtnPosition为默认值
					left += '<div id=\"Javascript.LeftDiv\" style=\"position: absolute;z-index:1000;width:';
					left += dtArrayThis[i][7];//Width
					left += 'px;top:-1000px;word-break:break-all;display:none;\">';
					left += closeStr;//closeStr
					left += '<br>';
					left += GetADDetail(dtArrayThis[i]);
					left += '</div>';
				}
				else
				{
					left += '<div id=\"Javascript.LeftDiv\" style=\"position: absolute;z-index:1000;text-align:right;width:';
					left += dtArrayThis[i][7];//Width
					left += 'px;top:-1000px;word-break:break-all;display:none;\">';
					left += GetADDetail(dtArrayThis[i]);//GetADDetail(dr)
					left += '<br>';
					left += closeStr;//closeStr
					left += '</div>';
				}
				left += '<script language=javascript>l = \"top\";</scr'+'ipt>';
				flag = true;
				if( parseInt(dtArrayThis[i][7]) > width)
				{//最大宽度变化
					width = parseInt(dtArrayThis[i][7]);
				}
				if(parseInt(dtArrayThis[i][8]) > height)
				{//最大高度变化
					height = parseInt(dtArrayThis[i][8]);
				}
				break;
				//对联左侧底部
			case 3:
				if(parseInt(dtArrayThis[i][13]) == 0)
				{//如果关闭按钮CloseBtnPosition为默认值
					left += '<div id=\"Javascript.LeftDiv\" style=\"position: absolute;z-index:1000;width:';
					left += dtArrayThis[i][7];//Width
					left += 'px;top:-1000px;word-break:break-all;display:none;\">';
					left += closeStr;//closeStr
					left += '<br>';
					left += GetADDetail(dtArrayThis[i]);//GetADDetail(dr)
					left += '</div>';
				}
				else
				{
					left += '<div id=\"Javascript.LeftDiv\" style=\"position: absolute;z-index:1000;text-align:right;width:';
					left += dtArrayThis[i][7];//Width
					left += 'px;top:-1000px;word-break:break-all;display:none;\">';
					left += GetADDetail(dtArrayThis[i]);//GetADDetail(dr)
					left += '<br>';
					left += closeStr;//closeStr
					left += '</div>';
				}
				left += '<script language=javascript>l = \"bottom\";</scr'+'ipt>';
				flag = true;
				if( parseInt(dtArrayThis[i][7]) > width)
				{//最大宽度变化
					width = parseInt(dtArrayThis[i][7]);
				}
				if(parseInt(dtArrayThis[i][8]) > height)
				{//最大高度变化
					height = parseInt(dtArrayThis[i][8]);
				}
				break;
				//对联右侧顶部
			case 2:
				if(parseInt(dtArrayThis[i][13]) == 0)
				{//如果关闭按钮CloseBtnPosition为默认值
					right += '<div id=\"Javascript.RightDiv\" style=\"position: absolute;z-index:1000;width:';
					right += dtArrayThis[i][7];//Width
					right += 'px;top:-1000px;word-break:break-all;display:none;\">';
					right += closeStr;//closeStr
					right += '<br>';
					right += GetADDetail(dtArrayThis[i]);//GetADDetail(dr)
					right += '</div>';
				}
				else
				{
					right += '<div id=\"Javascript.RightDiv\" style=\"position: absolute;z-index:1000;text-align:right;width:';
					right += dtArrayThis[i][7];//Width
					right += 'px;top:-1000px;word-break:break-all;display:none;\">';
					right += GetADDetail(dtArrayThis[i]);//GetADDetail(dr)
					right += '<br>';
					right += closeStr;//closeStr
					right += '</div>';
				}
				right += '<script language=javascript>r = \"top\";</scr'+'ipt>';
				flag = true;
				if( parseInt(dtArrayThis[i][7]) > width)
				{//最大宽度变化
					width = parseInt(dtArrayThis[i][7]);
				}
				if(parseInt(dtArrayThis[i][8]) > height)
				{//最大高度变化
					height = parseInt(dtArrayThis[i][8]);
				}
				break;
				//对联右侧底部
			case 4:
				if(parseInt(dtArrayThis[i][13]) == 0)
				{//如果关闭按钮CloseBtnPosition为默认值
					right += '<div id=\"Javascript.RightDiv\" style=\"position: absolute;z-index:1000;width:';
					right += dtArrayThis[i][7];//Width
					right += 'px;top:-1000px;word-break:break-all;display:none;\">';
					right += closeStr;//closeStr
					right += '<br>';
					right += GetADDetail(dtArrayThis[i]);//GetADDetail(dr)
					right += '</div>';
				}
				else
				{
					right += '<div id=\"Javascript.RightDiv\" style=\"position: absolute;z-index:1000;text-align:right;width:';
					right += dtArrayThis[i][7];//Width
					right += 'px;top:-1000px;word-break:break-all;display:none;\">';
					right += GetADDetail(dtArrayThis[i]);//GetADDetail(dr)
					right += '<br>';
					right += closeStr;//closeStr
					right += '</div>';
				}
				right += '<script language=javascript>r = \"bottom\";</scr'+'ipt>';
				flag = true;
				if( parseInt(dtArrayThis[i][7]) > width)
				{//最大宽度变化
					width = parseInt(dtArrayThis[i][7]);
				}
				if(parseInt(dtArrayThis[i][8]) > height)
				{//最大高度变化
					height = parseInt(dtArrayThis[i][8]);
				}
				break;
				//弹出窗口广告
			case 6:
				if(isShowPopAD)
				{//首页弹出广告
					//result += '<script>window.open ("../../frm/popup.aspx?adid=' + dtArrayThis[i][0] + '", "newwindow", "height=' + dtArrayThis[i][8] + ', width=' + dtArrayThis[i][7] + ', top=0, left=0, toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, status=no")</scr' + 'ipt>';
					result += '<script language=javascript>var hello = \'<html><body>';
					result += GetADDetail(dtArrayThis[i]);
					result += '</body></html>\';var newWindow=window.open();newWindow.document.open("text/html");newWindow.document.write(hello);newWindow.document.close();</scr' + 'ipt>';
				}
				break;
		}
	}
	return result;
}
//获取对联广告辅助显示代码
function getFlagHtml()
{
	var flagHtml = "";
	flagHtml += "<script language=javascript>var AdDivW = "+ width +"; var AdDivH = "+ height +";</scr"+"ipt>";
	if(left == "")
	{//无左侧
		flagHtml += '<div id=\"Javascript.LeftDiv\" style=\"position: absolute;z-index:1000;width:0px;height:0px;top:-1000px;word-break:break-all;display:none;\"><div></div></div>';
	}
	else
	{//有左侧
		flagHtml += left ;
		//flagHtml += "<script language=javascript>AdDivH = document.getElementById(\"Javascript.LeftDiv\").offsetHeight + 60;</scr"+"ipt>";
		flagHtml += "<script language=javascript>AdDivH = document.getElementById(\"Javascript.LeftDiv\").offsetHeight + " + height + ";</scr"+"ipt>";
	}
	if(right == "")
	{//无右侧
		flagHtml += '<div id=\"Javascript.RightDiv\" style=\"position: absolute;z-index:1000;width:0px;height:0px;top:-1000px;word-break:break-all;display:none;\"><div></div></div>';
	}
	else
	{//有右侧
		flagHtml += right ;
		//flagHtml += "<script language=javascript>AdDivH = document.getElementById(\"Javascript.RightDiv\").offsetHeight + 60;</scr"+"ipt>";
		flagHtml += "<script language=javascript>AdDivH = document.getElementById(\"Javascript.RightDiv\").offsetHeight + " + height + ";</scr"+"ipt>";
	}
	flagHtml += "<script language=javascript>window.attachEvent('onscroll',scall);window.attachEvent('onresize',scall);window.attachEvent('onload',scall);</scr"+"ipt>";
	return flagHtml;
}
//获取广告JS页面代码，对外提供的总出口
function showAD(pmStructID, pmCategoryType)
{
	thisStructID = pmStructID;//栏目StructID //传入
	thisCategoryType = pmCategoryType;//目录类型 con.fdCategoryType //传入
	var returnADHTML = "";
	if(GatThisPammers())
	{//如果当前页被设置为显示广告，则继续显示，否则返回空
		GetThisArrayDT();
		if(dtArrayThis.length > 0)
		{//有广告
			returnADHTML += "<script language=javascript>var l;var r;var PageWidth = " + PageWidth + ";</scr" + "ipt>";
			returnADHTML += GetADHtmlStr();
			
			if(flag)
			{//对联
				returnADHTML += getFlagHtml();
			}
		}
	}
	return returnADHTML;
}

