$(document).ready( function() {

	function setupNavigation() {
		// hide dropdown menu items on load
		$("dt.toggle").siblings("dd").addClass("hide");

		// dropdown open/close functionality
		$("dt.toggle").click( function() {
			if( $(this).hasClass("closed") ) {
				$(this).removeClass("closed").addClass("open");
				$(this).siblings("dd").removeClass("hide");
			} else {
				$(this).removeClass("open").addClass("closed");
				$(this).siblings("dd").addClass("hide");
			}
		});
		$("dt.toggle a").click( function(event) {
			event.stopPropagation();
		});
		$("#nav").css("visibility","visible").hide().fadeIn("slow");
	};

	setupNavigation();

	/* ////////// SLIDER ////////// */
	var ctamSlider = {
		slides: $("#slider > ul > li"),
		numberOfSlides: $("#slider > ul > li").length,
		currentPosition: 0,
		isRotating: true,
		init: function() {
			// Set container width equal to total width of all slides
			var slideWidth = $("#slider > ul > li")[0].offsetWidth;
			$('#slider ul').css("width", slideWidth * ctamSlider.numberOfSlides);

			// Append controls (pagination and previous/next buttons)
			$("#slider").parent().append("<div id='controls'><ul class='pagination'></ul></div>");
			for (var i = 0; i < ctamSlider.numberOfSlides; i++) {
				var className = $("#slider > ul > li:eq("+ i +")").attr("class");
				$(".pagination").append("<li class='"+ className + "'><a href='#'>" + (i+1) + "</a></li>");
			}
			$("#controls").append("<a href='#' id='btn-previous'>Previous</a> <a href='#' id='btn-next'>Next</a>");

			// Initialize controls for first load
			$(".pagination li:first-child").addClass("on");
			$("#slider").addClass("rotating");

			// Remove rotating class on hover
			$("#slider, #controls").hover( function() {
				if (ctamSlider.isRotating == true){
					$("#slider").removeClass("rotating");
				}
			}, function() {
				if (ctamSlider.isRotating == true){
					$("#slider").addClass("rotating");
				}
			});
			// Rotating functionality
			ctamSlider.startRotating();

			// Setup click events
			ctamSlider.events();

			// Extended Init?
			if ($("#slider").hasClass("extended")) {
				ctamSlider.extendedInit();
			}

			// fade in the slider
			$("#slider").parent().css('visibility','visible').hide().fadeIn('slow');

		},
		events: function() {
			// Pagination Buttons
			$(".pagination li a").bind("click", function() {
				ctamSlider.scrollTo($(this).index(".pagination li a"));
				$(".pagination li").removeClass("on");
				$(this).parent().addClass("on");
				ctamSlider.stopRotating();
				return false;
			});
			// Next Button
			$("#btn-next").bind("click", function() {
				ctamSlider.goNext();
				ctamSlider.stopRotating();
				return false;
			});
			// Previous Button
			$("#btn-previous").bind("click", function() {
				if(ctamSlider.currentPosition > 0) {
					ctamSlider.goPrev();
					ctamSlider.stopRotating();
				} else {
					ctamSlider.scrollTo(ctamSlider.numberOfSlides-1);
					ctamSlider.stopRotating();
				}
				return false;
			});
		},
		extendedInit: function() {
			// Wrap the pagination in a div
			$("#controls .pagination").wrap("<div class='pagination-wrapper' />");

			// Set container width equal to total width of all pagination items
			var paginationWidth = $("#controls .pagination > li")[0].offsetWidth;
			$('#controls .pagination').css("width", paginationWidth * ctamSlider.numberOfSlides);

			// Update the content for the pagination
			for (var i = 0; i < ctamSlider.numberOfSlides; i++) {
				var h1 = $("#slider li:eq("+ i +")").find("h1").text();
				var h2 = $("#slider li:eq("+ i +")").find("h2").text();
				var htmlString = "<span>"+h1+"</span>"+h2
				$("#controls .pagination li:eq("+ i +") a").html(htmlString);
			}

		},
		position: function(newPosition) {
			ctamSlider.currentPosition = newPosition;
		},
		goNext: function() {
			var nextSlide = ctamSlider.currentPosition +1;
			if(ctamSlider.currentPosition < (ctamSlider.numberOfSlides-1)) {
				ctamSlider.scrollTo(nextSlide);
			} else {
				ctamSlider.scrollTo(0);
			}
		},
		goPrev: function() {
			var prevSlide = ctamSlider.currentPosition -1;
			if(ctamSlider.currentPosition > 0) {
				ctamSlider.scrollTo(prevSlide);
			} else {
				ctamSlider.scrollTo(ctamSlider.numberOfSlides-1);
			}

		},
		scrollTo: function(slidePosition) {
			var slideWidth = $("#slider > ul > li")[0].offsetWidth;
			// Update position
			ctamSlider.position(slidePosition);

			// Animate
			$("#slider > ul").animate({
				"marginLeft": slideWidth * (-ctamSlider.currentPosition)
			}, "slow");

			// Update pagination
			$(".pagination li").removeClass("on");
			$(".pagination li:eq("+ ctamSlider.currentPosition +")").addClass("on");
		},
		startRotating: function() {
			if ($("#slider").hasClass("rotating")) {
				sliderRotating = setInterval( function() {
					if ($("#slider").hasClass("rotating")) {
						ctamSlider.goNext();
					}
				}, 8000);
			}
			ctamSlider.isRotating = true;
		},
		stopRotating: function() {
			$("#slider").removeClass("rotating");
			clearInterval(sliderRotating);
			ctamSlider.isRotating = false;
		}
	};
	if ($("#slider").length > 0) {
		ctamSlider.init();
	}

	// Add support for HTML5 placeholder
	if (Modernizr.input.placeholder)
		return;

	$("input[placeholder]").focus( function() {
		if ($(this).hasClass("placeholder")) {
			if ($(this).val() == $(this).attr("placeholder"))
				$(this).val("");
			$(this).removeClass("placeholder");
		}
	});
	$("input[placeholder]").keypress( function() {
		if ($(this).hasClass("placeholder")) {
			if ($(this).val() == $(this).attr("placeholder"))
				$(this).val("");
			$(this).removeClass("placeholder");
		}
	});
	$("input[placeholder]").blur( function() {
		if ($(this).val() != "")
			return;
		$(this).addClass("placeholder");
		$(this).val($(this).attr("placeholder"));
	});
	$("input[placeholder]").each( function() {
		if ($(this).val() != "" && $(this).val() != $(this).attr("placeholder"))
			return;
		$(this).val($(this).attr("placeholder")).addClass("placeholder");
	});
	$("form").submit( function() {
		$(this).find(".placeholder").each( function() {
			$(this).removeClass("placeholder");
			$(this).val("");
		});
	});
});
