/*
 * jQuery Clipregion Plugin - Current
 *   http://www.amountaintop.com/projects/clipregion/
 *
 * To use, download this file to your server, save as keynav.js,
 * and add this HTML into the <head>...</head> of your web page:
 *   <script type="text/javascript" src="clipregion.js"></script>
 *
 * Copyright (c) 2006 Mike Hostetler <http://www.amountaintop.com/>
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */
$.clipregion = new Object();

$.fn.clipregion = function(o) {
	$.clipregion.o = o;
	return this.each(function () {
		$.clipregion.init(this,$.clipregion.o);
	});
}

$.clipregion.getVisible = function() {
	var c = $.clipregion;
	var l = new Array();
//	return $(c.crId).find("li").gt(c.getIdx).lt(c.itemsToShow);
}

$.clipregion.init = function (crId,o) {
	var c = $.clipregion;
	c.crId = crId;

	/** 
	 * Options 
	 */
	c.itemHeight = o.itemHeight ? o.itemHeight : 100;
	c.itemWidth = o.itemWidth ? o.itemWidth : 100;
	c.totalItems = o.totalItems ? o.totalItems : $('ul > li',c.crId).size();
	c.itemsToShow = o.itemsToShow ? o.itemsToShow : 3;
	c.itemStep = o.itemStep ? o.itemStep : false;
	c.horiz = o.direction == 'vertical' ? false : true;
	c.debug = o.debug == true ? true : false;

	c.loadNext = o.loadNext ? o.loadNext : null;
	c.loadPrev = o.loadPrev ? o.loadPrev : null;

	/**
	 * Helpers
	 */
	c.crList = $('ul:first',$(c.crId)).get(0);

	if(c.horiz)
		c.itemSize = c.itemWidth; 
	else
		c.itemSize = c.itemHeight; 

	if(c.totalItems > 0) {
		c.resize();
	}

	c.setPos(0);
}

$.clipregion.resize = function() {
	var c = $.clipregion;

	/**
	 * Enforce necessary styles
	 */
	if(!c.debug)
		$(c.crId).css({'overflow':'hidden'});

	$(c.crList).css({
						'position':'relative',
						'z-index':1,
						'margin':'0px',
						'padding':'0px',
						'border':'0px'
					});
	
	if(c.horiz) {
		var listW = c.totalItems*c.itemWidth;
		var listH = c.itemHeight;
		var clipW = c.itemWidth*c.itemsToShow;
		var clipH = c.itemHeight;
	}
	else {
		var listW = c.itemWidth;
		var listH = c.totalItems*c.itemHeight;
		var clipW = c.itemWidth;
		var clipH = c.itemHeight*c.itemsToShow;
	}

	$(c.crList).css({	
						'width':(listW+100)+"px",
						'height':listH+'px'
					});

	$(c.crId).css({	
					'width':clipW+'px',
					'height':clipH+'px'
				  });

	$(c.crId).find("li").each(function() {
		var c = $.clipregion;

		this.style.styleFloat = 'left';
		this.style.cssFloat = 'left';

		$(this).css({
				'list-style':'none',
				'overslow':'hidden',
				'width':c.itemWidth+"px",
				'height':c.itemHeight+'px',
				'padding':'0px',
				'margin':'0px',
				'border':'0px'
			});
	});
}
$.clipregion.setPos = function (v) { 
	var c = $.clipregion;
	v = $.intval(v);
	if(c.horiz) {
		$(c.crList).css("left",v*-1+"px");
	}
	else {
		$(c.crList).css("top",v*-1+"px");
	}
}
$.clipregion.getPos = function() {
	var c = $.clipregion;
	if(c.horiz) {
		return $.intval($.css(c.crList,"left")) * -1;
	}
	else {
		return $.intval($.css(c.crList,"top")) * -1;
	}
}

$.clipregion.getIdx = function() {
	var c = $.clipregion;
	var pos = c.getPos();	
	
	return (Math.round(pos/c.itemSize));
}

$.clipregion.setIdx = function(idx) {
	var c = $.clipregion;
	c.setPos((idx*c.itemSize)+c.itemSize);	
}

$.clipregion.next = function() {
	var c = $.clipregion;

	var pos = c.getPos();	

	if(c.itemStep) {
		//Ensure current position is stepping on itemsize
		var newPos = (Math.round(pos/c.itemSize) * c.itemSize) + c.itemSize;
	}
	else {
		var newPos = pos + c.itemSize;
	}

	var end = (c.totalItems*c.itemSize) - (c.itemsToShow*c.itemSize);
	if(newPos > end)
		newPos = end;

	if(c.loadNext != null)
		c.loadNext(newPos);
	c.setPos(newPos);
}

$.clipregion.prev = function() {
	var c = $.clipregion;
	var pos = c.getPos();	

	if(c.itemStep) {
		//Ensure current position is stepping on itemsize
		var newPos = (Math.round(pos/c.itemSize) * c.itemSize) - c.itemSize;
	}
	else {
		var newPos = pos - c.itemSize;
	}

	if(newPos < 0)
		newPos = 0;

	if(c.loadPrev != null)
		c.loadPrev(newPos);
	c.setPos(newPos);
}

$.intval = function(v) {
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};

