var OneStop = {
	insert_cool_thing_link: function(links, el) {
		var which = Math.floor(Math.random() * (links.length + 1))
		el.append('<a href="' + links[which][1] + '"><img src="' + links[which][0] + '"/></a>')
	}
};


/* Add a truncate function to String */
(function() {
	var shortened = "";	
	String.prototype.truncate = function(length) {
  
	  if (this.length > length) {
	   shortened = this.slice(0, length - 3);
	   shortened = shortened.replace(/\w+$/, '');
	   return shortened + "&hellip;"
	  } else {
	    return this;
	  }
	};
}());

$(function() {
	
	function choose_bg_class() {
		function createCookie(name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		}

		function readCookie(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}

		var bg = 0;
		var currentVal = readCookie("lastBg");
		if (!currentVal) {
			bg = 1;
			createCookie("lastBg", 1)
		} else {
			bg = parseInt(currentVal)
			if (isNaN(bg) || bg + 1 > 6) {
				bg = 1
			} else {
				bg = bg + 1
			}
			createCookie("lastBg", bg)
		}
		return "bg" + bg;
	}
	
	
	$("div.figure").each(function(idx) {
		var targetWidth = $(this).find("img").attr("width");
		$(this).css("width", (10 + targetWidth) + 'px')
		$(this).css("max-width", (10 + targetWidth) + 'px')
	});
	
	$("div#panorama").removeClass("bg1").addClass(choose_bg_class());
	
	/* billboard - ripe for refactoring as a plugin */
	$("#billboard ul li a").click(function(e) {
		e.preventDefault();	
		var href = $($(this).attr('href')).find('a').attr('href');
		document.location.href = href;
	});	
	
	$("#billboard ul li a").mouseenter(function(e) {
		e.preventDefault();
		var theSlide = $(this).attr("href");
		$(this).closest("ul").find("a.active").removeClass("active");
		$(this).addClass("active");
		$("#billboard>div").hide();
		$(theSlide).show();		
	});
	
	$("#billboard ul li a:first").mouseenter();
	/* billboard ends */
	
	/* blog integration */
	
	$("a.blog-first").each(function() {
		var feedUrl = $(this).attr("title");
		if (!feedUrl) {return; }
		var yql = 'select * from rss where url="' + feedUrl + '" limit 1';
		var endpoint = "http://query.yahooapis.com/v1/public/yql?" + $.param({q: yql, callback: '', format: 'json'});
		var target = $(this).closest("p");
		$.getJSON(endpoint, function(data) {
			if (data.query && data.query.results && data.query.results.item){
				var item = data.query.results.item;
				var description = item.description.truncate(250);
				var out = '<h3><a href="' + item.link + '">' + item.title + '</a></h3>\n';
				out += '<p>' + description.replace("&hellip;", '&hellip; <a href="' + item.link + '">more</a>') + '</p>';
				target.replaceWith(out);
			}
		});
	});
	
	$("a.blog-link-list").each(function() {
		var feedUrl = $(this).attr("title");
		if (!feedUrl) {return; }
		var yql = 'select * from rss where url="' + feedUrl + '" limit 4';
		var endpoint = "http://query.yahooapis.com/v1/public/yql?" + $.param({q: yql, callback: '', format: 'json'});
		var target = $(this).closest("p");
		$.getJSON(endpoint, function(data) {
			var out = '<ul>\n', item = null;
			if (data.query && data.query.results && data.query.results.item) {
				$(data.query.results.item).each(function() {
					item = this;
					out += '<li><a href="' + item.link + '">' + item.title + '</a></li>\n';
				});
			}
			out += '</ul>';
			target.replaceWith(out);
		});
	});
	
	
	/* quick index seach */
	$('#qsselect').change(function(e) {
		document.location.href = $(this).val();
	})
	
});

