var PageManager = new Class({
	
	initialize: function(){
		this.container = document.body;
		this.config = {};
		this.loadConfig();
		this.createPage();
		this.initKeyboard();
		this.popups = new Popups();
		this.notifier = new Notifier(this);
		window.fireEvent('anchor');
		
	},

	setCookie: function(key,value){
		return Cookie.write('bwa.' + key,encodeURIComponent(JSON.encode(value)));
	},

	getCookie: function(key){
		return JSON.decode(decodeURIComponent(Cookie.read('bwa.' + key)));
	},
 
 	delCookie: function(cooky){
		cooky.dispose();
	},

	createPage: function(){
		
		var totalWidth = window.innerWidth;
		var totalHeight = window.innerHeight;
		var hcenter = totalWidth / 2;
		var vcenter = totalHeight /2;
		var mfwidth = (totalWidth < 1024 ) ? totalWidth : (totalWidth / 100 * 85);
		var mfheight = (totalHeight < 768 ) ? totalHeight : (totalHeight / 100 * 95);
		this.size = {
			'mfwidth':mfwidth,
			'mfheight':mfheight,
			'totalWidth':totalWidth,
			'totalHeight':totalHeight,
			'hcenter': hcenter,
			'vcenter': vcenter
		};

		this.mainframe = new Element ('div',{'class':'mainframe'});
		this.mainframe.setStyle('width',mfwidth);
		this.mainframe.setStyle('height',mfheight);
		if(mfwidth != totalWidth)
			this.mainframe.setStyle('left',(hcenter - (mfwidth / 2)));
		else
			this.mainframe.setStyle('left',0);
		
		if(mfheight != totalHeight)
			this.mainframe.setStyle('top',((vcenter) - (mfheight / 2)));
		else
			this.mainframe.setStyle('top',0);
		
		this.mainframe.inject(this.container);

		this.banner = new Element ('div',{'class':'banner'});
		this.banner.setStyle('background-image','url(' + this.config.banner + ')');
		if(this.config.bannerlogo){
			this.logo = new Element ('img',{'src':this.config.bannerlogo,'class':'logo'});
			this.logo.inject(this.banner);
		}
		this.banner.inject(this.mainframe);
		this.bannertext = new Element('div',{'class':'logotext','text':this.config.bannertext});
		this.bannertext.inject(this.banner);
		this.bannersub = new Element('div',{'class':'smallbox','html':this.config.bannersubtext});
		this.bannersub.inject(this.bannertext);

		this.navibar = new Navibar(this);
		this.navi = this.navibar.navibar;
		this.navibar.loadNaviItems();
		this.navibar.createNaviItems();
		
		this.content = new Element('div',{'class':'content'});
		this.content.setStyle('width',mfwidth);
		this.content.setStyle('height',(mfheight - this.navi.getCoordinates().height - this.banner.getCoordinates().height ));
		this.content.setStyle('top',this.navi.getCoordinates(this.mainframe).bottom);
		this.content.inject(this.mainframe);
		this.navi.fireEvent('contentReady');
		this.showPage(1);
 		if(this.getCookie('user')){
			this.userCookie = Cookie.read('bwa.user');
 			this.loadModule("Admin");
 		}
		this.mainframe.addEvent('moduleLoaded',function(name){
			switch(name){
				case 'Admin': this.adminModule = new AdminModule(this);
							  this.navibar.addAdminModule(this.adminModule);
							  break;
			}
			
		}.bindWithEvent(this));
		window.addEvent('anchor',function(ev){
			var tag = window.location.hash.split('#')[1];
			this.showPage(tag);
		}.bindWithEvent(this));
		
	},
 

	loadConfig:function(){
		this.req = new Request.JSON({
			url:'lib/requestAdapter.php',
			async:false,
			data: 'request=loadConfig&component=default',
			onSuccess: function (responseJSON){
				this.config = responseJSON;
			}.bindWithEvent(this),
		});
		this.req.send();
	},

	getNaviObjects: function(){
		this.getObjectsRequest = new Request.JSON({
			async: false,
			url: 'lib/requestAdapter.php',
   			data: "request=getNaviObjects",
			onSuccess: function(responseJSON){
				this.objects = responseJSON.data;
			}.bindWithEvent(this),
		});
		this.getObjectsRequest.send();

	},
	
	createNaviObjects: function (objs){
		this.navi.set('html','');
		var navitable = new Element('table',{'class':'navitable','border':0}).inject(this.navi);
		var navirow = new Element('tr').inject(navitable);
		objs.each(function (it) {
			if(it.page_parent == 0){
				
			
			var navicol = new Element('td',{'valgin':'middle'}).inject(navirow);
			var naviobj = new Element('naviobj',{
								'id':'naviButton' + it.page_name,
								'html':unescape(decodeURIComponent(it.page_name).trim())
						  }).inject(navicol);
			naviobj.addEvent('click',function(ev){
				
				this.showPage(it.page_id);
			}.bindWithEvent(this));
			}
		}.bindWithEvent(this));
		return objs;
	},

	createContent: function(content){
		this.content.set('html',content.header);
	},

	showPage: function (pageid,json) {
		if(!pageid)
			return;

		if(!json){
			var getPageReq = new Request.JSON({
				url: 'lib/requestAdapter.php',
				data: 'request=getPageContent&page_id=' + pageid,
				onSuccess: function(responseJSON){
									
					this.content.set('html',"<div style=\"z-index:1;padding:10px;font-size:12px;\">" +  decodeURIComponent(responseJSON.data.page_content) + "</div>");
// 					this.loader.style.display = 'none';
				}.bindWithEvent(this),
			});
// 			this.loader.fade('in');
			getPageReq.send();
		}
	},

	initKeyboard: function(){
		this.keyboardEvents = new Keyboard({
    		defaultEventType: 'keydown'
		});
		this.keyboardEvents.addEvents({
    		'ctrl+shift+alt+l': function(){
				if(!this.getCookie('user'))
					this.login();
				else{
					this.adminModule.user = "";
					Cookie.dispose('bwa.user');
					this.navibar.clearNavibar();
					this.navibar.loadNaviItems();
					this.navibar.createNaviItems();
				}
    		}.bindWithEvent(this)
		});
	},

	login: function(){
		this.loginPopup = this.popups.createPopup(this.mainframe,'Login')
    	this.popups.showLogin(this.loginPopup);
    	$('loginname').focus();
    	this.loginPopup.popupBase.addEvent('loginRequest',function(params){
			this.loginRequest = new Request.JSON({
				url: 		'lib/login.php',
				async:		false,
				onSuccess: 	function(responseJSON){
								if ((responseJSON.error == 0) && responseJSON){
									this.notifier.showNote("Login successful.",responseJSON.msg);
									this.userCookie = this.setCookie('user',responseJSON.data);
									this.loadModule('Admin');
								}else{
									this.notifier.showNote("Login failed.",responseJSON.msg);
									
								}
								this.loginPopup.popupBase.dispose();
								this.mainframe.fade(1);
								
							}.bindWithEvent(this),
			});
		this.loginRequest.send('request=login&' + params);
    	}.bindWithEvent(this));
		this.loginPopup.popupFrame.fade('in');
	},

	loadModule: function(modname){
		Asset.javascript("src/" + modname + ".js",{
			'events':{
				'load':function(){
					this.mainframe.fireEvent('moduleLoaded',modname);
				}.bindWithEvent(this),
			}
		});
	},

});
