var slider_interval = 8; // Interval time per slide in seconds
var timer;

$(document).ready(function() {
	
	var slider = $(".promotion_boxes");
	var navigator = $(".promotion_buttons");
	
	// Show first slide
	navigator.children('li').eq(0).addClass('current');
	slider.children('li').eq(0).show();

	// Setup the buttons
	navigator.children('li').click(function() {
		if (is_relaxed()) {
			var index = $(this).index();

			// Set the corresponding image
			slider.children("li:visible").fadeOut("slow");
			slider.children("li").eq(index).fadeIn("slow");
			
			// Set the correct indicator
			navigator.children("li").removeClass("current");
			navigator.children("li").eq(index).addClass("current");
			
			// Reset the timer
			reset_timer();
		}
		return false;
	});
	
	
	if($('.memberContainer').length) {
		$('.memberContainer').click(function() {
			var html = $('#'+$(this).attr('rel')).html();
			var width = 400;
			if($(this).hasClass('larger')) width=600;
			$.colorbox({
				html: html,
				speed: 0,
				innerWidth: width,
				onOpen: function() {
					
				}
			});
		});
	}
	
	$('.faq a.question').click(function() {
		$(this).siblings('p.answer').toggle();
		
		return false;
	});

	
	// Set the auto timer
	reset_timer();

	function reset_timer() {
		window.clearInterval(timer);
		timer = window.setInterval(function() {
			navigator.children("li").removeClass("current");
			if (!slider.children("li:visible").is(":last-child")) {
				var index = slider.children("li:visible").fadeOut("slow").next().fadeIn("slow").index();
				navigator.children("li").eq(index).addClass("current");
			} else {
				slider.children("li:visible").fadeOut("slow");
				slider.children("li:first-child").fadeIn("slow").children("a").addClass("current");
				navigator.children("li").eq(0).addClass("current");
			}
		}, slider_interval * 1000);
	}
	
	// Setup the facility locator list
	$("#branch_list ul li > span").prepend('<em class="plus"></em>').click(function() {
		$(this).parent().siblings('li').find('span').next().slideUp(100).prev().children('em').addClass("plus").removeClass("minus");
		$(this).children("em").toggleClass("plus").toggleClass("minus");
		$(this).next().slideToggle(100);
	});
	
	// Setup text placeholders
	$("input[placeholder]").bind('focus', function() { if ($(this).val() == $(this).attr('placeholder')) { $(this).removeClass('blur').val(""); }}).bind('blur', function() { if ($(this).val() == "") { $(this).addClass('blur').val($(this).attr('placeholder')); } });
	$("input[placeholder]").each(function() {
		$(this).val($(this).attr('placeholder'));
		$(this).trigger('focus').trigger('blur');
	});
	
	// Contact form
	$('form#contact').submit(function() {
		send($(this).serialize());
		
		return false;
	});
});

function send(datastr){
	$.ajax({	
		type: "POST",
		url: "/ajax/mail.php",
		data: datastr,
		cache: false,
		success: function(html){
			$('#contact').slideToggle(300);
			$('#contactForm').append('<span style="display:none;">'+html+'</span>');
			$('#contactForm span').fadeIn(1000);
		}
	});
}

// Returns true if animation queue is clear, and false if there's an animation currently playing.
function is_relaxed() {
  var count = $("*:animated").size();
  
  if (count == 0) return true;    
  return false;
}
