var Navigation = new Class({
	Implements: [Options, Events],
	options : {
		toUpdate : ["layoutContent", "header"],
		anchorSelectors: ["#menuContainer a", "#news a", ".home #contentRight .bottom ul a", "a.next", "#cities a", "#points a"],
		onComplete: $empty
	},
	
	hrefs:{},
	anchors:[],
	
    initialize: function(options){
		this.setOptions(options);
		this.wrapper = $(document.body).getFirst("div");
		this.currentSection = this.wrapper.get("class");
		this.loader = new Element("div", {"class":"loader"});
		this.parse();
		this.loadHistory();

		var splited = document.location.href.split("#");
		
		var url = splited[0];
		var key = url.replace(document.location.protocol+"//"+document.location.host, "");
		if($defined(splited[1]) && splited[1] != "") 
		{
			url = document.location.protocol+"//"+document.location.host+splited[1];
			var key = splited[1].replace(document.location.protocol+"//"+document.location.host, "");
		}
		
		
		this.hrefs[key] = url;
		this.processDynamicUrls();
		HistoryManager.start();
		this.history.setValue(0, key);
	},
	
	processDynamicUrls: function()
	{
		if(!$defined(document.dinamycUrls)) return;
		
		
		document.dinamycUrls.each(function(url){
			var key = url.replace(document.location.protocol+"//"+document.location.host, "");
			this.hrefs[key] = url;
		}.bind(this))
		
	},
	
	parse: function()
	{
		this.anchors = [];
		this.options.anchorSelectors.each(this.parseSelector.bind(this));
		this.addAnchorEvents();
	},
	
	parseSelector: function(selector)
	{
		$$(selector).each(this.processAnchor.bind(this));
	},
	
	processAnchor: function(anchor)
	{
		var key = anchor.get("href").replace(document.location.protocol+"//"+document.location.host, "");
		
		this.hrefs[key] = (anchor.get("href"));
		anchor.historyKey = key;
		this.anchors.push(anchor);
	},
	
	addAnchorEvents: function()
	{
		this.anchors.each(function(anchor, index){
			anchor.addEvent("click", function(e){if($defined(e))e.stop()});
			anchor.addEvent("click", function(){
				this.load(anchor.historyKey);
			}.bind(this))
		}.bind(this));
	},
	
	loadHistory: function()
	{
		this.currentIndex = -1;
		this.history = HistoryManager.register("show", [0],
				function(values, defaults){
					this.load(values[0]);
				}.bind(this), 
				function(values)
				{
					return values[0];
				},
				/.*/
				);
		
	},
	
	load: function(index)
	{
		var url = this.hrefs[index] || null;
		if (!url) return;
		
		var key = url.replace(document.location.protocol+"//"+document.location.host, "");
		this.history.setValue(0, key);
		this.currentIndex = index;
		
		var anchor = this.anchors[index];
		new Request.JSON({url:this.hrefs[index], 
			onRequest	:this.loading.bind(this),
			onComplete	:this.complete.bindWithEvent(this, [anchor])
		}).send();
	},
	
	loading: function()
	{
		$("layoutContent").setStyles({"opacity":".5"});
		this.loader.inject($("mainContent"));
	},
	
	loaded: function()
	{
		$("layoutContent").setStyles({"opacity":"1"});
		this.loader = this.loader.dispose();
	},
	
	complete: function(response, anchor)
	{
		this.loaded();
		this.wrapper.set("class", response.currentSection);
		this.options.toUpdate.each(function(el){
			$(el).set("html", response[el]);
		});
		
		if($defined(response.title)) document.title = response.title;
		
		this.parse();
		if(document.inlineEdit) document.inlineEdit.parseDom();
		
		this.fireEvent("onComplete", [response, anchor]);
		if(!$defined(anchor)) return;
		
		try{
			pageTracker._trackPageview(anchor.get("href"));
		}
		catch(exception){}
	}
});