// Created for www.yellow-button.com by Joel Davis
// This work by Yellow Button, Inc. is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.  <http://creativecommons.org/licenses/by-nc-sa/3.0/us/>

var transdur = 600;

function speedup() {
	transdur = transdur - 5;
	if (transdur < 200) {
		transdur = 200;
	}
}
$.fn.transHeight = function (inner) {
	var newheight = 0 + $(inner).height();
	return $(this).animate({height: newheight}, transdur);
}
function changeFeature(clickedbtn) {
	var clickedfeature = clickedbtn.attr('href');
	var currentsection = clickedbtn.closest('div.portfoliosection');
	var previousfeat = currentsection.data('activefeat');
	currentsection.data('activefeat', clickedfeature);

	// play transfer effect
	if (clickedfeature.substring(clickedfeature.length-6) == '-intro') {
		currentsection.effect("transfer",{ to: currentsection.find('.featurebox'), className: 'ui-effects-transfer' },transdur);
	} else {
		clickedbtn.effect("transfer",{ to: currentsection.find('.featurebox'), className: 'ui-effects-transfer' },transdur);
	}

	if (previousfeat != clickedfeature) {
		// dim the previous feature button
		$(previousfeat + '-btn').removeClass('selected');
		$(previousfeat + '-btn').css({opacity: 0.33});

		// highlight the current feature button
		clickedbtn.css({opacity: 1.0});
		clickedbtn.addClass('selected');

		// fade out previous 
		$(previousfeat).css("background", "#e0dcdc").fadeOut((transdur/2), function(){
			// fade in current
			$(clickedfeature).css("background","#e0dcdc").fadeIn(transdur, function(){
				// adjust height of portfolio to height of new content
				$('#portfolio').transHeight(currentsection);
			});
		});
	}
	speedup();
}
function changeSection(clickedbtn) {
	var clickedsec  = clickedbtn.attr('href');
	var previoussec = $('#portfolio').data('activesec');
	$('#portfolio').data('activesec', clickedsec);
	var sectionintrobtn = '#' + clickedsec.substring(11) + '-intro-btn';

	if (clickedsec == previoussec) {
		changeFeature($(sectionintrobtn));	
	} else {
		// change styling for previous and current section button
		$(previoussec + '-btn').removeClass('selected');
		clickedbtn.addClass('selected');
	
		// hide previous section
		$(previoussec).css("background", "e0dcdc").hide('drop', {direction: 'left'}, transdur, function(){
			//show the intro of the new section
			changeFeatureFast($(sectionintrobtn));
			//show the new section
			$(clickedsec).css("background", "#e0dcdc").show('drop',{direction: 'right'}, transdur, function(){
				// adjust height of #portfolio to fit new content
				$('#portfolio').transHeight(clickedsec);
			});
		});
	}
	speedup();
}
function changeFeatureFast(clickedbtn) {
	var clickedfeature = clickedbtn.attr('href');
	var currentsection = clickedbtn.closest('div.portfoliosection');
	var previousfeat = currentsection.data('activefeat');
	currentsection.data('activefeat', clickedfeature);

	if (previousfeat != clickedfeature) {
		// dim the previous feature button
		$(previousfeat + '-btn').removeClass('selected');
		$(previousfeat + '-btn').css({opacity: 0.33});

		// highlight the current feature button
		clickedbtn.css({opacity: 1.0});
		clickedbtn.addClass('selected');

		// fade out previous 
		$(previousfeat).hide();
		$(clickedfeature).show();
		$('#portfolio').transHeight(currentsection);
	}
}

// when document is ready
$(function() {
	// work with css to hide all feature tiles and all sections
	$('HTML').addClass('JS');

	// activate the intro feature tile of each section
	$('.portfoliosection').each(function(){
		var firstfeat = $(this).find('.portfoliotile:first');
		// store data
		$(this).data('activefeat', '#' + firstfeat.attr('id'));
		// show section
		firstfeat.show();
	});

	// show the intro section
	$('.portfoliosection:first').show();
	
	//store the active section
	$('#portfolio').data('activesec', '#portfolio-intro');

	$('#portfolio').transHeight('#portfolio-intro');

	// enable section buttons
	//$('.subnav li a').click(function(event) {
	$('#portfolionav').click(function(event) {
		event.preventDefault();
		changeSection($(event.target));
	});

	// enable feature buttons
	$('.sectionnav').click(function(event){
		event.preventDefault();
		changeFeature($(event.target).closest('a'));
	});

	// setup fancy zoom for zoom links
	$('a[rel*=zoom]').fancyZoom({directory: '/img/zoom'});
});
