(function($){
	$(function(){
		if($('.home #right').length > 0){
			$('.home #right').cycle({
				timeout: 6000
			});
		}
		if($('.home #cycle').length > 0){
			$('.home #cycle').cycle({
				fx: 'fade',
				sync: 0,
				timeout: 6000,
				speed: 3000
			});
		}
		
		$("#access .current-menu-parent, #access .current-menu-item").prev().find('.sep').css('color','#fff');
		$('#access a').hover(function(){
			$this = $(this);
			$this.next().css('color','#fff');
			$this.parent().prev().find('.sep').css('color','#fff');
		}, function(){
			$this = $(this);
			if($this.parent().not('.current-menu-item, .current-menu-parent').length > 0){
				if($this.parent().next().not('.current-menu-item, .current-menu-parent').length > 0){
					$this.next().css('color','#000');
				}
				if($this.parent().prev('.current-menu-item, .current-menu-parent').length < 1)
				$this.parent().prev().find('.sep').css('color','#000');
			}
		});
		
		function changeContent(currSlideElement, nextSlideElement, options, forwardFlag){
			// if($(nextSlideElement).find('.right').find('img').length > 0){
			if($(nextSlideElement).find('.right img').length > 0){
				var content = $(nextSlideElement).find('.right').html();
				$('#video').html(content);
			}
			// else if($(nextSlideElement).find('.question').find('.answer').length > 0){
			else if($(nextSlideElement).find('.question .answer').length > 0){
				$('.question').removeClass('active');
				var content = $(nextSlideElement).find('.question:first').addClass('active').find('.answer').html();
				$('#video div').html(content);
			}
		}
		
		$video = $('#video');
		var menu = $('#access .current-menu-parent .sub-menu').html();
		if(!menu) menu = $('#access .current-menu-item .sub-menu').html();
		$('#subnav ul').append(menu);
		
		$subpage = $('.sub-page');
		if($subpage.length > 0){
			$subpages = $('#pages .entry-content');
			$subpages.cycle({
				fx: 'none',
				slideResize: 0,
				containerResize: 0,
				before: changeContent,
				pager: '.sub-pagination',
				pagerAnchorBuilder: function(idx, slide) { 
					return '<li class="sp'+idx+'"></li>'; 
				}
			}).cycle('pause');
		}
		else{
			$('.sub-pagination').hide();
		}
		
		$questions = $('.question');
		if($questions.length > 0){
			$video.addClass('text').append('<div></div');
			var answer = $questions.first().addClass('active').find('.answer').html();
			$('#video div').html(answer);
			$questions.click(function(e){
				e.preventDefault();
				$this = $(this);
				$questions.removeClass('active');
				answer = $this.addClass('active').find('.answer').html();
				$('#video div').html(answer);
			});
		}
		
		$right = $('#pages .entry-content .right');
		if($right.length > 0){
			var html = $right.html();
			$video.addClass('text').append('<div></div>');
			$('#video div').html(html);
		}
		
		$email = $('.newslettersignup #signup');
		$('.newslettersignup form').submit(function(event){
			event.preventDefault(); 
			var data = $email.val();
			if(data.match(/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/gi)){
				$email.css('border-color','#00AF0B');
				$.post("/wp-content/themes/brodtaylor/process.php", { data: data} );
				$(this).replaceWith("<span class='success'>Thanks for signing up!</span>");
				return false;
			}
			else{
				$email.css('border-color','#D31145');
				return false;
			}
		});
		
		$('.newsletter').mouseenter(function(){
			$('.newslettersignup').fadeIn();
		});
		
		$('.newslettersignup').mouseleave(function(){
			$(this).fadeOut();
		});
		
	});
})(jQuery);
