$(document).ready(function() {

	$('ul#navigation li:first a:first').addClass('active');

	//NEWSLETTER SIGNUP
	var initVal = $('input.input-text').val();
	$("input.input-text").focus(function() {
		if($(this).hasClass("empty")) {
			$(this).val('');
		}
	});
	$("input.input-text").blur(function() {
		if($(this).val()) {
			$(this).removeClass("empty");
		}
		else {
			if($(this).val() == initVal) {
				$(this).val('');
			}
			$(this).val(initVal);
		}
	});

	// IE6 Hover Support
	$('#main-nav li').mouseover(function() {
		$(this).addClass('sfhover');
	});
	$('#main-nav li').mouseout(function() {
		$(this).removeClass('sfhover');
	});

	// HOMEPAGE - NEWS
	$('#news li div.content').hide();
	$('#news li div.content:first').show();
	$('#news li h3 a').click(function(e) {
		e.preventDefault();
		if ($(this).parent().next().is(':visible')) {
			return false;
		} else {
			$('#news li.open').removeClass('open');
			$('#news li div.content:visible').slideUp('500');
			$(this).parent().next().slideDown('500');
			$(this).parent().parent().addClass('open');
			return false;
		}
	});

	// HOMEPAGE - CAROUSEL
	var autoRotate = true;
	var delay = 8000;

	if (autoRotate && $('#carousel ul.slides li').length > 1) {
		$('#carousel ul.slides li').hide();
		$('#carousel ul.slides li:first').show();
		$('#carousel ul.slides li:first').addClass('active');

		var currNum = 0;

		function changeSlide() {
			if ((currNum + 1) >= $('#carousel ul.slides li').length)
				currNum = 0;
			else
				currNum++;
			var currSlide = $('#carousel ul.slides li.active');
			$('#carousel ul.slides li').eq(currNum).addClass('active');
			currSlide.removeClass('active');
			$('#carousel ul.slides li.active').stop(true,true).fadeIn('slow');
			currSlide.stop(true,true).fadeOut('slow');
			$('#carousel ul.navigation li a.active').removeClass('active');
			$('#carousel ul.navigation li').eq(currNum).children('a').addClass('active');
		}

		$('#carousel').mouseover(function() {
			clearInterval(timer);
		});
		$('#carousel').mouseout(function() {
			timer = setInterval(changeSlide, delay);
		});

		var timer = setInterval(changeSlide, delay);

		$('#carousel ul.navigation li a').click(function(e) {
			e.preventDefault();
			clearInterval(timer);
			var prevNum = currNum - 1;
			currNum = $('#carousel ul.navigation li').index($(this).parent()) - 1;
			if (prevNum != currNum) {
				changeSlide();
			}
			//reset currNum for clicking the same button more than once
			currNum = $('#carousel ul.navigation li').index($(this).parent());
		});
	}

	// MEDIA - CAROUSEL
	var currentImages = $('div.carousel div.images ul:first');
	$('ul.media-nav a').click(function(e) {
		e.preventDefault();
		if (!$(this).hasClass("active")) {
			$('ul.media-nav li a.active').removeClass("active");
			$(this).addClass("active");
			$(currentImages).fadeOut();
			currentImages = $('div.carousel div.images ul').eq($('ul.media-nav li').index($(this).parent()));
			currentImages.fadeIn();
			if ($('div.media div.carousel a.left-arrow').length) {
				$('div.carousel a.left-arrow').remove();
				$('div.media div.carousel').append('<span class="left-arrow">Previous</span>');
			}
			mediaCarousel();
		}
	});

	if ($('div.media div.images ul.screenshots').children().size() > 3)
		mediaCarousel();

	function mediaCarousel() {
		var currImages = 3;
		currentImages.css("left", 0);
		if (currentImages.children().length > 3) {
			$('div.media div.carousel span.right-arrow').remove();
			$('div.media div.carousel').append('<a class="right-arrow" href="#">Next</a>');
			$('div.media div.carousel a.right-arrow').click(function(e) {
				e.preventDefault();
				nextImages();
			});

			function nextImages() {
				var totalImages = $(currentImages).children().size();

				currImages += 3;
				$(currentImages).animate({left:'-=489'});
				if (totalImages - currImages < 1) {
					$('div.carousel a.right-arrow').remove();
					$('div.media div.carousel').append('<span class="right-arrow">Next</span>');
				}
				if ($('div.carousel span.left-arrow').length) {
					$('div.carousel span.left-arrow').remove();
					$('div.media div.carousel').append('<a class="left-arrow" href="#">Previous</a>');
					$('div.media div.carousel a.left-arrow').click(function(e) {
						e.preventDefault();
						previousImages();
					});
				}
			}

			function previousImages() {
				var totalImages = $(currentImages).children().size();

				currImages -= 3;
				$(currentImages).animate({left:'+=489'});
				if (currImages == 3) {
					$('div.carousel a.left-arrow').remove();
					$('div.media div.carousel').append('<span class="left-arrow">Previous</span>');
				}
				if ($('div.carousel span.right-arrow').length) {
					$('div.carousel span.right-arrow').remove();
					$('div.media div.carousel').append('<a class="right-arrow" href="#">Next</a>');
					$('div.media div.carousel a.right-arrow').click(function(e) {
						e.preventDefault();
						nextImages();
					});
				}
			}
		} else if ($('div.carousel a.right-arrow')) {
			$('div.carousel a.right-arrow').remove();
			$('div.media div.carousel').append('<span class="right-arrow">Next</span>');
		}
	}

	//INCARNATE SYSTEM - TREE ROTATOR
	var slideSpeed = 300,
		currTree = 0,
		nextTree,
		firstTree = $('#tree-container .tree:first-child'),
		readyToSlide = true;

	$('#tree-container .tree,#tree-container .tree .hr-1,#tree-container .tree .top,#tree-container .tree .clear,#tree-container .tree h3').hide();
	$('#tree-container .tree').each(function(){
		$(this)
			.css({
				'width' : $(this).width(),
				'height' : $(this).height(),
				'top' : 0,
				'padding-top' : 20,
				'position' : 'absolute'
			})
	});
	$('#tree-container .tree').hide();
	$('#tree-container').css({ 'width' : firstTree.width(), 'height' : firstTree.outerHeight() });
	firstTree.show();
	$('#tree-nav-menu li:first-child').addClass('active');

	//Event per menu item click
	$('#tree-nav-menu a').click(function(e){
		e.preventDefault();
		$('#tree-nav-menu li.active').removeClass('active');
		$(this).parent().addClass('active');

		var buttonClass = $(this).attr('class');
		nextTree = $('#tree-container #' + buttonClass).index();
		$('#tree-container').css('overflow','hidden');
		if((currTree != nextTree) && readyToSlide)
		{
			readyToSlide = false;
			slideTree();
			currTree = nextTree;
		}
	});

	function slideTree(){
		//get tree objects
		var treeToSlideOut = $('#tree-container div.tree:nth-child(' + (currTree + 1) + ')');
		var treeToSlideIn = $('#tree-container div.tree:nth-child(' + (nextTree + 1) + ')');

		//animate resize tree-container
		$('#tree-container').stop(true,true).animate({height: treeToSlideIn.outerHeight()},slideSpeed);

		//animate trees
		if(currTree < nextTree)
		{
			//slide right to left
			treeToSlideIn
				.show()
				.css('left',$('#tree-container').width())
				.stop(true,true)
				.animate({left: 0},slideSpeed);
			treeToSlideOut
				.stop(true,true)
				.animate(
					{left: -$('#tree-container').width()},
					slideSpeed,
					function(){
						$(this).hide();
						$('#tree-container').css('overflow','visible');
						readyToSlide = true;
					}
				);
		} else {
			//slide left to right
			treeToSlideIn
				.show()
				.css('left',-$('#tree-container').width())
				.stop(true,true)
				.animate({left: 0},slideSpeed);
			treeToSlideOut
				.stop(true,true)
				.animate(
					{left: $('#tree-container').width()},
					slideSpeed,
					function(){
						$(this).hide();
						$('#tree-container').css('overflow','visible');
						readyToSlide = true;
					}
				);
		}
	}; //end slideTree()
	//END OF INCARNATE SYSTEM - TREE ROTATOR

	//INCARNATE SYSTEM - TOOLTIP
	var ttFadeSpeed = 200;
	$('#tree-container .fruit')
		.hover(
			function(){
				var fruit = $(this),
					tooltip = fruit.children('.tooltip'),
					ttFruitHeight = fruit.outerHeight() + tooltip.outerHeight(),
					fruitTop = fruit.offset().top - $(window).scrollTop(),
					viewportHeight = $(window).height(),
					ttLeftPos = 0 - Math.abs((Math.round((tooltip.outerWidth() - fruit.outerWidth()) / 2))),
					ttTopPos = fruit.outerHeight() + 4;
				//IE fix
				$(this).addClass('ie-hover-fix');
				//check to see if tooltip should be drawn upward
				if((viewportHeight > ttFruitHeight) && (fruitTop > (viewportHeight - ttFruitHeight)))
				{
					ttTopPos = 0 - tooltip.outerHeight() - 6;
					tooltip.children('.point').addClass('point-bottom');
				} else {
					tooltip.children('.point-bottom').removeClass('point-bottom');
				}
				tooltip.css({
					'left' : ttLeftPos,
					'top' : ttTopPos
				});
				tooltip.stop(true,true).fadeIn(ttFadeSpeed);
			},function(){
				$(this)
					//IE Fix
					.removeClass('ie-hover-fix')
					.children('.tooltip')
					.stop(true,true)
					.fadeOut(ttFadeSpeed)
			}
		);
	//END OF INCARNATE SYSTEM - TOOLTIP

}); //END OF document.ready()
