dojo.require("dojo.string")
dojo.require("dojo.html.*")
dojo.require("dojo.html.layout")
dojo.require("perse.*")
dojo.require("perse.mvc")
dojo.require("perse.animation")

dojo.provide("lgz.web.navigation")

lgz.web.navigation = new function(){

	this.toggle = function(view, name, evt){
		if (evt.type == 'mouseover') {
			var model = view.lookup('detail')
			view.update('detail', {selected: name , deselected: model.selected})
		}
	}

	this.toggleout = function(view, name, evt){
		var model = view.lookup('detail')
		view.update('detail', {selected: '' , deselected: name})
	}

	this.show = function(view, name, model){
		if (model.deselected == null) {
			var children = dojo.html.getElementsByClass('detail', document.getElementById('page.footer'))
			for (var c=0; c<children.length; c++) children[c].style.display = 'none'
		}

		if (model.deselected) {
			var id = 'page.footer.' + model.deselected
			try {
				document.getElementById(id).style.display = 'none'
			} catch(e) {
				try {
					var parent_id = perse.helper.slice(id, 0, -2, '.')
					document.getElementById(parent_id).style.display = 'none'
				} catch(e) {
					
				}
			}
		}

		if (model.selected) {
			var id = 'page.footer.' + model.selected
			try {
				document.getElementById(id).style.display = 'block'
			} catch(e) {
				try {
					var parent_id = perse.helper.slice(id, 0, -2, '.')
					document.getElementById(parent_id).style.display = 'block'
				} catch(e) {
					
				}
			}
		}
	}

	this.reload = function(view, name, evt){
		var index = perse.helper.slice(name, -2, -1, '.')
		if (index) {
			var url = window.location
			window.location = url.toString().split('/').slice(0, -1).join('/') + '/' + index + '/'
		}
	}

	this.startRightMove = function(view, name, evt){
		view.update('move', {move: true})
		perse.animation.movePannel(document.getElementById('page.center.content'), view, 10, -10)
	}

	this.startLeftMove = function(view, name, evt){
		view.update('move', {move: true})
		perse.animation.movePannel(document.getElementById('page.center.content'), view, 10, 10)
	}

	this.stopMove = function(view, name, evt){
		view.update('move', {move: false})
	}

	// left navigation view
	this.LeftNavigation = new perse.mvc.View('page.leftnav')
	this.LeftNavigation.addModel('move', {move: false})
	this.LeftNavigation.addListener('onmouseover', this.startLeftMove, false)
	this.LeftNavigation.addListener('onmouseout', this.stopMove, false)

	// right navigation view
	this.RightNavigation = new perse.mvc.View('page.rightnav')
	this.RightNavigation.addModel('move', {move: false})
	this.RightNavigation.addListener('onmouseover', this.startRightMove, false)
	this.RightNavigation.addListener('onmouseout', this.stopMove, false)

	// Initialize the global registration
	this.init = function() {
		// VIEWS REGISTRATION
		perse.mvc.view.register(lgz.web.navigation.LeftNavigation)
		perse.mvc.view.register(lgz.web.navigation.RightNavigation)
	}
}

dojo.addOnLoad(lgz.web.navigation.init)
