var ScrollWithWindow = {};
ScrollWithWindow = Class.create({
	initialize: function(elm){
		this.elm = $(elm);
		this.pe = null;
		if(!this.elm){
			return null;
		}
		this.options = Object.extend({
			horizontal: false,
			vertical: true,
			offsets: [0, 0],
			delta: [0.45, 0.45],
			interval: 0.15,
			align: ['left', 'bottom'],
			style: {
				zIndex: '999'
			}
		}, arguments[1] || {});
		this.stop = [false, false];
		this.elm.absolutize();
		this.elm.setStyle(Object.extend(this.options.style, {'height': ''}));
		this.original_pos = [parseFloat(this.elm.getStyle('left')), parseFloat(this.elm.getStyle('top'))];
		Event.observe(window, 'scroll', this.startInterval.bind(this));
		if( window.scrollX || window.scrollY ){
			this.startInterval();
		}
	},
	startInterval: function(){
		if (!this.pe) {
			this.stop[0] = this.stop[1] = false;
			this.pe = new PeriodicalExecuter(this.move.bind(this), this.options.interval);
		}
	},
	stopInterval: function(){
		if( this.pe ){
			this.pe.stop();
			this.pe = null;
		}
	},
	getWindowScroll: function(){
		var scroll = [0, 0]
		if (!Object.isUndefined(window.scrollY)) {
			scroll = [window.scrollX, window.scrollY];
		}
		else 
			if (!Object.isUndefined(document.documentElement.scrollTop)) {
				scroll = [document.documentElement.scrollLeft, document.documentElement.scrollTop];
			}
			else 
				if (!Object.isUndefined(document.scrollTop)) {
					scroll = [document.scrollLeft, document.scrollTop];
				}
		return scroll;
	},
	getWindowDims: function(){
		if( !Object.isUndefined(window.innerHeight) ){
			return [window.innerWidth, window.innerHeight];
		}else if(!Object.isUndefined(document.documentElement.clientHeight)){
			return [document.documentElement.clientWidth, document.documentElement.clientHeight];
		}else if(!Object.isUndefined(document.body.clientHeight)){
			return [document.body.clientWidth, document.body.clientHeight];
		}
		return [0, 0];
	},
	move: function(){
		var change_x, change_y, new_x, new_y;
		var dims = this.elm.getDimensions();
		var page_dims = $$('html')[0];
		var window_dims = this.getWindowDims();
		var scroll = this.getWindowScroll();
		page_dims = {width: page_dims.scrollWidth, height: page_dims.scrollHeight};
		limit = [page_dims.width, page_dims.height];
		if( this.options.align[1] == 'bottom'){
			limit[1] = window_dims[1] + scroll[1];
		}
		if( this.options.align[0] == 'right'){
			limit[0] = window_dims[0] + scroll[0];
		}
		if( this.options.vertical ){
			var y = parseFloat(this.elm.getStyle('top'));
			change_y = (this.original_pos[1] + scroll[1] - y) * this.options.delta[1];
			if( change_y == 0.0){
				this.stop[1] = true;
			}
			new_y = y + change_y;
			if( new_y + dims.height >= limit[1]){
				new_y = limit[1] - dims.height;
				this.stop[1] = true;
			}
			if( new_y < this.original_pos[1] ){
				new_y = this.original_pos[1];
				this.stop[1] = true;
			}
			this.elm.setStyle({
				'top': new_y + 'px'
			});
		}else{
			this.stop[1] = true;
		}
		if( this.options.horizontal ){
			var x = parseFloat(this.elm.getStyle('left'));
			change_x = (this.original_pos[0] + scroll[0] - x) * this.options.delta[0];
			new_x = x + change_x;
			if( change_x == 0.0){
				this.stop[0] = true;
			}
			if( new_x + dims[0] >= limit[0]){
				new_x = limit[0] - dims[0];
				this.stop[0] = true;
			}
			if( new_x < this.original_pos[0] ){
				new_x = this.original_pos[0];
				this.stop[0] = true;
			}
			this.elm.setStyle({
				'left': new_x + 'px'
			});
		}else{
			this.stop[0] = true;
		}
		/*if( this.stop[0] && this.stop[1]){
			this.stopInterval();
		}*/
	}
});