var wt = {
	init : function() {
		wt.content.expando.init();	
		$("a.externallink").live("click",function(e){ e.preventDefault(); window.open($(this).attr("href")); });	
		if ($(".latestnews").length > 0) wt.content.blog.populateRecent();
	},
	content : {
		expando : {
			init : function()
			{
				$("div.expando").hide();
				$("div.description").append("<a class=\"more\">view more</a>");
				if ($("div.service:has(div.expando)").length > 0) {
						$("#primarycontent").prepend('<div id="expandocontrol"><a href="#" id="exexpand">expand all</a> <a href="#" id="excollapse">collapse all</a></div>');					
						$("#exexpand").click(function(e){ e.preventDefault(); wt.content.expando.showAll();});
						$("#excollapse").click(function(e){ e.preventDefault(); wt.content.expando.hideAll();});						
				}

				$("div.service:has(div.expando)").addClass("activesvc").click(function(e){ e.preventDefault();wt.content.expando.run($(this)); });
			},
			run : function(obj){
				$obj = $(obj);
				if ($obj.hasClass("open"))
				{
					$obj.removeClass("open")
						.find("a.more")
						.text("view more")
						.end()
						.children("div.expando")
						.hide("blind");
				}
				else
				{
					$obj.addClass("open")
						.find("a.more")
						.text("view less")
						.end()
						.children("div.expando")
						.show("blind", function(){ });
					$.scrollTo($obj,800);
				}
			},
			showAll : function()
			{
				$("div.expando").show("blind");
				$("div.service").addClass("open").find("a.more").text("view less");
				$.scrollTo("div#expandocontrol", 800);
			},
			hideAll : function()
			{
				$("div.expando").hide("blind");
				$("div.service").removeClass("open").find("a.more").text("view more");
			}
		},
		homehero : {
			interval : null,
			intervalDelay : 3000,
			currentImage : 0,
			init : function(){
				var imageset = ($("#homehero").attr("data-imageset")).split("|");
				for (i=0;i<imageset.length;i++)
				{
					$("#homehero").parents("div.intro").append("<img id='homehero" + i + "' src='images/" + imageset[i] + "' class='hidden" + ((i+1 == imageset.length) ? " last" : "") + "' />");
				}
				$("#homehero").addClass("active");
				wt.content.homehero.interval = window.setInterval(wt.content.homehero.rotate, wt.content.homehero.intervalDelay);
			},
			rotate : function() {
				var $nextImage = $("img.active").next("img");
				if ($("img.active").hasClass("last")) $nextImage = $("#homehero");
				$("img.active").css("z-index",10).removeClass("active").addClass("tohide");
				$nextImage.css("z-index",50).fadeIn(function(){$("img.tohide").fadeOut().removeClass("tohide");}).addClass("active");
			}
		},
		blog: {
	        postLimit: 3,
	        populateRecent: function () {
	            $("ul#blogposts").html("<li>loading...</li>")
	            $.ajax({
	                type: "GET",
	                dataType: "xml",
	                url: "http://worktankseattle.com/blog/?feed=rss2",
	                success: function (data) {
	                    wt.debug.log("fetched blog posts");
	                    var items = $("item:lt(" + wt.content.blog.postLimit + ")", data);
	                    var output = $(".latestnews");
	                    output.html("<h3>latest news</h3>");
	                    items.each(function () {
	                        wt.debug.log("inserted story");
	                        var date = ($("pubDate", this).text()).split(" ");
	                        var processeddate = date[2] + " " + date[1] + " " + date[3];
	                        wt.debug.log(date);
	                        output.append('<div class="article"><h4><a href="' + $("link", this).text() + '" class="externallink">' + $("title", this).text() + '</a></h4><p class="byline">' + processeddate + '</p></div>');
	                    });
	                },
	                error: function (request, status, errorthrown) {
	                    wt.debug.log("error: " + status);
	                }
	            });
	        }
		}
 	},
	people : {
		init : function(){
			$("#people").isotope({
				itemSelector : ".person",
				getSortData : {
					firstname : function ($elem)
					{
						return $elem.attr("data-firstname");
					},
					birthstate : function ($elem)
					{
						return $elem.attr("data-birthstate");
					},
					sign : function($elem)
					{
						return $elem.attr("data-sign");
					}
				}
			});

			$("div#controlbar span").click(function(e)
			{
				e.preventDefault();
				$("div#controlbar span").removeClass("active");
				$(this).addClass("active");
				var sortorder = $(this).attr("id");
				$("#people").isotope({sortBy: sortorder});
				$(".attr.active").removeClass("active");
				$("." + ((sortorder == "firstname") ? "title" : sortorder)).addClass("active");
			});

			
			$("#filter").change(function(e){
				var filter = $(this).val();
				if (filter != "all")
				{
					$("#people").isotope({filter : "." + filter});
				}
				else
				{
					$("#people").isotope({filter : ""});
				}
			})
		}
	},
	debug : {
		enabled : true,
		log : function(msg) { if (window.console && wt.debug.enabled) { console.log(msg);} }
	}
};

$(function(){
	wt.init();
});
