var fadedAmt = 0.2;
var fadeyAmt = 0.6;
var kindafadeyAmt = 0.85;
function fadeyIn() {$(this).stop().animate({opacity: 1}, "fast");};
function kindafadeyOut() {$(this).stop().animate({opacity: kindafadeyAmt}, "fast");};
function fadeyOut() {$(this).stop().animate({opacity: fadeyAmt}, "fast");};
var hashstr = window.location.hash.substr(1);

$(function() {
	// Fadey stuff
	var $fadeythings = $("#navigation li[class!='active'], #footer img, #contact .fadey, #portfoliothumbs img, .portfoliobutton img");
	$fadeythings.css("opacity",fadeyAmt);
	$fadeythings.hover(fadeyIn, fadeyOut);
	var $kindafadeythings = $("div.logo img, div.article div.content img");
	$kindafadeythings.css("opacity", kindafadeyAmt);
	$kindafadeythings.hover(fadeyIn, kindafadeyOut);
	
	// Container style correction for top and bottom items
	$(".article:first").css("padding-top", "0px");
	$(".article:last").css("border-bottom", "0px");
	$("tr:last").css("border-bottom", "0px");
	
	$("table.uniyear").toggle();
	$("table.unidegree a").click(function(event){
		$(this).toggle();
		$("table.uniyear").toggle();
		event.preventDefault();
	});
	
	// Fancybox	
	$('a:has(img)[href$=".gif"], a:has(img)[href$=".jpg"], a:has(img)[href$=".png"]').attr('rel','gallery');
	$('a[href$=".gif"], a[href$=".jpg"], a[href$=".png"]').fancybox({
		'titlePosition'	:	'inside',
	});
	$(".fancybox").fancybox({
		'titlePosition'	:	'inside',
	});
		
	// Ride the carousel...
	$("#portfolio").jcarousel({
		start: parseInt(hashstr ? hashstr : 1),
		scroll: 1,
		easing: "easeInOutQuint",
		itemFallbackDimension: 480,
		initCallback:   mycarousel_initCallback,
		itemFirstInCallback:  mycarousel_itemFirstInCallback,
		buttonPrevCallback:   mycarousel_buttonPrevCallback,
		buttonNextCallback:   mycarousel_buttonNextCallback,
		itemVisibleInCallback: {
			onBeforeAnimation: mycarousel_itemVisibleInCallbackBeforeAnimation,
			onAfterAnimation: mycarousel_itemVisibleInCallbackAfterAnimation
		},
		itemVisibleOutCallback: {
			onBeforeAnimation: mycarousel_itemVisibleOutCallbackBeforeAnimation
		}
	});
});

// Assign functionality to the controls
function mycarousel_initCallback(carousel) {
	$('#portfolio li div.desc').hide();
	$('#portfoliothumbs a').bind('click', function() {
		carousel.scroll($.jcarousel.intval($(this).attr("href").substr(1)));
		return false;
	});
 
	$('#portfolionext, .portfolioimglink').bind('click', function() {
		carousel.next();
		return false;
	});
 
	$('#portfolioprev').bind('click', function() {
		carousel.prev();
		return false;
	});
};

// Buttons
function mycarousel_buttonPrevCallback(carousel, button, enabled) {
	if(enabled) {
		$('#portfolioprev').attr("href", "#");
		$('#portfolioprev img').animate({opacity: fadeyAmt}, "fast");
		$('#portfolioprev img').hover(fadeyIn, fadeyOut);
	} else {	
		$('#portfolioprev').removeAttr("href"); 
		$('#portfolioprev img').animate({opacity: fadedAmt}, "fast");		
		$('#portfolioprev img').unbind('mouseenter mouseleave');		
	}
};
function mycarousel_buttonNextCallback(carousel, button, enabled) {
	if(enabled) {
		$('#portfolionext').attr("href", "#");
		$('#portfolionext img').animate({opacity: fadeyAmt}, "fast");
		$('#portfolionext img').hover(fadeyIn, fadeyOut);
	} else {
		$('#portfolionext').removeAttr("href"); 
		$('#portfolionext img').animate({opacity: fadedAmt}, "fast");		
		$('#portfolionext img').unbind('mouseenter mouseleave');		
	}
};

// Fades
function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
	// Fade in first thumb on load
	$('#portfoliothumbs a:nth-child('+idx+') img').animate({opacity: 1}, "slow");
	// Set the active thumbnail
	$('#portfoliothumbs a:nth-child('+idx+') img').unbind('mouseenter mouseleave');
};

// For an item that is being called in, as it comes in
function mycarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
	// Hide the text, faded in later
	$('div.desc', item).hide();
	// Set the active thumbnail
	$('#portfoliothumbs a:nth-child('+idx+') img').animate({opacity: 1}, "fast");
	$('#portfoliothumbs a:nth-child('+idx+') img').unbind('mouseenter mouseleave');
	// Change hash tag
	window.location.hash = idx;
};

// For an item that is being called in, after it comes in
function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
	// Fade in the text
	$('div.desc', item).fadeIn('slow');
};

// For items that are leaving, as they leave
function mycarousel_itemVisibleOutCallbackBeforeAnimation(carousel, item, idx, state) {
	// Fade out text
	$('.desc', item).fadeOut('slow');
	// Re-enable fading on the new inactive thumbnail
	$('#portfoliothumbs a:nth-child('+idx+') img').animate({opacity: fadeyAmt}, "slow");
	$('#portfoliothumbs a:nth-child('+idx+') img').hover(fadeyIn, fadeyOut);
};
