/*
innerHTML fix:
author: Dan Walker, Steve Hallman, Eric Webster

This script takes the controversial innerHTML method and tries to make it output compliant, clean markup which all browsers can identify.

Note that if you have a bad XHTML/XML parse you should check what the output of this script is and ensure its valid XHTML.

*/

jQuery.fn.extend({
xhtml: function( val ) {

	

	
	if (val == undefined)
	{
		if (this.length == 0) return null;
		/*
		// internet explorer's innerHTML is craptastic so we use some hackery to fix it
		// if(navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.indexOf("Windows") > -1){
		// BUG: self-closing <a> tags don't work , e.g "<a name='inline' />"
		*/
		
		var reTag = /(<\/?\w+)([^>]*>)/g;
		var reAttr = /(\w+=)((['"])[\s\S]*\3|[^\s>]+)/g;
		str = this[0].innerHTML;
			
		function fixAttr($0, $1, $2, $3) {
			if ($3) return $1.toLowerCase() + $2;
			else return $1.toLowerCase() + '"' + $2 + '"'
		} 		

		function fixTag($0, $1, $2) {
			return $1.toLowerCase() + $2.replace(reAttr, fixAttr);
		}

		/* clean up tags (make html tags and attributes lowercase) */
		str = str.replace(reTag, fixTag);

		/* make sure self-closing tags are closed */
		var reSelfClosing = new RegExp("\<(area|base|basefont|br|hr|img|input)(.*?)>", "g");
		
		str = str.replace(reSelfClosing, "<$1$2/>");
		/*
		// IE's treatment of UL's and DL's is mind boggling.
		// A big brain came up with this series of search/replaces that fixes them.  You go Dan.  
		*/
		str = str.replace(/<\/li>|<\/dt>/g, "");							// get rid of all closing li and dt tags (for IE)
		str = str.replace(/\s*<li([^>]*)>/g, "<\/li><li$1>");		// put a closing li in front of all opening li
		str = str.replace(/<\/ul>/g, "<\/li><\/ul>");					// put a closing li in front of closing ul
		str = str.replace(/<ul([^>]*)>\s*<\/li>/g, "<ul$1>");		// remove closing li's that appear directly after opening ul
		str = str.replace(/\s*<dd([^>]*)>/g, "<\/dt><dd$1>");	// put a closing dt in front of all opening dd's
		/* end dan hackery */
		return str;
	}
	else this.empty().append( val );
}

});

/*

MediaCenter V 0.2
Author: Eric Webster
History: 
v02 - #flashContainer is defined by the selector which the methods are being applied (ie $(this) 

*/

jQuery.fn.mediaCenter = function(settings) {

	settings = jQuery.extend({
	}, settings);
	settings.flashContainer = $(this);
	settings.XHTMLcontent = $(settings.XHTMLcontentContainer).xhtml();
	// possibly remove whitespace and newline from the content
	// var strSingleLineText = MediaCenter.XHTMLcontent.replace(new RegExp( "\\n", "g" ), "");
	//If Javascript is available remove the '#jsMessage' LI content node to reflect that the only remaing requirement is flash.
	$("#jsMessage").remove();
	$(settings.flashContainer).css({
		width: settings.flashWidth,
		height:settings.flashHeight
	})
	
	//this function is exposed so that Flash can read the content on the XHTML page.
	jQuery.fn.getContent = function() {
		return settings.XHTMLcontent;
	}

	/* THIS IS A GENERIC FLASH OBJECT THAT IS BEING INITALIZED*/
	var fO = new SWFObject(settings.flashSource, "mediacenter_flash_embed", "100%", "100%", settings.flashVersion);
	for(i in settings.flashParams){
		fO.addParam(i, settings.flashParams[i]);
	}
	for(i in settings.flashVars){
		fO.addVariable(i, settings.flashVars[i]);
	}
	if(settings.useXMLFile){fO .addVariable("xmlPath", settings.useXMLFile);}
	fO.write($(settings.flashContainer).attr("id"));
	
}
