function layout() { return $("link[@href$='layout.css']").attr("href").match(/\d+(?=\/layout.css)/); }
function masterPath() { return ($("script[@src$='master.js']").attr("src") || "").replace(/master.js$/, ""); }

function addStyle(content, media)  {
	var result;
	document.createStyleSheet ?
		(result = document.createStyleSheet()) :
		$("head").append(result = $(document.createElement("style")).attr("type", "text/css").get(0));
	result.media = media || "all";
	result.addRule ? (result.cssText = content) : (result.appendChild(document.createTextNode(content)));
	return result;
}

jQuery.getScript =  function( url ) {
	var head = $("head");
	var js = $(document.createElement('script')).attr({'type': 'text/javascript', 'src': url});
	head.append(js);
};

jQuery.cache = {};
jQuery.flushcache = function (key) { key ? delete jQuery.cache[key] : jQuery.cache = {}; }
jQuery.$ = function(key) { var cached = jQuery.cache[key]; return cached || (jQuery.cache[key] = $(key)); }

/* Heavy wizardry. Works around Atlas javascript breaking jQuery's css() method. */
var oldcurCSS = jQuery.curCSS;
jQuery.curCSS = function(elem, prop, force) {
	var intercept = (typeof Web != "undefined") && (typeof Web.Browser != "undefined") && (Web.Browser.isMozilla());
	if (intercept) {
		var oldGetter = elem.__lookupGetter__ ? elem.__lookupGetter__("currentStyle") : null;
		elem.__defineGetter__("currentStyle", function() { return null; });
	}
	var result = oldcurCSS(elem, prop, force);
	if (intercept && elem.__lookupGetter__) elem.__defineGetter__("currentStyle", oldGetter);
	return result;
}

jQuery.registry = {
	libraries : {"SWFObject" : "swfobject-readable.js"},
	init: function() {
		$.each(jQuery.registry.libraries, function(name) {
			var scriptPath = masterPath() + this;
			jQuery.registry[name] = function(callback) {
				jQuery.registry.libraries[name] = callback;
				jQuery.getScript(scriptPath);
			}
		});
	}, 
	loaded : function(name) {
		var callback = jQuery.registry.libraries[name];
		if (typeof callback == "function") callback();
	}
};

jQuery.registry.init();

function bottomOffset(el) { return el.offsetHeight + topOffset(el); }
function topOffset(el) {
	var sum = 0;
	do { sum += el.offsetTop + el.clientTop; } while (el = el.offsetParent);
	return sum;
}
	
if ($.browser.msie) $(function() {
	var layout11fix = (layout() == "11" && $.$("#body1.IE #webboxes").length);
	var resizeHandler = function() {
		$.$("#body1.IE #pageframe").css('width',
			Math.max(950, Math.min($.$('html').get(0).clientWidth, 10 * 100 * parseInt($.$('body').css('font-size'))
				* screen.logicalXDPI / 96 / 12)) + 'px');
		$.$("#body1.IE tr.pageframe_bottom *").css('height',
			Math.max(1, $.$('html').get(0).clientHeight - $.$('#pageframe-1').get(0).clientHeight) + 'px');
		$.$("#body1.IE tr.pageframe_main").css('height', $.$("#pageframe").get(0).clientHeight - 1 + 'px');
		if (layout11fix) {
			$.$("#webboxes").css('padding-bottom',
				Math.max(0, topOffset($.$('#footer').get(0)) - bottomOffset($.$('#webboxes > *:last-child').get(0))) + 'px');
			$.$("#sidebar").css('margin-bottom', '-' + $.$('#webboxes').css('padding-bottom'));
		};
	};

	$(window).resize(resizeHandler);
	$(window).load(resizeHandler);
	$("#pageframe").get(0).onresize = resizeHandler;
	if (layout11fix) $("#sidebar").get(0).onresize = resizeHandler;	
	
	addStyle("#body1.IE #pageframe { width: 100% !important; }" + 
		"#body1.IE tr.pageframe_main, #body1.IE tr.pageframe_bottom * { height: auto !important; }",
		"print");
});

$(function() {
	if (typeof flashDecorUrl != "undefined") {	
		$.registry.SWFObject( function() {
			var so = new SWFObject(flashDecorUrl, "decor1", "100%", "100%", "7", "#FFFFFF");
			so.addParam("wmode", "transparent");
			so.write("decor");
			$("#upgradeflash").css('visibility', 'visible');
		});
	}
});
