$(function() {
// -----------------------------
// SETTINGS
//------------------------------
	var currentColumn = 0; // (X-AXIS)
	var numOfCols = $('#slider-inner .column').size();
	var currentSlide = new Array(); // (Y-AXIS)
	for (var x=0;x<numOfCols;x++) { currentSlide[x] = 0; }
	var animDuration = 500;
	var t;
// -----------------------------
// FUNCTIONS
//------------------------------
	/**** Determines if the arrows are clickable. ****/
	function tal_update_arrows() 
	{
		// start with all arrows enabled
		$('#controls .disabled').each(function() {
			$(this).removeClass('disabled').addClass('enabled');
		});
		
		// Determine which column we're on
		if ((numOfCols - 1) == 0) { // There's only one column. Disable both left and right arrows
			$('#controls #left').removeClass('enabled').addClass('disabled');
			$('#controls #right').removeClass('enabled').addClass('disabled');
		} else if (currentColumn == 0) { // disable left arrow
			$('#controls #left').removeClass('enabled').addClass('disabled');
		}
		
		// Get number of slides for active column
		var numSlides = $('.active-col .slide').size() - 1;
		
		// Determine which slide we're on
		if (numSlides == 0) { // There's only one slide. Disable both up and down arrows.
			$('#controls #up').removeClass('enabled').addClass('disabled');
			$('#controls #down').removeClass('enabled').addClass('disabled');
		} else 	if (currentSlide[currentColumn] == 0) { // disable up arrow
			$('#controls #up').removeClass('enabled').addClass('disabled');
		}
	}
	
	/**** Fades out the arrows after a given time ****/
	function tal_fade_out(time)
	{
		var o = $('#controls');
		t = setTimeout(function()
		{
			o.fadeOut();
		}, time);
	}
	
	/**** The slider animation ****/
	function tal_animate(el, x) 
	{
		el.animate({ "left": "+="+x }, animDuration);
	}
	
	/**** Determines the direction and amount to slide ****/
	function tal_update_slider(axis, direction, index) // direction should be the arrow you clicked on
	{
		if (index == currentColumn) { return; }
		
		// get the column width
		var colWidth = $('.column').width();
		
		// determine the slide amount
		var x;
		if (direction == 'right') { x = index == 0 ? (numOfCols -1) * colWidth +'px' : (index - currentColumn) * -colWidth +'px'; }
		if (direction == 'left')  { x = (currentColumn - index) * colWidth +'px'; }
		
		// set active class
		$('.active-col').removeClass('active-col');
		$('.column').eq(index).addClass('active-col');
		
		// reset currentColumn
		currentColumn = index;
		
		// update the navbar
		$('header a').filter('.active-link').removeClass('active-link').end().eq(currentColumn).addClass('active-link');

		// slide, baby, slide
		tal_animate($("#slider-inner"), x);

		tal_update_arrows();
	}
	

// -----------------------------
// EVENTS
//------------------------------
	/**** Navigation Events ****/
	/* On-screen buttons */
	$('#right').click(function() { tal_update_slider('x', 'right', (currentColumn + 1) % numOfCols); });	
	$('#left').click(function() { if (currentColumn > 0) tal_update_slider('x', 'left', (currentColumn - 1) % numOfCols); });	
	$('#up').click(function() {
		var numOfSlides = $('.active-col').children('.slide').size();
		if (currentSlide[currentColumn] > 0) tal_update_slider('y', 'up', (currentSlide[currentColumn] - 1) % numOfSlides);
	});	
	$('#down').click(function() {
		var numOfSlides = $('.active-col').children('.slide').size();
		tal_update_slider('y', 'down', (currentSlide[currentColumn] + 1) % numOfSlides);
	});	
	
	/* Arrow keys */
	$(document).keydown(function(e){
		switch (e.keyCode) {
			case 37: // left
				$('#left').trigger('click'); break;
			case 39: // right
				$('#right').trigger('click'); break;
		}
	});
	
	/* Navbar Links */
	$('header a').click(function()
	{
		var e = $($(this).attr('href'));
		var index = $('#slider-inner .column').index(e);
		if (currentColumn != index) tal_update_slider('x', (index > currentColumn ? 'right' : 'left'), index);
		return false;
	});
	
	/**** Mobile Touch Events ****/
	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (agentID) {
		$("#viewport").touchwipe(
		{
			wipeLeft: function(){ $('#left').trigger('click'); },
			wipeRight: function(){ $('#right').trigger('click'); },
			min_move_x: 100,
		});
	} else {
		/* Fade the arrows back in on mouse move */
		$(document).mousemove(function()
		{
			clearTimeout(t);
			if($('#controls').is(':hidden')) {
				$('#controls').fadeIn();
			}
			tal_fade_out(2000);
		});
		tal_fade_out(3000);
	}
	
	tal_update_arrows();
	
	
	
	
	/*------- FIND A BETTER WAY TO DO THIS */
	
	/**** Features Slider ****/
	var fc_width = 1000;
	var scrubber_item;
	var scrubber_items = $("#featured-carousel #scrubber .item").size();
	var fc_active_item = $("#featured-carousel #scrubber .item").index('.active-item');
	
	var a = 0;
	var autoplay = setInterval(function() { 
		if (a < (scrubber_items-1)) {
			tal_animate($('#featured-carousel #billboard .ads'), '-1000px');
			$('#featured-carousel #scrubber .active-item').removeClass('active-item').next().addClass('active-item');
			a++;
        } else {
			tal_animate($('#featured-carousel #billboard .ads'), '3000px');
			$('#featured-carousel #scrubber .active-item').removeClass('active-item');
			$('#featured-carousel #scrubber .item:first').addClass('active-item');
			a=0;
		}
		fc_active_item = a;
	}, 7000 );
	
	$('#featured-carousel #scrubber .item').click(function(e) {
		scrubber_item = $('#featured-carousel #scrubber .item').index(this);
		if (scrubber_item != fc_active_item) {
			var fc_direction = (scrubber_item > fc_active_item ? 'right' : 'left');
			var moveX;
			if (fc_direction == 'right') { moveX = (scrubber_item == 0 ? (scrubber_items -1) * fc_width +'px' : (scrubber_item - fc_active_item) * -fc_width +'px'); }
			if (fc_direction == 'left')  { moveX = (fc_active_item - scrubber_item) * fc_width +'px'; }
			$('#featured-carousel #scrubber .active-item').removeClass('active-item');
			$(this).addClass('active-item');
			tal_animate($('#featured-carousel #billboard .ads'), moveX);
			fc_active_item = scrubber_item;
			clearTimeout(autoplay);
		}
	});


	/**** Bios Slider ****/
	var bio_width = 1000;
	var bio_scrubber_item;
	var bio_scrubber_items = $("#bios-carousel #scrubber .item").size();
	var bio_active_item = 0;
	
	$('#bios-carousel #scrubber .item').click(function(e) {
		bio_scrubber_item = $('#bios-carousel #scrubber .item').index(this);
		//alert('bio_scrubber_item = '+bio_scrubber_item+', bio_active_item = '+bio_active_item);
		if (bio_scrubber_item != bio_active_item) {
			var bio_direction = (bio_scrubber_item > bio_active_item ? 'right' : 'left');
			var bio_moveX;
			if (bio_direction == 'right') { bio_moveX = (bio_scrubber_item == 0 ? (bio_scrubber_items -1) * bio_width +'px' : (bio_scrubber_item - bio_active_item) * -bio_width +'px'); }
			if (bio_direction == 'left')  { bio_moveX = (bio_active_item - bio_scrubber_item) * bio_width +'px'; }
			$('#bios-carousel #scrubber .active-item').removeClass('active-item');
			$(this).addClass('active-item');
			tal_animate($('#bios-carousel #bios'), bio_moveX);
			bio_active_item = bio_scrubber_item;
		}
	});
	
	
	/**** Portfolio Overlays ****/
	$("a[rel]").overlay({
		mask: '#000',

		onBeforeLoad: function() {

			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".content-wrap");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}

	});


});
