$(document).ready(function(){
	
	$("body").addClass("js");
	
	carousel($('#studies')); //turn list into carousel/slider
	
	function carousel(el) {
		var width = 0;
		var count = 0;
		var hash = [];
		var step = null;

		el.children().children().each(function(){
			count += 1;
			width += $(this).outerWidth();
			hash[count - 1] = $(this).attr("id"); //array of case study ids used for the hash tag
			$(this).css({"float": "left", "clear": "none"});
			$(this).data("index", count);
		});
		
		var content = el.children().css({position: "absolute", "top": 0, "width": width + "px"}).wrap("<div id='carousel'></div>");
		
		el.prepend("<img src='images/arrow.right.gif' id='right' class='button' alt='right'>");
		el.prepend("<img src='images/arrow.left.gif' id='left' class='button' alt='left'>");
		
		var url = window.location.hash;
		if(url.indexOf("#") !== 0) { //no hash, show from start
			$("#left").hide();
			step = 1;
		} else { //load page on current item according to hash
			step = $(url).data("index");
			content.css("left", -845 * (step - 1));
			controls(step, true);
		}

		$("#left").click(function(){
			step--;
			slide("left", step);
		});
		
		$("#right").click(function(){
			step++;
			slide("right", step);
		});
		
		function slide(direction, step) {
			controls(step); //update left/right buttons according to the step
			var stepWidth = (direction == "left") ? "+=845" : "-=845";
			content.animate({left: stepWidth}, 500, function() {
				window.location.hash = hash[step -1]; //update hash with the new item
			});
		}
		
		function controls(step, init) { //set up left/right buttons
			if(init !== undefined) {
				if(step == 1) {
					$("#left").hide();
				} else if (step == count) {
					$("#left").show();
					$("#right").hide();
				}			
			} else {
				if(step == 1) {
					$("#left").fadeOut("fast");
				} else if (step == count) {
					$("#right").fadeOut("fast");
				} else {
					$('#left').fadeIn("fast");
					$('#right').fadeIn("fast");
				}
			}
		}
		
		$(".description .first a").click(function() {
			$(this).parent().parent().fadeOut("fast", function() {
				$(this).siblings(".last").fadeIn("fast");
			});
			return false;
		});
		
		$(".description .last a").click(function() {
			$(this).parent().parent().fadeOut("fast", function() {
				$(this).siblings(".first").fadeIn("fast");
			});
			return false;
		});
		
		$(".screens a").click(function() { //big daddy and active thumbnail states
			var link = this;
			if (!$(this).hasClass("active")) {
				$(this).siblings(".active").removeClass("active").children(".over").appendTo($(this));
				$(this).addClass("active").siblings("img").attr("src", $(link).attr("href"));
			}
			return false;
		});
		
	}
	
	
	$(".study img").hover(
		function() {
			$(this).css("opacity", .5);
		},
		function () {
			$(this).css("opacity", 1);
		}
	);
	
	$("input, textarea").focus(function () {
		if($(this)[0].defaultValue == $(this).val()) {
			$(this).val("");
		}
	});
	
	$("input, textarea").blur(function () {
		if($(this).val() == "") {
			$(this).val($(this)[0].defaultValue);
		}
	});
	
	
	$("#jobs a, button").hover(
		function () {
			$(this).children().children("img").css({'marginLeft' : '2px', 'marginRight' : '6px'});
		},
		function () {
			$(this).children().children("img").css({'marginLeft' : '0', 'marginRight' : '8px'});
		}
	);
	
	function cycle() { //cycles through big daddy images
		var count = $("#main img").length;
		$("#main img").eq(count - 1).fadeOut("slow", function () {
			$(this).prependTo("#main").show();
		});
	}
	
	var timeout = setTimeout(interval, 3000); //workaround instead of using setInterval
	
	function interval() { //workaround instead of using setInterval
		cycle(); 
		timeout = setTimeout(interval, 3000);
	}
	
	
	jQuery.validator.addMethod("defaultValue", function(value, element) { //custom validation rule so default text causes "required" errors to appear
    	return element.defaultValue != value;
	}, "This field is required.");
	
	$("#contact_us").validate({
	    rules : {
	        name : { defaultValue : true },
	        message : { defaultValue : true }
	    }
	});
	
	$("dt").click(function() {
		var dd = $(this).next("dd");
		$("dd:visible").not(dd).slideUp("normal", function () {
			var img = $(this).prev("dt").children().children("img");
			var src = ($(img).attr("src").indexOf("down") != -1) ? "images/jobs.arrow.right.gif" : "images/jobs.arrow.down.gif";
			$(img).attr("src", src);
		});
		dd.slideToggle("normal", function () {
			var img = $(this).prev("dt").children().children("img");
			var src = ($(img).attr("src").indexOf("down") != -1) ? "images/jobs.arrow.right.gif" : "images/jobs.arrow.down.gif";
			$(img).attr("src", src);
			var section = $(this).prev("dt").children().text().toLowerCase().replace(/\s/g, "-");
			if(dd.is(":visible")) {
				if($("#jobs").length != 0) {
					pageTracker._trackPageview("/jobs/" + section);
				} else {
					section = section.substring(0, section.indexOf('---'));
					pageTracker._trackPageview("/media/" + section);
				}
			}
		});
		return false;
	});

});