/**
 *    浮动广告
 *    var ad = FloatAd.newFloatAd({move:'H', title:"测试标题", strLink:"http://www.baidu.com", content:"测试内容"});
 *    ad.style.left = 100;
 *    .......
 *    FloatAd.newLeftFloatAd({imgUrl:"/1147882113969.gif", strLink:"http://www.baidu.com"}); 
 *    FloatAd.newRightFloatAd({imgUrl:"/1147882113969.gif", strLink:"http://www.baidu.com"}); 
 *	  FloatAd.newFixTopLeftFloatAd({imgUrl:"/1147882113969.gif", strLink:"http://www.baidu.com"}); 
 *	  FloatAd.newFixTopRightFloatAd({imgUrl:"/1147882113969.gif", strLink:"http://www.baidu.com"}); 
 */
var FloatAd = { 
	ads : [], 
	index : 0,
	newFloatAd : function(options){ 
		var ad = document.createElement("A");
		ad.setLeft = function(left){
			this.originalLeft = left || 10;
			this.style.left = this.originalLeft + "px";
		};
		ad.setTop = function(top){
			this.originalTop = top || 10;
			this.style.top = this.originalTop + "px";
		};
		
		ad.fix = options.fix || false;
		ad.dirV = true; 
		ad.dirH = true; 
		ad.autoMove = true; 
		ad.seed = this.index++;
		ad.style.position = "absolute";
		ad.setLeft(options.left || 2);
		ad.setTop(options.top || 200);
		
		ad.move = options.move || 'V' ;/*  H--水平方向 F--自由方向  V--垂直方向   */
		ad.timer = setInterval("FloatAd.float(" + ad.seed + ")", 50);
		this.ads[ad.seed] = ad;
				
		if(options.imgUrl){
			this.createImage(options, ad);
		}else{
			this.createText(options, ad);
		}
				
		if(options.strLink){ 
			ad.href = options.strLink; 
			ad.target = "_blank"; 
		}
		
		this.createClose(ad);
		document.body.appendChild(ad); 
		return ad; 
	},
	
	newLeftFloatAd : function(options){		
		options.left = options.left || 2; 
		options.top = options.top || 200;
		options.move = 'VL';
		return this.newFloatAd(options);		
	},
	
	newRightFloatAd : function(options){		
		options.top = options.top || 200;
		options.move = 'VR';
		var tempAd = this.newFloatAd(options);
		var left = document.documentElement.clientWidth + (document.documentElement.scrollLeft || document.body.scrollLeft) - tempAd.offsetWidth ;
		tempAd.setLeft(left);
		return tempAd;
	},
	
	newBottomLeftFloatAd : function(options){
		var tempAd = this.newLeftFloatAd(options);
		var top = document.documentElement.clientHeight + (document.documentElement.scrollTop || document.body.scrollTop)- tempAd.offsetHeight - 11; 
		tempAd.setTop(top);
		return tempAd;
	},
	
	newBottomRightFloatAd : function(options){
		var tempAd = this.newRightFloatAd(options);
		var top = document.documentElement.clientHeight + (document.documentElement.scrollTop || document.body.scrollTop)- tempAd.offsetHeight - 11; 
		tempAd.setTop(top);
		return tempAd;
	},
	
	newFixTopLeftFloatAd :  function(options){
		options = options || {};
		options.fix = true;	
		var tempAd = this.newLeftFloatAd(options);
		tempAd.autoMove = false;
		return tempAd;
	},
	
	newFixTopRightFloatAd :  function(options){
		options = options || {};
		options.fix = true;		
		var tempAd = this.newRightFloatAd(options);
		tempAd.autoMove = false;
		return tempAd;
	},
	
	newFixBottomLeftFloatAd :  function(options){
		options = options || {};
		options.fix = true;			
		var tempAd = this.newBottomLeftFloatAd(options);
		tempAd.autoMove = false;
		return tempAd;
	},
	
	newFixBottomRightFloatAd :  function(options){
		options = options || {};
		options.fix = true;		
		var tempAd = this.newBottomRightFloatAd(options);
		tempAd.autoMove = false;
		return tempAd;
	},
	
	
	createImage : function(options, ad){
		ad.image = new Image(); 
		ad.image.parent = ad; 
		ad.image.src = options.imgUrl;
		if(!ad.fix){
			ad.image.onmouseover = function(){this.parent.autoMove = false;} 
			ad.image.onmouseout = function(){this.parent.autoMove = true;}
		} 
		ad.image.border = 0; 
		ad.appendChild(ad.image); 
	},
	
	createText : function(options, ad){

		ad.text = document.createElement("DIV");
		ad.text.style = "z-index:1000;";
		ad.text.parent = ad;
		if(options.title){
			var titleDiv = document.createElement("DIV");
			titleDiv.style = "z-index:1000;";
			titleDiv.innerHTML = "<span class='floatad_title'>" + options.title + "</span>";
			ad.text.appendChild(titleDiv);
		}
		var contentDiv = document.createElement("DIV");
		contentDiv.style = "z-index:1000;";
		contentDiv.innerHTML = "<span class='floatad_content'>" + options.content + "</span>";
		ad.text.appendChild(contentDiv);
		
		ad.text.onmouseover = function(){this.parent.autoMove = false;} 
		ad.text.onmouseout = function(){this.parent.autoMove = true;} 
		ad.appendChild(ad.text); 
	},
	
	createClose : function(ad){
		ad.close = document.createElement("DIV");
		ad.close.parent = ad;
		ad.close.className = "floatad_close";
		ad.close.innerHTML = "<a href='javascript:FloatAd.close(" + ad.seed + ");'>关闭</a>";
		ad.close.onmouseover = function(){this.parent.autoMove = false;} 
		ad.close.onmouseout = function(){this.parent.autoMove = true;} 
		ad.appendChild(ad.close); 
	},
	
	close : function(floatId){
		var ad = this.ads[floatId];
		ad.style.display = "none";
		window.clearInterval(ad.timer);
	},
	
	float : function(floatId){ 
		var ad = this.ads[floatId];
		var curLeft = parseInt(ad.style.left); 
		var curTop = parseInt(ad.style.top);
		
		var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
		var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
		if(ad.originalScrollLeft != scrollLeft){
			ad.originalScrollLeft = scrollLeft;
			curLeft = scrollLeft + ad.originalLeft;
		}
		if(ad.originalScrollTop != scrollTop){
			ad.originalScrollTop = scrollTop;
			curTop = scrollTop + ad.originalTop;
		}
		
		var screenWidth = document.documentElement.clientWidth;
		var screenHeight = document.documentElement.clientHeight;
	
		if(ad.offsetWidth + curLeft > screenWidth + scrollLeft - 1){ 
			curLeft = scrollLeft + screenWidth - ad.offsetWidth; 
			ad.dirH = false; 
		} 
		if(ad.offsetHeight + curTop > screenHeight + scrollTop - 1){ 
			curTop = scrollTop + screenHeight - ad.offsetHeight; 
			ad.dirV = false; 
		} 
		if(curLeft < scrollLeft){ 
			curLeft = scrollLeft; 
			ad.dirH = true; 
		} 
		if(curTop < scrollTop){ 
			curTop = scrollTop; 
			ad.dirV = true; 
		}
		
		if(ad.autoMove){
			if(ad.move == 'F' || ad.move == 'H'){
				curLeft = curLeft + (ad.dirH ? 1 : -1); 
			}
			if(ad.move == 'F' || ad.move == 'VL' || ad.move == 'VR'){
				curTop = curTop + (ad.dirV ? 1 : -1);
			}
		}
		
		ad.style.left = curLeft + "px";
		ad.style.top =  curTop + "px";
	}
}