// $Id: nav.js 219 2008-01-07 17:29:45Z mwong $

// sort out the positioning, needs to be a bit shorter
	$('project-list').setStyle({marginTop: '-157px', height: '101px'});
	$('projects').setStyle({overflow: 'hidden'});

	if (location.pathname.indexOf('/projects/') == 0) {

		// show if we're on the projects page
		var a = $$('#projects-button a')[0];
		Event.observe(a, 'mouseover', function() {
			a.setStyle({cursor:'default'});
		});

	} else {

		// hide if we're not on the projects page
		$('project-list').setStyle({display: 'none'});

		// attach mouseover events to the project list div and projects button
		// so that the projects list is shown when you're over the list
		// and hidden as soon as you move your mosue off it
		var listeners = new Array('projects-button', 'project-list');
		numListeners = listeners.length;

		for (var i=0; i<numListeners; i++) {

			Event.observe(listeners[i], 'mouseover', function() {
					$('project-list').show();
					$('projects-button').addClassName('active');
			});

			Event.observe(listeners[i], 'mouseout', function() {
					$('project-list').hide();
					$('projects-button').removeClassName('active');
			});

		}

	}

	// make the projects button not do anything
	var a = $$('#projects-button a')[0];
	Event.observe(a, 'click', function(e) {
			Event.stop(e);
	});

	// navigation movements and buttons
	doProjectNav();




function doProjectNav() {

	var ul = $$('#projects-holder ul');
	var navListsShowing = 3;

	// if we have 3 or less lists showing then we don't bother
	if (ul.length <= navListsShowing) {
		//console.log('fasfsafsa');
		return;
	}

	// buttons should be inserted using js
	$('project-list').insert( {bottom: navButtonsHTML('project-left')})
	$('project-list').insert( {bottom: navButtonsHTML('project-right')})

	// set up the navigation movement
	var xMovement = 145;
	var current = 0;
	var moving = false;


	// Attach event listener and scroll to the right
	Event.observe('project-left', 'click', function(e) {

		if (moving == false && current > 0) {

			moving = true;
			new Effect.Move($('projects-holder'),{ x: (xMovement), y:0, mode: 'relative', afterFinish: function() {
							current--;
							changeNavButtons(current);
							moving = false;
						}
					});
		}
		Event.stop(e);
	});

	// Attach event listener and scroll to the left
	Event.observe('project-right', 'click', function(e) {

		if (moving == false && current < ul.length - navListsShowing) {

			moving = true;
			new Effect.Move($('projects-holder'),{ x: (xMovement * -1), y:0, mode: 'relative', afterFinish: function() {
							current++;
							changeNavButtons(current);
							moving = false;
						}
					});
		}

		Event.stop(e);
	});

	// initialise the nav buttons
	changeNavButtons(0);

	// see which column we're in and shift the columns if required
	shiftColumns();


	// some quite horrible code to make the projects list navigation
	// stick at the last column
	function shiftColumns() {

		// get all the lists in projects-holder
		// go thru all the lists that shouldn't show by default.
		// E.g miss out the first 3 columns
		// Check to see if we are at this url
		var cols = $$('#projects-holder ul');
		var num_shifts = 0;

		for (var i=navListsShowing; i<cols.length; i++) {

			var nav_col = cols[i].select('li a');

			for(var j=0; j < nav_col.length; j++) {

				if (location.href.indexOf(nav_col[j].href) > -1) {
					// found it, so we shift the projects-holder to the left
					// and check which arrows we should show
					num_shifts = i - navListsShowing + 1;
					$('projects-holder').setStyle({marginLeft : (xMovement * num_shifts * -1) + 'px'});
					current++;
					changeNavButtons(navListsShowing-num_shifts);
					break;
				}
			}

			// this is a bit dumb
			if (num_shifts) {
				break;
			}
		}

	}


//	document.write(location.hash);

	//
	// HTML for the navigation arrows
	function navButtonsHTML(my_id) {
		return '<div id="' + my_id + '" class="project-nav"><a href="#"><img src="img/' + my_id + '.gif" alt="" /></a></div>';
	}

	//
	// Make sure the navigation imgs are correct
	function changeNavButtons(i) {

		if (i <= 0) {
			$$('#project-left img')[0].src = '/img/none.gif';
		} else {
			$$('#project-left img')[0].src = '/img/project-left.gif';
		}

		if (i >= (ul.length - navListsShowing)) {
			$$('#project-right img')[0].src = '/img/none.gif';

		} else {

			$$('#project-right img')[0].src = '/img/project-right.gif';
		}

	}

}