function swpArticleImage(imageSrc,caption,destElId, elId) {
	var fullImage = document.getElementById(destElId);
	var otherEl = document.getElementById(elId);
	fullImage.src=imageSrc;
	if (caption!=='') {
		otherEl.innerHTML = caption;
	}
}

if (typeof and == "undefined") {
	and={};
}

// LW - gallery functions
and.Gallery = (function(){
	var Image = function(src, url, alt, caption) {
		return {
			src : src,
			url : url,
			alt : alt,
			caption : caption
		}
	}
	var images = [];
	var reset = function() {
		images.clear();
	}
	var addImage = function(src, url, alt, caption) {
		images.push(Image(src, url, alt, caption));
	}
	var init = function() {
		var thumbs = $$('#thumbs li');
		var anchor = $('galleryMainLink');
		var img = $('galleryMainImg');
		var caption = $('galleryMainCaption');
		
		thumbs.each(function(thumb, i) {
		    thumb.observe('mouseover', function() {
		    	img.src = images[i].src;
		    	anchor.href = images[i].url;
		        caption.innerHTML = images[i].caption;
		    });
		});
	}
	return {
		addImage : addImage,
		reset : reset,
		init : init
	}
})();

// LW - carousel functions
and.Carousel = (function(){
	//========= scroll config ===========
	var rotationTimeout = 5000; //duration before scroll occurs (ms)
	var itemSize = 189; //size of each thumbnail item, in the axis of scrolling
	var viewportSize=571; //size of containing viewport, in the axis of scrolling
	//========= animation effect config ===========
	var duration = 400; //animation duration (ms)
	var fps = 35; //frames per sec
	//========= end of config ===========
	
	var currentIdx = 0;
	var rotationTimer;
	var uniqueItemCount;
	var blockWidth;
	var carousel;
	var anchor;
	var img;
	var caption;

	var refresh = 1000/fps;
	var refreshCount = duration/refresh;
	var startPos = 0;
	var distance = 0;
	var animating = false;
	var startTime;
	var animTimer;
    
	var init = function() {
		carousel = $('carousel');
		anchor = $('carouselMainLink');
		img = $('carouselMainImg');
		caption = $('carouselMainCaption');
		
		uniqueItemCount = images.length;
		blockWidth = uniqueItemCount * itemSize;
		var fillerBlockCount = Math.ceil(viewportSize/blockWidth);
		
		var thumbHtml = $('carousel-thumbs').innerHTML;
		var fillerHtml = '';
		for (var i = 0; i < fillerBlockCount; i++) fillerHtml += thumbHtml;
		$('carousel-filler').innerHTML = fillerHtml;
		
		startAutoChange();
	}
	var Image = function(src, url, alt, caption) {
		return {
			src : src,
			url : url,
			alt : alt,
			caption : caption
		}
	}
	var images = [];
	var reset = function() {
		images.clear();
	}
	var addImage = function(src, url, alt, caption) {
		images.push(Image(src, url, alt, caption));
	}
	var hoverInImage = function(thumbName) {
		stopAutoChange();
		var id = thumbName.replace(/^carousel-thumb/,"");
		displayImage(id);
	}	    
	var hoverOutImage = function() {
		startAutoChange();
	}
	var setPos = function(pos) {
		carousel.style.top = pos+"px";
	}
	var getPos = function() {
		return parseInt(carousel.style.top);
	}
	var nextItem = function(){
		var pos = parseInt(carousel.style.top);
	    if(pos == -blockWidth){
			pos = 0;
			carousel.style.top = pos+"px";
	    }
	    startAnim(pos - itemSize);
	}
	var previousItem = function(){
		var pos = parseInt(carousel.style.top);
	    if(pos == 0){
			pos = -blockWidth;
			carousel.style.top = pos+"px";
	    }
	    startAnim(pos + itemSize)
	}
	var startAutoChange = function() {
		rotationTimer = setTimeout('and.Carousel.autoRotateImage()',rotationTimeout);
	}
	var stopAutoChange = function(){
		clearTimeout(rotationTimer);
	};
	var autoRotateImage = function() {
		displayImage((currentIdx + 1) % uniqueItemCount);
		startAutoChange();
	}
	var displayImage = function(n) {
		currentIdx = n;
    	img.src = images[n].src;
    	anchor.href = images[n].url;
        caption.innerHTML = images[n].caption;
	}
	//========= animation with kinetic effect ===========
	var startAnim = function(pos) {
		if (!animating) {
			animating = true;
			startPos = getPos();
			distance = pos - startPos;
			startTime = getNow();
			animTimer = setInterval('and.Carousel.doAnim()',refresh);
		}
	}
	var doAnim = function() {
		var elapsed = getNow() - startTime;
		var stepCount = Math.floor(elapsed/refresh);
		var delta = (stepCount >= refreshCount)?distance:Math.ceil((Math.cos(stepCount/refreshCount*Math.PI)/-2+0.5)*distance);
		setPos(startPos+delta);
		if (stepCount >= refreshCount || stepCount>200) { //capped in case of endless loop
			clearInterval(animTimer);
			animating = false;
		}
	}
	var getNow = function() {
		return (new Date()).getTime();
	}
	//========= animation end ===========
	return {
		addImage : addImage,
		reset : reset,
		init : init,
		nextItem : nextItem,
		previousItem : previousItem,
		hoverInImage : hoverInImage,
		hoverOutImage : hoverOutImage,
		autoRotateImage : autoRotateImage,
		doAnim : doAnim
	}
})();
