//parseUri 1.2.2
//(c) Steven Levithan <stevenlevithan.com>
// http://blog.stevenlevithan.com/archives/parseuri
//MIT License

var openinfobox = "";

function parseUri (str) {
 var o   = parseUri.options,
     m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
     uri = {},
     i   = 14;

 while (i--) uri[o.key[i]] = m[i] || "";

 uri[o.q.name] = {};
 uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
     if ($1) uri[o.q.name][$1] = $2;
 });

 return uri;
};

function info(what) {

	if (what.href.search(/\?/) != -1)
	{
		final_url = what.href + '&content_only=true';
	}
	else
	{
		final_url = what.href + '?content_only=true';
	}

	win = new Window( "info", {
		className:"mop",
		url:final_url,
		title:false,
		width:637,
		height:520,
		top:100,
		left:100,
		zIndex:1000,
		showEffectOptions:{duration: 0.3},
		hideEffectOptions:{duration: 0.4},
		maximizable:false,
		minimizable:false,
		draggable:false }
	);
	win.setDestroyOnClose();
	win.showCenter(true);

	win.refresh();
	return false;
}

parseUri.options = {
 strictMode: false,
 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
 q:   {
     name:   "queryKey",
     parser: /(?:^|&)([^&=]*)=?([^&]*)/g
 },
 parser: {
     strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
     loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
 }
};

function info2(what, item) {

	var nav = what;
	
	if (!jQuery('#footerinfofield').is(":hidden")) 
	{
		jQuery('#footerinfofield').slideToggle("fast", function() {});	
	}
	
	if (openinfobox != what && what != "x")
	{
		openinfobox = "";
		var ajaxurl = "/?site=info&nav="+nav+"&action=output&content_only=true&ajax=true&noseo=1";
		
		if(typeof item != 'undefined')
		{
			ajaxurl = ajaxurl + "&item=" + item
		}
		
		//console.log(what);
		new Ajax.Request( ajaxurl,
		{
			method: 'post',
			parameters: '1=1',
			onLoading: function()
			{
				// $('amount_changing').style.display = 'block';
			},
			onSuccess: function( rtn, json )
			{	
				var data = rtn.responseText.evalJSON();
				//console.log(data.content);
				jQuery('#footerheading').html(data.heading);
				jQuery('#footerinfocontent').html(data.content);
				if (jQuery('#footerheading').html() != "---")
				{
					jQuery('#footerinfofield').slideToggle("fast");
					openinfobox = what;
					jQuery('.footerscrolling').jScrollPane({
						

						showArrows: true,
						hideFocus:true
					});
				}
			}
		});
	}
	else
	{
		openinfobox = "";
	}
	return false;
}


document.observe("dom:loaded", function() {
	
	url = parseUri(document.URL);
	
	if( typeof(url.queryKey['info2']) != 'undefined')
	{
		info2(url.queryKey['info2']);
	}
  
	if( typeof(url.queryKey['info']) != 'undefined')
	{
		if( typeof(url.queryKey['scrollto']) != 'undefined'){
			var scrollTo = '?scrollToId=' + url.queryKey['scrollto'];
		}
		else
		{
			var scrollTo = '';
		}
		
		var a = document.createElement('a');
		a.setAttribute('href', '/info/' + url.queryKey['info'] + '/' + scrollTo);

		info(a);
	}
});
