//
// showdown-gui.js
//
// A sample application for Showdown, a javascript port
// of Markdown.
//
// Copyright (c) 2007 John Fraser.
//
// Redistributable under a BSD-style open source license.
// See license.txt for more information.
//
// The full source distribution is at:
//
//				A A L
//				T C A
//				T K B
//
//   <http://www.attacklab.net/>
//

//
// The Showdown converter itself is in showdown.js, which must be
// included by the HTML before this file is.
//
// showdown-gui.js assumes the id and class definitions in
// showdown.html.  It isn't dependent on the CSS, but it does
// manually hide, display, and resize the individual panes --
// overriding the stylesheets.
//
// This sample application only interacts with showdown.js in
// two places:
//
//  In startGui():
//
//      converter = new Showdown.converter();
//
//  In convertText():
//
//      text = converter.makeHtml(text);
//
// The rest of this file is user interface stuff.
//


//
// Register for onload
//
window.onload = startGui;


//
// Globals
//

var converter;
var convertTextTimer,processingTime;
var lastText,lastRoomLeft;
var inn_tekst,ut_tekst;
var maxDelay = 3000; // longest update pause (in ms)
var inn_namn, ut_namn;

//
//	Initialization
//

function startGui() {
	// find elements
	inn_tekst = document.getElementById("tekst");
	ut_tekst = document.getElementById("visning_tekst");
	inn_namn = document.getElementById("navn");
	ut_namn = document.getElementById("visning_namn");
	
	inn_namn.onblur = function() {
		ut_namn.innerHTML = inn_namn.value;
	}
	// First, try registering for keyup events
	// (There's no harm in calling onInput() repeatedly)
	window.onkeyup = inn_tekst.onkeyup = onInput;

	// In case we can't capture paste events, poll for them
	var pollingFallback = window.setInterval(function(){
		if(inn_tekst.value != lastText)
			onInput();
	},1000);

	// Try registering for paste events
	inn_tekst.onpaste = function() {
		// It worked! Cancel paste polling.
		if (pollingFallback!=undefined) {
			window.clearInterval(pollingFallback);
			pollingFallback = undefined;
		}
		onInput();
	}

	// Try registering for input events (the best solution)
	if (inn_tekst.addEventListener) {
		// Let's assume input also fires on paste.
		// No need to cancel our keyup handlers;
		// they're basically free.
		inn_tekst.addEventListener("input",inn_tekst.onpaste,false);
	}

	// build the converter
	converter = new Showdown.converter();

	// do an initial conversion to avoid a hiccup
	convertText();

	// give the input pane focus
	//inn_tekst.focus();

}

//
//	Conversion
//

function convertText() {
	// get input text
	var text = inn_tekst.value;
	
	// if there's no change to input, cancel conversion
	if (!text) {
		document.getElementById("visning").style.display = "none";
		return;
	}

	document.getElementById("visning").style.display = "block";

	if (text == lastText) {
		return;
	} else {
		lastText = text;
	}

	var startTime = new Date().getTime();

	// Do the conversion
	text = strip_tags(text);
	text = converter.makeHtml(text);

	// display processing time
	var endTime = new Date().getTime();	
	processingTime = endTime - startTime;

	// update right pane
	ut_tekst.innerHTML = text;
};


//
//	Event handlers
//

function onInput() {
// In "delayed" mode, we do the conversion at pauses in input.
// The pause is equal to the last runtime, so that slow
// updates happen less frequently.
//
// Use a timer to schedule updates.  Each keystroke
// resets the timer.

	// if we already have convertText scheduled, cancel it
	if (convertTextTimer) {
		window.clearTimeout(convertTextTimer);
		convertTextTimer = undefined;
	}

	var timeUntilConvertText = 0;
	timeUntilConvertText = processingTime;

	if (timeUntilConvertText > maxDelay)
		timeUntilConvertText = maxDelay;

	// Schedule convertText().
	// Even if we're updating every keystroke, use a timer at 0.
	// This gives the browser time to handle other events.
	convertTextTimer = window.setTimeout(convertText,timeUntilConvertText);
}

function strip_tags(str) {
	// var re= /<\S[^><]*>/g
	var re= /<(?!http)\S[^><]*>?/g
	return str.replace(re, "")
}
