﻿// JavaScript Document
/********************************************************************
主题:若干Flash或图片广告(包括流媒体、对联、悬浮以及列表式广告)
作者:Issac
日期:Oct.2005
说明:(*必填)
1. flash参数[对象ID,对象文件*,对象宽度*,对象高度*,是否透明]
2. Pic参数[对象ID,对象文件*,对象链接接地址,对象宽度,对象高度]
3. loadStreamAD(弹出流媒体对象*,悬挂流媒体对象*,影片剪辑实例名,是否可移,弹出次数,是否为日弹)
4. loadCoupletAD(左对联对象*,右对联对象*,是否同时关闭,是否可移)
5. loadFloatAD(悬浮对象*,是否可移,是否为右侧悬浮)
6. loadUlAD(目标容器ID*,对象数组*,组默认宽度,组默认高度)
********************************************************************/
/*基本参数*/
var PS_Id, LS_Id, PS_ClipName;
var isMoveStream, isMoveCouplet, isMoveFloat, floatWhichId;
var cssStr;
var coupletTop = 130;
var lastScrollY = - coupletTop;
var floatBottom = 0;
/*定义样式*/
cssStr = "<style type=\"text/css\">\n";
cssStr += "#leftCoupletBox {display: none; position: absolute; top: "+coupletTop+"px; left: 7px; width: 100px;}\n";
cssStr += "#rightCoupletBox {display: none; position: absolute; top: "+coupletTop+"px; right: 7px; width: 100px;}\n";
cssStr += "#popStreamBox {display: none; position: absolute; top: 50%; left: 50%; z-index: 9999;}\n";
cssStr += "#hangStreamBox {display: none; position: absolute; bottom: "+floatBottom+"px; left: 7px; width: 100px; text-align: center;}\n";
cssStr += "#rightFloatBox {display: none; position: absolute; bottom: "+floatBottom+"px; right: 7px; width: 100px; text-align: center;}\n";
cssStr += "#leftCoupletBox a, #rightCoupletBox a, #hangStreamBox a, #rightFloatBox a {display: block; font-size: 12px; color: #f00; text-decoration: none; line-height: 25px}\n";
cssStr += "#leftCoupletBox a:hover, #rightCoupletBox a:hover, #hangStreamBox a:hover, #rightFloatBox a:hover {display: block; font-size: 12px; color: #f00; text-decoration: underline; line-height: 25px}\n";
cssStr += "</style>\n";
document.writeln(cssStr);
/*定义广告容器层*/
document.writeln("<div id=\"popStreamBox\"></div>");
document.writeln("<div id=\"hangStreamBox\"></div>");
document.writeln("<div id=\"leftCoupletBox\"></div>");
document.writeln("<div id=\"rightCoupletBox\"></div>");
document.writeln("<div id=\"rightFloatBox\"></div>");

/*******弹出窗口*********/
function openWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
/******基本函数******/
/*判断是否为flash*/
function isSwf(oFile) {
	var extension = "."+oFile.split(".")[oFile.split(".").length-1];	//获得其扩展名
	if (extension==".swf" || extension==".SWF")
		return true;
	return false;
}
/*载入Flash*/
function loadSwf(oParameter) {
	var oId = oParameter[0];
	var oFile = oParameter[1];
	var oWidth = oParameter[2];
	var oHeight = oParameter[3];
	var isAlpha = oParameter[4];
	if (isAlpha==null)
		isAlpha = false;
	var str;
	str = "<object";
	str += (oId!=null)?" id=\""+oId+"\" ":" ";
	str += "classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\""+oWidth+"\" height=\""+oHeight+"\">\n";
	str += "<param name=\"movie\" value=\""+oFile+"\">\n";
	str += "<param name=\"quality\" value=\"high\">\n";
	str += "<param value=\"false\" name=\"menu\">\n";
	str += "<param value=\"opaque\" name=\"wmode\">\n";
	str += "<embed src=\""+oFile+"\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+oWidth+"\" height=\""+oHeight+"\"></embed>\n";
	str += (isAlpha)?"<param name=\"wmode\" value=\"transparent\">\n<param name=\"menu\" value=\"false\">\n":"";
	str += "</object>\n";
	return (str);
}
/*载入图片*/
function loadPic(oParameter) {
	if (typeof(oParameter) == "string") {
		var oFile = oParameter;
		var oId = oUrl = oWidth = oHeight = null;
	}
	else {
		oId = oParameter[0];
		oFile = oParameter[1];
		oUrl = oParameter[2];
		oWidth = oParameter[3];
		oHeight = oParameter[4];
	}
	var str;
	str  = (oUrl!=null)?"<a href=\""+oUrl+"\" target=\"_blank\"><img":"<a href=\"javascript:;\"><img";
	str += (oId!=null)?" id=\""+oId+"\" ":" ";
	str += (oFile!=null)?" src=\""+oFile+"\" ":" ";
	str += (oWidth!=null)?" width=\""+oWidth+"\" ":" ";
	str += (oHeight!=null)?" height=\""+oHeight+"\" ":" ";
	str += "border=\"0\" /></a>\n";
	return (str);
}
/*广告容器层控制*/
function swfBoxControl(oId,action) {
	switch (action) {
		case "hide":
			document.getElementById(oId).style.display = "none";
			break;
		case "show":
			document.getElementById(oId).style.display = "block";
			break;
		case "move":
			moveStream();
			moveCouplet();
			moveFloat();
			break;
	}
}
/*JS对Flash播放控制*/
function controlSwf(oId,cmdName,clipName) {
	var o=document.getElementById(oId);
	if (o)
		eval("o."+cmdName+"("+((cmdName=="TStopPlay"||cmdName=="TPlay")?"'"+clipName+"'":"")+")");
}

/******流媒体广告相关******/
/*载入流媒体广告*/
function loadStreamAD(oPopStream, oHangStream, oClipName, isMove, times, isDay) {
	if (isMove==null)
		isMoveStream = true;
	else
		isMoveStream = isMove;
	var LS_AStr = "<a href=\"javascript:streamControl('replay')\">点击重播</a>";
	PS_Id = oPopStream[0];
	LS_Id = oHangStream[0];
	if (oClipName!=null)
		PS_ClipName = oClipName;
	document.getElementById("popStreamBox").innerHTML = loadSwf(oPopStream);
	document.getElementById("hangStreamBox").innerHTML = loadSwf(oHangStream) + LS_AStr;
	var oPopStreamName = oPopStream[1].split("/")[oPopStream[1].split("/").length-1];
	if (streamTimes(oPopStreamName, times, isDay))
		swfBoxControl("popStreamBox","show");
	else {
		streamControl("stop");
	}
	popStreamCss ("popStreamBox",oPopStream[2],oPopStream[3]);
}
/*弹出流媒体样式*/
function popStreamCss (oId,oWidth,oHeight) {
	with (document.getElementById(oId).style){
		height = oHeight+"px";
		width = oWidth+"px";
		marginTop = -0.5 * oHeight + "px";
		marginLeft = -0.5 * oWidth + "px";
	}
}
/*控制流媒体动作*/
function streamControl(action) {
	switch (action) {
		case "stop":
			swfBoxControl("popStreamBox","hide");
			swfBoxControl("hangStreamBox","show");
			if (PS_ClipName!=null)
				controlSwf(PS_Id,"TStopPlay",PS_ClipName);
			else
				controlSwf(PS_Id,"StopPlay");
			break;
		case "replay":
			swfBoxControl("popStreamBox","show");
			swfBoxControl("hangStreamBox","hide");
			if (PS_ClipName!=null)
				controlSwf(PS_Id,"TPlay",PS_ClipName);
			else
				controlSwf(PS_Id,"Rewind");
			break;
		case "close":
			swfBoxControl("popStreamBox","hide");
			swfBoxControl("hangStreamBox","hide");
			if (PS_ClipName!=null)
				controlSwf(PS_Id,"TStopPlay",PS_ClipName);
			else
				controlSwf(PS_Id,"StopPlay");
			break;
	}
}
/*移动流媒体广告*/
function moveStream() {
	if (isMoveStream) {
		document.getElementById("hangStreamBox").style.top=document.body.scrollTop+document.body.clientHeight-document.getElementById("hangStreamBox").offsetHeight-floatBottom;
		return true;
	}
	return false;	
}
/*流媒体播放次数(cookies)*/
function streamTimes(name, times, isDay) {
	if (times==null)
		return true;
	if (isDay==null)
		isDay=true;
	var now = new Date();
	var tomorrow = new Date(now.getYear(),now.getMonth(),now.getDate()+1,0,0,0);
	var time = Math.floor((tomorrow - now) / 1000);
	time = (isDay)?time:1;
	var type = (isDay)?"S":"Y";
	//判断是否为新的流媒体广告,前后两次name不能相同
	if (name!=readCookie("streamName")) {
		delCookie("streamName");
		delCookie("streamTimes");
		writeCookie("streamTimes",0,time,type);
	}
	var count = readCookie("streamTimes");
	count ++;
	writeCookie("streamName",name,time,type);
	writeCookie("streamTimes",count,time,type);
	return (times>=count);
}

/******对联广告相关******/
/*载入对联广告*/
function loadCoupletAD(oLeftCouplet, oRightCouplet, isCloseDouble, isMove) {
	if (isMove==null)
		isMoveCouplet = true;
	else
		isMoveCouplet = isMove;
	var LC_AStr, RC_AStr;
	if (isCloseDouble==true)
		RC_AStr = LC_AStr = "<a href=\"javascript:swfBoxControl('leftCoupletBox','hide');swfBoxControl('rightCoupletBox','hide')\">关闭</a>";
	else {
		LC_AStr = "<a href=\"javascript:swfBoxControl('leftCoupletBox','hide')\">关闭</a>";
		RC_AStr = "<a href=\"javascript:swfBoxControl('rightCoupletBox','hide')\">关闭</a>"
	}
	if(oLeftCouplet!=null) {
		document.getElementById("leftCoupletBox").innerHTML = (isSwf(oLeftCouplet[1]))?(loadSwf(oLeftCouplet)+LC_AStr):(loadPic(oLeftCouplet)+LC_AStr);
		swfBoxControl("leftCoupletBox","show");
	}
	if(oRightCouplet!=null) {
		document.getElementById("rightCoupletBox").innerHTML = (isSwf(oRightCouplet[1]))?(loadSwf(oRightCouplet)+RC_AStr):(loadPic(oRightCouplet)+RC_AStr);
		swfBoxControl("rightCoupletBox","show");
	}
}
/*移动对联广告*/
function moveCouplet(){
	if (isMoveCouplet) {
		var diffY = document.body.scrollTop;
		percent = .1 * (diffY-lastScrollY);
		if (percent>0)
			percent = Math.ceil(percent);
		else 
			percent = Math.floor(percent);
		document.getElementById("leftCoupletBox").style.pixelTop += percent;
		document.getElementById("rightCoupletBox").style.pixelTop += percent;
		lastScrollY=lastScrollY+percent;
		return true;
	}
	return false;
}

/******悬浮广告相关******/
/*载入悬浮广告*/
function loadFloatAD(oFloat, isMove , isRight) {
	isMoveFloat = (isMove==null)?true:isMove;
	floatWhichId = (isRight==false)?"hangStreamBox":"rightFloatBox";
	var RF_AStr = "<a href=\"javascript:swfBoxControl('"+floatWhichId+"','hide')\">关闭</a>";	
	document.getElementById(floatWhichId).innerHTML = (isSwf(oFloat[1]))?(loadSwf(oFloat)+RF_AStr):(loadPic(oFloat)+RF_AStr);
	swfBoxControl(floatWhichId,"show");
}
/*移动悬浮(右侧)广告*/
function moveFloat() {
	if (isMoveFloat) {
		document.getElementById(floatWhichId).style.top=document.body.scrollTop+document.body.clientHeight-document.getElementById(floatWhichId).offsetHeight-floatBottom;
		return true;
	}
	return false;
}

/******列表式广告相关******/
/*载入列表式广告(需要单独样式表配合)*/
function loadUlAD(oId,oArray,dftWidth,dftHeight){
	var o = document.getElementById(oId);
	var str, tmpArray;
	str = "<ul>\n";
	for (var i=0; i<oArray.length; i++) {
		str += "<li>\n";
		if (typeof(oArray[i]) == "string") {
			if (isSwf(oArray[i])) {
				tmpArray = [null,oArray[i],dftWidth,dftHeight];
				str += loadSwf(tmpArray);
			}
			else {
				tmpArray = [null,oArray[i],null,dftWidth,dftHeight]
				str += loadPic(oArray[i]);
			}
		}
		else {
			if (isSwf(oArray[i][1])) {
				if (oArray[i][2]==null)
					oArray[i][2] = dftWidth;
				if (oArray[i][3]==null)
					oArray[i][3] = dftHeight;
				str += loadSwf(oArray[i]);
			}
			else {
				if (oArray[i][3]==null)
					oArray[i][3] = dftWidth;
				if (oArray[i][4]==null)
					oArray[i][4] = dftHeight;
				str += loadPic(oArray[i]);
			}
		}
		str += "</li>\n";
	}
	str += "</ul>\n";
	o.innerHTML = str;
}
//移动广告容器层window.setInterval("swfBoxControl(null,'move')",1);