var jsInit = document.getElementsByTagName('html')[0];
jsInit.className = 'JS JSinit';

// jQuery.support enhancements
$(document).ready(function() {
	// border-radius support
	if (typeof $.support.borderRadius == 'undefined') {
		$.support.borderRadius = false;
		$.each(['BorderRadius','MozBorderRadius','WebkitBorderRadius','OBorderRadius','KhtmlBorderRadius'], function() {
			if(document.body.style[this] !== undefined)
				$.support.borderRadius = this.valueOf();
			return (!$.support.borderRadius);
		});
	}

	// display:inline-block support
	if (typeof $.support.inlineBlock == 'undefined') {
		$.support.inlineBlock = false;
		$ib = $("<div style='display:inline-block' />");
		$ib.appendTo($('body'));
		if ($ib.css('display') == 'inline-block') {
			$.support.inlineBlock = 'inline-block';
//		} else {
//			$ib.css('display', '-moz-inline-block');
//			if ($ib.css('display') == '-moz-inline-block')
//				$.support.inlineBlock = '-moz-inline-block';
		}
		$ib.remove();
	}

	// absolute positioned element inside positioned (abs or rel) container
	// FF3/4 bugs 455338/437722/203225/63895. first bug filed 12/28/2000!
	// Opera 7 ? at least for tables ?
	// tables/buttons/fieldsets not supported as containers for positioned
	// child elements.  tables are never absolute containing blocks.  fieldsets
	// are not absolute containing blocks if they are relatively positioned.
	if (typeof $.support.fieldsetRelContainer == 'undefined') {
		$.support.fieldsetRelContainer = false;
		$fsi = $("<div style='left:0;position:absolute'>X</div>");
		$fso = $("<fieldset style='margin-top:-1000em;padding:0 0 0 14px;position:relative' />");
		$fso.append($fsi).appendTo($('body'));
		if ($fsi.position().left > 0)
			$.support.fieldsetRelContainer = true;
		$fso.remove();
	}

	// VML support
	// from Google Maps
	if (typeof $.support.VML == 'undefined') {
		var a, b;
		$.support.VML = false;
		a = document.body.appendChild(document.createElement('div'));
		a.innerHTML = '<v:shape id="vml_flag1" adj="1" />';
		b = a.firstChild;
		b.style.behavior = 'url(#default#VML)';
		$.support.VML = b ? typeof b.adj == 'object' : true;
		a.parentNode.removeChild(a);
	}

	// PIE behavior loaded
	// XXX not reliable - PIE loading asynchronously ?
//	$.support.PIE = (typeof PIE == 'undefined')?false:true;
//	alert('PIE: '+$.support.PIE);
});

(function($, unknown, width) {
	$.scrollbarWidth = function() {
		var parent;
		var child;

		if (width === unknown) {
			parent = $("<div style='height:150px;width:100%;overflow:auto;position:absolute;top:-250px'><div></div></div>").prependTo('body');
			child = parent.children();

			width = child.innerWidth() - child.height(199).innerWidth();

			parent.remove();
		}

		return width;
	};

	$.css3pie = {
//		bgColor: 'backgroundColor',
//		bgImage: 'backgroundImage',
		borderRadius: ['borderRadius', 'borderTopLeftRadius', 'borderTopRightRadius', 'borderBottomLeftRadius', 'borderBottomRightRadius']
		};

	// PIE removes existing borders on elements and uses IE vml to create 
	// new border with rounded corners.  This breaks on <fieldset> with
	// non-absolutely positioned <legend>, giving appearance that <legend>
	// is absolutely positioned.  The vml border bleads through <legend>.
	//
	// XXX FF 3/4 fix for rounded corners on <legend> impacts this fix for
	// XXX IE.  Both height & line-height for <legend> are set to 0.
	$.css3pie.fixLegend = function() {
		$('legend').each(function() {
			var $legend = $(this);

			// if already absolutely positioned, or if any
			// background specified, leave everything alone
			if ($legend.css('position') == 'absolute' ||
			    $legend.css('backgroundColor') != 'transparent' ||
			    $legend.css('backgroundImage') != 'none')
				return;

			var $fieldset = $legend.closest('fieldset');

// XXX PIE only fixes shortcut border-radius (not border-xxx-yyy-radius)
			// only fix a <fieldset> with border radius
			if (!$fieldset.css('border-radius'))
				return;

			// if for some reason <legend> height or width is 0,
			// border would bleed through anyway
			var height = $legend.innerHeight();
			var width = $legend.innerWidth();
// FF bug fix impacts everyone else, for now
if (height == 0) {
	height = parseInt($fieldset.css('lineHeight'))*parseInt($legend.css('fontSize'));
}
			if (height == 0 || width == 0)
				return;

			// <fieldset> needs to be positioned for relative
			// offset to work correctly
			var position = $fieldset.css('position');
			if (position == 'static') {
				$fieldset.css('position', 'relative');
			} else if (position == 'fixed') {
				// won't support fixed
				// but maybe if <fieldset> has background-color?
				return;
			}

			var offset = $legend.position();
			offset.bottom = offset.top + height - 1;
// XXX			offset.right = offset.left + width - 1;

			var borderWidth = {};
			borderWidth.top = parseInt($fieldset.css('borderTopWidth'));
// XXX			borderWidth.right = parseInt($fieldset.css('borderRightWidth'));
// XXX			borderWidth.bottom = parseInt($fieldset.css('borderBottomWidth'));
// XXX			borderWidth.left = parseInt($fieldset.css('borderLeftWidth'));

			// find closest ancestor with background-image and
			// background-color.  stop if ever see background-color.
			// XXX some kind of race condition.
			// XXX getting current css for background-color inside
			// XXX while loop fails.  presume PIE is processing
			// XXX rounded corners in background.  might need to
			// XXX use pie.js instead of pie.htc
			var $bgParent = $legend.parent();
			var c = $bgParent.css('background-color');
			var i = $bgParent.css('background-image');
			var bgColor = 'transparent';
			var bgImage;
			while ($bgParent) {
				if (!bgImage && i != 'url("about:blank")') {
					bgImage = i;
				}
				if (c != 'transparent') {
					bgColor = c;
					break;
				}
				$bgParent = $bgParent.parent();
				c = $bgParent.css('background-color');
				i = $bgParent.css('background-image');
			}

//			if (offset.top < 0 && offset.top + height < -borderTopWidth

			var $ele = $('<div>')
				.css({
					backgroundColor:bgColor,
					height:		borderWidth.top,
					position:	'absolute',
					top:		-borderWidth.top,
					width:		width
					});

			if (bgImage) {
				$ele.css({
// XXX					backgroundRepeat: XXX,
					backgroundImage: bgImage
					});
			}

			$ele.insertBefore($legend);
		});
	}
})(jQuery);

$(document).ready(function() {
	if (!$.support.borderRadius) {
		if ($.support.VML && $.support.PIE) {
			$.css3pie.fixLegend();
		}
	} else if ($.support.legendBleed) {
	}

	// warn on PIE elements that need vml with position static
	$('.ui-shadow, .ui-corner-all, .ui-corner-top, .ui-corner-tr, .ui-corner-bottom').each(function() {
		if ($(this).css('position') == 'static') {
			console.log('FIX: position static:', $(this), this);
		}
	});

	$(jsInit).removeClass('JSinit');
});

if (!window.console) {
	var console = { log: function(){} }
}

(function($){
	// log any ajax errors
	$(document).ajaxError(function(e, xhr, settings, exception) {
		console.log('ajaxError: '+settings.url+' ('+xhr.status+') ('+xhr.responseText+') ('+exception+')');
	});

	// initialization
	var scripts = document.getElementsByTagName('script');
	eval (scripts[scripts.length -1].innerHTML);
})(jQuery);

// vim: set tabstop=4 foldmethod=marker shiftwidth=4 softtabstop=4 noexpandtab:

