


function Skyscraper(lSkyscraper,lEnd,options){

	this.dom = {
		"end": $('#'+lEnd),
		"skyscraper": $('#'+lSkyscraper)
	}
	
	this.config = {
		'duration': ((options.duration)?options.duration:500),
		'offsetTop': ((options.offsetTop)?options.offsetTop:0),
		'offsetBottom': ((options.offsetBottom)?options.offsetBottom:0)
	}
	
	this.toInt = function(str){
		re = /(.*)px/
		w = re.exec(str);
		return w[1];
	}
	
	this.scroll = function(scrollTop){
		sTop = this.dom.skyscraper.position().top;
		sHeight = this.dom.skyscraper.innerHeight();
		sStartTop = sTop - this.toInt(this.dom.skyscraper.css('top'));
		eTop = this.dom.end.position().top-this.config.offsetBottom;
		
		scrollTop += this.config.offsetTop;
		if(scrollTop > eTop-sHeight) scrollTop = eTop-sHeight;
		if(scrollTop < sStartTop) scrollTop = sStartTop;
		
		this.dom.skyscraper.stop();
		this.dom.skyscraper.animate({
			'top': scrollTop-sStartTop
		}, this.config.duration);
	}
	
	this.init = function(){
		scrollEvent.addInstance(this);
		this.dom.skyscraper.css('position', 'relative');
		this.dom.skyscraper.css('top', '0px');
	}
	
	this.init();

}

function ScrollEvent(){

	this.instances = [];
	
	this.addInstance = function(inst){
		this.instances[this.instances.length] = inst;
	}
	
	this.init = function(){
		window.onscroll = function(){
			for(i=0;i<scrollEvent.instances.length;i++){
				scrolltop = $(window).scrollTop();
				scrollEvent.instances[i].scroll(scrolltop);
			}
		}
	}
	
	this.init();

}

scrollEvent = new ScrollEvent();
