$(document).ready(function() {
	SetupFlickr();
	//SetupYouTube();
	SetupTwitter();
	HandleTypography();

	//init_vimeo();			// DEFINED IN JQUERY :: VIMEO

// RESIZE VIDEO EMBEDS
	$('embed').each(function() {
		// DETERMINE HEIGHT
		var curHeight = $(this).attr('height');
		var curWidth = $(this).attr('width');
		
		var newHeight = curHeight * 430 / curWidth;
		
		$(this).attr('width', '430');
		$(this).attr('height', newHeight);
	});
	
	$('object').each(function() {
		// DETERMINE HEIGHT
		var curHeight = $(this).attr('height');
		var curWidth = $(this).attr('width');
		
		var newHeight = curHeight * 430 / curWidth;
		
		$(this).attr('width', '430');
		$(this).attr('height', newHeight);
		
		$(this).wrap('<div style="display: block; clear: both; height:' + newHeight + 'px "></div>');
	});

});

function HandleNavigation(curPage) {
	var flashvars = {
		currentPage: 		curPage,
		xmlPath: 			'http://blogs.lifeway.com/blog/women-ministry/navigation.xml'
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/women-ministry/swf/NAVIGATION.swf", "navigation", "390", "30", "9.0.0", "", flashvars, params);
}

function HandleTypography() {
	// HANDLE BLOG TITLE
	$('.blog_heading').each(function() {
		var myThis = $(this);
		
		// GET CONTENT
		curMonth = $(myThis).find('h3.month').text();
		curDate = $(myThis).find('h3.date').text();
		curYear = $(myThis).find("h3.year").text();
		curText = $(myThis).find('h2').text();
		curURL = $(myThis).find('h2').children('a').attr('href');
		curID = $(myThis).attr('id');
		
		HandleBlogTitle(curID, curMonth, curDate, curYear, curText, curURL);
	
		$(myThis).wrap('<div class="blog_heading_wrapper"></div>');
	});
	
	// H1 - PAGE HEADINGS
	$('h1').each(function() {
		var flashvars = {
			myText: $(this).text()
		};

		var params = {
			wmode: "transparent"
		};

		$(this).wrap('<div class="h1_wrapper"></div>');

		swfobject.embedSWF("http://blogs.lifeway.com/blog/women-ministry/swf/PAGE_HEADING.swf", $(this).attr('id'), "435", "27", "9.0.0", "", flashvars, params);
		
	});
	
	// ARCHIVE HEADINGS (INCLUDING CATEGORY HEADINGS)
	$('#archive_heading').each(function() {
			var flashvars = {
				myText: $(this).find('h2').text(),
				myHeading: $(this).find('h3').text(),
				myItemID: 'archive_heading'
			};

			var params = {
				wmode: "transparent"
			};

			$(this).wrap('<div class="h1_wrapper"></div>');

			swfobject.embedSWF("http://blogs.lifeway.com/blog/women-ministry/swf/ARCHIVE_HEADINGS.swf", "archive_heading", "435", "27", "9.0.0", "", flashvars, params);
	});
	
	// H4 - COMMENT HEADINGS
	$('h4').each(function() {
		var curID = $(this).attr('id');
		var curText = $(this).text();
		HandleH4(curID, curText);
		$(this).wrap('<div class="h4"></div>');
	});
	
	// H5 - SIDEBAR HEADINGS
	$('h5').each(function() {
		var curID = $(this).attr('id');
		var curText = $(this).text();
		HandleH5(curID, curText);
		$(this).wrap('<div class="h5"></div>');
	});	
}

function HandleBlogTitle(curID, curMonth, curDate, curYear, curTitle, curURL) {
	var flashvars = {
		myItemID:		curID,
		myMonth: 		curMonth,
		myDate: 		curDate,
		myYear: 		curYear,
		blogHeading: 	curTitle,
		myURL: 			curURL
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/women-ministry/swf/BLOG_HEADINGS.swf", curID, "435", "150", "9.0.0", "", flashvars, params);
}

function HandleH4(curID, curText) {
	var flashvars = {
		myText: curText
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/women-ministry/swf/COMMENT_HEADINGS.swf", curID, "435", "20", "9.0.0", "", flashvars, params);
}

function HandleH5(curID, curText) {
	var flashvars = {
		myText: curText
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/women-ministry/swf/SIDEBAR_HEADING.swf", curID, "140", "15", "9.0.0", "", flashvars, params);
}

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

function SetupFlickr() {
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=43515357@N06&format=json&jsoncallback=?", function(data){
	 	$.each(data.items, function(i,item){
			if (i < 18) {		// LIMIT DISPLAY TO 18 ITEMS
		
			// 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>");
		
			}
		});
	});
}

function SetupTwitter() {
	$("#twitter").tweet({
		username: ["chrisadams4"],
		join_text: "auto",
		avatar_size: 32,
		count: 1,
		auto_join_text_default: "I said,",
		auto_join_text_ed: "I",
		auto_join_text_ing: "I was",
		auto_join_text_reply: "I replied to",
		auto_join_text_url: "I was checking out",
		loading_text: "loading tweets..."
	});
}
