// claudiusbähr+friends JS Tour
// copyright 2010-2011 by claudiusbähr+friends http://www.cb-friends.de

if (window.addEventListener) { // NETSCAPE
	window.addEventListener('load', cbf_jstour_init, false);
} else if (window.attachEvent) { //IE
	window.attachEvent('onload', cbf_jstour_init);
}

// Animation variables
var cbf_jstour_timer;
var cbf_jstour_target = 0;
var cbf_jstour_fadein_elem;
var cbf_jstour_zIndex;

// Object variables
var cbf_jstour_container;
var cbf_jstour_elemsDiv;
var cbf_jstour_elems;

/**
 * CBF_JSTOUR_INIT
 * Initialises the main function
 *
 */

function cbf_jstour_init() {
	// Check if jstour_container is loaded
	if (document.getElementById('tx-cbfjstour-p1_container')) {
		// Define global vars for objects
		cbf_jstour_container	= document.getElementById('tx-cbfjstour-p1_container');
		cbf_jstour_elemsDiv		= document.getElementById('tx-cbfjstour-pi1_elems');
		cbf_jstour_elems		= getElementsByClassName('tx-cbfjstour-pi1_elem');
		// Set inline CSS styles
		cbf_jstour_setCSS();
		// Display the slider after CSS styles have been applied
		var cbf_jstour_postLoad = new Animation({
			from: 0,
			to: 1,
			tweenType: 'linear',
			ontween: function(value) {
				cbf_jstour_container.style.opacity = value;
				cbf_jstour_container.style.filter = 'alpha(opacity='+Math.ceil(value*100)+')';
			},
			oncomplete: function() {
				cbf_jstour_container.style.opacity = 1;
				cbf_jstour_container.style.filter = 'alpha(opacity=100)';
			}
		});
		cbf_jstour_postLoad.start();
		// Depending on mode, start animations
		if (cbf_jstour_mode == 0) { jstour_slider(); } else { cbf_jstour_blend(); }
		if (cbf_jstour_autoplay == 1) { cbf_jstour_timer = setInterval(cbf_jstour_next, cbf_jstour_duration); }
	}
}

/**
 * CBF_JSTOUR_SETCSS
 * Renders inline-styles
 *
 */

function cbf_jstour_setCSS() {
	// Styles for the elements container
	cbf_jstour_elemsDiv.style.width = cbf_jstour_width + 'px';
	cbf_jstour_elemsDiv.style.height = cbf_jstour_height + 'px';
	cbf_jstour_elemsDiv.style.width = cbf_jstour_elems.length * cbf_jstour_width + 'px';
	// Styles for the elements
	for (var i = 0; i < cbf_jstour_elems.length; i++) {
		// Set dimensions
		cbf_jstour_elems[i].style.width = cbf_jstour_width + 'px';
		cbf_jstour_elems[i].style.height = cbf_jstour_height + 'px';
		// CSS for blend mode
		if (cbf_jstour_mode == 1) {
			// Set z-index
			cbf_jstour_zIndex = i+1;
			cbf_jstour_elems[i].style.zIndex = cbf_jstour_zIndex;
			// Set opacity
			if (i != 0) {
				cbf_jstour_elems[i].style.opacity = 0;
				cbf_jstour_elems[i].style.filter  = 'alpha(opacity=0)';
			} else {
				cbf_jstour_elems[i].style.opacity = 1;
				cbf_jstour_elems[i].style.filter  = 'alpha(opacity=100)';
			}
		}
	}
}

/**
 * CBF_JSTOUR_NEXT
 * Slide to the next item
 *
 */

function cbf_jstour_next() {
	// Slider ?
	if (cbf_jstour_mode == 0) {
		cbf_jstour_target -= cbf_jstour_width;
		if (cbf_jstour_target <= (-1) * cbf_jstour_elems.length * cbf_jstour_width) { cbf_jstour_target = 0 } // return to start when finished
	} else
	// Blend ?
	if (cbf_jstour_mode == 1) {
		// Target element + 1
		cbf_jstour_target += 1;
		// Return to start when finished
		if (cbf_jstour_target >= cbf_jstour_elems.length) { cbf_jstour_target = 0 }
		// Set element to be faded in
		cbf_jstour_fadein_elem = cbf_jstour_elems[cbf_jstour_target];
		// Set new zIndex and reset opacity
		cbf_jstour_zIndex += 1;
		cbf_jstour_fadein_elem.style.opacity = 0;
		cbf_jstour_fadein_elem.style.filter  = 'alpha(opacity=0)';
		cbf_jstour_fadein_elem.style.zIndex  = cbf_jstour_zIndex;
		// Fade in the target element
		cbf_jstour_fadein.start();
	}
	// Set position index
	if (cbf_jstour_usePI == 1) {
		var pos_index = Math.abs(cbf_jstour_target/cbf_jstour_width);
		cbf_jstour_setPI(pos_index);
	}
}

/**
 * CBF_JSTOUR_PREV
 * Slide to the previous item
 *
 */

function cbf_jstour_prev() {
	// Slider ?
	if (cbf_jstour_mode == 0) {
		cbf_jstour_target += cbf_jstour_width;
		if (cbf_jstour_target > 0) { cbf_jstour_target = (-1) * (cbf_jstour_elems.length-1) * cbf_jstour_width; } // hop to end
		} else
	// Blend ?
	if (cbf_jstour_mode == 1) {
		// Target element + 1
		cbf_jstour_target -= 1;
		// Return to start when finished
		if (cbf_jstour_target < 0) { cbf_jstour_target = 0 }
		// Set element to be faded in
		cbf_jstour_fadein_elem = cbf_jstour_elems[cbf_jstour_target];
		// Set new zIndex and reset opacity
		cbf_jstour_zIndex += 1;
		cbf_jstour_fadein_elem.style.opacity = 0;
		cbf_jstour_fadein_elem.style.filter  = 'alpha(opacity=0)';
		cbf_jstour_fadein_elem.style.zIndex  = cbf_jstour_zIndex;
		// Fade in the target element
		cbf_jstour_fadein.start();
	}
	// Set position index
	if (cbf_jstour_usePI == 1) {
		var pos_index = Math.abs(cbf_jstour_target/cbf_jstour_width);
		cbf_jstour_setPI(pos_index);
	}
	delete(cbf_jstour_timer);
	return true;
}

/**
 * CBF_JSTOUR_SETPI
 * Updates the position index
 *
 */

function cbf_jstour_setPI(pos_index) {
	pos_index_items = getElementsByClassName('tx-cbfjstour-pi1_position_index_item');
	for (var i = 0; i < pos_index_items.length; i++) {
		if (pos_index != pos_index_items[i].id.replace(/tx-cbfjstour-pi1_position_index_item/g, "")) {
			pos_index_items[i].className = pos_index_items[i].className.replace(/ active/g, "");
		} else {
			pos_index_items[i].className = pos_index_items[i].className+' active';
		}
	}
}

/**
 * CBF_JSTOUR_SLIDER
 * The main slider function
 *
 */

function jstour_slider() {
	if (typeof jstour_slider_x == 'undefined') { jstour_slider_x = 0 }
	s = new Array();
	s[1] = 28;
	s[2] = 26;
	s[3] = 24;
	s[4] = 22;
	s[5] = 20;
	s[6] = 18;
	s[7] = 16;
	s[8] = 14;
	s[9] = 12;
	s[10] = 10;
	d = cbf_jstour_target - jstour_slider_x;
	jstour_slider_x = jstour_slider_x + (d/s[cbf_jstour_slidespeed]);
  	document.getElementById('tx-cbfjstour-pi1_elems').style.left = jstour_slider_x + 'px';
	// hide or show buttons
	if (cbf_jstour_target <= (-1)*cbf_jstour_width) {
			document.getElementById('jstour_next').setAttribute('className','button next'); // IE
			document.getElementById('jstour_next').setAttribute('class','button next'); // NETSCAPE
		} else {
			document.getElementById('jstour_next').setAttribute('className','button hide'); // IE
			document.getElementById('jstour_next').setAttribute('class','button hide'); // NETSCAPE
		}
	if (cbf_jstour_target <= (-1)*cbf_jstour_width) {
			document.getElementById('jstour_prev').setAttribute('className','button back');
			document.getElementById('jstour_prev').setAttribute('class','button back');
		} else {
			document.getElementById('jstour_prev').setAttribute('className','button hide');
			document.getElementById('jstour_prev').setAttribute('class','button hide');
		}
	if (cbf_jstour_target != 0) {
			document.getElementById('jstour_start').setAttribute('className','button hide');
			document.getElementById('jstour_start').setAttribute('class','button hide');
		} else {
			document.getElementById('jstour_start').setAttribute('className','button');
			document.getElementById('jstour_start').setAttribute('class','button');
		}
	setTimeout(jstour_slider, 20);
}

/**
 * cbf_jstour_blend
 * Setup animations for the blending mode (cbf_jstour_mode == 1)
 *
 */
 
function cbf_jstour_blend() {
	// The element to be faded in next (the second one by default)
	cbf_jstour_fadein_elem = cbf_jstour_elems[1];
	// Define the animations with animator.js
	cbf_jstour_fadein = new Animation({
		from: 0,
		to: 1,
		tweenType: 'linear',
		ontween: function(value) {
			cbf_jstour_fadein_elem.style.opacity = value;
			cbf_jstour_fadein_elem.style.filter  = 'alpha(opacity='+Math.ceil(value*100)+')';
		},
		oncomplete: function() {
			cbf_jstour_fadein_elem.style.opacity = 1;
			cbf_jstour_fadein_elem.style.filter	 = 'alpha(opacity=100)';
			// hide or show buttons
			if (cbf_jstour_target > 0) {
				document.getElementById('jstour_next').className = 'button next';
			} else {
				document.getElementById('jstour_next').className = 'button hide';
			}
			if (cbf_jstour_target > 0) {
				document.getElementById('jstour_prev').className = 'button back';
			} else {
				document.getElementById('jstour_prev').className = 'button hide';
			}
			if (cbf_jstour_target != 0) {
				document.getElementById('jstour_start').className = 'button hide';
			} else {
				document.getElementById('jstour_start').className = 'button';
			}
		}
	});
}
