(function($) {
	$(function() {
		var $menu = $("#menu-" + _.values(_WSL_).join("-"));
		if (!$menu.length) $menu = $("#menu-" + _WSL_.section + "-" + _WSL_.subsection);
		var $subsection = $menu.closest(".nav-subsection");
		
		$("#nav-" + _WSL_.section).add($menu).add($subsection).addClass("current");
		
		$subsection.find(".nav-articles").show();
		
		$(".nav-subsection h4 a").click(function(e) {
			e.preventDefault();
			var $this = $(this), $subsection = $this.closest(".nav-subsection");
			if (!$subsection.hasClass("current")) {
				$current = $(".nav-subsection.current");
				$current.removeClass("current").find(".nav-articles").slideUp();
				$current.find("h4 a").animate({
					color: "#444444",
					"background-color": "#ffffff"
				}, function() {
					$(this).removeAttr("style");
				});
				
				$this.css({
					color: "#444444",
					"background-color": "#ffffff"
				});
				$subsection.addClass("current").find(".nav-articles").slideDown();
				$this.animate({
					color: "#ffffff",
					"background-color": "#4D4D4D"
				});
			}
		});
		
		$("#stories-list li, .team-img").hover(function() {
			var $this = $(this);
			$(".overlay", $this).stop().animate({
				height: 250
			}, "fast");
			$(".story-head", $this).stop().animate({
				"margin-top": 100,
				"margin-bottom": 0
			}, "fast");
		}, function() {
			var $this = $(this);
			var aniHeight = $this.hasClass("team-img") ? 60 : 80;
			$(".overlay", $this).stop().animate({
				height: aniHeight
			}, "fast");
			$(".story-head", $this).stop().animate({
				"margin-top": 0,
				"margin-bottom": 20
			}, "fast");
		});
		
		$('<li class="print"><a href="#print">Click me to print</a></li>').prependTo("#tools").click(function() {
			e.preventDefault();
			window.print();
		});
		
		$("#year-select a").click(function(e) {
			e.preventDefault();
			var $this = $(this);
			if ($this.hasClass("selected")) return;
			var color = $("#year-select .selected").css("color");
			$("#year-select .selected").css("color", "").removeClass("selected");
			$this.css("color", color).addClass("selected");
			$(".tab_content").hide();
			
			$($this.attr("href")).show();
		});
		
		$("#stories-list li, .team-img").each(function() {
			var $url = $("a", this)[0].href;
			$(this).prepend('<a class="boxlink-cover" href="' + $url + '"/>');
		});
		
		$("#home-carousel-items").cycle({
			speed: 500,
			timeout: 6000,
			pager: "#home-carousel-switcher"
		});
		
		$("form").html5form();
		
		$("#client-list").attachClientList();
	});
	
	var $spinner = $('<div id="spinner"/>').ajaxStart(function(e) {
		$(this).appendTo(".loading");
	}).ajaxStop(function(e) {
		$(this).parent().removeClass("loading").end().detach();
	});
	
})(jQuery);

(function($) {
	
	var ClientList = function(el) {
		if ((this instanceof ClientList) === false) return new ClientList(el);
		
		var self = this;
		
		this.el = el;
		this.industries = [];
		
		el.addClass("loading");
		
		var request = $.ajax({
			url: "commands/clients.php",
			dataType: "json",
			context: this,
			success: this.addClients
		});
	};
	
	ClientList.prototype.addClients = function(data) {
		var self = this;
		var par = this.el.parent();
		this.el.detach();
		$.each(data.clients, function(index, clientDetails) {
			if ($.inArray(clientDetails.industry, self.industries) < 0) {
				self.industries.push(clientDetails.industry);
			}
			self.el.append(Client(clientDetails).el);
		});
		par.append(this.el);
		
		var $industries = $("#industries"), industryShort, $industry, totalWidth = 0;
		$.each(this.industries, function(index, value) {
			industryShort = value.split(" ")[0].toLowerCase();
			$industry = $('<span id="ind-' + industryShort + '"> ' + value.toLowerCase() + ' </span>').appendTo($industries);
			totalWidth += $industry.outerWidth();
		});
		$industries.width(totalWidth);
		
		var $indcontainer = $("#ind-container");
		var mainWidth = $indcontainer.outerWidth(), sOffset;
		$("#client-list .client").mouseenter($.throttle(500, function() {
			var $ind = $("#ind-" + $(this).data().details.industryShort);
			$(".current", $indcontainer).removeClass("current");
			$ind.addClass("current");
			sOffset = (mainWidth - $ind.outerWidth()) / -2;
			$indcontainer.stop().scrollTo(".current", 1000, {
				easing: "elasout",
				offset: {
					left: sOffset,
					top: 0
				}
			});
		}));
	};
	
	var Client = function(settings) {
		if ((this instanceof Client) === false) return new Client(settings);
		if (!settings) return null;
		
		settings.industryShort = settings.industry.split(" ")[0].toLowerCase();
		
		this.el = $(' <div class="client"> <span class="client-name" style="color: ' + settings.color.toLowerCase() + ';"></span> </div> ');
		this.el.children().text(settings.name);
		this.el.data("details", $.extend(true, {}, settings));
		
		if (settings.quote) {
			this.el.append('<q>' + settings.quote + '<cite style="color: ' + settings.color.toLowerCase() + ';"></cite></q>').addClass("quoted");
			this.el.find("cite").text(settings.cite);
		}
		
	};
	
	$.fn.attachClientList = function() {
		if (this.length > 0 && !this.clientList) {
			this.clientList = new ClientList(this);
		}
		return this;
	};
})(jQuery);

(function() {
	this.clean = function() {
		return this.filter(Boolean);
	};
	
	this.filter || (this.filter = function(fun) {
		var len = this.length;
		if (typeof fun != "function") throw new TypeError();
		
		var res = new Array(), thisp = arguments[1];
		for ( var i = 0; i < len; i++) {
			if (i in this) {
				var val = this[i];
				if (fun.call(thisp, val, i, this)) res.push(val);
			}
		}
		return res;
	});
}).apply(Array.prototype);

$.easing.elasout = function(x, t, b, c, d) {
	var s = 1.70158;
	var p = 0;
	var a = c;
	if (t == 0) return b;
	if ((t /= d) == 1) return b + c;
	if (!p) p = d * .3;
	if (a < Math.abs(c)) {
		a = c;
		var s = p / 4;
	}
	else var s = p / (2 * Math.PI) * Math.asin(c / a);
	return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
};
