/* javascript components: antegram*/

/*************
class Antegram
*************/

Antegram.prototype.view;
Antegram.prototype.getView=function(){return this.view;};
Antegram.prototype.model;
function Antegram(){
	
	this.model	=	new MainModel();
	this.view		=	new MainView(this.model);
};

Antegram.main=function(){
	var app				=	new Antegram();
	var viewRoot	=	Browser.getRootComponent();
	//
	viewRoot.add(app.getView().getView());
};

/* javascript components: model*/

/**************
class MainModel
**************/

MainModel.prototype.pages;
function MainModel(){
	
	this.pages=new Array();
	
	var e=XML.load("data/antegram.xml");
	var children=e.getElementsByTagName("section");
	for(var i=0;i<children.length;i++){
		this.pages[i]=new Page(children[i]);
	}
	
};

MainModel.prototype.getPage=function(index){
	return this.pages[index];
	
};
MainModel.prototype.getPageCount=function(){
	return this.pages.length;
	
};

/*********
class Page
*********/

Page.prototype.name;
Page.prototype.source;
Page.prototype.content;
Page.prototype.getContent=function(){return this.content;};
function Page(e){
	
	
	this.name=e.getAttribute('name');
	this.source=e.getAttribute('src');
	if(this.source==null)this.source=this.name+".xml";
	this.source="data/"+this.source;
	this.content=XML.load(this.source);
	
};
Page.prototype.toString=function(){
	return this.name;
	
};

/* javascript components: view*/

/*************
class MainView
*************/

MainView.prototype.menuModel;
MainView.prototype.menu;
MainView.prototype.viewport;
MainView.prototype.view;
MainView.prototype.getView=function(){return this.view;};
MainView.prototype.logo;
function MainView(model){
	
	this.model=model;
	this.initComponents();
	this.initActions();
	this.initLayout();
	this.showPage(model.getPage(0));
	
};
MainView.prototype.showPage=function(page){
	var e=page.getContent();
	var output=this.format(e);
	//alert(output);
	this.viewport.setHTMLContent(output);
	
};
MainView.prototype.valueChanged=function(e){
	var page=e.getSource();
	this.showPage(page);
	
};
MainView.prototype.format=function(e){
	//alert(e.nodeName);
	var type=e.nodeName;
	if(type=="#text"){
		return e.data;
	}
	var output="";
	if(type!="#document"){
		var tag=e.tagName;
		var attributes=e.attributes;
		output+="<"+tag;
		if(attributes!=null)for(var i=0;i<attributes.length;i++){
			output+=" "+attributes[i].name+"="+attributes[i].value;
		}
		output+=">";
	}
	
	var children=e.childNodes;
	for(var i=0;i<children.length;i++){
		var child=children[i];
		output+=this.format(child);
	}
	
	if(type!="#document"){
		output+="</"+tag+">";
	}
	
	return output;
	
};
MainView.prototype.initComponents=function(){
	this.viewport		=	new Block();
	this.menu				=	new HList();
	this.menuModel	=	new MainMenuModel(this.model);
	this.view				=	new Container("div");
	this.link				=	new Block("<img src='img/oogtech.png'>");
	this.link.setClass("logo");
	this.menu.setModel(this.menuModel);
	
};

MainView.prototype.initActions=function(){
	this.menu.addListSelectionListener(this);
	
};

MainView.prototype.initLayout=function(){
	this.view.add(this.link,this.menu,this.viewport);
	this.view.setClass("content");
	this.viewport.setClass("viewport");
	this.menu.setClass("menu");
};

/******************
class MainMenuModel
******************/

MainMenuModel.prototype.model;
function MainMenuModel(model){
	
	this.model=model;
	
};
MainMenuModel.prototype.getSize=function(){
	return this.model.getPageCount();
	
};

MainMenuModel.prototype.getElementAt=function(index){
	return this.model.getPage(index);
	
};
