/*
*	浮动广告类 V1.0.0
*	create 2008-1-8
*	last modified 2008-1-8
*	laoqiming@gmail.com
*	copyright (c) http://camen.cosbeauty.net
*/
if(typeof freesiteAdManager =='undefined'){
	freesiteAdManager = {};
	freesiteAdManager.ads =[];
	freesiteAdManager.find = function(id){
		for(var i=0;i<this.ads.length;i++){
			if(this.ads[i].id == id)
				return this.ads[i];
		}
		return ;
	}
}
function freesiteFloater(id , width ,height, type ){
	this.id = id;
	this.type = type || "img";
	this.position = [1,1];
	this.float = 1;
	this.offset = [0,0];
	this.index = freesiteAdManager.ads.length;
	freesiteAdManager.ads.push(this);
	this.src = "";
	this.link = "";
	this.text = "";
	this.closeButton =["show","","bottom",12];
	this.width = width || 88;
	this.height = height || 31;
	this.c_width = this.width;
	this.c_height = this.height;
	this.compatMode = false;
	if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
		this.compatMode = true;
	this.isIE6 = (navigator.appVersion.indexOf("MSIE 6.0")>-1?1:0);
	this.isIE7 = (navigator.appVersion.indexOf("MSIE 7.0")>-1?1:0);
	this.toString = function(){
		var html = '';
		if((this.type == "img" || this.type == "flash") && this.src == ""){
			alert( this.type + "类型必须提供src");
			return "";
		}
		if(this.type == "txt"  && this.text == ""){
			alert( this.type + "类型必须提供text");
			return "";
		}
		var ps = this.getPosition();
		var domPosition = "fixed";
		if(this.isIE6)domPosition="absolute";
		if(!this.float)domPosition="absolute";
		if(this.isIE7 && !this.compatMode)domPosition="absolute";
		var cbh ='';//关闭按钮html
		if(this.closeButton[0] == 'show')
			switch(this.closeButton[2]){
				case "top":
				case "bottom":
					this.c_height += this.closeButton[3];
					if( this.closeButton[1] != "" ){
						cbh = '<a href="javascript:void(freesiteAdManager.ads[' + this.index + '].hide())" title="关闭" style="display:block;width:' + this.width + 'px;height:' + this.closeButton[3] + 'px;float:left"><img src="' + this.closeButton[1] + '" border="0"/></a>';
					}else{
						cbh = '<a href="javascript:void(freesiteAdManager.ads[' + this.index + '].hide())" title="关闭" style="display:block;width:' + this.width + 'px;height:' + this.closeButton[3] + 'px;float:left">关闭</a>';
					}
					break;
				case "left":
				case "right":
					this.c_width += this.closeButton[3];
					if( this.closeButton[1] != "" ){
						cbh = '<a href="javascript:void(freesiteAdManager.ads[' + this.index + '].hide())" title="关闭" style="display:block;width:' + this.closeButton[3] + 'px;height:' + this.height + 'px;float:left"><img src="' + this.closeButton[1] + '" border="0"/></a>';
					}else{
						cbh = '<a href="javascript:void(freesiteAdManager.ads[' + this.index + '].hide())" title="关闭" style="display:block;width:' + this.closeButton[3] + 'px;height:' + this.height + 'px;float:left">关闭</a>';
					}
					break;
			}
		html = '<div id="freesiteFloaterContainer' + this.index + '" style="position:' + domPosition + ';width:' + this.c_width + 'px;height:' + this.c_height + 'px;top:' + ps.y + 'px;left:' + ps.x + 'px;">';
		if(this.closeButton[0] == 'show')
			if(this.closeButton[2] == "top" || this.closeButton[2] =="left"){
				html += cbh;
			}
		switch(this.type.toLowerCase()){
			case "img":
				if(!(this.width || this.height)){
					var i=new Image();
					i.src = this.src;
					this.width =i.width;
					this.height = i.height;
				}
				html += '<a href="' + this.link + '" target="_blank"><img src="' + this.src + '" width="' + this.width + '" height="' + this.height + '" style="float:left;border:0"/></a>';
				break;
			case "flash":
				html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + this.width + '" height="' + this.height + '" style="float:left"/>';
				html += '<param name="movie" value="' + this.src + '" />';
				html += '<param name="quality" value="high" />';
				html += '<param name="swliveconnect" value="true" />';
				html += '<param name="wmode" value="opaque" />';
				html += '<embed name="' + this.id + '" id="' + this.id + '" src="' + this.src + '" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + this.width + '" height="' + this.height + '" quality="high" swliveconnect="true" wmode="opaque" ></embed>';
				html += '</object>';
				break;
			case "txt":
				html += '<div style="width:' + this.width + 'px;height:' + this.height + 'px;float:left"><a href="' + this.link + '" target="_blank">' + this.text + '</a></div>';
				break;
			default:
				html += '<div style="width:' + this.width + 'px;height:' + this.height + 'px;float:left"><a href="' + this.link + '" target="_blank">' + this.text + '</a></div>';
				break;
		}
		if(this.closeButton[0] == 'show')
			if(this.closeButton[2] == "right" || this.closeButton[2] =="bottom"){
				html += cbh;
			}
		html += '</div>';
		return html;
	}
	this.render = function(){
		document.write(this.toString())
		var _self = this;
		this.attachEvent(window,"resize",function(){
			freesiteAdManager.ads[_self.index].setPosition();
		})
		if(this.float){
			if(!this.compatMode){
				this.attachEvent(window,"scroll",function(){
					freesiteAdManager.ads[_self.index].setPosition();
				})
			}else{
				this.attachEvent(window,"scroll",function(){
					freesiteAdManager.ads[_self.index].setPosition();
				})
			}
		}
	}
	this.attachEvent = function(a,b,c){
		if(a.attachEvent)
			a.attachEvent("on"+b,c);
		else
			a.addEventListener(b,c,false);
	}

	this.getPosition = function(){
		var ps = {x:0,y:0};
		if(this.position[0] >0){
			var db = this.compatMode ? document.documentElement : document.body;
			if(this.float && (this.isIE6 || (!this.compatMode && this.isIE7) ) ){
				ps.x += db.scrollLeft;
				ps.y += db.scrollTop;
			}
			ps.x += ((this.position[0]-1) % 3) * (db.clientWidth/2);
			ps.y += (Math.ceil(this.position[0] / 3)-1) * (db.clientHeight/2);
			ps.x -= ((this.position[1]-1) % 3) * (this.c_width/2);
			ps.y -= (Math.ceil(this.position[1] / 3)-1) * (this.c_height/2);
			ps.x += this.offset[0];
			ps.y += this.offset[1];
		}
		return ps;
	}
	this.setPosition = function(){
		var ps = this.getPosition();
		document.getElementById("freesiteFloaterContainer" + this.index).style.top = ps.y +"px";
		document.getElementById("freesiteFloaterContainer" + this.index).style.left = ps.x +"px";
	}
	this.hide = function(){
		document.getElementById("freesiteFloaterContainer" + this.index).style.visibility = "hidden";
	}
	this.show = function(){
		document.getElementById("freesiteFloaterContainer" + this.index).style.visibility = "visible";
	}
}