// In this file I keep all the code for third-party TiddlyWiki plugins
// Frank Dellaert, 2005

// A quick Google search on the plugin name should yield authorship,
// license, and documentation

// AliasPlugin

version.extensions.alias= {major: 1, minor: 0, revision: 3, date: new Date(2005,10.9)};
config.macros.alias= { };
config.macros.alias.handler = function(place,macroName,params) {
	var alias=params.shift(); if (!alias) return; alias=alias.replace(/ /g,"_"); // don't allow spaces in alias
	if (config.macros[alias]==undefined) // create new macro (as needed)
		{	 
		config.macros[alias] = { };
		config.macros[alias].handler =
			function (place,macroName,params)
				{ wikify(config.macros[macroName].text,place,null,null); }
		}
	config.macros[alias].text = params[0]?params.join(' '):alias;	// set alias text
}

// NestedSliderPlugin

version.extensions.nestedSliders = {major: 1, minor: 4, revision: 0, date: new Date(2005,11,11)};

config.formatters.push( {
	name: "nestedSliders",
	match: "\\+{3}",
	terminator: "\\n*\\={3}\\n?",
	lookahead: "\\+{3}(\\+)?(\\!*)?(\\[[^\\]]*\\])?(\\>?)\\n*",
	handler: function(w)
		{
			var lookaheadRegExp = new RegExp(this.lookahead,"mg");
			lookaheadRegExp.lastIndex = w.matchStart;
			var lookaheadMatch = lookaheadRegExp.exec(w.source)
			if(lookaheadMatch && lookaheadMatch.index == w.matchStart)
			{
				var show = lookaheadMatch[1]?"block":"none";
				var title = lookaheadMatch[1]?"<":">";
				var tooltip = lookaheadMatch[1]?"hide":"show";
				var header = lookaheadMatch[3];
				if (header) { // custom label and tooltip
					title = header.trim().substr(1,header.length-2);
					if ((pos=title.indexOf("|")) != -1)
						{ tooltip = title.substr(pos+1,title.length); title = title.substr(0,pos); }
					else
						{ tooltip += " "+title; }
				}
				if (lookaheadMatch[2]) { // use "Hn" header format instead of button/link
					var lvl=(lookaheadMatch[2].length>6)?6:lookaheadMatch[2].length;
					var btn = createTiddlyElement(w.output,"h"+lvl,null,null,title);
					btn.onclick=onClickNestedSlider;
					btn.style.cursor="pointer";
					btn.title=tooltip;
				}
				else
					var btn = createTiddlyButton(w.output,title,tooltip,onClickNestedSlider);
				var panel = createTiddlyElement(w.output,"span",null,"sliderPanel",null);
				panel.style.display = show;
				if (lookaheadMatch[4]) // automatic block quotes
					panel = createTiddlyElement(panel,"blockquote");
				w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
				w.subWikify(panel,this.terminator);
			}
		}
	}
)

function onClickNestedSlider(e)
{
	if (!e) var e = window.event;
	var theTarget = resolveTarget(e);
	var theLabel = theTarget.firstChild.data;
	var theSlider = this.nextSibling;
	var isOpen = theSlider.style.display!="none";
	// if using default button labels, toggle labels
	if (theLabel==">") theTarget.firstChild.data = "<";
	else if (theLabel=="<") theTarget.firstChild.data = ">";
	// if using default tooltips, toggle tooltips
	if (theTarget.getAttribute("title")=="show")
		theTarget.setAttribute("title","hide");
	else if (theTarget.getAttribute("title")=="hide")
		theTarget.setAttribute("title","show");
	if (theTarget.getAttribute("title")=="show "+theLabel)
		theTarget.setAttribute("title","hide "+theLabel);
	else if (theTarget.getAttribute("title")=="hide "+theLabel)
		theTarget.setAttribute("title","show "+theLabel);
	// show/hide the slider
//	if(config.options.chkAnimate)
//		anim.startAnimating(new Slider(theSlider,!isOpen,e.shiftKey || e.altKey,"none"));
//	else
		theSlider.style.display = isOpen ? "none" : "block";
	return false;
}

// GMapMacro

version.extensions.gmap = { major: 0, minor: 9, revision: 0, date: new Date(2005, 11, 2) };

config.macros.gmap = {}
config.macros.gmap.handler = function(place, macroName, params) {
	var element = createTiddlyElement(place, "div", "map", null, "");
	element.style.width = "300px";
	element.style.height = "300px";
	
	zoom = 4
	if(params.length >= 3) {
		zoom = params[2]
	}
	
	if (GBrowserIsCompatible()) {
		var map = new GMap(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.centerAndZoom(new GPoint(params[0], params[1]), zoom);
	}
}

// http://www.elsdesign.com/tiddlywiki/#InlineJavascriptPlugin

version.extensions.inlineJavascript= {major: 1, minor: 3, revision: 0, date: new Date(2005,11,9)};

config.formatters.push( {
	name: "inlineJavascript",
	match: "\\<script",
	lookahead: "\\<script(?: src=\\\"((?:.|\\n)*?)\\\")?\\>((?:.|\\n)*?)\\</script\\>",

	handler: function(w) {
		var lookaheadRegExp = new RegExp(this.lookahead,"mg");
		lookaheadRegExp.lastIndex = w.matchStart;
		var lookaheadMatch = lookaheadRegExp.exec(w.source)
		if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
			if (lookaheadMatch[1]) { // load a script library
				// make script tag, set src, add to body to execute, then remove for cleanup
				var script = document.createElement("script"); script.src = lookaheadMatch[1];
				document.body.appendChild(script); document.body.removeChild(script);
			}
			if (lookaheadMatch[2]) { // run inline script code
				var code="function _out(place){"+lookaheadMatch[2]+"};_out(w.output);"
				code=code.replace(/document.write\(/gi,'place.innerHTML+=(');
				var out=""; try { out = eval(code); } catch(e) { out = e.toString(); }
				if (out && out.length) wikify(out,w.output);
			}
			w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
		}
	}
} )

// DefaultView

function onClickDefaultView(e) {
  closeAllTiddlers();
  config.options.txtDefaultTiddlers = "";
  saveOptionCookie('txtDefaultTiddlers');
  var start = store.getTiddlerText("DefaultTiddlers");
  if(start)
    displayTiddlers(null,start,1,null,null);
}

config.macros["defaultView"] = {label: "default view", prompt: "Show the default tiddlers", title: "default view"};

config.macros.defaultView.handler = function(place) {
  createTiddlyButton(place,this.label,this.prompt,onClickDefaultView);
}

// CounterPlugin
// Frank Dellaert 2005
// This is a private TiddlyWikiMacro that allows
// [[StatCounter|www.statcounter.com]] counters to play nicely with
// TiddlyWiki.

// user details
config.macros.counter = {};
config.macros.counter.handler= function(place,macroName,params) {

  var sc_url = ""+document.location;
  sc_url = sc_url.substring(0, 150);
  sc_url = escape(sc_url);

  // do not generate a hit if this page is not at Geoegia Tech !
  if (sc_url.search(/www.cc.gatech.edu/) == -1) return;

  var sc_width=window.screen.width;

  var sc_referer = ""+document.referrer;
  sc_referer = sc_referer.substring(0, 150);
  sc_referer = escape(sc_referer);

  var sc_unique = 0;
  var sc_returning = 0;
  var sc_returns = 0;

  var sc_agent = navigator.appName+' '+navigator.appVersion;
  sc_agent = sc_agent.toUpperCase();

  // time 
  var sc_date = new Date();
  var sc_time = sc_date.getTime();
  var sc_time_difference = 60*60*1000;

  // navigation details
  var sc_title = ""+document.title;
  sc_title = sc_title.substring(0, 150);
  sc_title = escape(sc_title);

  // my identifier
  var sc_project=1135105;
  var sc_security="9efad4a5";

  var sc_http_url="http";
  var sc_security_code = sc_security;
  var sc_base_dir = sc_http_url+"://c11.statcounter.com/t.php?sc_project="+sc_project;

  var sc_tracking_url = sc_base_dir 
  +"&resolution="+sc_width
  +"&camefrom="+sc_referer
  +"&u="+sc_url
  +"&t="+sc_title
  +"&java=1"
  +"&security="+sc_security_code
  +"&sc_random="+Math.random();

  wikify("\n[img[StatCounter|"+sc_tracking_url + "][http://www.StatCounter.com]]",place);
}
