/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// config vars
	//
	//
var cur_item = 0;
var switcher_timer;
var item_array = new Array();
var is_loading = false;

/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// create_switcher()
	// Create the holder elements and each slide for the switcher
	// After everything each item is built, store them in the item_array and then display the first item
	//
	//
function create_switcher() {
	if(!$('home')) return false;
	//
	// create html elements
	//
	var item_switcher = Builder.node('div', { id: 'item_switcher' }, [
		Builder.node('div',{ id: 'item_container' }),
		Builder.node('div',{ id: 'preloader' }, [
			Builder.node('span', ''),
			Builder.node('div', {id: 'loader_graphic'})
		])
	]);
	
	
	//
	// append elements to document
	//
	$('mastL').insert(item_switcher);
	
	
	// Only needed when a navigation is in use
	//
	//init_navigation();
	
	
	//
	//
	// Create each item
	// 
	//	
	var item_01 = Builder.node('div', { id: 'test01', className: 'item' }, [
		
		Builder.node('a', { href:  'http://www.dulce4u.com/shop/product/product_id/15'}, 'Buy Now!!')
	]);
	
	var item_02 = Builder.node('div', { id: 'test02', className: 'item' }, [
		
		Builder.node('a', { href: 'http://www.dulce4u.com/shop/product/product_id/15'}, 'Check out our selection!!')
	]);
	
	var item_03 = Builder.node('div', { id: 'test03', className: 'item' }, [
		
		Builder.node('a', { href: 'http://www.dulce4u.com/shop/'}, 'A Que Te Gusta!!')
	]);
	
	
	
		
	item_array = [
				item_01,
				item_02, 
				item_03
				
				
				];
				
	show_item(cur_item);
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// show_item(itemNum)
	// Appends the current item to the DOM, fades away the preloader "curtain"
	//
	//
function show_item(itemNum) {
	var theItem = item_array[itemNum];
	$('item_container').appendChild(theItem);	
	var preloader = $('preloader');
	fadeElem(preloader, 1, 1, 0.0);
	setWindowLoc(theItem, preloader);
	
	timed_move();
	is_loading = false;
	
	
	// Only needed when a navigation is in use
	//
	//active_item(itemNum, 'activate');
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// unload_item(cur_item)
	// Fades up the preloader "curtain" so the next item can load
	//
	//
function unload_item(cur_item) {
	is_loading = true;
	stop_timer();
	fadeElem($('preloader'), 0.2, 0.0, 1);
	setTimeout(function() {$('item_container').removeChild(item_array[cur_item])}, 2000);
	
	// Only needed when a navigation is in use
	//
	//active_item(cur_item, 'deactivate');
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// active_item(itemNum, action)
	// Sets an active state for a navigation link
	//
function active_item(itemNum, action) {
	var nav = $('navigation');
	var cur_link = nav.getElementsByTagName('li')[itemNum].getElementsByTagName('a')[0];
	if(action == 'activate') {
		cur_link.className = 'active';
	}else{
		cur_link.className = '';
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// init_navigation()
	// If a navigation is needed this sets each links' onclick to call it's item
	//
	//
function init_navigation() {
	var nav_items = $('navigation').getElementsByTagName('li');
	for(var i=0; i < nav_items.length; i++) {
		var t = nav_items[i];
		t.item_num = i;
		t.a = t.getElementsByTagName('a')[0];
		t.a.onclick = function() {
			call_item(this.parentNode.item_num);
			return false;
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// call_item(itemNum)
	// switch to an item onclick (only when a navigation is present)
	// 
	//
function call_item(itemNum) {
	if(is_loading == false) {
		is_loading = true;
		var preloader = $('preloader');
		stop_timer();
		fadeElem($(preloader), 0.2, 0.0, 1);
		setTimeout(function() {$('item_container').removeChild(item_array[cur_item])}, 1000);
		
		// Only needed when a navigation is in use
		//
		//active_item(cur_item, 'deactivate');
		//active_item(itemNum, 'activate');
		
		var theItem = item_array[itemNum];
		setTimeout(function() {
			$('item_container').appendChild(theItem);
			fadeElem(preloader, 1, 1, 0.0);
			setWindowLoc(theItem, preloader);
			cur_item = itemNum;
			is_loading = false;
		}, 2000);
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// setWindowLoc(theItem, trigger)
	// set the onclick for the preloader "curtain" from the items' href value
	//
function setWindowLoc(theItem, trigger) {
	if(!theItem.getElementsByTagName('a')[0].href) return false;
	var destination = theItem.getElementsByTagName('a')[0].href;
	trigger.onclick = function() {
		window.location = destination;
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// next() and previous()
	// iterate through the items. This is called by the setInterval
	//
	//
function next() {
	unload_item(cur_item);
	if(cur_item == item_array.length - 1) {
		cur_item = 0;
	}else{
		cur_item++;
	}
	setTimeout(function() {show_item(cur_item)}, 2000);
	return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// timed movement
	//
	//
function timed_move() {
	switcher_timer = setInterval(next, 7000);
}

function stop_timer() {
	if(switcher_timer) {
		clearInterval(switcher_timer);
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// start it onload
	//
	//
addLoadEvent(function(){
	create_switcher();
});