/**
/ This controller is responsable to create, load and build view's layer content.
/ @Package: FireBrick
/ @Author: upa
/ @Last modify: May 15, 2008
/ @Charset: utf-8
/*/

function ViewController() {
	// Constructor
	
		this.rootPath = "/";
		this.mainMenuListElement;
	
	// End of Constructor
	
	// Methods
	
		/**
		/ Does a deep reload in the document
		/*/
		this.reloadDocument = function() {
			window.location.reload(true);
		};
		
		/**
		/ Sets the document title.
		/*/
		this.setDocumentTitle = function(title) {
			document.title = title;
		};
		
		/**
		/ Loads a document specified by the documentURL parameter.
		/*/
		this.loadDocument = function(documentURL) {
			window.location.assign(documentURL);
		};
		
		/**
		/ Transforms a XML response in a XHTML fragment (uses the XMLTransformer controller)
		/*/
		this.transformResponse = function(XMLResponse,XSLFileURL,container) {
			
			// XMLTransformer controller
			var xmlTransformer = new XMLTransformer();
			
			// Gets the XHTML fragment
			var fragment = xmlTransformer.transformToFragment(XMLResponse,XSLFileURL);
			
			// Checks the UA
			if(window.ActiveXObject) {
				
				// The fragment is a string, sets the innerHTML property
				container.innerHTML += fragment;	
			}
			else {
				// The fragment is a DocumentFragment, appends it
				container.appendChild(fragment);
			}
		};
		
		/**
		/ Pre-loads the main menu images.
		/*/
		this.preloadMainMenu = function() {
			
			// Sets the mainMenuListElement property
			this.mainMenuListElement = document.createElement("ul");
			
			// Creates the itens
			var item1 = this.createMainMenuItem("home-off.gif","home-on.gif","index.html","Clique para ir para a página principal.");
			var item2 = this.createMainMenuItem("quem-somos-off.gif","quem-somos-on.gif","quem-somos/index.html","Clique para conhecer nossa empresa.");
			var item3 = this.createMainMenuItem("tratamentos-off.gif","tratamentos-on.gif","tratamentos/index.html","Clique para conhecer os tratamentos oferecidos.");
			var item4 = this.createMainMenuItem("weekend-clinic-off.gif","weekend-clinic-on.gif","weekend-clinic/index.html","Clique para conhecer este serviço exclusivo.");
			var item5 = this.createMainMenuItem("contato-off.gif","contato-on.gif","contato/index.html","Clique para entrar em contato conosco.");
			
			// Appends the itens within the list
			this.mainMenuListElement.appendChild(item1);
			this.mainMenuListElement.appendChild(item2);
			this.mainMenuListElement.appendChild(item3);
			this.mainMenuListElement.appendChild(item4);
			this.mainMenuListElement.appendChild(item5);
		};
		
		/**
		/ Builds the main menu.
		/*/
		this.buildMainMenu = function(container) {
			
			// Appends the mainMenuListElement within the container
			container.appendChild(this.mainMenuListElement);
		};
		
		/**
		/ Builds the site address.
		/*/
		this.buildSiteAddress = function(container) {
			
			// Response handler
			function handleResponse(TextResponse) {
				
				// Checks the text response
				if(TextResponse) {
					
					// Sets the container innerHTML property
					container.innerHTML = TextResponse;					
				}
				else {
					throw("Falha na construção do rodape do site");
				}
			};
			
			// Request controller
			var requestController = new RequestController();
			requestController.configure(null,this.rootPath + "data/address.html",true,2,handleResponse);
			requestController.send();
		};
		
		/**
		/ Build the home's treatments list.
		/*/
		this.buildHomeTreatmentsList = function(container) {
			
			// this reference (bind problem)
			var t = this;
			
			// Response handler
			function handleResponse(XMLResponse) {
				
				// Checks the XML response
				if(XMLResponse) {
					
					// Transforms the XML
					t.transformResponse(XMLResponse,t.rootPath + "xsl/treatments-list-home.xsl",container);
				}
				else {
					throw("Falha na construção da lista de tratamentos.");	
				}
			};
			
			// Request controller
			var requestController = new RequestController();
			requestController.configure(null,this.rootPath + "data/treatments.xml",true,1,handleResponse);
			requestController.send();
		};
		
		/**
		/ Build the site map's treatments list.
		/*/
		this.buildSiteMapTreatmentsList = function(container) {
			
			// this reference (bind problem)
			var t = this;
			
			// Response handler
			function handleResponse(XMLResponse) {
				
				// Checks the XML response
				if(XMLResponse) {
					
					// Transforms the XML
					t.transformResponse(XMLResponse,t.rootPath + "xsl/treatments-list-site-map.xsl",container);
				}
				else {
					throw("Falha na construção da lista de tratamentos.");	
				}
			};
			
			// Request controller
			var requestController = new RequestController();
			requestController.configure(null,this.rootPath + "data/treatments.xml",true,1,handleResponse);
			requestController.send();
		};
		
		/**
		/ Build the treatments list.
		/*/
		this.buildTreatmentsList = function(container) {
			
			// this reference (bind problem)
			var t = this;
			
			// Response handler
			function handleResponse(XMLResponse) {
				
				// Checks the XML response
				if(XMLResponse) {
					
					// Transforms the XML
					t.transformResponse(XMLResponse,t.rootPath + "xsl/treatments-list.xsl",container);
				}
				else {
					throw("Falha na construção da lista de tratamentos.");	
				}
			};
			
			// Request controller
			var requestController = new RequestController();
			requestController.configure(null,this.rootPath + "data/treatments.xml",true,1,handleResponse);
			requestController.send();
		};
		
		/**
		/ Build the quick treatments list.
		/*/
		this.buildTreatmentsQuickList = function(container) {
			
			// this reference (bind problem)
			var t = this;
			
			// Response handler
			function handleResponse(XMLResponse) {
				
				// Checks the XML response
				if(XMLResponse) {
					
					// Transforms the XML
					t.transformResponse(XMLResponse,t.rootPath + "xsl/treatments-quick-list.xsl",container);
				}
				else {
					throw("Falha na construção da lista de tratamentos.");	
				}
			};
			
			// Request controller
			var requestController = new RequestController();
			requestController.configure(null,this.rootPath + "data/treatments.xml",true,1,handleResponse);
			requestController.send();
		};
		
		/**
		/ Create a main menu item.
		/*/
		this.createMainMenuItem = function(imageOffSrc,imageOnSrc,href,tip) {
			
			// Images directory
			var imagesDirectory = this.rootPath + "images/main-menu/";
			
			// Creates the image elements
			var image = new Image();
			var imageOn = new Image();
			
			// Configures the images
			image.src = imagesDirectory + imageOffSrc;
			imageOn.src = imagesDirectory + imageOnSrc;
			
			// Sets the image user-defined properties
			image.off = image.src;
			image.on = imageOn.src;
			
			// Sets the rollover behavior
			image.onmouseover = function() {
				this.src = this.on;	
			};
			
			image.onmouseout = function() {
				this.src = this.off;	
			};
			
			// Creates the a element
			var aElement = document.createElement("a");
			
			// Configures the a element
			aElement.href = this.rootPath + href;
			aElement.title = tip;
			
			// Appends the off image
			aElement.appendChild(image);
			
			// Creates the list item element
			var listItemElement = document.createElement("li");
			
			// Appends the aElement within the listItemElement
			listItemElement.appendChild(aElement);
			
			// Returns the list item element
			return(listItemElement);
		};
	
	// End of Methods

}
