/* Author: David Mauro
   For Mallory and Bianca
*/

$(document).ready(function() {
	$(window).trigger("resize");
	
	// Contact scroll to and bounce
	$("#contact").bind("mouseup", function() {
		var bouncing = false;
		$("html, body").animate({scrollTop: $("#contact-info").offset().top}, 300, "swing", function() {
			if (!bouncing) {
				var bounceHeight = 50;
				$("#contact-info")
				.animate({top:"-=" + bounceHeight + "px"},bounceHeight*2.3, "easeOutCubic")
				.animate({top:"+=" + bounceHeight + "px"},bounceHeight*2.3, "easeInCubic")
				.animate({top:"-=" + Math.floor(bounceHeight/1.5) + "px"},bounceHeight*2, "easeOutCubic")
				.animate({top:"+=" + Math.floor(bounceHeight/1.5) + "px"},bounceHeight*2, "easeInCubic")
				.animate({top:"-=" + Math.floor(bounceHeight/4.5) + "px"},bounceHeight, "easeOutCubic")
				.animate({top:"+=" + Math.floor(bounceHeight/4.5) + "px"},bounceHeight, "easeInCubic")
				.animate({top:"-=" + Math.floor(bounceHeight/8) + "px"},Math.floor(bounceHeight*0.8), "easeOutCubic")
				.animate({top:"+=" + Math.floor(bounceHeight/8) + "px"},Math.floor(bounceHeight*0.8), "easeInCubic");
				bouncing = true;
			}
		});
	});
	
	if ($("#content.home").length > 0) {
		var values = $("#intro-text span");
		var lastValue = "";
		var choice = 0;
		var fadeSpeed = 450;
		function updateIntro() {
			if (choice >= values.length) {
				choice = 0;
			}
			currentValue = $(values[choice]);
			if (lastValue != "") {
				lastValue.animate({opacity:0}, fadeSpeed, "swing", function() {
					$(this).addClass("hidden");
				});
			} 
				currentValue.css("opacity", 0).removeClass("hidden").animate({opacity:1}, fadeSpeed, "swing");
			lastValue = currentValue;
			choice++;
		}
		updateIntro();
		setInterval(function() {
			updateIntro();
		}, 2150);
	}
});

$(window).load(function() {
	// Line up blue paper lines at bottom
	$("#paper-bottom").css("backgroundPositionY", parseInt($("#content").outerHeight())+10);
	if ($("#client-overlay").length > 0) {
		$("#client-overlay").css("backgroundPositionY", parseInt($("#content").outerHeight())+10);
	}
	
	// rotate client images randomly
});

var clients = {};

clients.centerLeft = 310;

clients.showClient = function(client, referrer) {
    /*
	clients.goBack = true;
	if (client) {
		clients.client = $("#" + client);
	}
	if (referrer) {
		clients.referrer = $(referrer).attr("class");
	}
	if (referrer == null) {
		clients.referrer = clients.client.find("div > div:nth-child(2)").attr("class").split(" ")[0];
		clients.goBack = false;
	}
	if (!clients.lastClient || clients.client.attr("id") != clients.lastClient.attr("id") || clients.referrer != clients.lastReferrer) {
		if (clients.clientClone) {
			clients.clientHide();
		} else {
			clients.clientExpand();
		}
		clients.lastClient = clients.client;
		clients.lastReferrer = clients.referrer;
	} else {
		clients.scroll("client");
	}
	*/
}

clients.clientExpand = function(doneScrolling) {
	if (doneScrolling) {
		clients.clientDivLeft = clients.client.position().left;
		clients.clientDivTop = clients.client.position().top;
		clients.clientClone = clients.client.children("div").clone().addClass("client-clone").css({width:200, height:100, overflow:"hidden", position:"absolute", left:clients.clientDivLeft, top:clients.clientDivTop}).appendTo($("#client-list"));
		clients.client = null;
		$("#client-ul").animate({opacity:0}, 600, "easeOutCubic", function() {
			if (!clients.firstClientLeft) {
				clients.firstClientLeft = $("#client-list").find("li:first-child").position().left;
				clients.firstClientTop = $("#client-list").find("li:first-child").position().top;
			}
			// We have to set a min-height for the window so we can remove the client-ul so the page can expand from the client-clone
			$("#content").css("min-height", $("#content").height());
			$("#client-ul").addClass("hidden");
			clients.clientClone.animate({left:clients.centerLeft, top:clients.firstClientTop}, (Math.abs(clients.clientDivLeft - clients.centerLeft) + clients.clientDivTop - clients.firstClientTop)*1.2, "swing", function() {
				clients.clientClone.find("."+clients.referrer).removeClass("hidden").each(function() {
					$(".client-showcase", $(this)).each(function() {
						//Rotate images just a bit here
					});
				});
				clients.clientClone.css({width:820, position:"relative", left:0, top:0, marginLeft:$("#client-ul").css("margin-left"), marginTop:$("#client-ul").css("margin-top")}).animate({height:clients.clientClone.children("div").height()}, 300, "swing");
				$(".client-logo", clients.clientClone).css({position:"relative", left:clients.centerLeft});
			});
		});
	} else {
		clients.scroll("client", "show");
	}
}

clients.clientHide = function(doneScrolling) {
	if (doneScrolling) {
		clients.clientClone.animate({height:100}, 300, "swing", function() {
			$(".client-logo", clients.clientClone).css({position:"static"});
			clients.clientClone.css({width:200, position:"absolute", left:clients.centerLeft, top:clients.firstClientTop, marginLeft:0, marginTop:0}).animate({left:clients.clientDivLeft, top:clients.clientDivTop}, (Math.abs(clients.clientDivLeft - clients.centerLeft) + clients.clientDivTop - clients.firstClientTop)*1.2, "swing", function() {
				$("#client-ul").removeClass("hidden").animate({opacity:1}, 600, "easeInCubic", function() {
					clients.clientClone.remove();
					clients.clientClone = null;
					if (clients.client) {
						setTimeout(function() {
							clients.clientExpand();
						}, 300);
					} else {
						if (clients.goBack) {
							clients.scroll("referrer");
						}
						clients.lastClient = null;
					}
				});
			});
		});
	} else {
		clients.scroll("client", "hide");
	}
}

clients.scroll = function(scrollTarget, callback) {
	var scrolling = false;
	scrollTarget = (scrollTarget == "client") ? $("#client-list") : $("#services > ul li > h2."+clients.referrer);
	if (scrollTarget.length > 0) {
		$("html, body").animate({scrollTop:scrollTarget.offset().top}, Math.abs(scrollTarget.offset().top - $(window).scrollTop())*1.1, "swing", function() {
			if (!scrolling) {
				scrolling = true;
				if (callback) {
					if (callback == "show") {
						clients.clientExpand(true);
					}
					else 
						if (callback == "hide") {
							clients.clientHide(true);
						}
				}
			}
		});
	}
}



