$(document).ready(function() {
	enableImageLinks();
	enableFlashyScroll();
	enableAjaxCalls();
	$('#worknav span.smallimage').each(function(){$(this).toggle();});
	slideOut();
});

function enableImageLinks(){
	
	$('#folioitem ul').css('padding','0 300px');
	$("#folioitem").scrollTo($('#item1'));
	$('#folioselect').css('display', 'block');
	childCount = $('#folioitem ul').children().length;
	$('#folioitem ul').css('width',childCount*630+'px');
	$('#folioselect').append('<ul></ul>');
	for(i = 1;i<=childCount;i++){
		$('#folioselect ul').append('<li><a href="#" onClick = \'folioScrollTo('+i+');return false;\'>'+i+'</a></li>');
	}
}

//smoothes the effect over longer distances
var current = 1;
function folioScrollTo(i){
	$("#folioitem").stop().scrollTo($('#item'+i), Math.sqrt(Math.abs(i-current))*500, { easing:'easeOutBack' });
	current = i;
}

current_x = 0;
function enableFlashyScroll(){
	$('#hideimgnav').css('display','block');
	$('#hideimgnav a').mousedown(function(e){
		if($(this).html() == 'Hide')
			$(this).html('Show');
		else
			$(this).html('Hide');
		i = 1;
		$('#worknav span.smallimage').each(function(){
			i++;
			$(this).slideToggle({
				duration: 300,
				queue: "first",
				scope: "01",
				preDelay: 100*i
			});
		});
		$.fxqueues("first").start();
		e.preventDefault();
		return false;
		
	});
	
	$workscroll = $('#workscroll');
	$workscroll.css('overflow','hidden');
	width = $('#workscroll ul').width();
	//add the slider
	$('#scrollbar').slider({animate:false});
	$('#scrollbar').bind('slide', function(event, ui) {
		var value = $('#scrollbar').slider('option', 'value');
		var pxvalue = (value*(width-990))/100;
		$workscroll.scrollTo(pxvalue);
	});
	$('#scrollbar').bind('slidestop', function(event, ui) {
		var value = $('#scrollbar').slider('option', 'value');
		var pxvalue = (value*(width-990))/100;
		$workscroll.scrollTo(pxvalue,100, { easing:'easeInOutQuad' });
	});
	
	//add mousewheel scrolling support
	$workscroll.mousewheel(function(event, delta){
		if(delta < 0 && current_x < (width-990))
			current_x += -delta*196;
		if(delta > 0 && current_x > 0)
			current_x += -delta*196;
		$workscroll.scrollTo(current_x,100,{ easing:'easeInOutQuad' });
		var scrolledleft = $workscroll.scrollLeft();
		pxvalue = ((current_x)/(width-990)*100);
		$('#scrollbar').slider('option', 'value', pxvalue);
		return false;
	});	
	$workscroll.unmousewheel(function(){
		$workscroll.stop();
	});
	
	//add left-right navigation support
	$(window).keypress(function(e){
		var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
		if(key == 37 && ($workscroll.scrollLeft() > 0)){
			if(current_x > 0)
				current_x += -196;
			$workscroll.scrollTo(current_x,200,{ easing:'easeInOutQuad' });
		}
		if(key == 39 && ($workscroll.scrollLeft() < (width-960))){
			if(current_x < (width-990))
				current_x += 196;
			$workscroll.scrollTo(current_x, 200,{ easing:'easeInOutQuad' });
		}
		pxvalue = ((current_x)/(width-990)*100);
		$('#scrollbar').slider('option', 'value', pxvalue);
	});
}

function enableAjaxCalls(){
	var i=0;
	$('#worknav a').each(function(){
		$(this).click(function(e){
			var title = $(this).attr('title');
			$('#workcontent').animate({opacity:0.01,queue:true}, 300, "easeInOutCubic", function(){
				$('#workcontent').load('/ajax_call?id='+title, false, function(){
					enableImageLinks();
					$('#workcontent').animate({opacity:1,queue:true},400 ,"easeInOutCubic");
				}); 
			});
			return false;
		});
		i++;
	});
}

function slideOut(){
	i = 5;
	$('#worknav span.smallimage').each(function(){
		i++;
		$(this).slideToggle({
			duration: 300,
			queue: "first",
			scope: "01",
			preDelay: 100*i
		}, function(){alert('x');});
	});
	setTimeout(function(){
		if($('#hideimgnav a').html() == 'Show')
			$('#hideimgnav a').html('Hide');
		else
			$('#hideimgnav a').html('Show');
	}, 500);

	
}
