(function($){
	
	Advertising=function(type){
		var array = this.array = [];
		this.add=function(cf, p){//title, href, imagepath, width, height, position
			var o = {
				"float" : true,
				floatType : null,
				position : "left,out", //inner
				relative : p || "body",
				top : null,
				bottom : null,
				left : null,
				right : null
			};
			if(cf){
				for(var s in cf){
					o[s]=cf[s];
				}
			}
			/*o["title"]=title;
			o["href"]=href;
			o["imagepath"]=imagepath;
			o["width"]=width;
			o["height"]=height;*/
			array.push(o);
		}
		var has=function(str,str2){
			return str.indexOf(str2)>=0;
		}
		this.init=function(){
			$.each(array,function(k,v){
				var str = "<div class='advertising'>";
				str+="<div class='close'></div>";
				str+="<a href='{href}' target='_blank' title='{title}'>";
				str+="<img src='{imagepath}' width='{width}' height='{height}' alt='{title}' />";
				str+="</a>";
				str+="</div>";
				str=str.replace("{href}",v["href"]).replace("{title}",v["title"]).replace("{title}",v["title"]).replace("{imagepath}",v["imagepath"])
				str=str.replace("{width}",v["width"]).replace("{height}",v["height"]);
				var gg = $(str);
				var relativeObj = $(v["relative"] || document);
				relativeObj.append(gg);
				var top = v["top"];
				var left = v["left"];
				var right = v["right"];
				var bottom = v["bottom"];
				var position = v["position"];
				var width = v["width"];
				var height = v["height"];
				if(top!==null){
					gg.css("top",top);
				}
				if(bottom!==null){
					gg.css("bottom",bottom);
				}
				if(right!==null){
					gg.css("right",right);
				}
				if(left!==null){
					gg.css("left",left);
				}
				if(has(position,"left") && has(position,"out")){
					gg.css("left",-width);
				}
				if(has(position,"right") && has(position,"out")){
					gg.css("right",-width);
				}
				if(has(position,"left") && has(position,"inner")){
					gg.css("left",0);
				}
				if(has(position,"right") && has(position,"inner")){
					gg.css("right",0);
				}
				var dtop = gg.css("top");
				dtop=Number(dtop.substring(0,dtop.length-2));
				gg.data("defaultTop",dtop);
				v["obj"]=gg;
				v["relativeObj"]=relativeObj;
				Advertising.record(gg);
			});
			this.execute();
		}
	}
	Advertising.list=[];
	Advertising.record=function(obj){
		Advertising.list.push(obj);
	}
	Advertising.prototype.execute=function(){
		var array=this.array;
		function scrollAd(){
			$.each(array,function(k,v){
				var of = v["relativeObj"].offset();
				var scrollTop = $(document).scrollTop();
				var obj = $(v["obj"]);
				/*if(scrollTop<of.top){
					scrollTop=0;
				}else{
					scrollTop=scrollTop-of.top;
				}*/
				scrollTop=scrollTop+obj.data("defaultTop");
				//$(obj).offset().top
				var top = scrollTop;
				obj.animate({top:top},{duration:800,queue:false});
			});
		} 
		scrollAd();
		$(window).scroll(scrollAd);
	}
	
	$(document).ready(function(){
		//$(document).append($("<div id='container_advertising'></div>"));
		/*
		var str = "<div class='advertising'>";
		str+="<div class='close'></div>";
		str+="<a href='{href}' target='_blank' title='{title}'>";
		str+="<img src='{imagepath}' width='{width}' height='{height}' alt='{title}' />";
		str+="</a>";
		str+="</div>";
		str=str.replace("{href}","#").replace("{title}","广告").replace("{title}","广告").replace("{imagepath}","images/jpg/left_gg.jpg")
		str=str.replace("{width}","100").replace("{height}","350");
		var gg = $(str);
		$("#main").append(gg);
		gg.css("top","0px").css("left","-110px");
		*/
		
		/*
		var adver = new Advertising();
		adver.add({
			title : "广告", href : "#", imagepath : "images/jpg/left_gg.jpg", width : 100, height : 250, position : "left,out", top : 0
		});
		adver.add({
			title : "广告", href : "#", imagepath : "images/jpg/left_gg.jpg", width : 100, height : 250, position : "right,out", top : 0
		});
		adver.add({
			title : "广告", href : "#", imagepath : "images/jpg/left_gg.jpg", width : 100, height : 250, position : "right,out", top : 300
		});
		adver.add({
			title : "广告", href : "#", imagepath : "images/jpg/left_gg.jpg", width : 100, height : 250, position : "left,inner", top : 300
		});
		adver.init();
		*/
		(function(){
			$(".advertising").each(function(){
				var o = $(this);
				var dtop = o.css("top");
				dtop=Number(dtop.substring(0,dtop.length-2));
				o.data("defaultTop",dtop);
			});
			$(window).scroll(function(k,v){
				var scrollTop = $(document).scrollTop();
				$(".advertising").each(function(){
					var obj = $(this);
					var top=scrollTop+obj.data("defaultTop");
					obj.animate({top:top},{duration:800,queue:false});
				});
			});
		})();
		
		$(".advertising .close").click(function(){
			$(this).parent().hide();
		});
		
	});
})(jQuery);
