(function($) {
	var tab_changing = false;
	var timer_active = 0;
	var timer_time = 7000;
	var timer = false;
	var faq;
	$(document).ready(function () {
		Cufon.replace('h2,h3,.header-text,.menu,.feature-tab-content,.headlines-heading,.campaign-right-heading,.connected-heading,.box');
				
		$(".fancybox").fancybox();
		
		$(".menu li:not(.current-menu-item,.current-page-ancestor)").live('mouseover',function() {
			$(this).find("a").css('color','#fff');
			$(this).addClass('current-menu-hover');
			Cufon.replace($(this).find("a"));
		}).live('mouseout',function() {
			$(this).find("a").css('color','#008EAE');
			$(this).removeClass('current-menu-hover');
			Cufon.replace($(this).find("a"));
		});
		
		$(".feature-tab").live('mouseover',function() {
			if (!tab_changing)
			{
				clearTimeout(timer);
				$('.no-background').removeClass('no-background');
				$('.tab-active').removeClass('tab-active');
				$('.tab-content-active').removeClass('tab-content-active');
				$(this).addClass('tab-active').find('div').addClass('tab-content-active');
				Cufon.replace('.feature-tab-content');
				
				if ($(this).hasClass('tab-1'))
				{
					var id = 1;
				}
				else if ($(this).hasClass('tab-2'))
				{
					$('.tab-1').addClass('no-background');
					var id = 2;
				}
				else if ($(this).hasClass('tab-3'))
				{
					$('.tab-2').addClass('no-background');
					var id = 3;
				}
				
				if (!$('#feature-'+id+':visible').length > 0)
				{
					tab_changing = true;
					$('.feature:visible').fadeOut(250,function() {
						$('#feature-'+id).fadeIn(250);
						tab_changing = false;
					});
				}
			}
		});
		
		$(".feature-tab").live('click',function() {
			var id = $(this).attr('id').substring(12);
			window.location = $("#feature-"+id+" a").attr("href");
		});	
		
		$(".headline-slide:not(.headline-slide-active)").live('click',function() {
			$('.headline-slide-active').removeClass('headline-slide-active');
			$(this).addClass('headline-slide-active');
			$(".headline-dots").show();
			$(this).find('.headline-dots').hide();
			if ($(".headline-more:visible").length > 0)
			{
				$(".headline-more:visible").hide();
				$(this).find('.headline-more').show();
			}
			else
			{
				$(this).find('.headline-more').show();
			}
		});
		
		$(".content-dots").live('click',function() {
			$(this).hide();
			$('.content-more').show();
		});
		
		$(".faq-link").live('click',function() {
			var id = $(this).attr('id').substring(9);
			$(".faq-link").removeClass('faq-active');
			$(this).addClass('faq-active');
			var faq_div = $("#faq-"+id);
			faq_div.show();
			var faq_height = faq_div.height();
			faq_div.css('height',0);
			faq_div.animate({ height : faq_height }, { duration: 250, complete:function () {
				if (faq != undefined)
				{
					faq.slideUp(250,function() {
						faq = $("#faq-"+id);
					});
				}
				else
				{
					faq = $("#faq-"+id);
				}
			}});
			
		});
		
		$("#feature img").each(function(){
	        $('<img/>').attr('src') = $(this).attr('src');
	    });
	    
	    $("#feature").live('mouseover',function() {
			clearTimeout(timer);
		}).live('mouseout',function() {
			timer = setTimeout(testTimer,timer_time);
		});
		
		$('#search_address_input').autocomplete({
			source: document_root+'/wp-content/plugins/cluster/autocomplete.php',
			minLength: 2
		});
		
		$('#search_community_input').autocomplete({
			source: document_root+'/wp-content/plugins/community/autocomplete.php',
			minLength: 2
		});

		if ($(".tab-active").length > 0)
			timer = setTimeout(testTimer,timer_time);
	});
	
	function testTimer()
	{
		var current_id = parseInt($(".tab-active").attr('id').substring(12));
		var next_id = current_id + 1;
		
		if (next_id > 3)
			next_id = 1;
			
		$('.no-background').removeClass('no-background');
		$('.tab-active').removeClass('tab-active');
		$('.tab-content-active').removeClass('tab-content-active');
		$("#feature-tab-"+next_id).addClass('tab-active').find('div').addClass('tab-content-active');
		Cufon.replace('.feature-tab-content');
		
		if (next_id > 1)
			$('.tab-'+current_id).addClass('no-background');
			
		tab_changing = true;
		$("#feature-"+current_id).fadeOut(250,function() {
			$(".feature").hide();
			$('#feature-'+next_id).fadeIn(250);
			tab_changing = false;
		});
		
		timer = setTimeout(testTimer,timer_time);
	}
})(jQuery);
