/**
*	ScrollingPane class
*	@author Alex Tse
*/
function ScrollingPane(el, w) {
	var pane = $(el);
	var win = $(w);
	var posPin = [];
	var currentIndex = 0;
	
	/**
	*	Scroll to specific position pointer
	*	@param obj Position pointer object (element with rel='scrollingPosition') or pointer index (integer)
	*	@param direction Final Position, "<vertical> <horizontal>", <vertical> = top / bottom / middle, <horizontal> = left / right / center
	*	@param option Options of the animation, including mandatory 'duration' and/or 'accelerate' and/or 'onComplete' properties
	*/
	this.scrollTo = function(obj, direction, option) {
		if (!posPin[obj] && posPin.indexOf(obj) < 0) return;
		
		// parsing direction
		var aDirection = direction.match(/^(top|bottom|middle)?\s*(left|right|center)?$/i);
		if (!aDirection) return;
		
		// setting target element and index of the element
		var targetElement;
		if (typeof obj == 'number') {
			targetElement = posPin[obj];
			currentIndex = obj;
		} else {
			targetElement = obj;
			currentIndex = posPin.indexOf(obj);
		}
		
		var transformObjSet = {};
		
		// setting vertical position
		switch (aDirection[1]) {
			case "top":
				transformObjSet['top'] = -$(targetElement).offsetTop + 'px';
			break;
			case "bottom":
				transformObjSet['top'] = win.getHeight() - ($(targetElement).offsetTop + targetElement.getHeight()) + 'px';
			break;
			case "middle":
				transformObjSet['top'] = win.getHeight()/2 - ($(targetElement).offsetTop + targetElement.getHeight()/2) + 'px';			
			break;
		}
		
		// setting horizontal position
		switch (aDirection[2]) {
			case "left":
				transformObjSet['left'] = -$(targetElement).offsetLeft + 'px';
			break;
			case "right":
				transformObjSet['left'] = win.getWidth() - ($(targetElement).offsetLeft + targetElement.getWidth()) + 'px';
			break;
			case "center":
				transformObjSet['left'] = win.getWidth()/2 - ($(targetElement).offsetLeft + targetElement.getWidth()/2) + 'px';			
			break;
		}
		pane.transformStyleSet(transformObjSet, option);
	}
	
	/**
	*	Scroll to next indexed position pointer or reset to the first pointer automatically
	*	p.s: final position will be 'top left' corner of the panel window
	*	@param options Options of the animation, including mandatory 'duration' and/or 'accelerate' and/or 'onComplete' properties
	*/
	this.toggle = function(options) {
		this.scrollTo(currentIndex, 'top left', options);
		currentIndex = (currentIndex + 1) % posPin.length;
	}
	
	posPin = posPin.merge(pane.getElementsByAttribute('rel', 'scrollingPosition'));
	return this;
}

