var AA_ALWAYS_USE_FLASHPLAYER = false;

function dom_replaceTag(oldnode, newnode) {
	oldnode.parentNode.replaceChild(newnode, oldnode);
}

var AATitle = Class.create({
	initialize: function (text) {
		this.elt = create("div.title");
		this.temporalItems = [];
		/* perform markup */
		text = this.translate_markup(text);
		this.elt.innerHTML = text;
		/* could here we automagically seek out and replace video elements */
		this.walk(this.elt);
		this.hide();
	},
	translate_markup: function (text) {
		for (var mname in MARKUP) {
			var m = MARKUP[mname];
			text = text.replace(m.pattern, m.replacement);
		}
		text = text.replace(markup_iframe_pat, markup_iframe_repl);
		return text;
	},
	/* walk should detect / replace video elements, and accumulate them to 
	propogate setCurrentTime calls */
	walk: function (node) {
		// log("walk:", node.tagName);
		// nodeType == 1 for tags, 3 for text
		if ((node.nodeType == 1) && node.nodeName == "VIDEO") {
			// TODO: MAGIC!
			// log("I found a video tag");
			// log("video tag:", node, node.play);
			if (!AA_ALWAYS_USE_FLASHPLAYER && node.play) {
				// hooray, native support
				log("using native video element");
				this.temporalItems.push(node);
			} else {
				// FALLBACK TO FlashPlayer
				// replace with AAVideo object (& force to ScrubPlayer)
				// var video = new AAVideo(node.getAttribute("src"));
				log("falling back to flash player");
				var src = Object.extend(node.getAttribute("src"));
				src = src.replace(/\.og[gv]$/i, ".flv");
				var video = aa_play(src, 160, 120);
				// log("made a video element", video, video.elt);
				dom_replaceTag(node, video.elt);
				this.temporalItems.push(video);
			}
		} else if ((node.nodeType == 1) && node.nodeName == "AUDIO") {
			// TODO: MAGIC!
			if (!AA_ALWAYS_USE_FLASHPLAYER && node.play) {
				// hooray, native support
				log("using native audio element");
				this.temporalItems.push(node);
			} else {
				// FALLBACK TO FlashPlayer
				// replace with AAVideo object (& force to ScrubPlayer)
				log("falling back to flash player");
				var src = Object.extend(node.getAttribute("src"));
				src = src.replace(/\.ogg$/i, ".mp3");
				var audio = aa_play(src, 10, 10);
				// log("made an audio element", audio, audio.elt);
				dom_replaceTag(node, audio.elt);
				this.temporalItems.push(audio);
			}
		}
		// log("walk:", node.nodeType, node.childNodes);
		var children = node.childNodes;
		for (var i=0; i<children.length; i++) {
			var child = children[i];
			this.walk(child);
		}
	},
	show: function () {
		this.elt.style.display = "block";
		// this.elt.style.visibility = "visible";
	},
	hide: function () {
		this.elt.style.display = "none";
		// this.elt.style.visibility = "hidden";
	},
	getIsPlaying: function () {
		for (var i=0; i<this.temporalItems.length; i++) {
			var titem = this.temporalItems[i];
			// log("getIsPlaying for", titem, titem.paused);
			if (titem.paused != null)
				return !titem.paused;
			else return titem.isPlaying || (titem.getIsPlaying && titem.getIsPlaying());
		}
		return false;
	},
	getCurrentTime: function () {
		for (var i=0; i<this.temporalItems.length; i++) {
			var titem = this.temporalItems[i];
			// log("getCurrentTime for", titem, titem.currentTime);
			var itemCurrentTime = titem.currentTime || titem.getCurrentTime();
			if (itemCurrentTime != null) return itemCurrentTime;
		}
		return null;
	},
	setCurrentTime: function (ct) {
		for (var i=0; i<this.temporalItems.length; i++) {
			var titem = this.temporalItems[i];
			// log("setCurrentTime", titem);
			if (titem.setCurrentTime) {
				titem.setCurrentTime(ct);
			} else {
				try {
					titem.currentTime = ct;
				} catch (e) {
					log("caught exception");
				}
			}
		}
	},
	play : function () {
		for (var i=0; i<this.temporalItems.length; i++) {
			var titem = this.temporalItems[i];
			if (titem.play) titem.play();
		}
	},
	pause: function () {
		for (var i=0; i<this.temporalItems.length; i++) {
			var titem = this.temporalItems[i];
			if (titem.pause) titem.pause();
		}
	}
});

/* regex to replace iframe: markup */
// var iframepat = /^iframe:([\w.\-/,%&?]+)$/mg;
var markup_iframe_pat = /^iframe:(.+)$/mg;
var markup_iframe_repl = "<iframe src=\"$1\">no iframe support</iframe>";
var MARKUP = {};
function Markup (name, priority, pattern, replacement) {
	MARKUP['name'] = {name: name, priority: priority, pattern: pattern, replacement: replacement}
}

/* DEFAULT MARKUP */

Markup('iframe', 0, new RegExp("^iframe:(.+)$", "mg"), "<iframe src=\"$1\">no iframe support</iframe>");
Markup('video', 0, new RegExp("^video:(.+)$", "mg"), "<video src=\"$1\">no video tag support</video>");
Markup('audio', 0, new RegExp("^audio:(.+)$", "mg"), "<audio src=\"$1\">no audio tag support</audio>");

