window.project=(window.project)?window.project:{};(function($)
{project.FixedElementBehaviour=function(dom_element,options)
{this.dom_element=dom_element;this.win=$(window);this.position={"min":{"left":0,"top":0},"max":{"left":0,"top":0}};this.options={"animate":false,"speed":200,"parent_element":$("#main")};$.extend(this.options,options);this.initialize();};project.FixedElementBehaviour.prototype.initialize=function()
{var that=this;this.dom_element.css("position","absolute");this.dom_element.css("top",24);this.position.min=this.dom_element.offset();this.parent_padding_top=parseInt(this.dom_element.parent().css("paddingTop"));this.position_min_top=this.position.min.top-this.parent_padding_top;this.updatePosition();this.win.scroll(function()
{that.updatePosition();});};project.FixedElementBehaviour.prototype.updatePosition=function()
{var that=this;var new_margin_top=0;this.position.max.top=this.options.parent_element.height()-(this.dom_element.height()+(2*this.parent_padding_top));this.position_max_top=this.position.max.top+this.parent_padding_top;if(this.win.scrollTop()>this.position_min_top)
{new_margin_top=this.win.scrollTop()-this.position.min.top;if(this.options.animate)
{if(new_margin_top>this.position.max.top)
{new_margin_top=this.position.max.top;}}
else
{if(new_margin_top>this.position.max.top)
{new_margin_top=this.position.max.top;this.dom_element.css("marginTop",this.position_max_top);this.dom_element.css("position","absolute");}
else
{this.dom_element.css("marginTop",0);this.dom_element.css("position","fixed");}}}
else if(this.win.scrollTop()<=this.position.max.top)
{if(!this.options.animate)
{this.dom_element.css("position","");this.dom_element.css("marginTop",0);}}
if(this.options.animate)
{this.dom_element.stop().animate({marginTop:new_margin_top},{"duration":this.options.speed,"queue":false});}};JsBehaviourToolkit.registerHandler("fixed_element",project.FixedElementBehaviour);})(jQuery)
