/*
Copyright © 2011 Andrew Spendlove 
All rights reserved
*/
function ScrollElement() {return {ID	: "",AreaID: "",Element : null,Min	: 0 ,Max	: 0,Pos	: {y	: 0,Target : 0},ElementHeight : 0,AreaHeight	: 0,DelayScroll	: true ,DelayScrollTimer : null,MoveElementTimer 	: null ,MoveType	: 0,MoveAmount : 10,GetWidth	: function (objElement) {if (objElement != null) {return (!window.innerWidth > 0) ? objElement.offsetWidth : objElement.clientWidth;}else{return -1;}},GetHeight	: function (objElement) {if (objElement != null) {return (!window.innerWidth > 0) ? objElement.offsetHeight : objElement.clientHeight;}else{return -1;}},GetPosition : function (objElement) {var curleft = curtop = 0;if (objElement.offsetParent) {do {curleft += objElement.offsetLeft;curtop += objElement.offsetTop;} while (objElement = objElement.offsetParent);}return {x:curleft, y:curtop};},GetScrollPos	: function () {var intX = 0;var intY = 0;if( typeof( window.pageYOffset ) == 'number' ) {intY = window.pageYOffset;intX = window.pageXOffset;} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {intY = document.body.scrollTop;intX = document.body.scrollLeft;} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {intY = document.documentElement.scrollTop;intX = document.documentElement.scrollLeft;}return {x: intX, y:intY};},Init : function () {this.Element = document.getElementById(this.ID);this.ElementHeight = this.GetHeight(this.Element);this.AreaHeight = this.GetHeight(document.getElementById(this.AreaID));var objPos = this.GetPosition(this.Element);this.Min = objPos.y;this.Max = objPos.y + this.AreaHeight - this.ElementHeight;if (this.Max > this.Min) {this.Element.style.width = this.GetWidth(this.Element) + "px";this.Element.style.position = "absolute";this.Pos.y = this.Min;window.onscroll = function () {objScrollElement.DoScroll();};
if (this.MoveType != 0) {this.DoScroll();}else{this.MoveElement();}}},DoScroll : function () {if (this.MoveType == 1) {var objScrollPos = this.GetScrollPos();if (objScrollPos.y > this.Min && objScrollPos.y < this.Max) {this.Pos.Target = objScrollPos.y;}else if (objScrollPos.y <= this.Min) {this.Pos.Target = this.Min ;}else {this.Pos.Target = this.Max;}if (this.MoveElementTimer == null) {this.MoveElementTimer = window.setInterval(function () {objScrollElement.MoveElement();},25);}}else{if (this.DelayScroll) {if (this.DelayScrollTimer != null) {window.clearInterval(this.DelayScrollTimer);}this.DelayScrollTimer = window.setTimeout(function () {objScrollElement.MoveElement();},250);}else{this.MoveElement();}}},MoveElement : function () {if (this.MoveType == 1) {if (this.Pos.Target != this.Pos.y) {if (Math.abs(this.Pos.Target - this.Pos.y) < this.MoveAmount) {if ((this.Pos.Target - this.Pos.y) > 0) {this.Pos.y += (this.Pos.Target - this.Pos.y);}else{this.Pos.y = this.Min;}}else{this.Pos.y += (this.Pos.Target < this.Pos.y) ? -1*this.MoveAmount : this.MoveAmount;}if (this.Pos.y < this.Min) {this.Pos.y = this.Min;}this.Element.style.top = this.Pos.y  + "px";}else{window.clearInterval(this.MoveElementTimer );this.MoveElementTimer = null;}}else{var objScrollPos = this.GetScrollPos();if (objScrollPos.y > this.Min && objScrollPos.y < this.Max) {this.Element.style.top = objScrollPos.y + "px";}else if (objScrollPos.y <= this.Min) {this.Element.style.top = this.Min + "px";}}}}}

