$(document).keyup(function(e) {
	if(e.keyCode == 8) {
		var url = document.location.href;
		var splitter = url.split('#');
		splitter = splitter[1].split('section');
		var i = parseInt(splitter[1]);
		$.fn.contentSwitcher.linkClickBack(i)
	}
});

(function($) {

/***** SELECT THE ACTIVE TAB AND LINK BASED ON BOOKMARK (eg #section1) *****/

	var tabValue;
	var linkClick;
	var pathname = window.location.pathname;
	var href = window.location.href;
	tabCheck = href.split('#section');
	tabCheck = tabCheck[1];
	tabValue = 0;
	if (tabCheck) { tabValue = tabCheck - 1; }
	

/********************************************************************************/
/***** RUN THE PLUGIN **********************************************************/
/******************************************************************************/

	$.fn.contentSwitcher = function(options) {
		var options1 = options;
		var options2 = {
			speedSwitch: 		500,
			speedOpen: 			750,
			speedClose: 		750,
			allowClose: 		false,
			switcherCells:		'.switcher-cells',
			switcherCell:		'.switcher-cell',
			switcherNavigation:	'.switcher-navigation'
		};
		
		var params 		= $.extend(options2, options1);
		var transition 	= 'off';
			
	/***** ASSIGN OBJECTS TO VARIABLES *****/

		var container 	= $(this);
		var nav 		= params.switcherNavigation;
		var tabs 		= $(nav).find('a');
		var shell 		= params.switcherCells;
		var cells 		= $(shell).children();
		var links 		= $(nav).find('a');

	/***** ASSIGN PARAMETERS TO VARIABLES *****/

		var speedSwitch = params.speedSwitch;
		var speedOpen 	= params.speedOpen;
		var speedClose 	= params.speedClose;
		var allowClose 	= params.allowClose;


/********************************************************************************/
/***** LINKS *******************************************************************/
/******************************************************************************/

	/***** RESET ACTIVE STATES *****/

		var resetActive = function(active) {
			if ($(links[active]).attr('class') != 'selected') {
				$(links).each(function() {
					$(this).removeClass('selected');
					$(this).addClass('not-selected');
					Cufon.refresh('.switcher-navigation li a');
				});
				$(links[active]).removeClass('not-selected');
				$(links[active]).addClass('selected');
			} else {
				$(links).each(function(i) {
					if((allowClose === false && active != i) || allowClose !== false) { $(links[i]).removeClass('active'); }
				})
			}
		}
	
	/***** actions when you click *****/
	
		$.fn.contentSwitcher.linkClick = function(i) { linkClick(i); }
		linkClick = function(i) {
			if (transition == 'off') {
				var url = document.location.href;
				var splitter = url.split('#');
				//document.location.href = splitter[0] + '#section' + (i + 1);
				transition = 'on';
				resetActive(i);
				runSwitcher(i);
			}
		}

		$.fn.contentSwitcher.linkClickBack = function(i) { linkClickBack(i); }
		linkClickBack = function(i) {
			if (transition == 'off') {
				Cufon.refresh('.switcher-navigation li a');
				transition = 'on';
				resetActive(i);
				runSwitcher(i);
			}
		}
		



/********************************************************************************/
/***** SWITCHER ****************************************************************/
/******************************************************************************/
	
	/***** RUN THE SWITCHER *****/
	
		var runSwitcher = function(nextIndex) {
			var cells = $(shell).children();
			var nextCell = $(cells[nextIndex]);
			var closed = true;
			var nextCell;
			var currentCell;
			var currentIndex;

			$(cells).each(function(i) {
				if ($(cells[i]).is(':visible')) {
					currentCell = $(cells[i]);
					currentIndex = i;
					closed = false;
				}
			})
			if (closed === true) {
				opener(nextCell);
			} else {
				if (nextIndex == currentIndex) {
					if (allowClose !== false) {
						$(shell).css({ 'height': $(currentCell).height() + 'px' });
						closer(nextCell);
					} else {
						transition = 'off';
					}
				} else {
					switcher(currentCell, nextCell);
				}
			}
		}
	
	/***** SWITCHER *****/
	
		var switcher = function(currentCell, nextCell) {
			var height1 = $(currentCell).height();
			var height2 = $(nextCell).height();

			if($('.last-column')) {
				var minHeight = $('.last-column').height();
				if(height2 < minHeight) {
					height2 = minHeight;
				}
			}
			
			$(currentCell).css({ 'position': 'absolute' });
			$(nextCell).css({ 'position': 'absolute' });
			$(shell).css({ 'height': height1 + 'px' });

			var animate1 = $(currentCell).fadeOut(speedSwitch);
			var animate2 = $(nextCell).fadeIn(speedSwitch);
			var animate3 = $(shell).animate({ 'height': height2 + 'px' }, speedSwitch, function() { transition = 'off'; });
			animate = $(animate1).add(animate2).add(animate3);
		}
	
	/***** OPENER *****/
	
		var opener = function(nextCell) {
			var height2 = $(nextCell).height();
			$(nextCell).css({ 'position': 'absolute' });
			var animate1 = $(nextCell).fadeIn(speedOpen);
			var animate2 = $(shell).animate({ 'height': height2 + 'px' }, speedOpen, function() { transition = 'off'; });
			$(animate1).add(animate2);
		}
	
	/***** CLOSER *****/
	
		var closer = function(nextCell) {
			var height2 = 0;
			$(nextCell).css({ 'position': 'absolute' });
			var animate1 = $(nextCell).fadeOut(speedClose);
			var animate2 = $(shell).animate({ 'height': height2 + 'px' }, speedClose, function() { transition = 'off'; });
			$(animate1).add(animate2);
		}
		

/********************************************************************************/
/***** SWITCHER ****************************************************************/
/******************************************************************************/
		
		$(this).each(function() {
			$(tabs[tabValue]).addClass('selected');
			$(cells[tabValue]).show();

		/***** ASSIGN THE LINKS *****/

			$(links).each(function(i) {
				$(this).bind('click', function(e) {
					this.blur();
					linkClick(i);
					e.preventDefault();
				});
			});
		});
	}
})(jQuery);





