$(document).ready(function() {
	if(($.browser.msie && $.browser.version < 7) || $.browser.opera) {
		return false;
	}
	else {
		var fd = '#move-block';
		var fp = '#columns-position';
		var ftp = 20;
		if(window.fixer_div){fd = fixer_div;}
		if(window.fixer_parent){fp = fixer_parent;}
		if(window.fixer_top_padding){ftp = fixer_top_padding;}
		new FixerUpper(fd,fp,ftp); //affected div, parent, topPadding
	}
});
	
var FixerUpper = function(el,parent,padding) {
	this.el = $(el);
	this.elHeight = this.el.outerHeight({margin:true});
	this.relParent = $(parent);
	this.relParent.css('position','relative');
	this.padding = padding;
	this.bindScrollEvent();
	this.bindResizeEvent();
	this.setScrollOffsets();
	this.setElOffsets();
	this.initialElLeftOffset = this.elLeftOffset;
	this.initialElTopOffset = this.elTopOffset;
	this.initialElWindowTopOffset = this.elWindowTopOffset;
	this.setIsTouchingTop();
	this.setIsTooTall();
	this.determinePosition();
};

FixerUpper.prototype = {
	el:'',
	elHeight:0,
	relParent:'',
	padding:0,
	leftScrollOffset:0,
	topScrollOffset:0,
	elLeftOffset:0,
	elTopOffset:0,
	elWindowLeftOffset:0,
	elWindowTopOffset:0,
	initialElLeftOffset:0,
	initialElTopOffset:0,
	initialElWindowTopOffset:0,
	horizScrollState:0,
	isTouchingTop:0,
	isTouchingBottom:0,
	isTooTall:0,
	bindScrollEvent:function() {
		var me = this;
		$(window).bind('scroll',function() {
			me.setScrollOffsets();
			me.setElOffsets();
			me.determinePosition();
		});
	},
	bindResizeEvent:function() {
		var me = this;
		$(window).bind('resize',function(){
			me.setElOffsets();
			me.determinePosition(1);
		});
	},
	setIsTouchingTop:function() {
		if(this.topScrollOffset > (this.initialElWindowTopOffset - this.padding)){
			this.isTouchingTop = 1;
		}
		else {this.isTouchingTop = 0;}
	},
	setIsTouchingBottom:function() {
		if(this.topScrollOffset - this.initialElWindowTopOffset > $("#columns-position").height() - $("#move-block").height() - 60) { 
			this.isTouchingBottom = 1; 
		}
		else { this.isTouchingBottom = 0; }		
	},
	setIsTooTall:function() {
		if((this.el.height() + (this.padding + 10)) > $(window).height()){
			this.isTooTall = 1;
		}
		else {this.isTooTall = 0;}
	},
	setHorizScrollState:function() {
		if(this.leftScrollOffset == 0){this.horizScrollState = 0;}
		else {this.horizScrollState = 1;}
	},
	setLeftScrollOffset:function() {
		this.leftScrollOffset = $(window).scrollLeft();
		this.setHorizScrollState();
	},
	setTopScrollOffset:function() {
		this.topScrollOffset = $(window).scrollTop();
	},
	setScrollOffsets:function() {
		this.setLeftScrollOffset();
		this.setTopScrollOffset();
	},
	setElTopOffset:function() {
		this.elTopOffset = this.el.position().top;
	},
	setElLeftOffset:function() {
		this.elLeftOffset = this.el.position().left;
	},
	setElWindowLeftOffset:function() {
		this.elWindowLeftOffset = this.el.offset().left;
	},
	setElWindowTopOffset:function() {
		this.elWindowTopOffset = this.el.offset().top;
	},
	setElOffsets:function() {
		this.setElTopOffset();
		this.setElLeftOffset();
		this.setElWindowLeftOffset();
		this.setElWindowTopOffset();
		this.setIsTouchingTop();
		this.setIsTouchingBottom();
		this.setIsTooTall();
	},
	determinePosition:function(duringResize) {
		var duringResize = 0 || duringResize;
		if(duringResize == 1){
			if(this.isTouchingTop == 0) {
				this.setToStatic();
			}
			else {
				this.setToAbsolute();
			}	
		}
		else {	
			if(this.isTouchingBottom == 1 && this.isTooTall == 0) {  
				this.el.css({position:'absolute',left:this.initialElLeftOffset,top: $("#columns-position").height() - $("#move-block").height()}); 
			}
			else if(this.isTouchingTop == 1 && this.horizScrollState == 0 && this.isTooTall == 0){
				this.setToFixed();
			}				
			else if(this.isTouchingTop == 0 && this.horizScrollState == 0){
				this.setToStatic();
			}
			else if(this.isTouchingTop == 1 && this.horizScrollState == 1 && this.isTooTall == 0){
				this.setToAbsolute();
			}
			else if(this.isTouchingTop == 1 && this.horizScrollState == 1){
				this.setToStatic();
			}			
		}

	},
	setToFixed:function() {
		if(this.el.css('position') != 'fixed') {
			this.el.css({position:'fixed',left:this.elWindowLeftOffset,top:this.padding+'px'});
		}
	},
	setToStatic:function() {
		if(this.el.css('position') != 'static') {
			this.el.css({position:'static'});
		}
	},
	setToAbsolute:function() {
		var top = (this.topScrollOffset - this.initialElTopOffset) - (this.padding + 18);
		if(this.el.css('position')!='absolute'){
			this.el.css({position:'absolute',left:this.initialElLeftOffset,top:top});
		}
		else {this.el.css('top',top);}
	}
}

