ok2010_SvosjBanner = Class.create({
	upInitMap: null,	// lookup tables for scheduled rollUps/downs 
	downInitMap: null,
	upMap: null,		// lookup tables for ongoind rollUps/downs
	downMap: null,
	offsetMap: null,	// lookup table for each banner's info-content's top-offset
	heightMap: null,	// lookup table for each banner's height
	
	stepUp: null,		// number of pixels to step up/down
	stepDown: null,

	initialize: function() {
		
		// init magic numbers
		this.stepUp = 9;
		this.stepDown = 4;
		
		// init lookup tables
		this.upMap = new Hash();
		this.downMap = new Hash();
		this.upInitMap = new Hash();
		this.downInitMap = new Hash();
		this.offsetMap = new Hash();
		this.heightMap = new Hash();
		
		$$('div.ok2010_svosjbanner').first().select('div.banner').each(function(e){
			// identify banner element and register in lookup tables
			var elementID = e.identify();
			this.upMap.set(elementID, null);
			this.downMap.set(elementID, null);
			this.upInitMap.set(elementID, null);
			this.downInitMap.set(elementID, null);
			
			// determine actual height of info_container. store in lookup table
			var infoHeight = e.down('div.info_container').getHeight();
			this.heightMap.set(elementID, infoHeight);

			// place info container at the bottom
			this.resetInfoContainer(e);
			
			// add mouseover event on info container
			e.observe('mouseover', function(event) {
				event.stop();
				this.handleOver(event.element());
			}.bindAsEventListener(this));
			
			// add mouseout event on info container
			e.observe('mouseout', function(event) {
				event.stop();
				this.handleOut(event.element());
			}.bindAsEventListener(this));
		}.bind(this));
	},
	
	resetInfoContainer: function (element) {
		// determine top offset to show only header
		var bannerHeight = element.getHeight();
		var headerHeight = element.down('div.info_container').down('h1').getHeight();
		var offset = bannerHeight - headerHeight;
		
		this.offsetMap.set(element.identify(), offset);
		element.down('div.info_container').style.top = offset + "px";
	},
	
	handleOver: function (element) {
		// get hold of corresponding banner element and id
		var bannerElement = element.up('div.banner');
		var bannerID = bannerElement.identify();

		// check if a reverse (out) event is scheduled, and stop it
		if (this.downInitMap.get(bannerID) != null) {
			clearInterval(this.downInitMap.get(bannerID));
			this.downInitMap.set(bannerID, null);
		}
		
		// schedule a rollup in a jiffy if it's not already scheduled
		if (this.upInitMap.get(bannerID) == null) {
			var timerID = setTimeout("ok2010_svosjBanner.initUp('" + bannerID + "');", 100);
			this.upInitMap.set(bannerID, timerID);
		}
	},
	
	handleOut: function (element) {
		// get hold of corresponding banner element and id
		var bannerElement = element.up('div.banner');
		var bannerID = bannerElement.identify();

		// check if a reverse (over) event is scheduled, and stop it
		if (this.upInitMap.get(bannerID) != null) {
			clearInterval(this.upInitMap.get(bannerID));
			this.upInitMap.set(bannerID, null);
		}
		
		// schedule a rolldown in a jiffy if it's not already scheduled
		if (this.downInitMap.get(bannerID) == null) {
			var timerID = setTimeout("ok2010_svosjBanner.initDown('" + bannerID + "');", 100);
			this.downInitMap.set(bannerID, timerID);
		}
	},
	
	initUp: function (bannerID) {
		// clear lookup entry for task
		this.upInitMap.set(bannerID, null);
		
		// stop rollDown if running
		if (this.downMap.get(bannerID) != null) {
			clearInterval(this.downMap.get(bannerID));
			this.downMap.set(bannerID, null);
		}
		
		// start rollUp interval if not already running
		if (this.upMap.get(bannerID) == null && this.isOverflowing2($(bannerID))) {
			var timerID = setInterval("ok2010_svosjBanner.rollUp('"+bannerID+"');", 50);
			this.upMap.set(bannerID, timerID);
		}
	},
	
	initDown: function (bannerID) {
		// clear lookup entry for task
		this.downInitMap.set(bannerID, null);
		
		// stop rollUp if running
		if (this.upMap.get(bannerID) != null) {
			clearInterval(this.upMap.get(bannerID));
			this.upMap.set(bannerID, null);
		}
		
		// start rollDown interval if not already running
		if (this.downMap.get(bannerID) == null) { //TODO.. and not finished
			var timerID = setInterval("ok2010_svosjBanner.rollDown('"+bannerID+"');", 50);
			this.downMap.set(bannerID, timerID);
		}
	},
	
	rollUp: function(bannerID) {
		// get offset, remove "px" and convert to integer
		var offset = $(bannerID).down('div.info_container').style.top;
		offset = offset.substr(0, offset.length - 2);
		offset = parseInt(offset);
		offset -= this.stepUp;
		
		if (offset < 0) {
			// reached the top
			offset = 0;
		} else if (offset + this.heightMap.get(bannerID) < $(bannerID).getHeight()) {
			// too far up. adjust
			offset = $(bannerID).getHeight() - this.heightMap.get(bannerID);
		}

		$(bannerID).down('div.info_container').style.top = offset + "px";

		// stop interval if info container is no longer overflowing or at the top
		if (!this.isOverflowing2($(bannerID)) || offset == 0) {
			clearInterval(this.upMap.get(bannerID));
			this.upMap.set(bannerID, null);
		}
	},
	
	rollDown: function (bannerID) {
		// get offset, remove "px" and convert to integer
		var offset = $(bannerID).down('div.info_container').style.top;
		offset = offset.substr(0, offset.length - 2);
		offset = parseInt(offset);
		
		offset += this.stepDown;

		if (offset < this.offsetMap.get(bannerID)) {
			// continue sinking
			$(bannerID).down('div.info_container').style.top = offset + "px";
		} else {
			// reached the bottom. stop interval
			this.resetInfoContainer($(bannerID));
			clearInterval(this.downMap.get(bannerID));
			this.downMap.set(bannerID, null);
		}
	},
	
	isOverflowing2: function(e) {
		// checking element's height instead of scrollheight
		// msie doesnt handle overflow when element has abs pos

		// get top offset
		var offset = e.down('div.info_container').style.top;
		offset = offset.substr(0, offset.length - 2);
		offset = parseInt(offset);

		// get banner height
		var bannerHeight = e.getHeight();
		
		// get info_container height
		var infoHeight = this.heightMap.get(e.identify());
		
		if (offset + infoHeight <= bannerHeight) {
			return false;
		} else {
			return true;
		}
	}	
});
var ok2010_svosjBanner;

