/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// config vars
	//
	//
var stop_points = new Array();
var cur_point = 0;
var switcher_timer;

/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// create_switcher()
	//
	//
function create_switcher() {
	if(!$('home')) return false;
	var btn_class = 'controls';
	
	//
	// create html elements
	//
	var item_switcher = Builder.node('div', { id: 'item_switcher' }, [
		Builder.node('div',{ id: 'item_container' }, [
			Builder.node('div',{ id: 'item_slider' }, [
				
			]),
		]),
		Builder.node('a', {id: 'btn_prev', className: btn_class, href: '#previous'}, '<< Prev'),
		Builder.node('a', {id: 'btn_next', className: btn_class, href: '#next'}, 'Next >>')
	]);
	
	//
	// append elements to document
	//
	$('content_main').appendChild(item_switcher);
	
	//
	// Build array of items
	// [id, h3 content, strong content, p content, a content, a href];
	//
	var item_array = [
		[ 'matcha', 
		  'Experience', 
		  'Matcha', 
		  'Our delicious, vitamin-rich Ceremonial Green Tea served hot, iced or blended at The Coffee Cafe.',
		  'View Menu',
		  'menu/tea'
		],
		[ 'serving', 
		  'Also Serving...', 
		  '', 
		  'A variety of fine wines from around the world and a collection of fine imported beer.',
		  'View Menu',
		  'menu/wine_beer'
		],
		[ 'store', 
		  'Shop Online', 
		  '', 
		  'Get all The Coffee Cafe products you need for home and work by shopping at The Coffee Cafe Store.',
		  'Visit Store',
		  'store'
		],
		[ 'art', 
		  'Enjoy Latte Art', 
		  '',
		  'Watch our talented baristas create a work of art in your next coffee drink.',
		  'Read More',
		  'latte_art'
		]
	];
	
	//
	// create items from item_array
	//
	for(var i=0; i < item_array.length; i++) {
		var t = item_array[i];
		t.div_id = t[0];
		t.h3 = t[1];
		t.strong = t[2];
		t.p = t[3];
		t.a_content = t[4];
		t.a_href = base_path + t[5];

		//
		// creating item manually, get an uncaught exception ->
		// when I try to use Builder. Whatever the hell an uncaught exception is?
		//
		var item = document.createElement('div');
		item.setAttribute('id', t.div_id);
		item.className = 'item';
		item.loc = t.a_href;
		item.onclick = function() {
			window.location = this.loc;
		}
		
		var list = document.createElement('ul');
		
		//
		// create first list item (header)
		//
		var li = document.createElement('li');
		var h3 = document.createElement('h3');
		var h3Text = document.createTextNode(t.h3);
		h3.appendChild(h3Text);
		
		// if there is a need for a strong tag in the header
		if(t.strong != '') {
			var strong = document.createElement('strong');
			var strongText = document.createTextNode(' ' + t.strong);
			strong.appendChild(strongText);
			h3.appendChild(strong);
		}
		li.appendChild(h3);
		list.appendChild(li);
		
		//
		// create second list item (paragraph)
		//
		var li = document.createElement('li');
		var p = document.createElement('p');
		var pText = document.createTextNode(t.p);
		p.appendChild(pText);
		li.appendChild(p);
		list.appendChild(li);
		
		//
		// create third list item (a tag)
		//
		var li = document.createElement('li');
		var a = document.createElement('a');
		a.setAttribute('href', t.a_href);
		var aText = document.createTextNode(t.a_content);
		a.appendChild(aText);
		li.appendChild(a);
		list.appendChild(li);
		
		
		//
		// attach item to list
		//
		item.appendChild(list);
		
		//
		// attach item to slider
		//
		attach_item(item, i);
		
		if(i == item_array.length - 1) {
			init_controls();
			timed_move();
		}
	}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// attach_item(theItem, itemNum)
	//
	//
function attach_item(theItem, itemNum) {
	var new_item = $('item_slider').appendChild(theItem);
	
	var item_width = new_item.offsetWidth;
	var new_point = item_width * itemNum;
	new_item.style.left = new_point + "px";
	stop_points[itemNum] = new_point;
	
	if(itemNum != 0) {
		var item_height = new_item.offsetHeight;
		new_item.style.top = ((new_item.offsetHeight * itemNum) * -1) + "px";
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// next() and previous()
	//
	//

function next() {
	if(cur_point == stop_points.length - 1) {
		cur_point = 0;
	}else{
		cur_point++;
	}
	var new_position = stop_points[cur_point] * -1;
	move_elem(new_position);	
	return false;
}
function previous() {
	if(cur_point == 0) {
		cur_point = stop_points.length - 1;
	}else{
		cur_point--;
	}
	var new_position = stop_points[cur_point] * -1;
	move_elem(new_position);
	return false;
}

function stop_timer() {
	if(switcher_timer) {
		clearInterval(switcher_timer);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// move_elem(thePosition)
	//
	//
function move_elem(thePosition) {
	var the_elem = $('item_slider');
	new Effect.Move(the_elem, {
		x: thePosition, y: 0, mode: 'absolute',
		transition: Effect.Transitions.sinoidal
	});
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// init_controls()
	// prepare next and last buttons
	//
function init_controls() {
	$('btn_prev').onclick = function() {
		stop_timer();
		previous();
		return false;
	}
	$('btn_next').onclick = function() {
		stop_timer();
		next();
		return false;
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// timed movement
	//
	//
function timed_move() {
	switcher_timer = setInterval(next, 8000);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// start it onload
	//
	//
addLoadEvent(function(){
	create_switcher();
});