
var firstCarousel=new Array();

function _checkWrap(items,i){
		if(!firstCarousel.loop){
			return false
		}
		//if(firstCarousel.xpos[i]<(firstCarousel.width*-2)){
		if(parseInt(firstCarousel.items[i].style.left)<(firstCarousel.width*-1)){
			//firstCarousel.xpos[i]=firstCarousel.xpos[i]+(firstCarousel.width*(items.length));
			firstCarousel.xpos[i]=firstCarousel.xpos[i]+firstCarousel.wrapWidth;
			//firstCarousel.items[i].style.left = (firstCarousel.xpos[i]+firstCarousel.width+firstCarousel.offsetpx)+"px";
			firstCarousel.items[i].style.left = (parseInt(firstCarousel.items[i].style.left)+firstCarousel.wrapWidth)+"px";
			//alert (firstCarousel.xpos[i]+firstCarousel.offsetpx);
			return 
		}
		//if(firstCarousel.xpos[i]>=(firstCarousel.width*(items.length-1))){
		//alert(firstCarousel.items[i].style.left+' wrapwidth='+firstCarousel.wrapWidth+'-width='+firstCarousel.width+'_xpos='+firstCarousel.xpos[i]+' offset='+firstCarousel.offsetpx);
		//alert(parseInt(firstCarousel.items[i].style.left)+' >- '+(firstCarousel.wrapWidth-firstCarousel.offsetWidthpx));
		if(parseInt(firstCarousel.items[i].style.left)>=(firstCarousel.wrapWidth-firstCarousel.width)){
			//firstCarousel.xpos[i]=firstCarousel.xpos[i]-(firstCarousel.width*items.length);
			firstCarousel.xpos[i]=firstCarousel.xpos[i]-firstCarousel.wrapWidth;
			//firstCarousel.items[i].style.left =(firstCarousel.xpos[i]-firstCarousel.width+firstCarousel.offsetpx)+"px";
			firstCarousel.items[i].style.left =(parseInt(firstCarousel.items[i].style.left)-firstCarousel.wrapWidth)+"px";
			return 
		}
}

function Carousel(a,b,paramVisible,paramWidthScale,paramInterval,paramIncrement,e,f,g,h,paramPrevImg,paramNextImg){
	firstCarousel.xpos=new Array();
	firstCarousel.loop=b;
	firstCarousel.animating=false;
	firstCarousel.widthScale=paramWidthScale;
	firstCarousel.visibleItems=paramVisible;
	firstCarousel.interval = paramInterval;
	firstCarousel.animIncrement = paramIncrement;
	firstCarousel.animationStage = 0;
	firstCarousel.animationSteps = 20;
	firstCarousel.windowSize=e;
	firstCarousel.itemSpacing=f;
	firstCarousel.offset=g;
	firstCarousel.containerId=a;
	firstCarousel.tag=h;
	firstCarousel.holder = document.getElementById(firstCarousel.containerId).getElementsByTagName(firstCarousel.tag)[0];
	
	firstCarousel.items=firstCarousel.holder.getElementsByTagName('li');
	var D=firstCarousel.items[0];
	firstCarousel.width=(D.getElementsByTagName('img')[0].offsetWidth*firstCarousel.widthScale)+firstCarousel.itemSpacing;
	firstCarousel.offsetpx=Math.round(firstCarousel.width*firstCarousel.offset);
	// set width and height
	firstCarousel.holder.style.width = ((firstCarousel.windowSize*firstCarousel.width)-firstCarousel.itemSpacing)+ 'px';
	firstCarousel.holder.style.height = D.getElementsByTagName('img')[0].offsetHeight+'px';
	//alert(D.getElementsByTagName('img')[0].offsetHeight);
	//alert(C+' '+A);
	
	firstCarousel.mult=firstCarousel.width;
	firstCarousel.currentVisibleItems=[];
	// add controls
	var control = document.createElement('img');
	control.setAttribute('src', paramPrevImg);
	control.setAttribute('alt', 'Previous item');
	addCarouselListener(control,'click',carouselClickLeft);
	prevPointer=document.getElementById('carouselControlLeft').appendChild(control);
	//prevPointer.style.paddingTop = (D.getElementsByTagName('img')[0].offsetHeight/2 - prevPointer.offsetHeight/2) + 'px';
	var control2 = document.createElement('img');
	control2.setAttribute('src', paramNextImg);
	control2.setAttribute('alt', 'Next item');
	addCarouselListener(control2,'click',carouselClickRight);
	nextPointer=document.getElementById('carouselControlRight').appendChild(control2);
	//nextPointer.style.paddingTop = (D.getElementsByTagName('img')[0].offsetHeight/2 - nextPointer.offsetHeight/2) + 'px';
	document.getElementById('carouselControlRight').style.left=(30+((firstCarousel.windowSize*firstCarousel.width)-firstCarousel.itemSpacing))+'px';
	
		
	_setDefaultPositions(firstCarousel.items);
	_moveItems(firstCarousel.items,0)
	}

function carouselClickLeft() {
	_moveItems(firstCarousel.items,firstCarousel.width);
}
function carouselClickRight() {
	_moveItems(firstCarousel.items,-firstCarousel.width);
}

function addCarouselListener(element, type, expression, bubbling) {
		bubbling = bubbling || false;
		if(window.addEventListener)	{ // Standard
			element.addEventListener(type, expression, bubbling);
			return true;
		} else if(window.attachEvent) { // IE
			element.attachEvent('on' + type, expression);
			return true;
		} else return false;
	}
	
function _setDefaultPositions(items){
	//alert(items.length);
	for (var i=0; i<items.length;i++){
		//alert(i);
		items[i].style.position='absolute';
		firstCarousel.xpos[i]=i*firstCarousel.width;
	}
	firstCarousel.wrapWidth = items.length*firstCarousel.width;
}
	
function _moveItems(items,vector){
	if (firstCarousel.animating==false){
		var D=this;
		var B=[];
		var C=0;
		var A=firstCarousel.mult*firstCarousel.visibleItems;
		for (var i=0; i<items.length;i++){
		// alert(firstCarousel+'_'+firstCarousel.xpos.length+'_'+firstCarousel.visibleItems+'_'+firstCarousel.xpos+'_'+i);
			firstCarousel.xpos[i]=firstCarousel.xpos[i]+vector;
			//_checkWrap(items,i);
			if(firstCarousel.xpos[i]>=0&&firstCarousel.xpos[i]<A){
				B.push(i)
				//yui-carousel-item-selected
				// add class to selected item
				items[i].className = 'item news-page-list-item yui-carousel-item-selected';
			}else{
				items[i].className = 'item news-page-list-item';
			}
			if (vector==0){
				items[i].style.left=(firstCarousel.xpos[i]+firstCarousel.offsetpx)+"px";
				items[i].style.top="0px";
				_checkWrap(items,i);
			}
			//alert(firstCarousel.xpos[i]);
		}
		if (vector!=0){ //start animation
			firstCarousel.animating=true;
			firstCarousel.animationStage=0;
			firstCarousel.timer = setInterval(carouselAnimate,firstCarousel.interval);
			firstCarousel.currentVisibleItems=B
		}
	}
}
	
function carouselAnimate() {
	var targetX;
	var complete=false;
	var increment=firstCarousel.animIncrement;
	var direction;
	if ((firstCarousel.xpos[0]+firstCarousel.offsetpx)-firstCarousel.items[0].offsetLeft>=0) {
		direction=1;
	}else{
		direction=-1;
	}	//=((firstCarousel.xpos[0]+firstCarousel.offsetpx)-firstCarousel.items[0].offsetLeft)/Math.abs((firstCarousel.xpos[0]+firstCarousel.offsetpx)-firstCarousel.items[0].offsetLeft);
	var vector=increment*direction;
	//for (var i=0; i<items.length;i++){
	targetX=firstCarousel.items[0].offsetLeft+vector;
	//alert(vector+'_'+targetX+'_'+(firstCarousel.xpos[0]+firstCarousel.offsetpx)+'_'+firstCarousel.items[0].offsetLeft);
	if ((targetX>=(firstCarousel.xpos[0]+firstCarousel.offsetpx) && direction>0) || (targetX<=(firstCarousel.xpos[0]+firstCarousel.offsetpx) && direction <0)) {
		for (var i=0; i<firstCarousel.items.length;i++){
			_checkWrap(firstCarousel.items,i);
			firstCarousel.items[i].style.left=(firstCarousel.xpos[i]+firstCarousel.offsetpx)+"px";
			firstCarousel.items[i].style.top='0px';
		}
		firstCarousel.animating=false;
		firstCarousel.animationStage=0;
		clearInterval(firstCarousel.timer);
		firstCarousel.timer = null;

	}else{
		//alert(firstCarousel.items[0].offsetLeft+'_'+firstCarousel.items[0].style.left);
		for (var i=0; i<firstCarousel.items.length;i++){
			_checkWrap(firstCarousel.items,i);
			firstCarousel.items[i].style.left = (firstCarousel.items[i].offsetLeft+vector)+'px';
			firstCarousel.items[i].style.top='0px';
		}
		//firstCarousel.animating=false;
		//firstCarousel.animationStage=0;
		//clearInterval(firstCarousel.timer);
		//firstCarousel.timer = null;
		//firstCarousel.timer = setInterval(carouselAnimate,firstCarousel.interval);
	}
}
	

