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


function HandleTypography() {	
	// TWITTER
	var url = "http://twitter.com/status/user_timeline/jasonhayes.json?count=1&callback=?";
	var myTwitter = '';

	$.getJSON(url, function(data) {
		//Step through each tweet.
		$.each(data, function(i, item) {
			myTwitter = item.text;
		});
		
		$('#twitter').html(myTwitter);
			
		FlashTwitterText(myTwitter);
	});
	
	
	// PAGE TITLE
	$('h1').each(function() {
		// GET CONTENT
		var curText = $(this).text();
		var curID = $(this).attr('id');
		var curColor = '';
		
		// CHECK TO SEE IF THIS IS IN A STRIPE
		if ($(this).parent().parent().parent().hasClass('stripe')) {
			curColor = 'FFFFFF';
		}
		
		$(this).wrap('<div class="h1"></div>');
		SwapOutH1(curText, curID, curColor);	
	});
		
	// BLOG TITLE
	$('h2').each(function() {
		// GET CONTENT
		var curText = $(this).text();
		var curID = $(this).attr('id');
		var curURL = $(this).children('a').attr('href');
		var curColor = '';
		var curRolloverColor = '';
		
		// CHECK TO SEE IF THIS IS IN A STRIPE
		if ($(this).parent().parent().parent().hasClass('stripe')) {
			curColor = 'FFFFFF';
			curRolloverColor = '5E5E5E5E';
		}
		
		$(this).wrap('<div class="h2"></div>');
		SwapOutH2(curText, curID, curURL, curColor, curRolloverColor);
	});
	
	// BLOG DATE
	// LOOPS THROUGH ALL THE BLOG DATES, GATHERS INFORMATION AND PASSES TO SwapOutBlogDate
	$('.blog_date').each(function() {
		var curMonth = $(this).find('h3.month').text();
		var curDate = $(this).find('h3.date').text();
		var curYear = $(this).find('h3.year').text();
		var curID = $(this).attr('id');
		var curColor = '';
		
		// CHECK TO SEE IF THE DATE IS WITHIN A STRIPE
		// IF IT'S NOT, CHANGE THE COLOR
		if (!$(this).parent().parent().parent().hasClass('stripe')) {
			curColor = '5D5D5D';
		}
		
		$(this).wrap('<div class="h3"></div>');
		SwapOutBlogDate(curMonth, curDate, curYear, curColor, curID);
	}); 
	
	// H4
	// LOOPS THROUGH ALL THE H4S, GATHERS INFORMATION, AND PASSES TO SwapOutH4
	$('h4').each(function() {
		var h4Text = $(this).text();
		var h4ID = $(this).attr('id');
		var h4Width = $(this).attr('rel');
		$(this).wrap('<div class="h4"></div>');
		SwapOutH4(h4Text, h4ID, h4Width);
	});
	
}

function SwapOutSpeakingDate(myID, myDate) {
	var flashvars = {
		myDate: 			myDate
	}

	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/jason-hayes/swf/SPEAKING_DATE.swf", myID, "270", "55", "9.0.0", "", flashvars, params);	
}

function SwapOutH1(myText, myID, myDefaultColor) {
	var flashvars = {
		myText: 			myText,
		myItemID: 			myID,
		myDefaultColor: 	myDefaultColor,
	}

	var params = {
		wmode: "transparent"
	};

	swfobject.embedSWF("http://blogs.lifeway.com/blog/jason-hayes/swf/H1.swf", myID, "550", "72", "9.0.0", "", flashvars, params);
}

function SwapoutContentInformation(myAuthor, myPostDetails, myID) {
	var flashvars = {
		commentAuthor: 			myAuthor,
		commentPostDetails: 	myPostDetails,
		commentID: 				myID
	}

	var params = {
		wmode: "transparent"
	};

	swfobject.embedSWF("http://blogs.lifeway.com/blog/jason-hayes/swf/COMMENT_DETAILS.swf", myID, "550", "40", "9.0.0", "", flashvars, params);
}

function SwapOutH2(myText, myID, myURL, myDefaultColor, myRolloverColor) {
	var flashvars = {
		myText: 			myText,
		myURL: 				myURL,
		myItemID: 			myID,
		myDefaultColor: 	myDefaultColor,
		myRolloverColor: 	myRolloverColor
	}

	var params = {
		wmode: "transparent"
	};

	swfobject.embedSWF("http://blogs.lifeway.com/blog/jason-hayes/swf/H2.swf", myID, "550", "72", "9.0.0", "", flashvars, params);
}

function HandleArchiveHeading(myActualTag, myTagHeading) {
	var flashvars = {
		actualTag: 			myActualTag,
		tagHeading: 		myTagHeading
	}

	var params = {
		wmode: "transparent"
	};
	$('#archive_heading').wrap('<div class="archive_heading"></div>');
	swfobject.embedSWF("http://blogs.lifeway.com/blog/jason-hayes/swf/ARCHIVE_HEADING.swf", "archive_heading", "950", "115", "9.0.0", "", flashvars, params);
}

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

// SWAP OUT BLOG DATE
function SwapOutBlogDate(myMonth, myDate, myYear, myColor, myID) {
	var flashvars = {
		myMonth: 	myMonth,
		myDate: 	myDate,
		myYear: 	myYear,
		myColor: 	myColor
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/jason-hayes/swf/H3.swf", myID, "270", "400", "9.0.0", "", flashvars, params);
} 

function SwapOutH4(myText, myID, myWidth) {
	var flashvars = {
		myText: 	myText
	}
	
	var params = {
		wmode: "transparent"
	};
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/jason-hayes/swf/H4.swf", myID, myWidth, "38", "9.0.0", "", flashvars, params);
}


function FlashTwitterText() {	
	var flashvars = {
		myFlashCSS: 	'http://blogs.lifeway.com/blog/jason-hayes/flash_styles.css',
		myTwitter: 		$('#twitter').text()
	}
	
	var params = {
		wmode: "transparent"
	};
	
	$('#twitter').wrap('<div class="twitter"></div>');
	
	swfobject.embedSWF("http://blogs.lifeway.com/blog/jason-hayes/swf/TWITTER.swf", "twitter", "555", "100", "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, '550', newHeight, '550', newHeight, false, false);
	
	$('#'+curItemID).parent().css('height', newHeight);
}


