// base on jQuery

var Ad = function(){};
$.extend(Ad.prototype, {
	init: function(opt) {
		this.items = [];
		this.btnCloseImage = 'images/close.gif';
		this.btnCloseWidth = 18;
		this.btnCloseHeight = 18;
		this.iconWidth = 80;
		this.iconHeight = 80;
		this.adProcessing = null;
		this.droped = [];
	},
	add: function(key, dat) {
		var idx = this.items.length;
		this.items[idx] = dat;
		this.adProcessing = idx;
		this.items = $.unique(this.items);
	},
	drop: function(node) {
		var idx = this.adProcessing;
		if (this.droped[idx]) return;
		ad = this.items[this.adProcessing];
		node.fadeOut();
		if (this.items[idx].position=='fullscreen') {
			this.addFsIcon(idx);
		}
		this.droped[idx] = true;
	},
	addFsIcon: function(idx) {
		ad = this.items[idx];
		var right = ((document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth) - 980) / 2 + 10;
		var ico = $('<div><a href="'+ad.url+'" target="_blank"><img src="'+ad.ico+'" width="'+this.iconWidth+'" height="'+this.iconHeight+'" border="0" /></a></div>').css({position:'absolute', top:'60px', right:right+'px','z-index':10});
		$('body').append(ico);
	},
	addCloseBtn: function(node) {
		var self = this;
		var btn = '<img src="' + this.btnCloseImage + '" width="' + this.btnCloseWidth + '" height="' + this.btnCloseHeight + '" border="0" title="close" />';
		btn = $(btn);
		btn.css({position:'absolute',right:'5px',top:'5px', cursor: 'pointer', 'z-index': 100});
		node.append(btn);
		btn.click(function(){ self.drop(node); });
	},
	fullscreenAdFloat: function(node) {
		var self = this;
		var t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		var w = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
		var h = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		var width = node.width(), height = node.height();
		var left = ((w - width) / 2);
		var top = (t+(h - height) / 2);
		node.css('left',left+'px');
		node.css('top',top+'px');
	},
	createFsAd: function(ad) {
		var self = this;
		var conId = "_ad"+ad.id;
		var node = '<div id="'+conId+'">';
		if (ad.type=='img') {
			node += '<a href="'+ad.url+'" target="_blank"><img src="'+ad.src+'" width="'+ad.width+'" height="'+ad.height+'" border="0" alt="advertisement" /></a>';
		}
		if (ad.type=='swf') {
		    node +='<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="'+ad.width+'" height="'+ad.height+'">';
		    node +='<param name="movie" value="'+ad.src+'" /><param name="quality" value="high" /><param name="wmode" value="transparent" />';
		    node +='<embed src="'+ad.src+'" width="'+ad.width+'" height="'+ad.height+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed></object>';
		}
		node+='</div>';
		node = $(node);
		node.css({position:'absolute', left:'0px', top:'0px', width:ad.width+'px', height:ad.height+'px', 'z-index': 100});
		$("body").append(node);
		this.addCloseBtn(node);

		self.fullscreenAdFloat(node);
		$(window).scroll(function(){self.fullscreenAdFloat(node)});
		$(window).resize(function(){self.fullscreenAdFloat(node)});

		var t = setTimeout(function(){self.drop(node)},6000);
	},
	showAll: function() {
		var self = this;
		$.each(this.items, function(key, ad){
			if (typeof(ad)!='undefined') {
				if (ad.position=='fullscreen') self.createFsAd(ad)
			}
		});
	},
	say: function() {
		alert(this.msg[0]);
	}
});

