/**
 * country start teaser carousel
 * 
 * requires jquery.carousel.js and jquery.js
 * 
 * does not use default external controls
 * 
 */
var carouselObj = null;
var teaserCount = 0;		// amount of teasers
var pageCount = 0;			// amount of pages
var TEASERSPERPAGE = 3;		// teasers visible per page
var IMAGEIDPREFIX = "ctrlBtn";
var IMAGEACTIVESRC = "/r/images/interface/carousel_active.png";
var IMAGEINACTIVESRC = "/r/images/interface/carousel_inactive.png";
var current = 1;			// the current "first" item

/**
 * inital callback, sets the global variables for teaser and page count
 * pageCount is teaserCount/teasers per page, result is ceiled if necessary
 * @param carousel
 */
function teaserCarousel_initCallback(carousel) {
	carouselObj = carousel;
	var ctrlButtons = getElementsByClass('controlBtnLink','a','document', false);
	teaserCount = ctrlButtons.size();
	if((teaserCount % TEASERSPERPAGE) == 0) {
		pageCount = teaserCount/TEASERSPERPAGE;
	} else {
		pageCount = Math.ceil(teaserCount/TEASERSPERPAGE);
	}
	//initialize controls for the first time
	changeControls(1);
}

/**
 * Callback function that gets called before animation onto a new item begins
 * @param carousel carousel object
 * @param item the new "first" item
 * @param idx index of the new "first" item
 * @param state state of carousel (next, previous or init)
 * @return nothing
 */
function teaserCarousel_itemFirstInCallbackBeforeAnimation(carousel, item, idx, state) {
	//if auto-mode is enabled, update controls to new position
	if(carouselObj.options.auto > 0) {
		changeControls(idx);
	}
	//reset the current "first" item
	current = idx;
}

/**
 * function is called once user clicks any of the buttons
 * @param triggerElm	anchor calling the function
 * @param teaserNr		number of the selected teaser
 */
function changeTeaser(triggerElm, teaserIndex) {
	imgElm = triggerElm.firstChild;
	//stop automatic scrolling
	carouselObj.stopAuto();
	carouselObj.options.auto = 0;
	//try to find out if we need to scroll to the clicked item
	newTeaserIndex = generateNewTeaserIndex(teaserIndex);
	if (newTeaserIndex != -1) {
		//scroll to the new item
		carouselObj.scroll(newTeaserIndex);
		//update controls
		changeControls(newTeaserIndex);
	}
}

/**
 * Checks if the new teaserIndex is outside the current page
 * @param teaserIndex the new teaserIndex
 * @return the new position to scroll to or -1 if no scrolling needed
 */
function generateNewTeaserIndex(teaserIndex) {
	var lastIndex = current + TEASERSPERPAGE - 1;
	if (teaserIndex > lastIndex) {
		return (teaserIndex - lastIndex) + current;
	} else if (teaserIndex < current) {
		return teaserIndex;
	} else {
		return -1;
	}
}
 
/**
 * change corresponding control buttons to highlight buttons
 * for teasers visible in the panel
 * if there are only teasers for exactly one page => highlight all controls
 * otherwise highlight only those visible on the page
 * 
 * @param teaserIndex	index of teaser to be shown
 * @return
 */
function changeControls(teaserIndex) {
	if((teaserIndex <= TEASERSPERPAGE) && (teaserIndex <= teaserCount) && (pageCount == 1)) {
		activateAllButtons();
	} else {
		currentPage = getCurrentPage(teaserIndex);
		resetAllButtons();
		
		if(currentPage < pageCount) {
			var adjacentElms = getNeighbourElements(teaserIndex, true);
			if(adjacentElms != null && adjacentElms.size()>0) {
				for(i=0;i<adjacentElms.size();i++) {
					adjacentElms[i].src = IMAGEACTIVESRC;
				}
			}
		} else {
			var adjacentElms = getNeighbourElements(teaserIndex, false);
			if(adjacentElms != null && adjacentElms.size()>0) {
				for(i=0;i<adjacentElms.size();i++) {
					adjacentElms[i].src = IMAGEACTIVESRC;
				}
			}
		}
	}
}

/**
 * function to determine the page number on which selected teaser is shown
 * by either ceiling current index / number of possible teasers on one page
 * which is fine
 * or in case of even result index / number of possible teasers +1
 * since jCarousel tries to move selected id to the leftmost position 
 * @param teaserIndex	currently selected index
 * @return				page index the selected item is shown on
 */
function getCurrentPage(teaserIndex) {
	var currentPage = 0;
	var testEven = teaserIndex % TEASERSPERPAGE;
	if(testEven == 0) {
		currentPage = teaserIndex/TEASERSPERPAGE; 
	} else {
		currentPage = Math.ceil(teaserIndex/TEASERSPERPAGE);
	}
	return currentPage;
}

/**
 * function the retrieve the HTML objects of the images that need to be changed to active state
 * since the carousel is not circular, the last page might need to activate buttons left of the index
 * siblings to the right are calculated by currently selected + number of possible teasers on one page -1
 * siblings to the left are calculated by complete amount - number of possible teasers on one page +1
 * 
 * @param currentIndex	index of currently selected teaser
 * @param toRight		boolean decides whether to add siblings in the right direction or left
 * @return				array of HTML Objects
 */
function getNeighbourElements(currentIndex, toRight) {
	var neighboursArray = new Array();
	if(toRight == true) {
		var maxCount = (currentIndex + TEASERSPERPAGE)-1;
		for(i=currentIndex;i<=maxCount;i++) {
			imgElm = document.getElementById(IMAGEIDPREFIX+i);
			if(imgElm != null) {
				neighboursArray.push(imgElm);
			}
		}
	} else {
		var startIndex = (teaserCount-TEASERSPERPAGE)+1;
		for(i=startIndex;i<=teaserCount;i++) {
			imgElm = document.getElementById(IMAGEIDPREFIX+i);
			if(imgElm != null) {
				neighboursArray.push(imgElm);
			}	
		}
	}
	return neighboursArray;
}

/**
 * set all buttons to active state
 * used when there is only one page
 */
function activateAllButtons() {
	for(i=1;i<=teaserCount;i++) {
		imageElm = document.getElementById(IMAGEIDPREFIX+i);
		imageElm.src = IMAGEACTIVESRC;
	}
}

/**
 * reset all control buttons to the inactive state
 */
function resetAllButtons() {
	for(i=1;i<=teaserCount;i++) {
		imageElm = document.getElementById(IMAGEIDPREFIX+i);
		imageElm.src = IMAGEINACTIVESRC;
	}
}