var teaserRotation_visibleDiv			= $("teaserRotation_div1");
var teaserRotation_imageActive			= 1;
var teaserRotation_automaticActiveImage	= 0;
var teaserRotation_interval;

/*
 * switches between the two big teaser images
 *
 * @param string s_newSource
 */
function teaserRotation_toggle(s_newSource){
	if($("teaserRotation_div1").fx) {
		$("teaserRotation_div1").fx.stop();
	}
    if($("teaserRotation_div2").fx) {
    	$("teaserRotation_div2").fx.stop();
    }
    if (teaserRotation_visibleDiv == $("teaserRotation_div1")) {
        $("teaserRotation_image2").src = s_newSource;
        $("teaserRotation_div1").fx = new Fx.Style($("teaserRotation_div1"), 'opacity', {duration: fadingDuration}).start(0);
        $("teaserRotation_div2").fx = new Fx.Style($("teaserRotation_div2"), 'opacity', {duration: fadingDuration}).start(1);
        teaserRotation_visibleDiv = $("div2");
    } else {
        $("teaserRotation_image1").src = s_newSource;
        $("teaserRotation_div1").fx = new Fx.Style($("teaserRotation_div1"), 'opacity', {duration: fadingDuration}).start(1);
        $("teaserRotation_div2").fx = new Fx.Style($("teaserRotation_div2"), 'opacity', {duration: fadingDuration}).start(0);
        teaserRotation_visibleDiv = $("teaserRotation_div1");
    }
}

/*
 * preloads the big images
 *
 * @param array a_buttonsAndImages
 */
function teaserRotation_loadImages(a_buttonsAndImages){
	for (biPairKey in a_buttonsAndImages) {
		var s_source = teaserRotation_clearImageSource(a_buttonsAndImages[biPairKey]);
		var image = new Element('img',{ src: s_source, style:'margin:3px;' });
		//$(biPairKey).image = image;
	}
}

/*
 * inits the fader
 */
function teaserRotation_faderInit(){
	teaserRotation_loadImages(teaserRotation_images);
	var list = $$('#rotateTeaser_ImageList li img');
	teaserRotation_init(list);
	teaserRotation_automaticRotation();
}

/*
 * adds the functions onclick, onmouseover and onmouseout to the thumb images
 *
 * @param array a_list
 */
function teaserRotation_init(a_list) {
	a_list.each(function(o_element) {
		var o_element = $(o_element);

		o_element.onclick = function() {

			if (!arguments[0] || arguments[0] != 'automatic') {
				teaserRotation_stopAutomaticRotation();
			}

			teaserRotation_toggle(teaserRotation_clearImageSource(o_element.src));
			teaserRotation_minimizeThumb(teaserRotation_imageActive);

			var i_imageNumber = o_element.id.substring(5);
			teaserRotation_imageActive = i_imageNumber;

			if (!arguments[0] || arguments[0] != 'automatic') {
				teaserRotation_automaticActiveImage = teaserRotation_imageActive - 1;
			}
			teaserRotation_changeLink();
		};

		o_element.onmouseover = function() {
			var i_thumbNumber = o_element.id.substring(5);
			teaserRotation_maximizeThumb(i_thumbNumber);
		};

		o_element.onmouseout = function() {
			var i_thumbNumber = o_element.id.substring(5);
			if (teaserRotation_imageActive != i_thumbNumber) {
				teaserRotation_minimizeThumb(i_thumbNumber);
			}
		};

	});
}

/*
 * removes not need path parts
 *
 * @param string s_imageSource
 */
function teaserRotation_clearImageSource(s_imageSource) {
	return s_imageSource.replace('_rotationThumb', '_rotation');
}

/*
 * adds the function onclick to the slider-buttons
 */
function teaserRotation_addButtons() {
	$('rotateTeaser_sb_left').addEvent('click', teaserRotation_previous);
	$('rotateTeaser_sb_right').addEvent('click', teaserRotation_next);
}

/*
 * minimizes a thumb using an special effect
 *
 * @param integer i_thumbId
 */
function teaserRotation_minimizeThumb(i_thumbId) {
	var i_duration = 200;
	if (arguments[1]) {
		i_duration = arguments[1];
	}

	var fx_thumb = new Fx.Styles($('thumb' + i_thumbId), {duration:i_duration, wait:false});
	fx_thumb.start({'height' : 65, 'width' : 100, margin: 0});
}

/*
 * enlarges a thumb using an special effect
 *
 * @param integer i_thumbId
 */
function teaserRotation_maximizeThumb(i_thumbId) {
	var i_duration = 200;
	if (arguments[1]) {
		i_duration = arguments[1];
	}

	var fx_thumb = new Fx.Styles($('thumb' + i_thumbId), {duration:i_duration, wait:false});
	fx_thumb.start({'height' : 75, 'width' : 113, margin: -5});
}

/*
 * starts the automatic teaser-rotation
 */
function teaserRotation_automaticRotation() {
	teaserRotation_interval = window.setInterval("teaserRotation_next('automatic')", displayTime);
}

/*
 * stops the automatic teaser-rotation
 */
function teaserRotation_stopAutomaticRotation() {
	if (teaserRotation_isDefined('teaserRotation_interval')) {
		window.clearTimeout(teaserRotation_interval);
	}
}

/*
 * checks if a variable is defined
 *
 * @param mixed variable
 */
function teaserRotation_isDefined(variable){
	return this[variable] === undefined ? false : true;
};

/*
 * switches to the next image
 */
function teaserRotation_next() {
	var list = $$('#rotateTeaser_ImageList li img');

	teaserRotation_automaticActiveImage++;
	if (teaserRotation_automaticActiveImage == list.length) {
		teaserRotation_automaticActiveImage = 0;
	}

	if (!arguments[0]) {
		teaserRotation_stopAutomaticRotation();
		list[teaserRotation_automaticActiveImage].onclick();
	} else {
		list[teaserRotation_automaticActiveImage].onclick(arguments[0]);
	}

	if (teaserRotation_automaticActiveImage + 1 > list.length) {
		teaserRotation_maximizeThumb(1, 350);
	} else {
		teaserRotation_maximizeThumb(teaserRotation_automaticActiveImage + 1, 350);
	}
}

/*
 * switches to the previous image
 */
function teaserRotation_previous() {
    var list = $$('#rotateTeaser_ImageList li img');

    teaserRotation_stopAutomaticRotation();

    if (teaserRotation_automaticActiveImage == 0) {
		teaserRotation_automaticActiveImage = list.length;
	}
	teaserRotation_automaticActiveImage--;

	list[teaserRotation_automaticActiveImage].onclick();

    if (teaserRotation_automaticActiveImage < 0) {
		teaserRotation_maximizeThumb(list.length, 350);
	} else {
		teaserRotation_maximizeThumb(teaserRotation_automaticActiveImage + 1, 350);
	}
}

/*
 * switches the link of the current big image
 */
function teaserRotation_changeLink() {
	if (teaserRotation_links[teaserRotation_imageActive-1] == '' || teaserRotation_links[teaserRotation_imageActive-1] == '#') {
		$('teaserRotation_image1').style.cursor = 'default';
		$('teaserRotation_image2').style.cursor = 'default';
	} else {
		$('teaserRotation_image1').style.cursor = 'pointer';
		$('teaserRotation_image2').style.cursor = 'pointer';
	}
	$('rotateTeaser_link_1').href = teaserRotation_links[teaserRotation_imageActive-1];
	$('rotateTeaser_link_2').href = teaserRotation_links[teaserRotation_imageActive-1];
}

window.addEvent('load', teaserRotation_faderInit);
