var Navibar = new Class({
	initialize: function(page){
		this.page = page,
		this.isAdmin = false;
		this.navibar = new Element ('div',{'class':'navi'});
		this.navibar.setStyle('background-image','url(' + this.page.config.navibar + ')');
		this.navibar.setStyle('width',this.page.size.mfwidth);
		this.navibar.inject(this.page.mainframe);
		this.naviboxid = 0;
		this.naviHelper = new Element('div',{'class':'naviHelper'}).inject(document.body);
		this.adminmodule = false;
		
		
		
		this.navibar.addEvent('contentReady',function(){
			this.page.content.addEvent('mouseenter',function(ev){
				$$('div.navibox').each(function(it){
					it.style.display = 'none';
				});
			});
		}.bindWithEvent(this));
		
		
		this.navibar.addEvent('adminModule',function(){
			var adminButton = $('naviButtonAdmin');
			adminButton.removeEvents('click');
			adminButton.addEvent('click',function(ev){
				ev.stop();
				this.showAdminPage();
			}.bindWithEvent(this.adminmodule));
		}.bindWithEvent(this));
		
		
		
		
	},
 
 	checkAdminModule: function(adminmodule){
		var req = new Request.JSON ({
			url: 'lib/adminRequests.php',
   			data: 'request=loggedIn&session=' + adminmodule.user.user_session,
			async: false,
	  		onSuccess: function(responseJSON){
				if(responseJSON.error == 0){
					this.isAdmin = true;
					var ap = {
						'page_active':'1',
	  					'page_menu':'1',
						'page_id': 'Admin',
						'page_name':'Admin',
						'page_parent':'0'
					};		
					this.naviItems.pages.include({'children':false,'level':0,'page':ap});
					
				}
			}.bindWithEvent(this)
		});
		req.send();
		
		if(this.isAdmin)
			this.adminmodule = adminmodule;
	},
 
 	addAdminModule:function(adminmodule){
		this.checkAdminModule(adminmodule);
		if(this.adminmodule){
			this.clearNavibar();
			this.createNaviItems();
		}
	
	},
 	
 
 	createNaviItems: function(){
		
		var navitable = new Element('table',{'class':'navitable','border':0}).inject(this.navibar);
		var navirow = new Element('tr').inject(navitable);
// 		if(this.page.user)
// 			this.addAdminModule();

		this.naviItems.pages.each(function(it){
			
			if(it.page.page_menu.toInt() == 0)
				return;
			
 			if(!this.adminmodule && (it.page.page_active.toInt() == 0)){
				return;
			}
			var navicol = new Element('td',{'valgin':'middle'}).inject(navirow);
			if(this.adminmodule)
				var objname = unescape(decodeURIComponent(it.page.page_name).trim()) + " (" + it.page.page_id + ")";
			else
				var objname = unescape(decodeURIComponent(it.page.page_name).trim());
			
			var naviobj = new Element('naviobj',{
				'id':'naviButton' + it.page.page_id,
				'html':objname
			}).inject(navicol);
			
			if(it.page.page_active == 0){
				naviobj.setStyle('color','red');
			}
			
			naviobj.addEvent('click',function(){
				$$('div.navibox').each(function(it){
					it.style.display = 'none';
				});
				this.page.showPage(it.page.page_id);
			}.bindWithEvent(this));
	
			naviobj.addEvent('mouseenter',function(ev){
// 				console.log(ev,naviobj);
				
				if(($(ev.target.id) != naviobj.id) )
				$$('div.navibox').each(function(foo){
					foo.style.display = 'none';
				});
			}.bindWithEvent(this));
			
		}.bindWithEvent(this));
		
		this.naviItems.pages.each(function(it){
			if(it.page.page_menu.toInt() == 0)
				return;
			
			if(!this.adminmodule && (it.page.page_active.toInt() == 0)){
				return;
			}
			if(it.children){
				this.createChildNavi(it);
			}
		}.bindWithEvent(this));
		
		if(this.isAdmin){
			this.navibar.fireEvent('adminModule');
		}
	},
 
 	clearNavibar: function(){
		this.navibar.getChildren().each(function(it){it.dispose();});
	},
 
 	createChildNavi: function(item){
			
		var navibox = new Element('div',{'class':'navibox',id:'navibox' + item.page.page_id,'level':item.level,'groupid':0});
		
		
		if(item.level == 0){
			
			navibox.inject(this.navibar);
			var pos = $('naviButton' + item.page.page_id).getCoordinates(this.navibar);
			var pos2 = $('naviButton' + item.page.page_id).getCoordinates();
			navibox.style.left = pos.left;
			navibox.style.top = this.navibar.getStyle('heigth');
			navibox.style.display = 'none';
			navibox.style.position = 'absolute';
			$('naviButton' + item.page.page_id).addEvent('mouseenter',function(ev){
				navibox.style.right = $('naviButton' + item.page.page_id).getStyle('width');
				navibox.style.display = 'block';
				navibox.style.opacity = 0.1;
				navibox.fade('in');
				this.groupid = item.page.page_id;
				var need = ((item.childcount * 180));
				var freespace = (this.page.size.totalWidth - pos2.right);
				//console.log(pos2.right +' | f:'+freespace +' | n:'+  need + '| #' + item.childcount )
				if(freespace < need)
					this.openDirection = "right";
				else
					this.openDirection = "left";
				
			}.bindWithEvent(this));
		}else{
			
			navibox.set('groupid',this.groupid);
 			navibox.inject($('naviButton' + item.page.page_id));
			var pos1 = $('naviButton' + item.page.page_id).getCoordinates($('navibox' + item.page.page_id));
		
			navibox.style.top = 0;//pos1.bottom;
			navibox.style.display = 'none';
			navibox.style.position = 'absolute';
				
			$('naviButton' + item.page.page_id).addEvent('mouseenter',function(ev){

				if(this.openDirection == "left" && ( true) )
					navibox.style.left = $('naviButton' + item.page.page_id).getStyle('width');
				else 
					navibox.style.right = $('navibox' + item.page.page_id).getStyle('width');
				
				navibox.style.display = 'block';
				navibox.style.opacity = 0.1;
				navibox.fade('in');
				this.groupid = item.page.page_id;
			}.bindWithEvent(this));
		}
		
		var reallyNeedBox = false;
		item.children.each(function(_child,key){
			if(((_child.page.page_active == 0) && !this.adminmodule ) || (_child.page.page_menu == 0))
				return;
			
			
			this.groupid = _child.page.page_id;
			reallyNeedBox = true;
			navibox.set('groupid',this.groupid);
			
			if(this.adminmodule)
			var objname = unescape(decodeURIComponent(_child.page.page_name).trim()) + " (" + _child.page.page_id + ")";
			else
				var objname = unescape(decodeURIComponent(_child.page.page_name).trim());
			
			var col = new Element('div',{'groupid':this.groupid,'class':'naviButton','id':'naviButton' + _child.page.page_id,'html':'<span style="padding:5px;">' + objname +  '</span>'}).inject(navibox);
			if(key.toInt() < (item.children.length -1))
			var sep = new Element('hr',{'class':'fine'}).inject(navibox);
			col.style.width = navibox.getStyle('width');
			col.style.position = 'relative';
			col.style.height = '13px';
			col.style.cursor = 'pointer';
			if(_child.page.page_active == 0)
				col.style.color = 'red';
			if(_child.children){
// 				navibox.set('groupid',this.groupid);
				this.createChildNavi(_child);
			}
					
			
			// onclick row !!!
			col.addEvent('click',function(ev){
				ev.stop();
				$$('div.navibox').each(function(it){
					it.style.display = 'none';
				});
				this.page.showPage(_child.page.page_id);
			}.bindWithEvent(this));
			
			col.addEvent('mouseover',function(ev){
				ev.stop();
				$$('div.navibox').each(function(it){
					if (it.get('level') >= _child.level )
						it.style.display = 'none';
				}.bindWithEvent(this));
				
				var shower = $($('navibox' + _child.page.page_id));
				if(shower)
					shower.style.display = 'block';

			}.bindWithEvent(this));
			
			
			
		}.bindWithEvent(this));
		if(!reallyNeedBox)
			navibox.dispose();
		
		
	},
 
 	loadNaviItems: function(){
		var loadItemsReq = new Request.JSON({
			async: false,
			url: 'lib/requestAdapter.php',
			data: "request=getPageTree",
			onSuccess: function(responseJSON){
				this.naviItems = responseJSON.data;
			}.bindWithEvent(this),
		});
		loadItemsReq.send();

	},
	
	
});
