

// < JavaScript 1.2 Compatibility

if (!Array.prototype.push) {
	Array.prototype.push = function(item) {
		this[this.length] = item;
	}
}

function elog(msg) {
	if (typeof(dbg_log) != "undefined") dbg_log(msg);
}

function TQS(search) {
	this.from(search)
}
TQS.prototype.from = function(search) {
	this.pairs = [];
	if (search == null || search == '') return;
	if (search.indexOf('?') == 0) search = search.substring(1);
	
	search = search.split('&');
	
	for (var i = 0 ; i < search.length ; i++) {
		if (search[i] != '') {
			var nv = search[i].split('=');
			
			if (nv.length == 1)
				this.pairs.push([decodeURIComponent(nv[0]), null]);
			else
				this.pairs.push([decodeURIComponent(nv[0]), decodeURIComponent(nv[1])]);
		}
	}
}
TQS.prototype.visit = function(v) {
	for (var i = 0 ; i < this.pairs.length ; i++) {
		v(this.pairs[i][0], this.pairs[i].length > 1 ? this.pairs[i][1] : null);
	}
}

function doV3BackCompat() {
	// Build up the arguments for adding the spud.
	var containerID = "" + Math.random();
	var queryString = new TQS("calendar=kiwanisclub");
	var args = "{";
	var properties = "";
	var calendar, widget = '"main"';

	queryString.visit(function(n, v) {
		if (n == "calendar")
			calendar = '"' + v + '"';
		else if (n == "widget")
			widget = '"' + v + '"';
		else
			args += (args.length > 1 ? ", " : "") + n + ': "' + v + '"';
	});
	
	args += " }";

	var spudProperties = {
		main: {
			trumba_noAsyncNav: "noAsyncNav",
			trumba_detailBase: "detailBase"
		},
		
		crawler: {
			trumba_CrawlerWidth:           "Width",
			trumba_CrawlerFontSize:        "FontSize",
			trumba_CrawlerFontFamily:      "FontFamily",
			trumba_CrawlerFontWeight:      "FontWeight",
			trumba_CrawlerFontColor:       "FontColor",
			trumba_CrawlerHoverColor:      "HoverColor",
			trumba_CrawlerBackgroundColor: "BackgroundColor",
			trumba_CrawlerBorder:          "Border",
			trumba_CrawlerSpeed:           "Speed"
		},
		
		upcoming: {
			trumba_upcomingBorderColor: "BorderColor",
			trumba_upcomingHeaderColor: "HeaderColor",
			trumba_upcomingHideHeader:  "HideHeader",
			trumba_upcomingHideFooter:  "HideFooter",
			trumba_upcomingHideBorder:  "HideBorder"
		}
	};
	
	var widgetNQ = widget.substring(1, widget.length - 1);

	// All spuds get the following properties.
	
	var propVars = {
		trumba_teaserBase: "teaserBase"
	};

	var baseVars = spudProperties[widgetNQ];
	
	for (var n in baseVars)
		propVars[n] = baseVars[n];
	
	for (var prop in propVars) {
		if (typeof(window[prop]) != "undefined") {
			properties += (properties.length > 1 ? ', ' : '') + propVars[prop] + ': ';
			
			switch (typeof(window[prop])) {
				case "string" : properties += '"' + window[prop] + '"';         break;
				case "boolean": properties += window[prop] ? 'true' : 'false';  break;
				case "number" : properties += window[prop];                     break;
				default       : properties += 'null';                           break;
			}
		}
	}
	
	// Now look for style sheets on a per spud basis.
	
	var styleSheets = window["trumba_" + widgetNQ + "StyleSheets"];
	
	if (styleSheets && styleSheets.length) {
		properties += (properties.length > 1 ? ', ' : '') + 'styleSheets: [';

		for (var i = 0 ; i < styleSheets.length ; i++) {
			properties += (i > 0 ? ', ' : ' ') + '"' + styleSheets[i] + '"';
		}
		
		properties += " ]";
	}

	// Check to see if their is a parent frame with our controller.  If so
	// we'll tell it who we are and let it do the spud insertion.
	
	var controller = null;
	
	if (window.parent != null && window != window.parent) {
		try {
			// This should RIP on all browsers when the parent window and
			// our window have different domains.
			
			controller = window.parent.$Trumba;
		} catch (e) {
		}
	}

	var html = "";

	if (controller) {
		// Fix for Opera 8.  The DOM is screwed up in Opera 8.  If you get the IFRAME
		// element it's ownerDocument is set to the IFRAME contentDocument not it's
		// true container.  The fix is to call a method in the parent document that
		// sets a global variable to the iframe node.  Why this works I do not know.
		// Found by major trial and error.
		
		if (/opera/i.test(navigator.userAgent))
		{
			var operaVersion;
			
			if (navigator.userAgent.indexOf("Opera/") != -1) // Standard Opera UA
				operaVersion = parseFloat(navigator.userAgent.split("Opera/")[1]);
			else if (navigator.userAgent.indexOf("Opera ") != -1) // Cloaked as IE
				operaVersion = parseFloat(navigator.userAgent.split("Opera ")[1]);
			else
				operaVersion = parseFloat(navigator.appVersion);
			
			if (operaVersion < 9) {
				window.parent.$Trumba.Opera8Fixup();
			}
		}

		eval("args = " + args + ";");
		eval("properties = {" + properties + " };");
		window.parent.$Trumba.addBackCompatSpud(eval(calendar), eval(widget), args, properties, window.frameElement.id || window.frameElement.name);
	}
	else {
		// We are rendering inline, splat the document HTML for spud.
		// IE takes a long time to load spuds.js thus the window.setTimeout()s.
		
		html  = "<" + "div id=\"" + containerID + "\">\<\/div>";
		
		// Insert both the spud and the add method.
		if (typeof(trumba_wroteSpudsJS) == "undefined") {
			trumba_wroteSpudsJS = true;
			html += "<" + "script type=\"text/javascript\" src=\"http://www3.trumba.com/scripts/spuds.js\">\<\/script>";
		}

		var waitor = function() {
			if (typeof($Trumba)=="undefined") {
				window.setTimeout(arguments.callee, 100);
				return;
			}

			eval("var props = { webName: " + calendar + ", spudType: " + widget + ", spudId: \"" + containerID + "\", url: " + args + (properties.length ? ", " + properties : "") + " }");
			$Trumba.addSpud(props);
		}

		document.write(html);
		window.setTimeout(waitor, 100);
	}
}

doV3BackCompat();

