$(document).ready(function() {

	// RESIZE EMBEDS WITHIN POSTS
	$('.one_post').each(function() {
		$('object').wrap('<div cass="vimeo" style="width:536px; height:295px; margin-bottom:15px"></div>');
		$('object').attr('width', '536');
		$('object').attr('height', '295');
		$('embed').attr('width', '536');
		$('embed').attr('height', '295');
	});
	
	// CORRECT THE SIDEBAR HALVES
	$('#sidebar ul.half li:odd').css({'margin-right':'0', 'width':'129px'}).children('a').css('width','99px');
	$('#sidebar ul.half li:even').css({'width':'129px'}).children('a').css('width','99px');
	
	// ADD CLEARING SPACE EVERY 2ND BOOK
	$('#sidebar > .books > a:odd').after('<br clear="both" />');
	
	// SWAP OUT THE TWITTER IN THE HEADER
	// PASS IN THE TWITTER USERNAME
	// HandleTwitter('rickhowerton');
	
	// SWAP OUT THE TYPOGRAPHY
	HandleTypography();
	
});

/**
* LoadNavigation
* Loads the Flash navigation into the placeholder
*/
function LoadNavigation(currentPage) {
	var flashvars = {
		xmlPath: 	'http://blogs.lifeway.com/blog/small-group-training/navigation.xml',
		currentPage: currentPage
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/small-group-training/swf/NAVIGATION.swf", "navigation", "650", "36", "9.0.0", "", flashvars, params);
}

/**
* HandleTypography
* Swaps out all the static headings with flash typography
*/
function HandleTypography() {
	// H1
	$('h1').each(function() {
		var myID = $(this).attr('id');
		var myText = $(this).text();
		$(this).wrap('<div class="h1"></div>');
		SwapOutH1(myID, myText);
	});
	
	// H2
	$('h2').each(function() {
		var myID = $(this).attr('id');		// get the id
		var myText = $(this).text();		// get the text
		var myURL = $(this).children('a').attr('href'); // get the url
		$(this).wrap('<div class="h2"></div>');
		SwapOutH2(myID, myText, myURL);
	});
	
	// H3
	$('h3').each(function() {
		var myID = $(this).attr('id');		// get the id
		var myText = $(this).text();		// get the text
		$(this).wrap('<div class="h3"></div>');
		SwapOutH3(myID, myText);
	});
	
	
	// H4
	$('h4').each(function() {
		var myID = $(this).attr('id');		// get the id
		var myText = $(this).text();		// get the text
		$(this).wrap('<div class="h4"></div>');
		SwapOutH4(myID, myText);
	});
	
	// H5
	$('h5').each(function() {
		var myID = $(this).attr('id');
		var myText = $(this).text();
		$(this).wrap('<div class="h5"></div>');
		SwapOutH5(myID, myText);
	});
}

/**
* SwapOutH1
* swaps out all the H1s with Flash Typography
* H1s typically appear as page titles
* @param curID - string containing the id of where the Flash will go
* @param curText - string contaiing the text to display
*/
function SwapOutH1(curID, curText) {
	var flashvars = {
		myText: 	curText,
		myItemID: 	curID
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/small-group-training/swf/PAGE_TITLE.swf", curID, "536", "44", "9.0.0", "", flashvars, params);
}

/**
* SwapOutH2
* swaps out all the H2s with Flash Typography
* H2s typically appear as blog titles
* @param curID - string containing the id of where the Flash will go
* @param curText - string containing the text to display
* @param curURL - string containing the url of the page that will load whne you click on the text
*/
function SwapOutH2(curID, curText, curURL) {	
	var flashvars = {
		myText: 	curText,
		myURL: 		curURL,
		myItemID: 	curID
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/small-group-training/swf/BLOG_TITLE.swf", curID, "536", "44", "9.0.0", "", flashvars, params);
}

/**
* ResizeFlashH2
* Resizes the Flash Heading
* @param: arrayFromFlash
* [0] contains the new height
* [1] contains the item id
*/
function ResizeFlashH2(arrayFromFlash) {	
	var newHeight = arrayFromFlash[0];
	var curItemID = arrayFromFlash[1];
	
	// PASS IN: id, min width, min height, max width, max height, horizontal center, vertical center
	swffit.fit(curItemID, '536', newHeight, '536', newHeight, false, false);
	
	$('#'+curItemID).parent().css('height', newHeight);
}


/**
* SwapOutH3
* Swaps out all the H4s with Flash Typography
* H3s Typically appear as the blog date
* @param - curID
* @param - curText
*/
function SwapOutH3(curID, curText) {	
	var flashvars = {
		myText: 	curText
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/small-group-training/swf/BLOG_DATE.swf", curID, "220", "16", "9.0.0", "", flashvars, params);
}

/**
* SwapOutH4
* Swaps out all the H4 with Flash Typography
* H4s typically appear in the sidebar
* @param curID
* @param curText
*/
function SwapOutH4(curID, curText) {
	var flashvars = {
		myText: 	curText
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/small-group-training/swf/SIDEBAR_HEADINGS.swf", curID, "220", "18", "9.0.0", "", flashvars, params);
}

/**
* SwapOutH5
* Swaps out all the H5 with Flash Typography
* H4s typically appear in the sidebar
* @param curID
* @param curText
*/
function SwapOutH5(curID, curText) {	
	var flashvars = {
		myText: 	curText
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/small-group-training/swf/H5.swf", curID, "500", "24", "9.0.0", "", flashvars, params);
}

/**
* HandleTwitter
* Retrieves the Twitter feed
* Sticks the most recent in tweet in the placeholder
* Calls the function to replace the text with the Flash Typogrpahy (Will replace the static text)
* @param myUsername - string containing the person's twitter username
*/
function HandleTwitter(myUsername) {
	// SET THE TWITTER URL
	var url = "http://twitter.com/status/user_timeline/" + myUsername + ".json?count=1&callback=?";
	var myTwitter = '';

	// GET THE TWITTER FEED
	$.getJSON(url, function(data) {
		//Step through each tweet.
		$.each(data, function(i, item) {
			myTwitter = item.text;
		});
		// PLACE THE TEXT ON THE PAGE (IN CASE THE USER DOESN'T HAVE FLASH)
		$('#recent_twitter').html(myTwitter);
		// REPLACES THE TEXT WITH THE FLASH COUNTERPART
		FlashTwitterText(myTwitter);
	});
}	

/**
* FlashTwitterText
* Replaces the Twitter text with Flash typography
* Links are replaced within the swf file
* @param myTwitterText - string containing the twitter text
*/
function FlashTwitterText(myTwitterText) {
	var flashvars = {
		myFlashCSS: 	'http://blogs.lifeway.com/blog/small-group-training/flash_styles.css',
		myTwitter: 		myTwitterText
	}

	var params = {
		wmode: "transparent"
	};

	swfobject.embedSWF("http://blogs.lifeway.com/blog/small-group-training/swf/TWITTER.swf", "recent_twitter", "555", "100", "9.0.0", "", flashvars, params);
}
