﻿
$(document).ready(function(){
	
	var imgSrc = "js/images/wygxx.jpg",  //图片地址
	    imgWidth = 100,  //图片宽度
		imgHeight = 300,  //图片高度
	    href = "http://www.ahpc.gov.cn/hd_show.jsp?lid=15800&name=%E9%A2%86%E5%AF%BC%E4%BF%A1%E7%AE%B1 ";  //连接地址
	var adDiv = document.createElement("div");
	adDiv.style.position = "absolute";
	adDiv.style.top = "50px";
	adDiv.style.filter = "alpha(opacity=0)";
	adDiv.style.opacity = 0;
	adDiv.id = "sideAd";
	adDiv.innerHTML = "<a href=\""+href+"\" target=\"_blank\"><img src=\""+imgSrc+"\" width=\""+imgWidth+"\" height=\""+imgHeight+"\" border=\"0\"/></a><span style=\"display:block;text-align:center;cursor:pointer;\" onclick=\"this.parentNode.style.display='none'\">关闭</span>";
	document.getElementsByTagName("body")[0].appendChild(adDiv);
	window.setTimeout(function(){new AHEIC.lianb.SideAd("sideAd","news")},2000);
});


/*
  名称：图片漂浮代码
  作者：lianbin
  版本：1.0
*/
$(document).ready(function(){	
    ad.init().start(ad);  //注释掉该行即可关闭漂浮
})
var ad={
	/*更换漂浮内容请修改下面4句*/
	imgUrl:"piao/piao.jpg",   //图片地址
	url:"http://www.ahpc.gov.cn/zxft/zxftswg2012.jsp",   //访问地址
	imgWidth:300,  //图片宽度
	imgHeight:60,    //图片高度
	/*************************************************/
	step:3, //步长
	time:100,  //移动时间间隔，单位毫秒
	id:"ad",
	xmin:true,  //true向右移动，false向左移动
	ymin:true,   //true向下移动，false向上移动
	width:0,
	height:0,
	interval:null,
	initWidthAndHeight:function(){
		this.width=$(window).width();
		this.height=$(window).height();
		this.imgHeight+=20;
		this.imgWidth+=5;
	},
	init:function(){
		var div=null,
		    img=null,
			self=this;
		this.initWidthAndHeight();
		img="<a href=\""+this.url+"\" target=\"_blank\"><img src=\""+this.imgUrl+"\" border=\"0\"/></a><br/><a href=\"javascript:ad.remove();\">关闭</a>";
		div=document.createElement("div");
		div.setAttribute("id",this.id);
		div.style.position="absolute";
		div.style.top="0px";
		div.style.left="0px";
		div.style.width=this.imgWidth+"px";
		div.style.height=this.imgHeight+"px";
		div.style.zIndex=5;
		div.onmouseover=function(){self.stop(self);}
		div.onmouseout=function(){self.start(self);}
		$(div).append(img);
		$("body").append(div);
		return this;	
	},
	piao:function(){
		var scrollTop=$(document).scrollTop(),
		    x=$("#ad").offset().left,
		    y=$("#ad").offset().top;
			if(x<0){
				 this.xmin=true;
				 x=0;
			}else if(x>this.width-this.imgWidth){
				this.xmin=false;
				x=this.width-this.imgWidth;
			}
			if(y<scrollTop){
				this.ymin=true;
				y=scrollTop;
			}else if(y>scrollTop+this.height-this.imgHeight){
				this.ymin=false;
				y=scrollTop+this.height-this.imgHeight;
			}
			if(this.xmin) x+=this.step;
			else x-=this.step;
			if(this.ymin) y+=this.step;
			else y-=this.step;
	        $("#"+this.id).css({top:y+"px",left:x+"px"})
	},
	start:function(self){
		self.interval=setInterval("ad.piao()",self.time);
	},
	stop:function(self){
		clearInterval(self.interval);
	},
	remove:function(){
		$("#"+this.id).remove();
	}
};

var Namespace = new Object();
Namespace.register = function(path) {
	var arr = path.split(".");
	var ns = "";
	for ( var i = 0; i < arr.length; i++) {
	   if (i > 0)
		ns += ".";
	   ns += arr[i];
	   eval("if(typeof(" + ns + ") == 'undefined') " + ns + " = new Object();");
	}
}
Namespace.register("AHEIC.lianb.SideAd");
AHEIC.lianb.Class = { 
    create: function() { 
        return function() { 
            this.initialize.apply(this , arguments); 
        } 
    } 
}
AHEIC.lianb.SideAd = AHEIC.lianb.Class.create();
AHEIC.lianb.SideAd.prototype = {
	initialize:function(id,targetId){
		this.obj = document.getElementById(id);
		this.obj.style.position = "absolute";
		this.obj.style.zIndex = 100; 
		this.target = document.getElementById(targetId);
		this.speed = 0;
		this.timer = null;
	    this.obj.style.top = $(this.obj).offset().top + $(document).scrollTop() + "px";
		var self = this;
		self.resize(self)();
		$(window).resize(self.resize(self));
	    $(this.obj).fadeTo(1000,1,function(){
			//$(window).scroll(self.scrollEvent(self)); 
			self.timer = window.setInterval(self.goTime(self),35);
	    });
	},
	goTime:function(self){
		return function(){
			var obj = self.obj,
				top = $(obj).offset().top,   
				//target = $(window).height() + $(document).scrollTop() - obj.offsetHeight;
				target = 500;
			self.speed += 3;
			top += self.speed;
			
			if(top > target)
			{
				top = target;
				self.speed *= -0.7;
			}
		
			if(top===target && Math.abs(self.speed) < 3)
			{
				clearInterval(self.timer);
				self.timer = null;
				obj.style.top = target + "px";
			}
			obj.style.top = top + "px";
        }
	},
	scrollEvent:function(self){
			if(self.timer == null){
				self.timer = window.setInterval(self.goTime(self),35);
			}else{
				window.clearInterval(self.timer);
				self.timer = window.setInterval(self.goTime(self),35);
			}
    },
	resize:function(self){
		return function(){
		    self.obj.style.left = $(self.target).offset().left + self.target.offsetWidth + "px";
		}
	}
}
