/***
*
*  Menu Handler Class:  
*  - a simple class that simply blocks the default of <a> (for local pages), then loads the linked page - returned as 'page'.
*  - the function addLinks may be called repeatedly.  so only one instance of the class in necesary.
*  
***/
var MenuHandler = new Class({
        //
		//  initialize
		//
		Implements: [Options,Events],
		options: {
				active: '#0ac',
				visited: '#666',
				hover: '#069',
				content_path:"/",
				content_id: "main_content",
				error_htmlstr:"<div id='load_error'>LOAD ERROR(2)</div>",
				init_page:0,
				fixedExt:'',
				index:'index'
		},
		initialize: function(options){
				this.initialized = false
				this.setOptions(options);
				this.linksEnabled = true
		},	
		//
		//  public
		//
		addMenu: function(btns){
				this.pages = $$(btns)
				this.addLinks(this.pages,true);
				this.currentName = this.dropext(this.pages[this.options.init_page].get('html'))
				this.setCurrent()
				this.fireEvent('MENU_READY')
		},
		addLinks: function(btns,ismenu){
				$$(btns).each(function(elm,index){
						this.addLink(elm,ismenu,index)
				}.bind(this))
		},
		addLink: function(elm,ismenu,index){
				elm = document.id(elm)
				if (ismenu){
						elm.store('color',elm.getStyle('color'))
						elm.store('visited',false)
						elm.store('index',index)
				}
				this.setlink(elm,ismenu)
		},
		//
		//  LINKS
		//
		setlink: function(elm,ismenu){
				elm.addEvent('click',function(e){
						if (!elm.get('href').contains('http://') || !elm.get('href').contains('https://')) {
								e.stop()
								if (this.dropext(elm.get('href')) != this.currentName && !this.isLoading && this.linksEnabled) this.clicked(elm)
						}
						elm.blur()
				}.bind(this))
				if (ismenu){
						elm.addEvent('mouseover',function(){
								if (this.options.hover) elm.setStyle('color',this.options.hover)
								this.overElement = elm
								this.overRef = elm.get('href')
								this.fireEvent('OVER')		
						}.bind(this))
						elm.addEvent('mouseout',function(e){
								if (this.options.hover) {
										if (elm == this.currentElement && this.options.active) elm.setStyle('color',this.options.active)
										else elm.setStyle('color',elm.retrieve('color'))
								}
								this.overElement = null
								this.overRef = null
								this.outElement = elm
								this.outRef = elm.get('href')
								this.fireEvent('OUT')
						}.bind(this))
				}
		},
		clicked: function(elm){
				this.clearCurrent()
				this.requestElement = elm
				this.requestRef = elm.get('href')
				this.requestName = this.dropext(elm.get('href'))
				this.fireEvent("PAGE_REQUEST")
				this.isLoading = true
				var index = elm.retrieve('index')
				var req = new Request({
						url: this.contentURL(this.requestRef),					   
						onSuccess: function(htmlstring) {
								this.page = new Element('div',{id:'ajaxholder'})
								this.page.set('html',htmlstring)
								this.error = null
								this.currentName = this.requestName
								this.currentRef = this.requestRef
								this.setCurrent()
								this.requestRef = this.requestName = this.isLoading = null
								this.errorRef = this.errorName  = null
								this.errorPage = false
								this.fireEvent('PAGE_LOADED')
						}.bind(this),
						onFailure: function(err) {
								this.page = new Element('div',{id:'ajaxholder'})
								this.page.set('html',this.options.error_htmlstr+"[clicked]:  "+this.contentURL(this.requestRef)+",  "+this.requestRef)
								this.error = err
								this.errorName = this.requestName
								this.errorRef = this.requestRef
								this.clearCurrent()
								this.requestRef = this.requestName = this.isLoading = null
								this.currentRef = this.currentName = null
								this.errorPage = true
								this.fireEvent('ERROR')
						}.bind(this)
				})	
				req.send()
		},
		//
		//  internal
		//
		setCurrent: function(elm){
				this.clearCurrent()
				if (!elm) this.currentElement = this.getButton(this.currentName) 
				else {
						this.currentElement = elm
						this.currentName = dropext(elm.get('html'))
				}		
				if (this.currentElement){
						this.currentRef = this.currentElement.get('href')
						if (this.options.active) this.currentElement.setStyle('color',this.options.active)
				}
		},
		clearCurrent: function(){
				if (this.currentElement) {
						var clr = this.options.visited ? this.options.visited : this.currentElement.retrieve('color')
						this.currentElement.setStyle('color',clr)
				}		
		},
		//
		// utils
		//
		getButton: function(str){
				var btn = null
				this.pages.some(function(elm){
						var ismatch = (this.dropext(elm.get('href')) == str)
						if (ismatch) btn = elm
						return ismatch	
				}.bind(this))
				return btn
		},
		contentURL: function(url){
				if (this.options.fixedExt){
						if (url.length == 0 || url == "/") url = this.options.index
						if (url.search(this.options.fixedExt) == -1) url = url + "." + this.options.fixedExt
				}
				if (this.options.content_path.length == 0) return url
				else if (url.charAt(0) == "/" && this.options.content_path.charAt(this.options.content_path.length-1)) return this.options.content_path + url.substr(1) 
				else if (url.charAt(0) == "/" || this.options.content_path.charAt(this.options.content_path.length-1)) return this.options.content_path + url 
				else return this.options.content_path + "/" + url
		},
		dropext: function(str){
				return this.options.fixedExt ? str.split(".")[0] : str
		}
});
