/*
 * lm_scrolllayer, Version 0.1.0, Package 
 * (c) 2006 Mathias Hayoz, http://lemats.net
 *
 * Based on Prototype.
 */
 
/** Declaration */
lm_scrolllayer_Layer = Class.create();

/** Properties */
lm_scrolllayer_Layer.CSSCLASS           = 'lmScrolllayer';
lm_scrolllayer_Layer.DEFAULT_SCROLLSIZE = 15; // px
lm_scrolllayer_Layer.DEFAULT_TIMEOUT    = 80; // ms
lm_scrolllayer_Layer.lists = new Object();

/** Body */
lm_scrolllayer_Layer.prototype = {
	
	/**
	 * Constructor
	 */	
	initialize : function(id, height, width, step, timeout) {
		this.id = id;
		this.height = height;		
		this.width = width;
		
		if (step != null) {
			this.scrollSize = step;
		} else {
			this.scrollSize = lm_scrolllayer_Layer.DEFAULT_SCROLLSIZE;
		}
		
		if (timeout != null) {
			this.timeout = timeout;
		} else {
			this.timeout = lm_scrolllayer_Layer.DEFAULT_TIMEOUT;
		}
		
		this.layerElement = null;
		this.realHeight = 0;
		this.realWidth = 0;
		this.clipTop = 0;
		this.clipBottom = this.height;
		this.topper = 0;
		this.time = null;
	},

	init : function() {
		// Get Layer Element
		this.layerElement = $(this.id);
		
		// Add Classname to the layer
		//Element.addClassName(this.layerElement, lm_scrolllayer_Layer.CSSCLASS);
		
		// Check for Support
		if (!this._isSupported()) return; // not supported. Return.
		
		// Get Real Layer Height/Width
		var dim = Element.getDimensions(this.layerElement);
		this.realHeight = dim["height"];
		this.realWidth = dim["width"];
		// Set width automaticly if not set (== 0)
		if (this.width == 0) {
			this.width = this.realWidth;
		}
		
		// Set Style
		var layerStyle = new Object();
		Element.makeClipping(this.layerElement);
		layerStyle["clip"] = this._getClip();
		layerStyle["position"] = "absolute";
		layerStyle["left"] = 0;
		layerStyle["top"] = 0;
		
		Element.setStyle(this.layerElement , layerStyle);
	},
	
	up : function() {
		this._scroll((-1) * this.scrollSize);
	},
	
	down : function() {
		this._scroll(this.scrollSize);
	},
	
	stop : function() {
		if (this.time) {
			clearTimeout(this.time);
		}
	},
	
	// PRIVATE
	
	_scroll : function(offset) {
		// Check for Support			
		if (!this._isSupported()) return; // not supported. Return.	
		
		this.clipTop += offset;
		this.clipBottom += offset;		
		this.topper -= offset;
		
		if (this.clipTop < 0 || this.clipBottom > this.realHeight) {
			this.clipTop -= offset;
			this.clipBottom -= offset;
			this.topper += offset;
			return;
		}
		
		// Set Clip
		var layerStyle = new Object();
		layerStyle["clip"] = this._getClip();
		layerStyle["top"] = this.topper + 'px';
		Element.setStyle(this.layerElement , layerStyle);		

		// Callback
		this.time = setTimeout('lm_scrolllayer_Layer._scroll("' + this.id + '", ' + offset + ');', this.timeout);	
	},
	
	_isSupported : function() {
		if (!this.layerElement || this.layerElement == null) {
			return false; // not supported
		}
		else {
			return true;
		}
	},
	
	_getClip : function() {
		return 'rect(' + this.clipTop + 'px, ' + this.width + 'px, ' + this.clipBottom + 'px, 0px)';
	}
	
}

lm_scrolllayer_Layer.create = function(id, height, width, step, timeout) {
	lm_scrolllayer_Layer.lists[id] = new lm_scrolllayer_Layer(id, height, width, step, timeout);
	return lm_scrolllayer_Layer.lists[id];
}

lm_scrolllayer_Layer.get = function(id) {
	return lm_scrolllayer_Layer.lists[id];
}

lm_scrolllayer_Layer.up = function(id) {
	var list = lm_scrolllayer_Layer.get(id);
	list.up();
}

lm_scrolllayer_Layer.down = function(id) {
	var list = lm_scrolllayer_Layer.get(id);
	list.down();
}

lm_scrolllayer_Layer.stop = function(id) {
	var list = lm_scrolllayer_Layer.get(id);
	list.stop();
}

/**
 * Needet for asynchronous setTime call
 */
lm_scrolllayer_Layer._scroll = function(id, offset) {
	var list = lm_scrolllayer_Layer.get(id);
	list._scroll(offset);
}

