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;

function contentFlowEffectCallback() {
	// 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';
			}
		}
	}catch(e){}
	if (startAgain) {
		timeOutId = setTimeout("contentFlowEffect()", options_interval*1000);
	}
}

// go through the effects
function contentFlowEffect() {
	// 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});

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

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

			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++) {
				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';
				}
			}
			timeOutId = setTimeout("contentFlowEffect()", options_interval*1000);
		}
	}
}






























var contentFlowElement_rightsCurrentNrRight = 1;
var contentFlowElement_rightsNextNrRight = 1;
var currentContentFlowEffectRight;
var options_effectRight;
var options_lengthRight;
var options_intervalRight;
var timeOutIdRight;
var startAgainRight = 1;
var currentSlidePositionRight = 0;
var slideWidthRight = 720;
var callbackCountRight = 0;

function contentFlowEffectCallbackRight() {
	// go through elements
	// ST Change to try to cath the error which kills everything
	try {
		for (var i=1; i <= contentFlowElement_rights.length; i++) {
			if(i != contentFlowElement_rightsCurrentNrRight) {
				$('contentFlowElement_right' + i).style.zIndex=98;
				$('contentFlowElement_right' + i).setOpacity(0.01);
				$('contentFlowElement_right' + i).style.display='none';
			}
		}
	}catch(e){
	
	}
	if (startAgainRight) {
		timeOutIdRight = setTimeout("contentFlowEffectRight()", options_intervalRight*1000);
	}
}

// go through the effects
function contentFlowEffectRight() {
	// if it is the last image restart
	if(contentFlowElement_rightsNextNrRight > contentFlowElement_rights.length){
		contentFlowElement_rightsNextNrRight = 1;
	}
	
	$('contentFlowElement_right' + contentFlowElement_rightsCurrentNrRight).style.zIndex=99;
	$('contentFlowElement_right' + contentFlowElement_rightsNextNrRight).setOpacity(0.01);
	$('contentFlowElement_right' + contentFlowElement_rightsNextNrRight).style.display='inline-block';
	$('contentFlowElement_right' + contentFlowElement_rightsNextNrRight).style.zIndex=100;

	currentContentFlowEffectRight = new Effect.Appear('contentFlowElement_right' + contentFlowElement_rightsNextNrRight, {from: 0.01, to: 1, delay: 0, duration: options_lengthRight, afterFinish: contentFlowEffectCallbackRight});

	// set the counters
	contentFlowElement_rightsCurrentNrRight = contentFlowElement_rightsNextNrRight;
	contentFlowElement_rightsNextNrRight++;
}

function contentFlowSlideEffectCallback() {
	callbackCountRight++;

	if (callbackCountRight > contentFlowElement_rights.length) {
		callbackCountRight = 0;
	};
}

function contentFlowSlideEffect() {
	contentFlowElement_rightsNextNrRight = contentFlowElement_rightsCurrentNrRight + 1;

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

	slideToContent(contentFlowElement_rightsNextNrRight);
}

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

	callbackCountRight++;

	var calculatedPosition = currentSlidePositionRight;
	calculatedPosition = (contentFlowElement_rightsCurrentNrRight - nr) * slideWidthRight;

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

	currentSlidePositionRight = calculatedPosition;
	contentFlowElement_rightsCurrentNrRight = nr;

	clearTimeout(timeOutIdRight);
	timeOutIdRight = setTimeout("contentFlowSlideEffect()", options_intervalRight*1000);
}

// initialize
function contentFlowInitRight() {
	// global variable of the contentFlowElement_rights
	contentFlowElement_rights = $$('.contentFlowContent_right');

	if(contentFlowElement_rights.length > 0) {
		options_effectRight = $$('.contentFlow_right .options .effect')[0].innerHTML;
		options_lengthRight = $$('.contentFlow_right .options .length')[0].innerHTML;
		options_intervalRight = $$('.contentFlow_right .options .interval')[0].innerHTML;
		
		var stopLinks = '';

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

		if (options_effectRight == 'slide') {
			var positionLeft = 0;
			for (var i=1; i <= contentFlowElement_rights.length; i++) {
				var element = $('contentFlowElement_right' + i);
				element.style.left = positioncurrentContentFlowEffectRightLeft + 'px';
				positionLeft += slideWidthRight;
				element.observe("click", cocontentFlowElement_rightsCurrentNrRightRightntentFlowSlideEffect);
			}

			slideToContent(getRandomRight(1,contentFlowElement_rights.length), true);
		} else {
			// always +1 to the array index
			contentFlowElement_rightsCurrentNrRight = getRandomRight(1,contentFlowElement_rights.length);
			
			contentFlowElement_rightsNextNrRight = contentFlowElement_rightsCurrentNrRight + 1;
			
			// go through elements
			for (var i=1; i <= contentFlowElement_rights.length; i++) {
				if (i == contentFlowElement_rightsCurrentNrRight) {
					$('contentFlowElement_right' + i).style.zIndex=100;
					$('contentFlowElement_right' + i).setOpacity(1);
					$('contentFlowElement_right' + i).style.display='inline-block';
				} else {
					$('contentFlowElement_right' + i).style.zIndex=98;
					$('contentFlowElement_right' + i).setOpacity(0.01);
					$('contentFlowElement_right' + i).style.display='none';
				}
			}
			timeOutIdRight = setTimeout("contentFlowEffectRight()", options_intervalRight*1000);
		}
	}
}

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




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:loaded", function() { 
		contentFlowInitRight();
		contentFlowInit();
});

