﻿// JavaScript Document


//divId,广告层ID
//width,广告层宽度
//height,层高度
//left,边框距离
//top,顶部距离
//value,内容
//place,位置："left"-居左；"right"-居右
//turnRight,是否向右移动：true-从左向右移动（默认）；false-从右向左移动
//turnDown,是否向下移动：true-从上向下移动（默认）；false-从下向上移
//step,移动速度：正数，数值越大移动速度越快；正常值为 0-1 之间的小数，默认值0.5
//move,移动方向：plane-水平移动；upright-垂直移动；htincline-斜向移动；默认为空值不移动
//fastness,是否锁定窗口位置：true-锁定广告在窗口可视区漂浮（默认）；false-广告在整个页面漂浮

var FloatAd = function(divId,width,height,left,top,value,place,turnRight,turnDown,step,move,fastness){
	
	this.InstanceName = divId;
	this.width = width || 0;
	this.height = height || 0;
	this.left = left || 0;
	this.top = top || 0;
	this.value = value || 'no info';
	this.place = place || 'left';
	this.step = step || 0.5;
	this.move = move ;
	this.fastness = fastness || true;
	this.turnRight = turnRight || true;
	this.turnDown = turnDown || true;
	//this.Config	 = new Object();
}


FloatAd.prototype.orientation = function(){
	
	var objDiv = document.getElementById(this.InstanceName);
	var objClose = document.getElementById("Close"+this.InstanceName);
	var windowWidth = document.body.clientWidth;//窗口可见宽度
	var scrollLeft = document.body.scrollLeft; 
	var scrollTop = document.body.scrollTop;
	
	var divLeft, divTop, closeLeft, closeTop;
	
	//对联广告定位
	if (this.place == "right"){
		divLeft = scrollLeft + windowWidth - objDiv.offsetWidth - this.left;
	}
	else{
		divLeft = scrollLeft + this.left;
	}
	divTop = scrollTop + this.top;
	
	//关闭按钮定位
	closeLeft = divLeft + objDiv.offsetWidth - objClose.offsetWidth;
	closeTop = divTop;
	
	objDiv.style.top = divTop;
	objDiv.style.left = divLeft;
	objClose.style.top = closeTop;
	objClose.style.left = closeLeft;
	
}



FloatAd.prototype.closeDiv = function(){
	document.getElementById(this.InstanceName).style.display = "none";
	document.getElementById("Close"+this.InstanceName).style.display = "none";
}


FloatAd.prototype.swim = function(){
	
	var objDiv = document.getElementById(this.InstanceName); 
	var objClose = document.getElementById("Close"+this.InstanceName);
	var windowWidth = document.body.clientWidth;//窗口可见宽度
	var windowHeight = document.body.clientHeight;
	var scrollLeft = document.body.scrollLeft; 
	var scrollTop = document.body.scrollTop;
	var R = windowWidth - objDiv.offsetWidth;
	var B = windowHeight - objDiv.offsetHeight;
	var L = T = 0;
	
	var divLeft, divTop, closeLeft, closeTop;
	
	//判断是否固定窗口位置。
	if (!this.fastness){
	    scrollTop=0; 
	    scrollLeft=0;
	    R=document.body.scrollWidth-objDiv.offsetWidth;
	    B=document.body.scrollHeight- objDiv.offsetHeight;
	}
		
	divTop = scrollTop + this.top;
	if (this.place == "right"){divLeft = scrollLeft + windowWidth - objDiv.offsetWidth - this.left;}
	else{divLeft = scrollLeft + this.left;}
	closeLeft = divLeft + objDiv.offsetWidth - objClose.offsetWidth;
	closeTop = divTop;
	
	objDiv.style.left = divLeft;
	objDiv.style.top = divTop;
	objClose.style.left = closeLeft;
	objClose.style.top = closeTop;
	
	switch (this.move){
		
		case "plane":
			this.left = this.left + this.step*(this.turnRight?1:-1);
			if (this.left < L){ this.turnRight = true; this.left = L} 
			if (this.left > R){ this.turnRight = false;this.left = R} 
			break;
			
		case "upright":
			this.top = this.top + this.step*(this.turnDown?1:-1);
			if (this.top < T){ this.turnDown = true; this.top = T } 
			if (this.top > B){ this.turnDown = false; this.top = B } 
			break;
			
		case "incline":
			this.left = this.left + this.step*(this.turnRight?1:-1);
			if (this.left < L){ this.turnRight = true; this.left = L} 
			if (this.left > R){ this.turnRight = false;this.left = R} 
			this.top = this.top + this.step*(this.turnDown?1:-1);
			if (this.top < T){ this.turnDown = true; this.top = T } 
			if (this.top > B){ this.turnDown = false; this.top = B } 
			break;
			
	}
	
}


FloatAd.prototype.printDiv = function(){
	
	document.write("<div id='"+this.InstanceName+"' class='div'>"+this.value+"</div>")
	document.write("<img id='Close"+this.InstanceName+"' width=10 src='images/closeButton.gif' class='closeButton' style='display:none'>")
	
	var objDiv = document.getElementById(this.InstanceName) ;
	var objClose = document.getElementById("Close"+this.InstanceName);
	
	objDiv.style.width = this.width;
	objDiv.style.height = this.height;
	
	objDiv.onmouseover = function(){}
	objClose.onclick = function(){objDiv.style.display='none'; objClose.style.display='none';}
	
}
