﻿var Each=function (a,fn){for(var i=0;i<a.length;i++)fn.call(a[i],i,a)}
var UIs=function (tag){return this.getElementsByTagName(tag)};
var $=function(id){return typeof(id)=="string"?document.getElementById(id):id;}
function v_bannerportals(offsetTop,rate){
	this.rate=rate==0?0:(rate||20);
	this.offsetTop=offsetTop||100;
	this.css();
}
v_bannerportals.prototype={
	regMedia:function(position,type,target,media,parentElement){
		var t1=this._template(type)
		t1=t1.replace("{$target}",target);
		t1=t1.replace("{$img}",media);
		t1=t1.replace("{$closeId}","v_bannerportals_close_"+position);
		var div=document.createElement("div");
		div.id="v_bannerportals_"+position;
		div.style.display="none";
		div.style.position="absolute";
		div.style.top="100px";
		div.innerHTML=t1;
		div.className="v_bannerportals"
		$(parentElement)?$(parentElement).appendChild(div):document.body.appendChild(div);
		var $this=this;
		$("v_bannerportals_close_"+position).onclick=function(){$this.close();}
	},
	_template:function(type){
			var hm="";
			switch(type){
				case "img":
					hm='\
						<div><a href="javascript:void(0)" id="{$closeId}" class="v_bannerportals_close" title="关闭"></a><a href="{$target}" target="_blank"><img src="{$img}"/></a></div>\
					';
				break;
				case "flash":
					break;
				default:
					hm='\
						<div><a href="javascript:void(0)" id="{$closeId}" class="v_bannerportals_close" title="关闭"></a><a href="{$target}" target="_blank"><img src="{$img}"/></a></div>\
					';
					break;
			}
		return hm;
	},
	css:function(cssText){
		this.addCSS(cssText||"\
			.v_bannerportals a img{border-width:0px;}\
			.v_bannerportals div{position:relative;}\
			.v_bannerportals div a.v_bannerportals_close{position:absolute;right:3px;top:3px;display:block;text-decoration:none;background:url(/images/v_bannerportals_dj1.gif) no-repeat;width:15px;height:15px;border-width:0px;}\
			.v_bannerportals div a.v_bannerportals_close:hover{background:url(/images/v_bannerportals_dj2.gif) no-repeat;}\
		");
	},
	addCSS:function(css){//author:果果
		var D=document,$=D.createElement('style');
		$.setAttribute("type","text/css");
		$.styleSheet&&($.styleSheet.cssText=css)||$.appendChild(D.createTextNode(css));
		D.getElementsByTagName('head')[0].appendChild($);
	},
	close:function(){
		if($("v_bannerportals_left"))$("v_bannerportals_left").style.display="none";
		if($("v_bannerportals_right"))$("v_bannerportals_right").style.display="none";
		clearTimeout(window.v_bannerportalsTimer);
		return false;
	},
	scroll:function(){
		var v_bannerportals_left=null;
		var v_bannerportals_right=null;
		if($("v_bannerportals_left")){
			v_bannerportals_left=$("v_bannerportals_left")
			v_bannerportals_left.style.display="";
			this._move(v_bannerportals_left,this.offsetTop,this.rate)
		}
		if($("v_bannerportals_right")){
			v_bannerportals_right=$("v_bannerportals_right")
			v_bannerportals_right.style.display="";
			v_bannerportals_right.style.right="0px";
			this._move(v_bannerportals_right,this.offsetTop,this.rate)
		}
	},
	_move:function(obj,offsetTop,rate){
		var $this=this;
		var from=parseInt(obj.style.top,10);
        var yOffset;
        var to=window.pageYOffset?window.pageYOffset:document.documentElement.scrollTop;
        to=to+offsetTop;
        if(to!=from){
            yOffset = rate?Math.ceil(Math.abs(to-from)/rate):Math.abs(to-from);
            if(to<from)yOffset=-yOffset;
            var oldTop=parseInt(obj.style.top,10);
            obj.style.top=(oldTop+yOffset)+"px";
        }
        if(obj.style.display!="none"){
            window.v_bannerportalsTimer=window.setTimeout(function(){$this._move(obj,offsetTop,rate)},$this.timeout);
        }else{
        	clearTimeout(window.v_bannerportalsTimer);
        }
	}
}
