var conentFlowStopAndSelect = 0;
var contentFlowElementsCurrentNr = 1;
var contentFlowElementsNextNr = 1;
var currentContentFlowEffect;
var options_effect;
var options_length;
var options_interval;
var timeOutId;
var startAgain = 0;
var currentSlidePosition = 0;
var slideWidth = 940;
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;
		$('contentFlowElement' + contentFlowElementsNextNr).setOpacity(0.01);
		$('contentFlowElement' + contentFlowElementsNextNr).style.display='inline-block';
		$('contentFlowElement' + contentFlowElementsNextNr).style.zIndex=100;

		slideOutTextarea(contentFlowElementsCurrentNr);
		new Effect.Appear('contentFlowElement' + contentFlowElementsNextNr, {to: 1, delay: 0, duration: 0.2, afterFinish: contentFlowContinueAfterClick});

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

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

// continue content flow after setting fixed element
function contentFlowContinueAfterClick() {
	conentFlowStopAndSelect = 1;
	startAgain = 0;
	//timeOutId = setTimeout("contentFlowEffect()", options_interval*1000);
	slideInTextarea(contentFlowElementsCurrentNr);
}

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

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';
				slideOutTextarea(i);
			}
		}
	}catch(e){}

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

// 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;

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

	$('contentFlowSelectID_' + contentFlowElementsNextNr).addClassName('contentFlowSelectActive');
	$('contentFlowSelectID_' + contentFlowElementsCurrentNr).removeClassName('contentFlowSelectActive');

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

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

	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;
		}
		for (var i=1; i <= contentFlowTextareaElements.length; i++) {
			contentFlowTextareaElements[(i-1)].id = 'contentFlowTextareaElement' + i;
		}

		contentFlowElementsCurrentNr = 1

		contentFlowElementsNextNr = contentFlowElementsCurrentNr + 1;

		// go through elements
		for (var i=1; i <= contentFlowElements.length; i++) {
			slideOutTextarea(i);

			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="contentFlowSelectID_' + i + '" onclick="contentFlowSelectNr(' + i + ');">' + i + '</div>';
		}
		timeOutId = setTimeout("contentFlowContinue()", options_interval*1000);

		$$('.contentFlow .numbers')[0].innerHTML = stopLinks;
		$('contentFlowSelectID_' + contentFlowElementsCurrentNr).addClassName('contentFlowSelectActive');
		slideInTextarea(contentFlowElementsCurrentNr);
	}
}

function slideInTextarea(number) {
	new Effect.Move('contentFlowTextareaElement' + number, {
		y: 85, x: 662, mode: 'absolute',
		duration: 1
	});
}

function slideOutTextarea(number) {
	new Effect.Move('contentFlowTextareaElement' + number, {
		y: 85, x: 1000, mode: 'absolute',
		duration: 0
	});
}

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


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);


