(function($)
{
	$.ajaxHistory=new function()
	{
		var RESET_EVENT='historyReset';
		var _currentHash=location.hash;
		var _intervalId=null;
		var _observeHistory;
		this.update=function()
		{
		};
		var _defaultReset=function()
		{
			var output=$('.remote-output');
			if(output.children().size()>0)
			{
				output.empty()
			}
		};
		$(document).bind(RESET_EVENT,_defaultReset);
		if($.browser.msie)
		{
			var _historyIframe;
			$(function()
			{
				_historyIframe=$('<iframe style="display: none;"></iframe>').appendTo(document.body).get(0);
				var iframe=_historyIframe.contentWindow.document;
				iframe.open();
				iframe.close();
				iframe.location.hash=_currentHash.replace('#','')
			}
			);
			this.update=function(hash)
			{
				_currentHash=hash;
				var iframe=_historyIframe.contentWindow.document;
				iframe.open();
				iframe.close();
				iframe.location.hash=hash.replace('#','')
			};
			_observeHistory=function()
			{
				var iframe=_historyIframe.contentWindow.document;
				var iframeHash=iframe.location.hash;
				if(iframeHash!=_currentHash)
				{
					_currentHash=iframeHash;
					if(iframeHash!='#')
					{
						$('a[@href$="'+iframeHash+'"]').click();
						location.hash=iframeHash
					}
					else
					{
						location.hash='';
						//$(document).trigger(RESET_EVENT)
					}
				}
			}
		}
		else if($.browser.mozilla||$.browser.opera)
		{
			this.update=function(hash)
			{
				_currentHash=hash
			};
			_observeHistory=function()
			{
				if(location.hash)
				{
					if(_currentHash!=location.hash)
					{
						_currentHash=location.hash;
						$('a[@href$="'+_currentHash+'"]').click()
					}
				}
				else if(_currentHash)
				{
					_currentHash='';
					$(document).trigger(RESET_EVENT)
				}
			}
		}
		else if($.browser.safari)
		{
			var _backStack,_forwardStack,_addHistory;
			$(function()
			{
				_backStack=[];
				_backStack.length=history.length;
				_forwardStack=[]
			}
			);
			var isFirst=false;
			_addHistory=function(hash)
			{
				_backStack.push(hash);
				_forwardStack.length=0;
				isFirst=false
			};
			this.update=function(hash)
			{
				_currentHash=hash;
				_addHistory(_currentHash)
			};
			_observeHistory=function()
			{
				var historyDelta=history.length-_backStack.length;
				if(historyDelta)
				{
					isFirst=false;
					if(historyDelta<0)
					{
						for(var i=0;i<Math.abs(historyDelta);
						i++)_forwardStack.unshift(_backStack.pop())
					}
					else
					{
						for(var i=0;i<historyDelta;i++)_backStack.push(_forwardStack.shift())
					}
					var cachedHash=_backStack[_backStack.length-1];
					$('a[@href$="'+cachedHash+'"]').click();
					_currentHash=location.hash
				}
				else if(_backStack[_backStack.length-1]==undefined&&!isFirst)
				{
					if(document.URL.indexOf('#')>=0)
					{
						$('a[@href$="'+'#'+document.URL.split('#')[1]+'"]').click()
					}
					else
					{
						$(document).trigger(RESET_EVENT)
					}
					isFirst=true
				}
			}
		}
		this.initialize=function(callback)
		{
			if(typeof callback=='function')
			{
				$(document).unbind(RESET_EVENT,_defaultReset).bind(RESET_EVENT,callback)
			}
			if(location.hash&&typeof _addHistory=='undefined')
			{
				$('a.remote[@href$="'+location.hash+'"]').click()
			}
			if(_observeHistory&&_intervalId==null)
			{
				_intervalId=setInterval(_observeHistory,200)
			}
		}
	};
	$.fn.remote=function(output,settings)
	{
		settings=$.extend(
		{
			hashPrefix:'remote-'
		}
		,settings||
		{
		}
		);
		var target=$(output).size()&&$(output)||$('<div></div>').appendTo('body');
		target.addClass('remote-output');
		return this.each(function(i)
		{
			var remoteURL=this.href;
			var hash='#'+settings.hashPrefix+(i+1);
			this.href=hash;
			$(this).click(function(e)
			{
				var trueClick=e.clientX;
				target.load(remoteURL,function()
				{
					if(trueClick)
					{
						$.ajaxHistory.update(hash)
					}
				}
				)
			}
			)
		}
		)
	};
	$.fn.history=function()
	{
		return this.click(function(e)
		{
			var trueClick=e.clientX;
			if(trueClick)
			{
				$.ajaxHistory.update(this.hash)
			}
		}
		)
	}
}
)(jQuery);

// Carregando

$(document).ready(function() {
	$().ajaxStart(function() {$('body').append("<div id=\"loading\">carregando...</div>");});
	$().ajaxStop(function() {$("#loading").remove();});
});

// Links remote
$(function() {
	$('a.remote').attr("href", function(index) { return this.href + '?js=enabled'; });
	$('a.remote').remote('#conteudo');
	$.ajaxHistory.initialize();

	$('a.remote').click( function() {
		$('a.remote').removeClass("selecionado")
		$(this).addClass("selecionado");
	});
});
