
/**
 * 
 *  Scroller / Tweener Javascript for MurMur.
 *  Contact pod1.com for assistance
 * 
 */

	Effect.Scroll = Class.create();
	
	Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {
	
		initialize: function(element) {
			this.element = $(element);
			var options = Object.extend({
				x:    0,
				y:    0,
				mode: 'absolute'
			} , arguments[1] || {}  );
			this.start(options);
		},
	
		setup: function() {
			if (this.options.continuous && !this.element._ext ) {
				this.element.cleanWhitespace();
				this.element._ext=true;
				this.element.appendChild(this.element.firstChild);
			}
	
			this.originalLeft=this.element.scrollLeft;
			this.originalTop=this.element.scrollTop;
	
			if(this.options.mode == 'absolute') {
				this.options.x -= this.originalLeft;
				this.options.y -= this.originalTop;
			} else {
	
			}
		},
		
		update: function(position) {
			newPosition = easeInOutQuad(position, 0, 1, 1);
			this.element.scrollLeft = this.options.x * newPosition + this.originalLeft;
			this.element.scrollTop  = this.options.y * newPosition + this.originalTop;
		}
		
	});
	





	function moveTo(container, element, duration){
	
	  var duration = duration || 1;
	
	  if(typeof($(element)) != 'undefined')
	  {
		  Position.prepare();
		  container_x = Position.cumulativeOffset($(container))[0];
		  element_x = Position.cumulativeOffset($(element))[0];
		  new Effect.Scroll(container, {x:(element_x-container_x), y:0, duration: duration});
	  }
	  return false;
	}


	function easeInOutQuad(t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	};

	// set up scroller

	$('holder').setStyle( { overflow : 'hidden'});
	$('post_wrapper').setStyle( { width: '10000px'});
	
	var postElements = $$('li.post');
	postElements.each ( function (e) { 
		
		e.setStyle( {float: 'left' } );
	} );
	
	
	var currentElement = 1;
	var totalElements = postElements.length;
	
	function canGoNext() {
		return (currentElement < totalElements);
	}
	
	function canGoPrevious() {
		return (currentElement > 1);
	}
	
	function goNext() {
		currentElement++;
		moveTo('holder', postElements[ ( currentElement - 1 ) ], 0.5);
		updatePagination()
	}
	
	function goPrevious() {
		currentElement--;
		moveTo('holder', postElements[ ( currentElement - 1 ) ], 0.5);
		updatePagination()
	}
	
	function updatePagination() {
		if(canGoNext()) {
			$('gonext1').show();
			$('gonext').show();
		} else {
			$('gonext1').hide();
			$('gonext').hide();
		}
		if(canGoPrevious()) {
			$('goprevious1').show();
			$('goprevious').show();
		} else {
			$('goprevious1').hide();
			$('goprevious').hide();
		}
	}
	
	function timerScroll() {
		if(!canGoNext()) {
			currentElement = 0;
		}
		goNext();
		updatePagination();
	}
	
	function stopTimer() {
		//window.clearInterval(timerId);
	}
	
	//var timerId = setInterval( timerScroll, 4000 );
	
	updatePagination();





/***** NEW SLIDER SCRIPT *****/
	
	
	
/**
* RECENTLY VIEWED SECTION
*
**/
var FirstShowing = 0; // -- removed for fall back as its not always the first element showing then
var ShowingAtATime = 3;
var RecentlyViewedIncrement = 1;
var CurrentlyInList	= 0;
var LastViewable = 0;
var Slideactive = 'false';
var ActiveLink = 1;

/**
* Sets values for recently viewed variables in order to show next
*
**/
function ShowNext(Container)
{
	if(Slideactive == 'false')
	{
		CurrentlyInList = Container.getElementsByTagName('li').length;

		LastViewable = CurrentlyInList;

		FirstShowing = FirstShowing + RecentlyViewedIncrement;

		setTimeout('MoveSlider($(\''+Container.id+'\'), \'next\', '+(RecentlyViewedIncrement*475)+')', 200);
		ActiveLink = ActiveLink + 1;
	}
}

/**
* Sets values for recently viewed variables in order to show previous
*
**/
function ShowPrevious(Container)
{
	if(Slideactive == 'false')
	{
		if(-1 < FirstShowing - RecentlyViewedIncrement)
		{
			FirstShowing = FirstShowing - RecentlyViewedIncrement;
			CurrentlyInList = Container.getElementsByTagName('a').length;

			MoveSlider(Container, 'previous', (RecentlyViewedIncrement*475));
		}
		ActiveLink = ActiveLink - 1;
	}
}

function MoveSlider(Container, Direction, Offset)
{
	if('next' == Direction)
	{
		Offset = Offset * -1;
	}
	new Effect.Move(Container, { x:  (Offset), y: 0, duration: 0.5, beforeStart: function() { Slideactive = 'true'; }, afterFinish: function() { setTimeout("Slideactive = 'false', 100"); } });

	//hide/show next link
	if(ActiveLink == (LastViewable))
	{
		$('next-link').hide();
		$('next-link2').hide();
		ActiveLink = ActiveLink - 1;
	}
	else
	{
		$('next-link').show();
		$('next-link2').show();
	}

	//hide/show previous link
	if(0 == FirstShowing)
	{
		$('previous-link').hide();
		$('previous-link2').hide();
		ActiveLink = ActiveLink + 1;
	}
	else
	{
		$('previous-link').show();
		$('previous-link2').show();
	}
}
