var counter = 0;
var item_array = new Array();
var title_array = new Array();
var hide_array = new Array();

function init_slider()
{
	// Function to create the slideshow for the Weapon7 Site
	// Items should be in a ul#slide contained in a seperate li tag for each
	var slider = $('slider');
	if(slider != null)
	{
		item_array = $$('ul#slide li');
		$('slider').setStyle('height','300px');
		for(var i = 0; i < item_array.length; i++)
		{
			// Add the class, and hide 
			item_array[i].set('class','rel');
			// Grab the title and insert into the title array
			var children = item_array[i].getChildren();
			for(var j = 0; j < children.length; j++)
			{
				if(i != 0)
				{
					item_array[i].fade('out');
				}
				if(children[j].hasClass("title"))
				{
					title_array[i] = children[j];
					children[j].setStyle('display','none');
				}
				else if(children[j].hasClass("swf"))
				{
					children[j].setStyle('display','none');
				}
			}
		}
		set_title();
		set_number();
	}
}

function next_item()
{
	if(counter < (item_array.length-1))
	{
		// We can show the next item
		// First hide the previous item
		check_for_swf(counter);
		item_array[counter].fade('out');
		counter++;
		check_for_swf(counter);
		item_array[counter].fade('in');
	}
	else
	{
		// Go to the start of the list again
		item_array[counter].fade('out');
		counter = 0;
		check_for_swf(counter);
		item_array[counter].fade('in');
	}
	set_title();
	set_number();
}

function prev_item()
{
	if(counter > 0)
	{
		// We can show the next item
		item_array[counter].fade('out');
		check_for_swf(counter);
		counter--;
		check_for_swf(counter);
		item_array[counter].fade('in');
	}
	else
	{
		// Go to the start of the list again
		item_array[counter].fade('out');
		counter = (item_array.length-1);
		check_for_swf(counter);
		item_array[counter].fade('in');
	}
	set_title();
	set_number();
}

function set_title()
{
	// Reset existing
	$('work_title').set('html','&nbsp;');
	var new_title = title_array[counter].innerHTML;
	$('work_title').set('html', new_title);
}

function set_number()
{
	$('work_count').set('html','&nbsp;');
	var count_str = (counter+1) + " of " + (item_array.length); 
	$('work_count').set('html',count_str);
}

function handle_key(key)
{
	if(key == "left")
	{
		//Get new content
		prev_item();
	}
	else if(key == "right")
	{
		//Get previous content
		next_item();
	}
}

function check_for_swf(count)
{
	// Check the current
	var children = item_array[count].getChildren();
	for(var j = 0; j < children.length; j++)
	{
		if(children[j].hasClass("swf"))
		{
			// Set the container to not display
			if(children[j].style.display == "none")
			{
				children[j].setStyle('display','block');
			}
			else if(children[j].style.display == "block")
			{
				children[j].setStyle('display','none');
			}
			else 
			{
				children[j].setStyle('display','block');
			} 
		}
	}
}

function init_more_text()
{
	// Grab everywhere on the document where there is a 'read more' link.
	// Get the corresponding hide text (should have a similar ID)
	hide_array = $$('a.readmore');
	for(var i = 0; i < hide_array.length; i++)
	{
		// Get the ID of the element and hide the text that corresponds
		var el_id = hide_array[i].id;
		hide_array[i].set('events', {
			'click': function(){
				if($(el_id+'text').style.display == "none")
				{
					// Reveal
					$(el_id).setStyle('display','none');
					$(el_id+'text').setStyle('display','block');
				}
				else
				{
					// Hide
					$(el_id+'text').setStyle('display','none');
				}
			}
		});
		$(el_id+'text').setStyle('display','none');
	}
}

var current_seq = 0;
var seq_length = 0;

function init_sequence(){
	var seq = $('sequence');
	if(seq != null){
		var count = 0;
		var seqA = $$('div#sequence ul li');
		seqA.each(function(el){
			if(count != 0){
				// Hide all others
				//el.setStyle('display','none');
				el.fade('out');
			}
			// Add a class of 'rel'
			el.addClass('rel');
			count++; // Counter
		});
		seq_length = seqA.length;
		(function(){
			sequence.periodical(2500);
		}).delay(1000);
	}
}

var sequence = function(){
	// Start the sequence
	var next;
	if(current_seq == seq_length - 1){
		next = 0;
	} else {
		next = current_seq + 1;
	}
	swap_items(current_seq,next);
	current_seq = next;
}

function swap_items(current,next){
	var lists = $$('div#sequence ul li');
	var current = lists[current];
	var next = lists[next];
	//Hide the current and show the next
	//current.fade('out');
	current.set('tween', {duration: 'long'});
	next.set('tween', {duration: 'long'});
	
	current.tween('opacity', '0');
	next.tween('opacity','1');
	//next.fade('in');
	//next.get('tween', { property: 'opacity', duration: 'normal'}).start(0);
	
}



