$(document).ready(function() {

	LoadNavigation();		// LOAD THE NAVIGATION
	HandleTypography();		// HANDLE THE TYPOGRAPHY
	HandleFlickrFeed();		// HANDLE THE FLICKR FEED
	HandleTwitter();		// HANDLE THE TWITTERS
	ResizeEmbeds();			// RESIZE EMBEDS
	
});

// RESIZES VIDEOS
function ResizeEmbeds() {
	
	$('object').each(function() {
		// CHANGE THE OBJECT PROPERTIES
		$(this).attr('width', 480).attr('height', 388);
		
		// CHANGE THE EMBED PROPERTIES
		$(this).find('embed').attr('width', 480).attr('height', 388);
		
		$(this).wrap('<div style="height: 388px; display: block;"></div>');
		
	});
	
	return;
}

// LOAD IN THE NAVIGATION
function LoadNavigation() {
	var flashvars = {
		xmlPath: 	'http://blogs.lifeway.com/blog/marriage-family/navigation.xml'
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/marriage-family/swf/NAVIGATION.swf", "navigation", "100", "60", "9.0.0", "", flashvars, params);
}

// LOAD PAGE HEADING
function LoadPageHeading() {
	var myText = $('#page_heading').text();
	
	var flashvars = {
		myText: 	myText
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/marriage-family/swf/PAGE_HEADINGS.swf", "page_heading", "480", "100", "9.0.0", "", flashvars, params);
	
	return;
}

// LOAD IN THE FLICKR FEED
function HandleFlickrFeed() {
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=22659348@N02&format=json&jsoncallback=?", function(data){
	  $.each(data.items, function(i,item){
		
		// BUILD STRING FOR THUMBNAILS
		var curImage = item.media.m;				
		var myThumb = curImage.replace("_m", "_s");

	    $("<img class=\"thumb left\"/>").attr("src", myThumb).appendTo("#flickr_feed")
	      .wrap("<a href='" + item.link + "'></a>");
	  });
	});
}

// LOAD IN THE TWITTER FEED
function HandleTwitter() {
	$("#twitter").tweet({
		username: ["lifewaymarriage"],
		join_text: "auto",
		avatar_size: 32,
		count: 1,
		auto_join_text_default: "",
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading tweets..."
	});
}

// Makes calls to swap out all the Typography for Flash Elements
function HandleTypography() {
	// BLOG DATE
	$('.blog_date').each(function() {
		var myMonth = $(this).children('h3.month').text();
		var myDate = $(this).children('h3.date').text();
		var myID = $(this).attr('id');
		
		SwapOutPostDate(myMonth, myDate, myID);
	});
	
	// BLOG TITLE
	$('h2').each(function() {
		// GET CONTENT
		var thisItemID = $(this).attr('id');
		var thisTitle = $(this).children('a').text();
		var thisURL = $(this).children('a').attr('href');
		
		SwapOutH2(thisItemID, thisTitle, thisURL);
		
		$(this).wrap('<div class="h2"></div>');
	});
	
	// H4
	$('h4').each(function() {
		var myText = $(this).text();
		var myID = $(this).attr('id');
		
		SwapOutStandardText(myID, myText, "480", "30", "COMMENT_HEADING.swf");
	})
	
	// SIDEBAR HEADINGS :: h5
	$('h5').each(function() {
		var myText = $(this).text();
		var myID = $(this).attr('id');
		var curWidth = 110;
		
		if ($(this).attr('rel')) {
			curWidth = $(this).attr('rel');
		}
		
		SwapOutStandardText(myID, myText, curWidth, "30", "SIDEBAR_HEADING.swf");
		
		$(this).wrap('<div class="h5"></div>');
	});
	
	// FOOTER HEADINGS :: h6
	$('h6').each(function() {
		var myText = $(this).text();
		var myID = $(this).attr('id');
		
		SwapOutStandardText(myID, myText, "400", "30", "FOOTER_HEADINGS.swf");
		
		$(this).wrap('<div class="h6"></div>');
	});
	
	// LABELS
	$('label').each(function() {
		var myText = $(this).text();
		var myID = $(this).attr('id');
		
		SwapOutStandardText(myID, myText, "480", "13", "LABELS.swf");
	})
};

function SwapOutH2(thisItemID, thisTitle, thisURL) {	
	var flashvars = {
		myText: 	thisTitle,
		myURL: 		thisURL,
		myItemID: 	thisItemID
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/marriage-family/swf/BLOG_TITLE.swf", thisItemID, "355", "45", "9.0.0", "", flashvars, params);
	
	return;
}

function SwapOutStandardText(thisID, thisText, flashWidth, flashHeight, swfName) {	
	var pathName = "http://blogs.lifeway.com/blog/marriage-family/swf/" + swfName;
	
	var flashvars = {
		myText: 	thisText
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF(pathName, thisID, flashWidth, flashHeight, "9.0.0", "", flashvars, params);
}

/*	FUNCTION: 		SwapOutPostDate
	DESCRIPTION: 	Swaps out the blog post date with the flash equivalent
	IN: 			thisMonth - string containing the month (in numerical form)
					thisDate - string containing the date of the post (with preceding 0s)
	OUT: 			n/a
*/
function SwapOutPostDate(thisMonth, thisDate, thisID) {	
	var flashvars = {
		myID: 		thisID,	
		myMonth: 	thisMonth,
		myDate: 	thisDate
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/marriage-family/swf/BLOG_DATE.swf", thisID, "125", "115", "9.0.0", "", flashvars, params);
}

// 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, '355', newHeight, '355', newHeight, false, false);
	
	$('#'+curItemID).parent().css('height', newHeight);
}
