$(document).ready(function() {
	// fix png for IE
	//$('img[@src$=.png], div').ifixpng();
	

	// remove any borders on last LI element
	$("#content ul").each(function(){
		 $(this).children("li:last").css({borderRight:"0",borderBottom:"0",background:"none",paddingRight:"0",marginRight:"0"});
	});

	// LAVALAMP
	if ( $('#scrollme ul').length ) {
		$('#scrollme ul').lavaLamp({
			fx: 'easeInExpo',
			speed: 300,
			autoReturn: false
		});
		
		var dateClicked = $('#scrollme ul li:first').children('a').html();
		//$('#date h2').html(dateClicked);
		var text_id = $('#scrollme ul li:first').children('a').attr('name');
		$('#date').html($('div#'+text_id).html()+"<br clear='all' />");		
	}

	// Scroll timeline
	var index = $("#scrollme ul li").length;
    $("a.back").fadeOut();
	if ( $('#scrollme').length ) {
		$('#scrollme').serialScroll({
			items:'li',
			prev:'a.back',
			next:'a.next',
			axis:'x',
			start:0,
			duration:300,
			force:true,
			cycle:false,
			onBefore:function(button, e, elem, $pane, $items, pos){
				if ( $items == 0 ) 
				{
					$("a.back").fadeOut();
				}

				if ( $items == index-4 ) 
				{
					$("a.next").fadeOut();
				}
			}
		});
	}

    $("a.back").click(function(){
        $("a.next:hidden").fadeIn();
        return false;
    });
    $("a.next").click(function(){
        $("a.back:hidden").fadeIn();
        return false;
    });
	
	// handle click on years in timeline
	$('#scrollme ul li').click(function(){
		var dateClicked = $(this).children('a').html();
		//$('#date h2').html(dateClicked);
		var text_id = $(this).children('a').attr('name');
		$('#date').html($('div#'+text_id).html()+"<br clear='all' />");
		$('#lindex').val($('#scrollme ul li').index(this));

		return false;
	});
	
	var totalLi = parseFloat($('#scrollme ul li').length - 2 );
	var pos = 0;
	$('#nextEntry').click(function(){
		// Jump to last Event 
		var nextPos = totalLi;
		
		var dateClicked = $('#scrollme ul li:eq(' + nextPos + ')').children('a').html();
		if ( dateClicked != null ) {
			//$('#date h2').html(dateClicked);
			var text_id = $('#scrollme ul li:eq(' + nextPos + ')').children('a').attr('name');
			$('#date').html($('div#'+text_id).html()+"<br clear='all' />");
		}
		
		$('#scrollme').trigger( 'goto', [ (totalLi-6) ] );
		$('#scrollme ul li:eq('+(totalLi)+')').mouseover();

		
		return false;
	});

	$('#previousEntry').click(function(){
		// Jump to first event
		var nextPos = 0;

		var dateClicked = $('#scrollme ul li:eq(' + nextPos + ')').children('a').html();
		if ( dateClicked != null ) {
			//$('#date h2').html(dateClicked);
			var text_id = $('#scrollme ul li:eq(' + nextPos + ')').children('a').attr('name');
			$('#date').html($('div#'+text_id).html()+"<br clear='all' />");
		}

		//reposition the lavalamp menu
		$('#scrollme').trigger( 'goto', [ 0 ] );
		$('#scrollme ul li:first').mouseover();
		
		return false;
	});	

	

	
	

	

	

});

// Define indexOf for IE
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
} 