jQuery.fn.iealpha = function() {
	return $("*", this).each(function(){
		var imgURL = $(this).css("background-image");
		var imgURLLength = imgURL.length;
		
		if(imgURL != 'none' && imgURL.substring(imgURLLength - 5, imgURLLength - 2) == 'png'){
			$(this).css({
				background: "",
				filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src='" + imgURL.substring(5, imgURLLength  - 2) + "')"
			});
		};
		
		if($(this).is("img") && $(this).attr("src").substring(imgURLLength - 5, imgURLLength - 2) == "png"){
			
			var original = $(this);
			var element = $("<div></div>");
			
			$(element).attr("title", original.attr("alt")).attr("disabled", "true").css({
				filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='crop', src='" + el.getProperty('src') + "')",
				position: "relative",
				background: "transparent",
				padding: $(original).css("padding"),
				margin: $(original).css("margin"),
				border: $(original).css("border"),
				height: $(original).css("height"),
				width: $(original).css("width")
			});
			
			$(this).remove().before(element);
		};
	});
};

jQuery.fn.unselectable = function() {
	if($.browser.msie) {
		this.onselectstart = 'return false';
	} else if($.browser.mozilla) {
		this.css('-moz-user-select', 'none');
	} else if($.browser.safari) {
		this.css('-webkit-user-select', 'none');
	} else {
		this.onselectstart = function() { return false; }
	}	return this;
}

window.onload = function(){
	// Stuff to perform when the entire document
	// is loaded, images and everything!
	$(document).iealpha();
}

$(document).ready(function() {
/*
	$.ajax({
		type: 'get',
		url: "index.php",
		data: "page=timeline",
		dataType: "html",
		async: false,
		success: function(thedata) {
			$('#tlribbon').prepend($('#timeline', thedata));
		}
	});
*/
	
	var randnum;
	if($('#home').length > 0) {
		randnum = 0;
		$('#timeline').css('background-color', '#060716').hide();
		
		/*
$('#tlribbon').css('position', 'relative').append('<div id="preface"></div>');
		$('#preface').css({
			backgroundColor: '#060716',
			height: '21.3em',
			width: '100%',
			backgroundImage: 'url(./_media/_graphics/timeline/explore.gif)',
			backgroundRepeat: 'no-repeat',
			backgroundPosition: 'center center',
			position: 'absolute',
			top: 0,
			display: 'none'
		}).oneTime(2500, function(){
			$(this).fadeIn(2000, "easeOutQuart");
		});
		
		$('#preface').oneTime(4500, function() { 
			$(this).fadeOut(2000, "easeInQuart", function() {
				$(this).remove();
			});
		});
*/
		
		$('#timeline').oneTime(2000, function(){
			$(this).fadeIn(2000, "easeOutQuart");
		});
		
		$('#timeline').oneTime(4000, function(){
			slide_to((parseInt($('.nbw_events li').eq(randnum).css('left')) / $('.nbw_events li').eq(randnum).parent('.track').width()) * 100, $('.nbw_events li').eq(randnum));
		});
		
	} else {
		randnum = (Math.round((Math.random() * 30) - 1)); // MASSIVE KLUDGE (total length is static)
		$('#timeline').oneTime(100, function(){
			slide_to((parseInt($('.nbw_events li').eq(randnum).css('left')) / $('.nbw_events li').eq(randnum).parent('.track').width()) * 100, $('.nbw_events li').eq(randnum));
		});	
	}


/*
	$('#main.nav li a').hover(function(){
		$(this).animate({
			backgroundColor: '#b88a21'
		}, 'slow');
	}, function(){
		$(this).animate({
			backgroundColor: '#76a4d1'
		}, 'slow');
	});
*/

	$('.importance_low').css('opacity', 0.2);
	$('.importance_medium').css('opacity', 0.6);

	i = 0;
	
	timeline_begin_extreme = 1800;
	timeline_end_extreme = 2020;
	distance = 8; // distance from parallax focus (NB: closest track to viewer is furthest from focus)
	relativity = [4, 24, 8]; // the order in which the tracks appear in the markup

// store events before removing them;

	global_events = $("ul.events li.global").remove();
	nbw_events = $("ul.events li.nbw").remove();
	var decades;
	decades = $("ul.track li.decade").each(function(){
		$("*", this).not('.year').remove();
		return $(this);
	});
	controls = $("div.control");
	$("#timeline").empty().append(controls).append('<ul class="track decadle"></ul>').append('<ul class="track nbw_events"></ul>').append('<ul class="track global_events"></ul>');
	$('.decadle').html(decades);
	$('.global_events').html(global_events);
	$('.nbw_events').html(nbw_events);
	$('ul.track').each(function(){
		$(this).fadeIn();
		this.track_begin_year = $('li:first .year',  this).html(); // first year of the timeline
		this.track_end_year = $('li:last .year',  this).html(); // last year of the timeline
		if(this.track_begin_year < timeline_begin_extreme) timeline_begin_extreme = this.track_begin_year;
		if(this.track_end_year > timeline_end_extreme) timeline_end_extreme = this.track_end_year;
		this.track_length = timeline_end_extreme - timeline_begin_extreme; // length in number of years
		$(this).width((this.track_length * (distance * relativity[i])));
		$('li', this).each(function(){
			var event_year = $('.year', this).html(); // get the year of the event
			$(this).css('left', (event_year - timeline_begin_extreme) * (distance * relativity[i]));
		});
		i++;
	});
	
	/*
for(d = Math.floor(Math.random() * 1000); d >= 1; d--){
		$('ul.global_events').append('<li class="global pepper">&#8226;</li>');
	}
	
	$('ul.global_events li.pepper').each(function(){
		$(this).css('left', Math.floor(Math.random() * $('ul.global_events').width()));
		$(this).css('font-size', Math.floor(Math.random() * 50));
	});
*/
	
	var viewer_width = $('#timeline').width();
	
	// make sure the width of the viewer is always accurate
	$(window).resize(function(){
		viewer_width = $('#timeline').width();
	});
	
	var count = $('#timeline ul.track').length;
	
	$('#beginning').click(function() {
		update_status(0);
		animate_to(0);
	});
	
	$('#end').click(function() {
		update_status(1);
		animate_to(1);
	});
	
	active = false;
	
	$('#timeline').hover(function(){
		$(this).css('cursor', 'ew-resize');
	}, function(){
		$(this).css('cursor', 'normal');
	});
	
	$('#timeline').mousedown(function(e){
		active = true;
	});
	
	$(document).mouseup(function(e){
		active = false;
	});
	
	$('#timeline').unselectable();
	
	$('#timeline').mousemove(function(e){
		if(active == true) {
			viewer_width = $('#timeline').width();
			viewer_mouseX = e.pageX - this.offsetLeft; // mouse position in pixels
			viewer_mouseXper = Math.round((viewer_mouseX / viewer_width) * Math.pow(10, 4)) / Math.pow(10, 4); // mouse position in percentage-width, rounded to 4 decimal places
/* 			update_status(viewer_mouseXper); */
			go_to(viewer_mouseXper);
		}
	});
	
	/*
current = $('.nbw_events li').eq(0); // attach crawler to track
	
	$('#control_left').click(function(){
		crawl_to($(this), 'prev');
	});
	
	$('#control_right').click(function(){
		crawl_to($(this), 'next');
	});
*/

	/* slide_to((parseInt($('.nbw_events li').eq(0).css('left')) / $('.nbw_events li').eq(0).parent('.track').width()) * 100, $('.nbw_events li').eq(0)); */
	
	var next = $('.nbw_events li').eq(1);
	var previous = $('.nbw_events li').eq($('.nbw_events li').size()-1);
	var current = $('.nbw_events li').eq(0);
	
	$('#control_left').click(function(e){
		$('.focused').fadeTo('fast', 1, function(){
			$(this).removeClass('focused');
		});
		if(($('.nbw_events li').index(current) == 0)) {
			current = $('.nbw_events li').eq($('.nbw_events li').size()-1);
			slide_to((parseInt($(current).css('left')) / $(current).parent('.track').width()) * 100, current);
			previous = current.prev('.nbw_events li');
			next = $('.nbw_events li').eq(0);
		} else {
			current = previous;
			slide_to((parseInt($(current).css('left')) / $(current).parent('.track').width()) * 100, current);
			previous = current.prev('.nbw_events li');
			next = current.next('.nbw_events li');
		}
	});
	
	$('#control_right').click(function(e){
		$('.focused').fadeTo('fast', 1, function(){
			$(this).removeClass('focused');
		});
		if(($('.nbw_events li').index(current) == $('.nbw_events li').size()-1)) {
			current = $('.nbw_events li').eq(0);
			slide_to((parseInt($(current).css('left')) / $(current).parent('.track').width()) * 100, current);
			previous = $('.nbw_events li').eq(0);
			next = $('.nbw_events li').eq(1);
		} else { 
			current = next;
			slide_to((parseInt($(current).css('left')) / $(current).parent('.track').width()) * 100, current);
			previous = current.prev('.nbw_events li');
			next = current.next('.nbw_events li');
		}
	});
	
	$('.nbw').click(function(e){
		$('.focused').fadeTo('fast', 1, function(){
			$(this).removeClass('focused');
		});
		slide_to((parseInt($(this).css('left')) / $(this).parent('.track').width()) * 100, $(this));
		next = $(this).next('.nbw_events li');
		previous = $(this).prev('.nbw_events li');
	});
	
	/*
$('#timeline').mousewheel(function(event, delta) {
		viewer_width = $('#timeline').width();
			viewer_mouseX = event.pageX; // mouse position in pixels
			viewer_mouseXper = Math.round(((event.pageX + delta) / viewer_width) * Math.pow(10, 4)) / Math.pow(10, 4); // mouse position in percentage-width, rounded to 4 decimal places
		go_to(viewer_mouseXper);
	});
*/
	
	function update_status(mouseXper) {
		if((Math.round(mouseXper * 100) * Math.pow(10, 4) / Math.pow(10, 4)) < 1) {
			$('#status').html("0");
		} else $('#status').html(Math.round(mouseXper * 100) * Math.pow(10, 4) / Math.pow(10, 4));
	}
	
	function animate_to(where) {
		$('#timeline ul.track').each(function(){
			viewer_mouseXper = where;
			$(this).animate({
				left: viewer_mouseX - viewer_mouseXper * $(this).width()
			}, 2000, "easeOutCubic");
		});
	}
	
	function slide_to(where, who) {
		$('#timeline ul').each(function(){
			viewer_mouseXper = where * .01;
			$(this).animate({
				left: ($('#timeline').width() / 2) - viewer_mouseXper * $(this).width()
			}, 1000, "easeInOutCubic", function(){
/* 				alert($(who).eq(0).attr('class')); */
				$(who).eq(0).addClass('focused');
				$(who).fadeTo('fast', 1);
			}); // perform end callback on who at this point
		});
	}
	
	function crawl_to(from, direction) {
		$('.focused').fadeTo('fast', 0, function(){
			from.removeClass('focused');
		});
	}
	
	function go_to(where) {
		$('#timeline ul').each(function(){
			viewer_mouseXper = where;
			$(this).css('left', viewer_mouseX - viewer_mouseXper * $(this).width());
		});
	}
	
	s = 0;
	temp = 20;
	$('li.global').css('top', function(){
		if(s >= 120) {
			s = 25; return s;
		} 
		if(s <= 120) {
			s = s + 20; return s;
		}
	});
	
	/*
$('li.global').css('top', function(){
		result = Math.floor(Math.random() * 170) - $('li.global').eq(0).height();
		remainder = result % 20;
		if(remainder < 20) {
			result += (20 - remainder);
		}
		return result;
	});
*/
	
/* 	$('li.decade .year').append('s'); */
});