$(document).ready(function() {
	
	var moreThanOneSlide = ($("#content li").length > 1) ? true : false;
	
	var sliderOptions = {
		easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
		autoPlay: false,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
		delay: 3000,                    // How long between slide transitions in AutoPlay mode
		startStopped: false,            // If autoPlay is on, this can force it to start stopped
		animationTime: 600,             // How long the slide transition takes
		hashTags: true,                 // Should links change the hashtag in the URL?
		buildNavigation: moreThanOneSlide,          // If true, builds and list of anchor links to link to each slide
		buildArrows: moreThanOneSlide,
		pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
		startText: "Go",                // Start text
		stopText: "Stop"               // Stop text
	};
	
	$('.anythingSlider').anythingSlider(sliderOptions);
	var animationSpeed = 1000;
	
	$('#about-button').click(function() {
		$("#projects-menu").stop().css('display', 'none');
		$(this).find('a').stop().css("background", "transparent");
		$("#about-menu").stop().css("display", "block");
		$('#logo').stop().animate({ opacity: 0 }, animationSpeed);
		$("#about-menu").stop().animate({ opacity: 1}, animationSpeed);
		$(this).stop().animate({ width: '300px', backgroundColor: '#666'}, animationSpeed);
		$('#projects-button a').stop().animate({ backgroundColor: '#b3b3b3' }, animationSpeed);
		$('#projects-button').stop().animate({ backgroundColor: '#458dd4' }, animationSpeed);
		var pageId = $(this).find('a').attr('class');
		$.post('/index.php/ajax/get_sections_for_page_id', {id: pageId}, function(result) {
			document.title = "Welcome | fatcrowdesign.com";
			replaceSections(result);
			replaceEditPageLink(pageId, 'About');
		});
		
		return false;
	});
	
	$('#projects-button').click(function() {
		$("#about-menu").stop().css('opacity', 0);
		$(this).find('a').stop().css("background", "transparent");
		$("#projects-menu").stop().css("display", "block");
		$('#logo').stop().animate({ opacity: 0 }, animationSpeed);
		
		$("#projects-menu").stop().animate({ opacity: 1 }, animationSpeed);
		$('#about-button a').stop().animate({ backgroundColor: '#b3b3b3'}, animationSpeed);
		$('#about-button').stop().animate({ width: '150px', backgroundColor: '#fbe605'}, animationSpeed);
		$(this).stop().animate({ backgroundColor: '#666' }, animationSpeed);
		
		var pageId = $(this).find('a').attr('class');
		$.post('/index.php/ajax/get_sections_for_page_id', {id: pageId}, function(result) {
			document.title = "Welcome | fatcrowdesign.com";
			replaceSections(result);
			replaceEditPageLink(pageId, 'Projects');
		});
		
		
		return false;
	});
	
	$("#home-button").click(function() {
		var pageId = $(this).attr('class');
		$("#projects-menu").stop().css('display', 'none');
		$("#about-menu").stop().css('display', 'none');
		$('#logo').stop().css({ opacity: 1 });
		$('.menu-button a').stop().animate({ backgroundColor: '#b3b3b3'}, animationSpeed);
		$('#projects-button').stop().animate({ backgroundColor: '#458dd4'}, animationSpeed);
		$('#about-button').stop().animate({ width: '213px', backgroundColor: '#fbe605'}, animationSpeed);
		$(".sub-menu").stop().animate({ opacity: 0 }, animationSpeed);
		$.post('/index.php/ajax/get_sections_for_page_id', {id: pageId}, function(result) {
			document.title = "Welcome | fatcrowdesign.com";
			replaceSections(result);
			replaceEditPageLink(pageId, 'Welcome');
		});
		return false;
	});
	
	$(".sub-menu a").click(function() {
		var pageId = $(this).attr('class');
		var pageTitle = $(this).html();
		var windowTitle = pageTitle + " | fatcrowdesign.com ";
		$.post('/index.php/ajax/get_sections_for_page_id', {id: pageId}, function(result) {
			document.title = windowTitle;
			replaceSections(result);
			replaceEditPageLink(pageId, pageTitle);
		});
		$(".sub-menu a").removeClass('current');
		$(this).addClass('current');
		return false;
	});
	
	var startId = 'home-button';
	if ($('body').hasClass('cat-1'))
	{
		startId = 'about-button';
	}
	else if ($('body').hasClass('cat-2'))
	{
		startId = 'projects-button';
	}
	$("#" + startId).trigger('click');
	
	function replaceSections($list)
	{	
		$("#content li").remove();
		$(".thumbNav").remove();
		$("#content").append($list);
		moreThanOneSlide = ($list.length > 1) ? true : false;
		$('.anythingSlider').anythingSlider({
			easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
			autoPlay: false,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
			delay: 3000,                    // How long between slide transitions in AutoPlay mode
			startStopped: false,            // If autoPlay is on, this can force it to start stopped
			animationTime: 600,             // How long the slide transition takes
			hashTags: true,                 // Should links change the hashtag in the URL?
			buildNavigation: moreThanOneSlide,          // If true, builds and list of anchor links to link to each slide
			buildArrows: moreThanOneSlide,
			pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
			startText: "Go",                // Start text
			stopText: "Stop"               // Stop text
		});
	}
	
	function replaceEditPageLink(id, title)
	{
		var link = $('.edit-page').attr('href');
		if (link)
		{
			var splitLink = link.split('/');
			var newLink = '';
			var lastIndex = splitLink.length - 1;
			
			for (var i = 0; i < lastIndex; i++)
			{
				newLink += splitLink[i];
				newLink += "/";
			}
			newLink += id;
			$('.edit-page').attr('href', newLink).html("Edit " + title);
		}
	}
	
});

