var MarkFloat = new Class({
	Implements: [Event, Options],

	options: {
		img :  '',
		link : '#',
		Speed : 0.5,
		width : 150,
		height: 150
	},

	initialize: function(els, options){
		this.obj = $(els) || els;
		this.setOptions(options);
		this.isLeft = true;
		this.isTop = true;
		this.width = this.options.width;
		this.height = this.options.height;
		this.myAD = new Element('a', {
			'href':this.options.link,
			'id' : 'ad' + $random(1, 100),
			'target': '_blank',
			'styles': {
				'position': 'absolute',
				'left': 0,
				'top': 0
			}
		});
		this.myImg = new Element('img', {
			'src': this.options.img,
			'styles': {
				'border': 'none',
				'width': this.width,
				'height': this.height
			}
		});
		this.myAD.grab(this.myImg);
		this.obj.grab(this.myAD);
	},

	
	Start: function(){
		
		if (this.timer==undefined && this.timer==null)
		{
			this.timer = this.Float.periodical(this.options.Speed * 100, this);
		}
		var Me = this;
		this.myImg.addEvent('mouseover', function(){
			if (Me.timer){
				Me.timer = $clear(Me.timer);
			}
		});
		this.myImg.addEvent('mouseout', function(){
			Me.Start();
		});
	},
	
	Float: function(){
		var toLeft = this.myAD.getStyle('left').toInt();
		var toTop = this.myAD.getStyle('top').toInt();
		var size = getSize();
		var client = $(document.body).getSize();
		var scroll = $(document.body).getScroll();

		if(this.width + toLeft > client.x + scroll.x - 1){
			toLeft = scroll.x + client.x - this.width;
			this.isLeft = false;
		}
		if(toLeft < scroll.x)
		{
			toLeft = scroll.x;
			this.isLeft = true;
		}

		if(this.height + toTop > client.y + scroll.y - 1)
		{
			toTop = scroll.y + client.y - this.height;
			this.isTop = false;
		}
		if(toTop < scroll.y)
		{
			toTop = scroll.y;
			this.isTop = true;
		}
		
		toLeft = toLeft + (this.isLeft ? +1 : -1) + "px";
		toTop = toTop + (this.isTop ? +1 : -1) + "px";

		this.myAD.setStyles({
			'left': toLeft,
			'top': toTop
		});
	},
	
	Fix: function(l, t){
		this.myAD.setStyles({
			'left': l,
			'top':t
		});
		var ad = this.myAD;
		window.addEvent('scroll', function(){
			var scroll = $(document.body).getScroll();
			ad.setStyles({
				'left': l + scroll.x,
				'top': t + scroll.y
			});
		});
		
	},

	Stop: function(){
		if (this.timer){
			this.timer = $clear(this.timer);
		}
	}

});