var FixedElement = Class.create(
{
	element: null,
	options: { top: 0,left: 0 },

	initialize: function(Element,options)
    {
		this.element=$(Element);
		Object.extend(this.options, options || {});
		
		
		Event.observe(window, 'resize',this._moveElement.bind(this));
		Event.observe(window, 'scroll',this._moveElement.bind(this));
	},
	_moveElement: function()
	{
		var windowHeight = this._getWindowHeight();
		var windowWidth = this._getWindowWidth();
		var offset = this._getOffset(this.element.parentNode);
		var o = document.viewport.getScrollOffsets();
		 
		this.element.style.position = 'absolute';
		var top=(o[1]);
		if(top>this.options.top)
			this.element.style.top = top + 'px';
		else
			this.element.style.top = this.options.top+'px';
		
		var left=(windowWidth - offset.left);
		if(left > this.options.left)
			this.element.style.left = (windowWidth - offset.left) + 'px';
		else 
			this.element.style.left=this.options.left+'px';		
	},
	_getOffset: function(obj)
	{
		var x=0,y=0;
		while (obj!=null){
		 x+=obj.offsetLeft-obj.scrollLeft;
		 y+=obj.offsetTop-obj.scrollTop;
		 obj=obj.offsetParent;
		}
		return {left:x,top:y};
	},
	_getWindowWidth: function()
	{
		var windowWidth=0;
		if (typeof(window.innerWidth)=='number') {
			windowWidth=window.innerWidth;}
		else if (document.documentElement && document.documentElement.clientWidth) {
		    windowWidth = document.documentElement.clientWidth;}
		else if (document.body&&document.body.clientHWidth) {
			windowWidth=document.body.clientWidth;}
		return windowWidth;
	},
	_getWindowHeight: function()
	{
		var windowHeight=0;
		if (typeof(window.innerHeight)=='number') {
			windowHeight=window.innerHeight;
		}
	    else if (document.documentElement && document.documentElement.clientHeight) {
	         windowHeight = document.documentElement.clientHeight;}
		if (document.body&&document.body.clientHeight) {
	         windowHeight=document.body.clientHeight;
		}
		return windowHeight;
	}
});
