/* Requires jQuery */

var boilerplate = {

	init: function() {
		boilerplate.rollovers();
		boilerplate.firstLast([ 'li', 'section' ]);
		boilerplate.popups();
	},
	rollovers: function() {
		// Preload all rollovers
		$("a.rollover img, input.rollover, img.rollover").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace(/.gif$/ig,"_OVER.gif").replace(/.jpg$/ig,"_OVER.jpg");
			$("<img>").attr("src", rollON);
		});
		// Link rollovers
		$("a.rollover img, input.rollover, img.rollover").mouseover(function(){
			imgsrc = $(this).attr("src");
			matches = imgsrc.match(/_OVER/);
			// don't do the rollover if state is already ON
			if (!matches) {
				imgsrcON = imgsrc.replace(/\.(gif|jpg|png)$/ig,"_OVER.$1"); // strip off extension
				$(this).attr("src", imgsrcON);
			}
		});
		$("a.rollover img, input.rollover, img.rollover").mouseout(function(){
			$(this).attr("src", imgsrc);
		});
	},
	firstLast: function(elem) {
		var first_childs = elem.map(function(el) { return el + ':first-child' }).join(', ');
		var last_childs = elem.map(function(el) { return el + ':last-child' }).join(', ');
		$(first_childs).addClass('first');	 
		$(last_childs).addClass('last');	 
	},
	popups: function() {
		$('a[rel="external"],a.popup').click( function() {
			window.open( $(this).attr('href') );
			return false;
		});
	}

};
jQuery(boilerplate.init);
