/*

Franklin David Lancaster, 2009


*/

//This function should handle everything! Including adding the applications to the page -> calling a new app should just generate the app not append it...
var sectionDefaults = [
		{name:'Home', root:'/', res:'/home/index.php', view:''},
		{name:'Projects', root:'/projects/', res:'/lib/php/project_functions.php'},
		{name:'Library', root:'/library/', res:'/library/library.php'},
		{name:'Contact', root:'/contact/', res:'/contact/index.php'},
		{name:'Login', root:'/login/', res:'/about/index.php', view:''}];
	

function urlEval(){

	var url = window.location.toString();
	var index = 0;
	
	if(url.split('#')[1]) {
		var urlparts = url.split('#');
		var path = urlparts[1];
		var parts = path.split('/');
		
		if(parts[0] == '') parts.shift();
		
		sectionDefaults.each(function(item,i){
		
			var root = item.root.split('/');
			root.erase(''); //clean up the array		
			
			if(root[0] == parts[0]) { index = i; }
		});
	}
	
	return index;

}

//page manager is the controller

var pageManager2 = new Class({
	Implements: [Options, Events],
	options:{
		lang:1,
		languageURL: '/lib/php/localisation.php'
	},
	
	initialize: function(parent, options){
		var self = this;
		this.setOptions(options);
		if(!parent) var parent=$(document.body);
		
		var wrapper = new Element('div', {'class':'wrapper', 'styles': {'width':1000, 'min-height':'100%', 'height':'auto !important', 'height':'100%','margin':'0 auto -30px', 'position':'relative', 'overflow':'hidden'}});
	
		var main = new Element('div', {'styles': {'width':1000, 'height':'80%', 'margin':'0 auto 0', 'background':'#f0eff9'}});
	
		var headerDiv  = new Element('div',{'styles': {'width':1000}});
		var sidebarDiv  = new Element('div', {'styles': {'width':150, 'height':'100%', 'margin':0, 'float':'left'}});

		this.content = new Element('div', {'styles': {'width':850, 'height':'100%', 'margin':0, 'float':'left', 'overflow':'hidden', 'overflow-y':'auto'}});
	
		var footer = new Element('div', {'class':'footer', 'styles': {'height':30}});
		var push = new Element('div', {'class':'push', 'styles' : {'height':30}});
	
		main.adopt([sidebarDiv, this.content]);
		wrapper.adopt(headerDiv, main, push);
		
		this.SideNav = new sideBar2({width:150, itemWidth:120});
		sidebarDiv.grab(this.SideNav);
		this.SideNav.addEvent('change', function(id,view){ self.displayItem(id,view); });
	
		this.Header = new header2(headerDiv);
		this.Header.addEvent('changeSection', function(id){ self.changeSection(id); });
		this.Header.addEvent('changeLanguage', function(id){ self.changeLanguage(id); });
			
		parent.adopt([wrapper, footer]);
		
		this.getLanguages();
	},
	
	getLanguages: function(){
	
		var self = this;
	
		new Request.JSON({url: this.options.languageURL, onSuccess: function(result){
			self.oLanguages = result;
			self.Header.loadLanguages(result);
			self.changeLanguage(self.options.lang);
		 }}).get();
	},
	
	displayItem: function(itemId, view){

		if(this.currentView) this.currentView.clear();	
		var self = this;
		switch(view) {
  			case 1:
  				
  				this.currentView = new projectViewer2(this.content);

  				new Request.JSON({url: this.currentSectionObject.res, onSuccess: function(project){
   					self.currentView.display(project);
   					self.updateUrl(project[0].itemName);
   				}}).get({'id': itemId,'language':this.currentLanguageID});
  				
  				
  			break;
 			case 2:
    			//Link view (This view has been depricated. Links are now direct from case 6 or 7
    			this.currentView = new linkView({first:itemId, parent:this.content, pageManager:this});
  				this.currentView.init();
    		break;
			case 3:
    			//RSS view
    			this.currentView = new rssView({item:itemId, parent:this.content, initiator:this, section:this.currentSectionObject});
  				this.currentView.init();
    		break;
			case 4:
    			//Document view
    			this.currentView = new pageView({item:itemId, parent:this.content, initiator:this, section:this.currentSectionObject});
  				this.currentView.init();
			break;
			case 5:
    			//Map view
    			this.currentView = new mapView2(this.content);
    			
    			new Request.JSON({url: this.currentSectionObject.res, onSuccess: function(data){
   					self.currentView.display(data);
   					self.updateUrl(data[0].itemName);
   				}}).get({'id': itemId,'language':this.currentLanguageID});
  				//this.currentView.init();
    		break;
    		case 6:
    			//Link Overview
    			this.currentView = new menuSubItems2(this.content, {thumbnail:true, description:true, headerText:true, width:'100%'});
   				
   				this.currentView.addEvent('change', function(id){ self.SideNav.changeItem(id); });
   				
   				var currentObject = this.SideNav.getCurrentObject();
   				
   				new Request.JSON({url: self.currentSectionObject.res, onSuccess: function(result){ self.currentView.createHeader(result); }}).get({'id': currentObject.id,'language':this.currentLanguageID});
   				
   				if($type(currentObject.children) == 'array') {
					currentObject.children.each(function(item){
						new Request.JSON({url: self.currentSectionObject.res, onSuccess: function(result){ 
							//console.log(result);
							self.currentView.subLink(result);
						}}).get({'id': item, 'language':self.currentLanguageID});
					});
   				}	
    			
    		break;
    		case 7:
    			//menu preview
    			this.currentView = new menuSubItems2(this.content, {description:true, headerText:false, externalLinks:true, width:'100%'});
   				
   				var currentObject = this.SideNav.getCurrentObject();
   				
   				new Request.JSON({url: self.currentSectionObject.res, onSuccess: function(result){ self.currentView.createHeader(result); }}).get({'id': currentObject.id, 'language':this.currentLanguageID});
   				
   				if($type(currentObject.children) == 'array') {
					currentObject.children.each(function(item){
						new Request.JSON({url: self.currentSectionObject.res, onSuccess: function(result){ 
							//console.log(result);
							self.currentView.subLink(result);
						}}).get({'id': item, 'language':self.currentLanguageID});
					});
   				}	
  			default:
  				//default view
  			break;
		}
	
	},
	
	changeSection: function(sectionID){
	
		var self = this;		
		
		new Request.JSON({url: sectionDefaults[sectionID].res, noCache:true, method:'get', onSuccess: function(menuData){
			
			if(self.currentView) self.currentView.clear();
			self.currentSectionObject = {
				'sectionID':sectionID,
				'name':self.currentSections[sectionID],
				'root':'/' + self.currentSections[sectionID] + '/',
				'res': sectionDefaults[sectionID].res
			};
			self.SideNav.loadMenu(menuData);
			self.Header.highlight(sectionID);
			
   		}}).get({'language':this.currentLanguageID});
	
	},
	
	changeLanguage: function(languageID){
	
		var self = this;
		
		this.oLanguages.each(function(item, i){
		
			if(item.id == languageID) {
				self.currentLanguageID = languageID;
				self.currentLanguageCode = item.code;
				self.currentSections = item.sections;
				self.Header.changeMenu(item.sections, languageID);
				if(self.currentSectionObject) self.changeSection(self.currentSectionObject.sectionID);
				else self.changeSection(urlEval());
			}
		
		}, this);
	},
	
	updateUrl: function(itemName){
	
		var newURL = '/' + this.currentLanguageCode.toLowerCase() + '/' + this.currentSectionObject.name.toLowerCase().replace(' ', '-') + '/' + itemName.toLowerCase().replace(' ', '-');
		window.location.hash = newURL;
		//console.log(this.currentSectionObject.name);
		//console.log(itemName.toLowerCase().replace(' ', '-'));
	}
	
});

/*
		
function pageManager(){

	var me = this;
	
	var wrapper = new Element('div', {'class':'wrapper', 'styles': {'width':1000, 'min-height':'100%', 'height':'auto !important', 'height':'100%','margin':'0 auto -30px', 'position':'relative', 'overflow':'hidden'}});
	
	var main = new Element('div', {'styles': {'width':1000, 'height':'80%', 'margin':'0 auto 0', 'background':'#f0eff9'}});
	
	var headerDiv  = new Element('div',{'styles': {'width':1000}});
	var sidebarDiv  = new Element('div', {'styles': {'width':150, 'height':'100%', 'margin':0, 'float':'left'}});
	//var content = new Element('div', {'styles': {'width':850, 'margin':0}});

	var content = new Element('div', {'styles': {'width':850, 'height':'100%', 'margin':0, 'float':'left', 'overflow':'hidden', 'overflow-y':'auto'}});
	
	var footer = new Element('div', {'class':'footer', 'styles': {'height':30}});
	var push = new Element('div', {'class':'push', 'styles' : {'height':30}});
	
	this.SideNav;
	this.Header;
	
	this.currentView;
	this.currentSection;
		
	this.init = function(){
	
		//evaulate URL on page launch
		
		//contentFrame.grab(content);
		main.adopt([sidebarDiv, content]);
		wrapper.adopt(headerDiv, main, push);
		
		me.SideNav = new sideBar({container:sidebarDiv, manager:me, width:150, itemWidth:120});
		me.SideNav.init();
		
		me.Header = new header({parent:headerDiv});
		me.Header.init();
		
		me.changeSection(urlEval());
				
		$(document.body).adopt([wrapper, footer]);		
	
	
	};
	
	this.displayItem = function(itemId, view){
	
		if(me.currentView) me.currentView.clear();
		
		switch(view) {
  			case 1:
  				//Text slideshow view
  				me.currentView = new projectViewer({first:itemId, parent:content, section:me.currentSection});
  				me.currentView.init();
  			break;
 			case 2:
    			//Link view (This view has been depricated. Links are now direct from case 6 or 7
    			me.currentView = new linkView({first:itemId, parent:content, pageManager:me});
  				me.currentView.init();
    		break;
			case 3:
    			//RSS view
    			me.currentView = new rssView({item:itemId, parent:content, initiator:this, section:me.currentSection});
  				me.currentView.init();
    		break;
			case 4:
    			//Document view
    			me.currentView = new pageView({item:itemId, parent:content, initiator:this, section:me.currentSection});
  				me.currentView.init();
			break;
			case 5:
    			//Map view
    			me.currentView = new mapView({first:itemId, parent:content, section:me.currentSection});
  				me.currentView.init();
    		break;
    		case 6:
    			//Link Overview
    			me.currentView = new menuSubItems(me.SideNav.currentItem.data, {parent:content, thumbnail:true, description:true, headerText:true, width:'100%'});
    			me.currentView.init();
    		break;
    		case 7:
    			//menu preview
    			me.currentView = new menuSubItems(me.SideNav.currentItem.data, {parent:content, description:true, headerText:false, externalLinks:true, width:'100%'});
    			me.currentView.init();
    			
  			default:
  				//default view
  			break;
		}
		
	
	
	};
	
	this.changeSection = function(sectionID){
	
		//if there is a sidebar then changeitems
	
		//check that sectionID does not equal currentSection
		
		//tell menu bar application to load items
		if(me.currentView) me.currentView.clear();
		
		me.currentSection = sections[sectionID];
		
		//alert(SideNav);
		me.Header.highlight(sectionID);
		
		me.SideNav.currentSection = me.currentSection;
		me.SideNav.loadMenu(0);
		
		me.SideNav.onload = function(){ 
			me.SideNav.currentItem = me.SideNav.menuItems[0];
			me.SideNav.changeItem(me.SideNav.menuItems[0].data.id);
		};
		
		
		
		//wrapper.grab(main);
	
	};



}
*/
