$(document).ready(function(){
	
	
	currentTabId = "#tab-" + $(".cta.tabs ul li.active").index();
	
	$(".cta.tabs ul.tabs a").click( function () { 
		tabId = "#tab-" + $(this).parent().parent().index();
		if(currentTabId != tabId) {
			$(".cta.tabs ul li").removeClass('active');
			$(this).parent().parent().addClass('active');
			$(".cta.tabs .content").removeClass('active');
			$(tabId).addClass('active');
			Cufon.refresh();

			currentTabId = tabId;
		}
		return false;
	});
	
	$("a.field-btn").click( function () { 
		if($(this).siblings('ul').hasClass('open')) {
			$(this).removeClass('open');
			$(this).siblings('ul').find('input').attr('checked','');
			$(this).siblings('ul').stop(true,true).slideUp(function() { $(this).removeClass('open'); });
		} else { 
			$(this).addClass('open');
			$(this).siblings('ul').stop(true,true).slideDown(function() { $(this).addClass('open'); });
		}
		return false;
	});
	
	if ($('#visuals.home img').length > 0) $('#visuals.home img').Animation();
	
})


$.fn.Animation = function(options) { 
	var defaults = {
		speed: 			5000,
		fadeSpeed: 		2000,
		activeImage:	0,
		imageCount:		0,
		TO:				''
	};	
	
	var options = $.extend(defaults, options);
	var jQueryMatchedObj = this; 
	options.imageCount = jQueryMatchedObj.length;
				
	function start() {
		window.clearTimeout(options.TO);
		options.TO = window.setTimeout(function() { animateOut() }, options.speed); 
	} 	
	
	function animateIn()  {
		if (options.activeImage+1 < options.imageCount) {
			if($(jQueryMatchedObj[(options.activeImage+1)]).children('div.hiddenswf-container').length > 0) {
				$(jQueryMatchedObj[(options.activeImage+1)]).children('div.flash').html($(jQueryMatchedObj[(options.activeImage+1)]).children('div.hiddenswf-container').html());
			}
			$(jQueryMatchedObj[(options.activeImage+1)]).fadeIn(options.fadeSpeed, endAnimation);
		} else {
			$(jQueryMatchedObj[0]).fadeIn(options.fadeSpeed, endAnimation);
		}
	}
	
	function animateOut () { 
		$(jQueryMatchedObj[(options.activeImage)]).fadeOut(options.fadeSpeed, function() { 
				if($(jQueryMatchedObj[(options.activeImage)]).children('div.flash').length > 0) {
					$('div.flash').empty();
				}
			});
		animateIn();
	}
	
	function animate() {
		if(options.activeImage+1 < parseInt(options.imageCount)) {
			$(jQueryMatchedObj[(options.activeImage+1)]).fadeIn(options.fadeSpeed, endAnimation);
		} else {
			$(jQueryMatchedObj[options.imageCount-1]).fadeOut(options.fadeSpeed, endAnimation);
		}
	}
	
	function endAnimation() {
		if (options.activeImage+1 < options.imageCount) {
			options.activeImage++;
		} else {
			options.activeImage = 0;
		}
		start();
	}
	
	if(options.imageCount > 1)
	start(jQueryMatchedObj);
}
