/**
 * AS3Highlighter 0.9
 * http://www.nochump.com/blog/?p=12
 * (c) 2007 David Chang
 * 
 * AS3Highlighter is freely distributed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
if(typeof nochump == "undefined") nochump = {};
if(!nochump.highlighter) nochump.highlighter = {};
//
nochump.highlighter.As3PatternList = function() {
	this.length = 0;
	var span = function(cname) {
		return "<span class='" + cname + "'>$1</span>";
	}
	// handle angle brackets first
	this.push([/\</g, "&lt;"]);
	this.push([/\>/g, "&gt;"]);
	// do strings and class next to avoid problems
	//this.push([/("[^"\r\n]*")/g, "<strong>$1</strong>"]); // strings
	this.push([/\b(class)\b/g, span("asClass")]);
	this.push([/\b(interface)\b/g, span("asInterface")]);
	this.push([/\b(function)\b/g, span("asFunction")]);
	this.push([/\b(package)\b/g, span("asPackage")]);
	this.push([/\b(trace)\b/g, span("asTrace")]);
	this.push([/\b(var)\b/g, span("asVar")]);
	this.push([/(\/\/.*)/g, span("asComment")]);
	this.push([/(\/\*[^\*][^.|.]*?\*\/)/g, span("asComment")]); // block comments
	this.push([/(\/\*\*[^.|.]*?\*\/)/g, span("asDoc")]);
	this.push([/\b(break|case|catch|continue|default|do|each|else|finally|for|if|in|label|return|super|switch|throw|try|while|with)\b/g, span("asReserved")]); // statements
	this.push([/\b(dynamic|final|internal|native|override|private|protected|public|static)\b/g, span("asReserved")]); // attribute keywords
	this.push([/\b(const|extends|get|implements|namespace|set)\b/g, span("asReserved")]); // definition keywords (left out: class,interface,function,package,var)
	this.push([/\b(import|include|use)\b/g, span("asReserved")]); // directives
	this.push([/\b(false|null|this|true)\b/g, span("asReserved")]); // primary expression
	this.push([/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;"]); // tabs
	this.push([/\n/g, "<br />"]); // new line
};
nochump.highlighter.As3PatternList.prototype = {
	push:function(o) {
		this[this.length++] = o;
	}
};
//
nochump.highlighter.Highlighter = function() {
	this.patterns = {};
};
nochump.highlighter.Highlighter.prototype = {
	addPatternList:function(codeId, patternList) {
		this.patterns[codeId] = patternList;
		if(!this.defaultPatterns) this.defaultPatterns = patternList;
	},
	transformCode:function(code, codeId) {
		var patterns = this.patterns[codeId] || this.defaultPatterns;
		for(var i = 0; i < patterns.length; i++) {
			code = code.replace.apply(code, patterns[i]);
		}
		return code;
	},
	highlightIframe:function(elementId, cssUrl) {
		var e = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
		var util = nochump.highlighter.HighlighterUtil;
		var iframe = util.replaceWithIframe(e, this.transformCode(e.innerHTML));
		if(cssUrl) util.appendStyleSheet(cssUrl, util.getIframeDocument(iframe));
	},
	highlightClass:function(className, codeId, cssUrl) {
		var util = nochump.highlighter.HighlighterUtil;
		var elements = util.getElementsByClass(className);
		for(var i = 0; i < elements.length; i++) {
			elements[i].innerHTML = this.transformCode(elements[i].innerHTML, codeId);
		}
		if(cssUrl) util.appendStyleSheet(cssUrl);
	}
};
//
nochump.highlighter.HighlighterUtil = {
	replaceWithIframe:function(elementId, content) {
		var e = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
		var iframe = document.createElement("iframe");
		iframe.id = e.id;
		e.parentNode.replaceChild(iframe, e);
		var doc = this.getIframeDocument(iframe);
		doc.open();
		doc.write("<html><head /><body><pre>" + content + "</pre></body></html>");
		doc.close();
		return iframe;
	},
	getIframeDocument:function(iframe) {
		return iframe.contentDocument || iframe.contentWindow.document;
	},
	appendStyleSheet:function(url, doc) {
		if(!doc) doc = document;
		if(doc.createStyleSheet) {
			doc.createStyleSheet(url);
		} else {
			var style = doc.createElement('style');
			style.type = "text/css";
			style.appendChild(doc.createTextNode("@import '" + url + "';"));
			doc.getElementsByTagName("head")[0].appendChild(style);
		}
	},
	getElementsByClass:function(className) {
		var elements = document.getElementsByTagName('*') || document.all;
		var found = [];
		for(var i = 0; i < elements.length; i++) {
			if (elements[i].className == className) found[found.length] = elements[i];
		}
		return found;
	}
}
//
var as3Highlighter = new nochump.highlighter.Highlighter();
as3Highlighter.addPatternList("as3", new nochump.highlighter.As3PatternList());
