var conentFlowStopAndSelect = 0;
var contentFlowElementsCurrentNr = 1;
var contentFlowElementsNextNr = 1;
var currentContentFlowEffect;
var options_effect;
var options_length;
var options_interval;
var timeOutId;
var startAgain = 1;
var currentSlidePosition = 0;
var slideWidth = 720;
var callbackCount = 0;


/*
	pictureCollection[currentPicture].style.zIndex = 99;
	pictureCollection[loadPicture].setOpacity(0.01);
	pictureCollection[loadPicture].style.display='inline-block';
	pictureCollection[loadPicture].style.zIndex=100;
*/

// stop animation and select this element
function contentFlowSelectNr(nr) {
	if (timeOutId) {
		clearTimeout(timeOutId);
	}
	if (currentContentFlowEffect) {
		currentContentFlowEffect.cancel();
	}
	startAgain = 0;

	if(nr != contentFlowElementsCurrentNr) {
		contentFlowElementsNextNr = nr;
		$('contentFlowElement' + contentFlowElementsCurrentNr).style.zIndex=99;
		
		$('selectNumber'+ contentFlowElementsCurrentNr).removeClassName('act');
		
		$('contentFlowElement' + contentFlowElementsNextNr).setOpacity(0.01);
		$('contentFlowElement' + contentFlowElementsNextNr).style.display='inline-block';
		$('contentFlowElement' + contentFlowElementsNextNr).style.zIndex=100;
		$('selectNumber'+ contentFlowElementsNextNr).addClassName('act');
	
		new Effect.Appear('contentFlowElement' + contentFlowElementsNextNr, {to: 1, delay: 0, duration: 0.2});

		// set the counters
		contentFlowElementsCurrentNr = contentFlowElementsNextNr;
		contentFlowElementsNextNr++;
		// ST Change to try to cath the error which kills everything
		try {
			for (var i=1; i <= contentFlowElements.length; i++) {
				if(i != contentFlowElementsCurrentNr) {
					$('contentFlowElement' + i).style.zIndex=98;
					$('contentFlowElement' + i).setOpacity(0.01);
					$('contentFlowElement' + i).style.display='none';
					$('selectNumber'+ i).removeClassName('act');
				}
			}
		}catch(e){}
	}
}

// select current and show it
function contentFlowSelectCurrent() {
	if (timeOutId) {
		clearTimeout(timeOutId);
	}
	startAgain = 0;
}

// continue content flow after setting fixed element
function contentFlowContinue() {
	//alert('continue flow content');
	conentFlowStopAndSelect = 0;
	startAgain = 1;
	timeOutId = setTimeout("contentFlowEffect()", 1000);
}

function contentFlowEffectCallback() {
	//alert('callback');
	// go through elements
	// ST Change to try to cath the error which kills everything
	try {
		for (var i=1; i <= contentFlowElements.length; i++) {
			if(i != contentFlowElementsCurrentNr) {
				$('contentFlowElement' + i).style.zIndex=98;
				$('contentFlowElement' + i).setOpacity(0.01);
				$('contentFlowElement' + i).style.display='none';
				$('selectNumber'+ i).removeClassName('act');
			}
		}
	}catch(e){}

	if (startAgain) {
		timeOutId = setTimeout("contentFlowEffect()", options_interval*1000);
	}
}

// go through the effects
function contentFlowEffect() {
	//alert('start');

	// if it is the last image restart
	if(contentFlowElementsNextNr > contentFlowElements.length){
		contentFlowElementsNextNr = 1;
	}

	$('contentFlowElement' + contentFlowElementsCurrentNr).style.zIndex=99;
	$('contentFlowElement' + contentFlowElementsNextNr).setOpacity(0.01);
	$('contentFlowElement' + contentFlowElementsNextNr).style.display='inline-block';
	$('contentFlowElement' + contentFlowElementsNextNr).style.zIndex=100;
	$('selectNumber' + contentFlowElementsNextNr).addClassName('act');

	currentContentFlowEffect = new Effect.Appear('contentFlowElement' + contentFlowElementsNextNr, {from: 0.01, to: 1, delay: 0, duration: options_length, afterFinish: contentFlowEffectCallback});

	// set the counters
	contentFlowElementsCurrentNr = contentFlowElementsNextNr;
	contentFlowElementsNextNr++;
}

function contentFlowSlideEffectCallback() {
	callbackCount++;

	if (callbackCount > contentFlowElements.length) {
		callbackCount = 0;
	};
}

function contentFlowSlideEffect() {
	contentFlowElementsNextNr = contentFlowElementsCurrentNr + 1;

	// if it is the last image restart
	if(contentFlowElementsNextNr > contentFlowElements.length){
		contentFlowElementsNextNr = 1;
	}

	slideToContent(contentFlowElementsNextNr);
}

function slideToContent(nr, fast) {
	if (callbackCount > 0 || nr == contentFlowElementsCurrentNr) {
		return false;
	}

	callbackCount++;

	var calculatedPosition = currentSlidePosition;
	calculatedPosition = (contentFlowElementsCurrentNr - nr) * slideWidth;

	//slideWidth
	var slideDuration = options_length;
	if (fast) {
		slideDuration = 0;
	}
	new Effect.multiple(contentFlowElements, function(el) {
		new Effect.Move(el, { x: calculatedPosition, y: 0, duration: slideDuration, afterFinish: contentFlowSlideEffectCallback});
	});

	currentSlidePosition = calculatedPosition;
	contentFlowElementsCurrentNr = nr;

	clearTimeout(timeOutId);
	timeOutId = setTimeout("contentFlowSlideEffect()", options_interval*1000);
}

function nextFadeOverElement() {
	contentFlowElementsNextNr = contentFlowElementsCurrentNr + 1;

	// if it is the last image restart
	if(contentFlowElementsNextNr > contentFlowElements.length){
		contentFlowElementsNextNr = 1;
	}

	contentFlowSelectNr(contentFlowElementsNextNr);
}

// initialize
function contentFlowInit() {
	// global variable of the contentFlowElements
	contentFlowElements = $$('.contentFlowContent');

	if(contentFlowElements.length > 0) {
		options_effect = $$('.contentFlow .options .effect')[0].innerHTML;
		options_length = $$('.contentFlow .options .length')[0].innerHTML;
		options_interval = $$('.contentFlow .options .interval')[0].innerHTML;
		
		var stopLinks = '';

		for (var i=1; i <= contentFlowElements.length; i++) {
			contentFlowElements[(i-1)].up().id = 'contentFlowElement' + i
		}

		if (options_effect == 'slide') {
			var positionLeft = 0;
			for (var i=1; i <= contentFlowElements.length; i++) {
				var element = $('contentFlowElement' + i);
				element.style.left = positionLeft + 'px';
				positionLeft += slideWidth;
				element.observe("click", contentFlowSlideEffect);

				stopLinks = stopLinks + '<div class="contentFlowSelect" id="selectNumber'+i+'" onclick="slideToContent(' + i + ');">' + i + '</div>';
			}

			slideToContent(getRandom(1,contentFlowElements.length), true);
		} else {
			// always +1 to the array index
			contentFlowElementsCurrentNr = getRandom(1,contentFlowElements.length);
	
			contentFlowElementsNextNr = contentFlowElementsCurrentNr + 1;

			// go through elements
			for (var i=1; i <= contentFlowElements.length; i++) {
				$('contentFlowElement' + i).observe('mouseover', contentFlowSelectCurrent);
				$('contentFlowElement' + i).observe('mouseout', contentFlowContinue);

				$('contentFlowElement' + i).observe("click", nextFadeOverElement);

				if (i == contentFlowElementsCurrentNr) {
					$('contentFlowElement' + i).style.zIndex=100;
					$('contentFlowElement' + i).setOpacity(1);
					$('contentFlowElement' + i).style.display='inline-block';
				} else {
					$('contentFlowElement' + i).style.zIndex=98;
					$('contentFlowElement' + i).setOpacity(0.01);
					$('contentFlowElement' + i).style.display='none';
				}
	
				stopLinks = stopLinks + '<div class="contentFlowSelect" id="selectNumber'+i+'" onmouseout="contentFlowContinue();" onmouseover="contentFlowSelectNr(' + i + ');">' + i + '</div>';
			}
			timeOutId = setTimeout("contentFlowEffect()", options_interval*1000);
		}

		$$('.contentFlow .numbers')[0].innerHTML = stopLinks;
	}
}

function getRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
	return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

//document.observe("dom:ready", contentFlowInit);
//Event.observe(window, 'load', contentFlowInit);

//create onDomReady Event
window.onDomReady = initReady;
// Initialize event depending on browser
function initReady(fn) {
	//W3C-compliant browser
	if(document.addEventListener) {
		document.addEventListener("DOMContentLoaded", fn, false);
	}
	//IE
	else {
		document.onreadystatechange = function(){readyState(fn)}
	}
}

//IE execute function
function readyState(func) {
	// DOM is ready
	if(document.readyState == "interactive" || document.readyState == "complete") {
		func();
	}
}

window.onDomReady(contentFlowInit);



