function scroll(){
	var tallest = 0;
	$(".scroll .items").children().each(function(){
		if ($(this).outerHeight(true) > tallest){
			tallest = $(this).outerHeight(true);
		}
	});
	var wrap = $(document.createElement("div")).addClass("wrap");
	var prev = $(document.createElement("a")).addClass("prev");
	var next = $(document.createElement("a")).addClass("next");
	var navi = $(document.createElement("div")).addClass("navi");
	$(".scroll").wrap(wrap).after(next).after(navi).after(prev);
	$(".scroll").height(tallest).addClass("scrollable").scrollable({
		clickable: false,
		onSeek: function(){
			this.getItemWrap().find(".videoplayer").each(function(){
				if ($f(this).isLoaded()){
					$f(this).unload();
					return false;
				}
			});
		},
		size: 1
	});
	navi.css("left", Math.round((navi.parent().outerWidth() - navi.outerWidth() + prev.outerWidth() - next.outerWidth())/2));
}

function autoscroll(){
	$("div.scrollable").scrollable({
		size: 1,
		loop: true
	}).autoscroll({
		interval: 5000
	});
	$(".browse").click(function(){return false});
}

function bodyswitcher(){
// get window width, add css hooks to body
	var width = $(window).width();
	$("body").addClass((width<720)?"narrow":(width>=720&&width<960)?"standard":"wide");
}

function columnize(){
// for anything with a class of "columnsx*", store its children, then convert * to a number 
	$("body:not(.narrow) #main [class*='columns']").each(function(){
		var kids = $(this).children();
		var cols = parseInt($(this).attr("class").match(/columnsx(\d)/)[1]);
// proceed if the number of columns has been determined and the body doesn't
// have a class of "standard", or it does, but 3 or more columns were requested
		if (cols && !$("body").hasClass("standard") || cols && (cols - 1 > 1)){
			if ($("body").hasClass("standard")) cols--;
// wrap the item in an appropriately classed div before emptying it
			var wrapper = $(document.createElement("div")).addClass("columnized");
			$(this).wrap(wrapper).empty();
// divide the (remaining) children between the (remaining) number of columns to
// be filled, where each column is a clone of the previously emptied item
			for (var i=cols; i>0; i--){
				var end = Math.ceil(kids.length / i);
				var col = $(this).clone().append(kids.splice(0, end));
// append the column before the item, add a css hook, and break if no children remain
				$(this).before(col.addClass("c" + (cols - i + 1)));
				if (kids.length == 0) break;
			}
// remove the original item
			$(this).remove();
		}
	});
}

function columnizenav(){
	if (!$("body").hasClass("narrow")){
		$("#subnav ul").each(function(){
			var kids = $(this).children();
			if (kids.length > 3){
				var cols = 4;
				if ($("body").hasClass("standard")) cols--;
				$(this).empty();
				for (var i=cols; i>0; i--){
					var end = Math.ceil(kids.length / i);
					var col = $(this).clone().append(kids.splice(0, end));
					$(this).before(col);
					if (kids.length == 0) break;
				}
				$(this).remove();
			}
		});
	}
}

function lightbox(){
	$(".thumbs a, table.gallery a").lightBox({
		fixedNavigation: true,
		imageLoading: '/assets/templates/mcc/i/lightbox/loading.gif',
		imageBtnClose: '/assets/templates/mcc/i/lightbox/close.gif',
		imageBtnPrev: '/assets/templates/mcc/i/lightbox/prev.gif',
		imageBtnNext: '/assets/templates/mcc/i/lightbox/next.gif',
		imageBlank: '/assets/templates/mcc/i/lightbox/blank.gif'
	});
}

function prefill(){
// find each empty input with a class of "prefill", set its value to its title, and add a css hook
	$("input.prefill").each(function(){
		if (!$(this).val()){
			var title = $(this).attr("title");
			$(this).val(title).addClass("prefilled");
// on focus, if the input value is unchanged, clear it and remove the css hook
			$(this).focus(function(){
				if ($(this).val() == title){
					$(this).val("").removeClass("prefilled");
				}
			});
// on blur, if the input value is empty, re-set its value to its title, and re-add the css hook
			$(this).blur(function(){
				if (!$(this).val()){
					$(this).val(title).addClass("prefilled");
				}
			});
		}
	});
}

function tooltips(){
	if (!$("body").hasClass("narrow")){
// create and insert the element to hold the tooltip
		$('<div id="tooltips"></div>').insertBefore("#header");
// apply tooltips to each of the navigation tabs
		$(".tabs :not(.current) a[title]").tooltip("#tooltips");
	}
}

function video(){
	flowplayer("a.player", "/assets/templates/mcc/f/flowplayer-3.1.5.swf", {
		plugins: {
			controls: {
				autoHide: 'always'
			}
		}
	});
}

function video2(){
	$("a.player").each(function(){
		$(this).css({
			'height': $(this).children("img:first").height(),
			'width': $(this).children("img:first").width()
		});
	});
	$(".videoplayer").flowplayer("s/flowplayer-3.1.1.swf");
}

function dateline(){
	$(".dateline li:nth-child(even)").addClass("even");
}

function audio(){
	flowplayer("a.audio", "/assets/templates/mcc/f/flowplayer-3.2.7.swf", {
		plugins: {
			controls: {
				autoHide: false,
				fullscreen: false,
				height: 30
			}
		},
		clip: {
			autoPlay: false,
			onBeforeBegin: function() {
				$f("a.audio").close();
			}
		}
	});
}

function zebrastripe(){
// add a class of "odd" to every other table row and .links list item
	$("body:not(.capwiz,.search) #spare table:not(.gallery,.holder) tr:nth-child(odd), #spare .links li:nth-child(odd)").addClass("odd");
// toggle a class of "hover" on table rows to replicate a css :hover
	$("body:not(.capwiz,.search) table:not(.gallery,.holder) tr").hover(function(){
		$(this).addClass("hover");
	}, function(){
		$(this).removeClass("hover");
	});
}

function accordion(){
	$(".accordion > li").each(function(){
		$(this).children(":not(a)").hide();
		$(this).children("a").click(function(){
			$(this).siblings().toggle("fast");
			return false;
		});
	});
}

function toggler(){
	$("a.toggler").each(function(){
		var target = "#" + $(this).attr("href").split("#")[1];
		$(target).hide();
		$(this).click(function(){
			$(target).toggle("fast");
			return false;
		});
	});
}

$(document).ready(function(){
	accordion();
	audio();
	autoscroll();
	bodyswitcher();
	columnize();
	columnizenav();
	dateline();
	lightbox();
	prefill();
	toggler();
	tooltips();
	video();
	zebrastripe();
});
