/* =Load Functions
-----------------------------------------------------------------------------*/
jQuery(document).ready(function(){
	doNewWin();
	listHover();
	if($("#home").length>0) dropCap();
	slideShow();
});

/* =Open a new window
-----------------------------------------------------------------------------*/
function doNewWin() {
	$("a[href^='http://']").addClass("newWin");
	$("a.newWin").each(function(){
		var x = $(this);
		var linkTitle = "(This link opens a new window)";
		var newTitle = (empty(x.attr("title"))) ? linkTitle : x.attr("title")+" "+linkTitle;
		x.attr("title", newTitle);
		x.removeClass("newWin").addClass("ext");
		x.click(function(){window.open(x.attr("href"));return false;});
	});
}


/* =Add class "hover to all li's and buttons
-----------------------------------------------------------------------------*/
function listHover(){
	$("li, button").hover(function(){$(this).addClass("hover")},function(){$(this).removeClass("hover")});
}


/* =Add DropCap
-----------------------------------------------------------------------------*/
function dropCap(){
	var fp = $('#main p')[0];
	if (!fp ) return false;
	var node = fp ;
	while (node.childNodes.length){node = node.firstChild;}
	var text = node.nodeValue;
	var fl = text.substr(0,1);
	var match = /[a-zA-Z]/.test(fl);
	if ( match ) {
		node.nodeValue = text.slice(1);
		$('<span class="drop img'+fl.toLowerCase()+'">'+fl+'</span>').prependTo(fp);
	}
}


/* =Create Slideshow
-----------------------------------------------------------------------------*/
var debug;
function slideShow(){
	if(!$("body").is("#home")) return false;
	// get the images
	getImgs();
}
function getImgs(month){
	var GET = ((month == undefined || empty(month)) ? 'ajax=true' : 'm='+month);
	$.ajax({
		type: "GET",
		url: 'template/slideshow.php',
		data: GET,
		success: function(msg){
			var bugCheck = msg.split("%");
			debug = (bugCheck[0] == 'debug');
			msg = (debug) ? bugCheck[1] : msg;
			msgArrs = msg.split("|");
			var imgs = [];
			for(var i=0; i<msgArrs.length; i++){
				var msgE = msgArrs[i].split("=");
				var msgNum = msgE[0];
				var msgContArr = msgE[1].split(",");
				var s = msgContArr[0];
				var c = msgContArr[1];
				imgs[i] = {src: s, caption: c};
			}
			doSlideshow(imgs);
		}
	});
}
function doSlideshow(imgs){
	$("#bigSidePic").addClass("slideshow").html('');
	$.slideshow(
		{
			container : 'bigSidePic',
			loader: 'images/loading.gif',
			linksPosition: 'top',
			linksClass: 'pagelinks',
			linksSeparator : ' | ',
			fadeDuration : 500,
			activeLinkClass: 'activeSlide',
			nextslideClass: 'nextSlide',
			prevslideClass: 'prevSlide',
			captionPosition: 'bottom',
			captionClass: 'slideCaption',
			autoplay: 7,
			images : imgs
		}
	);
	// hide these elements
	$(".slideshowLinks, .slideshowCaption, .prevSlide, .nextSlide").addClass("none");
	$(".slideshowHolder").addClass("top");
}
/* =Helper Functions
-----------------------------------------------------------------------------*/
function empty(x){	return (x == '' || x == null);}
