function getCurrentLeftPosition(id) {
	var pos = $('#' + id).show().position();
	return pos.left;
}

function goPrev(id, containerid, pageid, width, itemsShown, speed, inactiveOpacity) {
	if ($('#' + id).is(':animated')) {
		return false;
	}
	var currentLeft = getCurrentLeftPosition(id);
	var totalItems = $('#' + id + '> li').length;
	var totalWidth = -(width * totalItems);	
	var viewableWidth = -(width * itemsShown);
	var pages = $('#' + pageid + ' > .page').length;	
	var inactiveOpacity;
	$('#' + pageid + ' > .page').removeClass('current');
	if(pages > 1) {
		$('#' + containerid + ' .next').css({opacity:1,cursor:'pointer'});
	}
	if(-(currentLeft%viewableWidth) > 0) {
		if(pages == 2) {
			$('#' + containerid + ' .previous').css({opacity:inactiveOpacity,cursor:'default'});
		}
		$('#' + id).animate( { left: currentLeft + (totalItems%itemsShown)*width }, speed);
		$('#' + pageid + ' > .page').eq(pages-2).addClass('current');
	} else if(currentLeft == -(width*itemsShown)) {
		$('#' + id).animate( { left: currentLeft + width * itemsShown }, speed);
		$('#' + pageid + ' > .page').eq(parseInt((currentLeft - viewableWidth)/viewableWidth)).addClass('current');
		$('#' + containerid + ' .previous').css({opacity:inactiveOpacity,cursor:'default'});
	} else if(currentLeft >= '0') {
		$('#' + id).css('left: 0');		
		$('#' + pageid + ' > .page').eq(0).addClass('current');		
	} else {
		$('#' + id).animate( { left: currentLeft + width * itemsShown }, speed);
		$('#' + pageid + ' > .page').eq(parseInt((currentLeft - viewableWidth)/viewableWidth)).addClass('current');
		$('#' + containerid + ' .next').css({opacity:1});
	}	
}

function goNext(id, containerid, pageid, width, itemsShown, speed, inactiveOpacity) {		
	if ($('#' + id).is(':animated')) {
		return false;
	}
	var currentLeft = getCurrentLeftPosition(id);
	var totalItems = $('#' + id + '> li').length;
	var totalWidth = -(width * totalItems/2);	
	var viewableWidth = -(width * itemsShown);
	var leftOverWidth = totalWidth - currentLeft - viewableWidth;
	var pages = $('#' + pageid + ' > .page').length;
	var inactiveOpacity;
	if(pages > 1) {
		$('#' + containerid + ' .previous').css({opacity:1, cursor:'pointer'});
		$('#' + pageid + ' > .page').removeClass('current');
		if ((leftOverWidth == 0) || (totalItems < 4)) {
			$('#' + id).css('left', currentLeft);		
			$('#' + pageid + ' > .page').eq(pages-1).addClass('current');
		} else if(((leftOverWidth) > viewableWidth) && ((leftOverWidth) < 0)) {
			$('#' + id).animate( { left: currentLeft - (totalItems%itemsShown) * width }, speed);
			$('#' + pageid + ' > .page').eq(pages-1).addClass('current');
			$('#' + containerid + ' .next').css({opacity:inactiveOpacity,cursor:'default'});
		} else if(-leftOverWidth == width*itemsShown) {			
			$('#' + pageid + ' > .page').eq(parseInt((currentLeft + viewableWidth)/viewableWidth)).addClass('current');
			$('#' + id).animate( { left: currentLeft + viewableWidth }, speed);	
			$('#' + containerid + ' .next').css({opacity:inactiveOpacity,cursor:'default'});
		} else {
			$('#' + pageid + ' > .page').eq(parseInt((currentLeft + viewableWidth)/viewableWidth)).addClass('current');
			$('#' + id).animate( { left: currentLeft + viewableWidth }, speed);		
		}
	}
}

function pagination(id, containerid, pageid, width, itemsShown, speed, inactiveOpacity) {
	var inactiveOpacity;
	if ($('#' + id).is(':animated')) {
		return false;
	}
	var totalItems = $('#' + id + '> li').length/2;
	var pages = parseInt(totalItems/itemsShown);	
	if ((totalItems%itemsShown) > 0) {
		pages += 1;
	}
	$('#' + containerid + ' .previous').css({opacity:inactiveOpacity,cursor:'default'});
	if(pages > 0 && pages < 2) {
		$('#' + pageid + ' > li').addClass('current');
		$('#' + containerid + ' .previous').css({opacity:inactiveOpacity,cursor:'default'});
		$('#' + containerid + ' .next').css({opacity:inactiveOpacity,cursor:'default'});
	} else if(pages > 1) {		
		$('#' + pageid + ' > li').remove();	
		for(i = 0; i < pages; i++) {
			$('<li class="page">' + (i+1) + '</li>').appendTo('#' + pageid);			
		}
		$('#' + pageid + ' > .page').each(
			function(i) {
				$( this ).bind ("click", function(){
					$('#' + containerid + ' .previous').css({opacity:1});
					$('#' + containerid + ' .next').css({opacity:1});
					if(i == 0) {
						$('#' + containerid + ' .previous').css({opacity:inactiveOpacity,cursor:'default'});
					}
					$('#' + pageid + ' > .page').removeClass('current');
					if(i == pages-1) {
						$('#' + containerid + ' .next').css({opacity:inactiveOpacity,cursor:'default'});
						$(this).addClass('current');
						if(totalItems%itemsShown > 0) {
							$('#' + id).animate( { left: -((i-1)*width*itemsShown+width*(totalItems%itemsShown)) }, speed);
						} else {
							$('#' + id).animate( { left: -(i*width*itemsShown) }, speed);
						}
					} else {
						$(this).addClass('current');
						$('#' + id).animate( { left: -(i*width*itemsShown) }, speed);
					}
				});
			}
		);
	} else {
		$('<li>&nbsp;</li>').appendTo('#' + pageid);
	}	
}	
