function makeOverlayBox(container)
{
	var c = document.createElement('div');
	
	if ($(container).parent().css('position') == 'static')
		container.parentNode.style.position = 'relative';
	
	var lp = 0, tp = 0;
	if (/IE 6/.test(navigator.userAgent)) {
		lp = parseInt($(container).parent().css("padding-left"));
		// tp = parseInt($(container).parent().css("padding-top"));
	}
	function znan(v) {
		return isNaN(v) ? 0 : v;
	}
	$(c).css({
		position:	'absolute',
		left: 		znan($(container).offset().left - $(container).parent().offset().left - lp),
		top:  		znan($(container).offset().top  - $(container).parent().offset().top - tp),
		width:		znan($(container).width()),
		height:		znan($(container).height())
	});

	return c;
}

var initialState = { };
var initialStateCount = 0;
var activeState = { };
var ipl = true;

function doNavigate() {
	var n = $("[rel~=x-ajax]");
	var Q = { };
	for (var i in initialState)
		Q[i] = initialState[i];

	var toActivate = [ ];
	var h = location.hash.substring(1).split('&');
	for (var i = 0; i < h.length; i++) {
		var p = h[i].split('=');
		if (p[0])
			Q[decodeURIComponent(p.shift())] = decodeURIComponent(p.join('='));
	}
	var foundLinks = [ ];
	if (window.console) console.log(Q);
	n.each(function() {
		var gotAny = false;
		for (var i in this.ajaxData) {
			gotAny = true;
			if (Q[i] != this.ajaxData[i]) return;
		}
		if (gotAny) {
			var c = 0, d = 0;
			for (var i in this.ajaxData) {	
				if (activeState[i] != this.ajaxData[i]) d++;
			}
			if (d > 0)
				toActivate.push(this);
			foundLinks.push(this);
		}
	});

	while (toActivate.length) {
		for (var i in toActivate[0].ajaxData) {
			if (i)
				activeState[i] = toActivate[0].ajaxData[i];
		}
		$(toActivate.shift()).click();
	}
}

var lastHash;

$(function() {
	var n = $("[rel~=x-ajax]");
	if (n.size()) {
		initialState = { };
		$("[rel~=x-initial]").each(function() {
			for (var i in this.ajaxData) {
				initialState[i] = this.ajaxData[i];
				activeState[i] = this.ajaxData[i];
			}
		});
		doNavigate();
		lastHash = location.hash;
		setInterval(function() {
			if (lastHash != location.hash) {
				lastHash = location.hash;
				doNavigate();
			}
		}, 100);
	}
	
	ipl = false;
});

function ajaxSetFragment(p) {
	var s = p.split('?').pop();
	var t = s.split('&');
	var Q = { };
	var h = location.hash.substring(1).split('&');
	for (var i = 0; i < h.length; i++) {
		var p = h[i].split('=');
		Q[decodeURIComponent(p.shift())] = decodeURIComponent(p.join('='));
	}
	for (var i = 0; i < t.length; i++) {
		var p = t[i].split('=');
		Q[decodeURIComponent(p.shift())] = decodeURIComponent(p.join('='));
	}
	delete Q.container;
	
	var hv = [];
	for (var i in Q) {
		if (i && initialState[i] !== void(0) && Q[i] != initialState[i]) {
			hv.push(encodeURIComponent(i) + '=' + encodeURIComponent(Q[i]));
		}
	}
	if (hv)
		lastHash = location.hash = '#' + hv.join('&');
	else {
		location.hash = '';
	}
}

function ajaxSetActiveState(O) {
	for (var i in O) {
		if (initialState[i] !== void(0))
			activeState[i] = O[i];
	}
}

/**
 * /share/share.php hookup...
 */
$(function() {
	$("a[href*='share/share.php']").click(function() {
		tb_show("", $('base').attr('href') + '/share/share.php?link=' + escape(location.href) + "&TB_iframe=true&height=360&width=600");
		return false;
	});
	$("a[href^='http://www.facebook.com/sharer.php']").click(function() {
        window.open(this.href, "_blank", "height=425,width=600");
		//tb_show("", this.href + "&TB_iframe=true&height=425&width=600");
		return false;
	});
});
