var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;
var $A = YAHOO.util.Easing;
var $ = $D.get; // get element by ID


function scrollMe(containerNode, startngAt, endScrollAt) {
	var currentValue = endScrollAt;
	var anim = new YAHOO.util.Anim(containerNode.getElementsByTagName('ul')[0], { left: { to: endScrollAt } }, 0.6, $A.easeOut);
	anim.animate();
}

function initiateScroller(containerNode, direction, elementWidth, startngAt, numberOfMin) {
	/*
	@containerNode: the bounding box in which things are scrolled
	@direction: which direction to scroll
	@elementWidth: width of each LI element in the scrollable area
	@startngAt: starting position for scroller
	@numberOfMin: fewest number of scrollable items to show by default		
	*/
	
	var viewWindowWidth=$D.getStyle(containerNode,'width').replace("px","");
	

	//calc amount of move based on the width of each element and how many are showing at once.
	
	var amountToMove;
	if (numberOfMin == 2) {
		// If we are dealing with a 2-wide thing, use half the window width
		amountToMove = viewWindowWidth / 2;
	} else {
		amountToMove = elementWidth;
	}
	
	var numberOfElements = containerNode.getElementsByTagName('li').length;
	var totalNegative =- (numberOfElements * amountToMove);
	
	//make the value negative, if scrolling left
	if(direction == 'right') {
		amountToMove = -amountToMove;
	}
	
	var endScrollAt = startngAt + amountToMove; // HERE i think is the issue?

	if(direction=='left' && endScrollAt>0){
		endScrollAt=0;
	}
		
	//do scroll
	scrollMe(containerNode, startngAt, endScrollAt);
	
	//now reset buttons as appropriate
	var previousButton = $D.getElementsByClassName('previous', 'a', containerNode)[0];
	if(endScrollAt != 0) {
		previousButton.onclick = function(){initiateScroller(containerNode,'left',elementWidth, endScrollAt, numberOfMin)};
		$D.removeClass(previousButton,"disabled");
	} else {
		previousButton.onclick = function(){};
		$D.addClass(previousButton,"disabled");
	}
	
	var nextButton = $D.getElementsByClassName('next','a',containerNode.id)[0];
	if (endScrollAt-viewWindowWidth > totalNegative){
		nextButton.onclick = function(){initiateScroller(containerNode,'right', elementWidth, endScrollAt, numberOfMin)};
		$D.removeClass(nextButton,"disabled");
	} else {
		nextButton.onclick = function(){}
		$D.addClass(nextButton,"disabled");
	}
}
function checkMeta() {
  var metas = document.getElementsByTagName('META');
  var i;
  for (i = 0; i < metas.length; i++)
    if (metas[i].getAttribute('name') == "DC.language")
      break;
  var TestVar = metas[i].getAttribute('content');
  return TestVar;
}

// Given a div initiates the scroller
function configScroller(containerNode, numberOfMin, widthOfElement) {
	//generates buttons if appropriate
	if(containerNode.getElementsByTagName('li').length > numberOfMin) {
		var locale= checkMeta();
		//language textual options
		var moretext="";
		if(locale=="fr-ch"){
			moretext="Autres";
		}
		else if(locale=="es-es"){
		}
		else if(locale=="pt-br"){
		}
		else if(locale=="ru-ru"){
		}
		else if(locale=="zh-cn"){
		}
		else{
			moretext="More";
		}
		var nexttext="Next stories";
		var previoustext="Previous stories";
		
		var scrollerNav = document.createElement('div');
		scrollerNav.className="scrollernav";
		
		//this additional to the width is necessary to account for the margin required to allow the nav buttons to float next to each item
			widthOfElement+=50;		
		
		var previousButton = document.createElement('a');
		previousButton.className="previous disabled";
		previousButton.setAttribute('title',previoustext);
		previousButton.appendChild(document.createTextNode(previoustext));
		//previousButton needs no onclick as yet. Defined in initScroller

		var nextButton = document.createElement('a');
		nextButton.className="next";
		nextButton.setAttribute('title',nexttext);
		nextButton.appendChild(document.createTextNode(nexttext));
		nextButton.onclick = function(){initiateScroller(containerNode,'right',widthOfElement, 0, numberOfMin)}
		
		var more = document.createElement('p');
		more.className="more";
	
		// append elements
		scrollerNav.appendChild(previousButton);
		scrollerNav.appendChild(nextButton);
		containerNode.appendChild(scrollerNav);
		containerNode.appendChild(more);
	}
}


function init() {
	var scrollers=$D.getElementsByClassName('scroller','div',$('pagebody'));
	for(i=0; i<scrollers.length; i++) {
		//find width of a sample item, so we can calculate movement
		var sampleNode= scrollers[i].getElementsByTagName('li')[0];
		var widthOfElement = parseInt($D.getStyle(sampleNode,'width').replace("px",""));
		if($D.hasClass(scrollers[i], 'scroller2')) {
			// its a 2scroller so we dont show the scroll buttons unless there are more than 2 things
			configScroller(scrollers[i], 2, widthOfElement);
		} else {
			configScroller(scrollers[i], 1, widthOfElement);
		}
	}
}

$E.onDOMReady(init); 

