function getOffsets(e) {
	var o = {
		height: e.offsetHeight,
		width: e.offsetWidth
	};
	
	var x = e.offsetLeft;
	var y = e.offsetTop;
	var p = e.offsetParent;
	
	while(p && (p.nodeType != 9)) {
		x += p.offsetLeft;
		y += p.offsetTop;
		p = p.offsetParent;
	}
	o.left = x;
	o.top = y;
	return o;
}


var stepSize = 2;
var delay = 20;

var direction = false;

var frameElementName = 'polaroidimagefieldtextscroll';
var scrollElementName = 'polaroidimagefieldtextscrollinner';
var upElementName = 'polaroidimagefieldtextscrollerup';
var downElementName = 'polaroidimagefieldtextscrollerdown';

function scrollUp() {
	 direction = 'up';
	 startTimer();
}
function scrollDown() {
	 direction = 'down';
	 startTimer();
}
function scrollDone() {
	direction = false;
}
function scroller(){
	var frameHeight = parseInt(document.getElementById(frameElementName).offsetHeight);
	var scrollerHeight = parseInt(document.getElementById(scrollElementName).offsetHeight);
	var scrollerPos = parseInt(document.getElementById(scrollElementName).style.top);
	if (isNaN(scrollerPos)) {
		scrollerPos = 0;
	}
	// alert(frameHeight + ':' + scrollerHeight + ':' + scrollerPos);
	if (direction=="down"){
		 if (scrollerHeight-frameHeight+scrollerPos>0){
		 		parseInt(document.getElementById(scrollElementName).style.top);
		 		var newTop = parseInt(document.getElementById(scrollElementName).style.top) - stepSize + "px";
		 		
		 		document.getElementById(scrollElementName).style.top = newTop;
		 		
				document.getElementById(upElementName).style.visibility="visible";
				startTimer();
		 } else {
		 	  document.getElementById(downElementName).style.visibility="hidden";
		 };
	} else if (direction=="up") {
		 if (scrollerPos<0){
		 		document.getElementById(scrollElementName).style.top=parseInt(document.getElementById(scrollElementName).style.top)+stepSize+"px";
				document.getElementById(downElementName).style.visibility="visible";
				startTimer();
		 } else {
		 	  document.getElementById(upElementName).style.visibility="hidden";
		 };
	};
};

function startTimer(){
	setTimeout("scroller()", delay);
};

function testforscroll() {
	var frameHeight=parseInt(document.getElementById(frameElementName).offsetHeight);
	var scrollerHeight=parseInt(document.getElementById(scrollElementName).offsetHeight);
	if (frameHeight >= scrollerHeight){
		document.getElementById(upElementName).style.visibility="hidden";
		document.getElementById(downElementName).style.visibility="hidden";	
	};
	document.getElementById(upElementName).style.visibility="hidden";
};
$(document).ready(function() {
	if (document.getElementById(frameElementName)) {
		testforscroll();
	}
});
