// fold < creditos & copyright >
/* +++++++ CREDITOS & COPYRIGHT +++++++ 

   Funções de Javascript para controlar o interface de Front-End do projecto Millions Wheels [www.millionswheels.com]
   Criadas e adaptadas por alvaro Saraiva @ glups.pt @ julho 2007
   Update por alvaro Saraiva @ glups.pt @ julho 2007
   
   Glups - Atelier de Novas Tecnologias, Lda
   Rua Jaime Campos, Bloco D, Piso 3 Esq., 5000-431 Vila Real | Portugal
   Tel. [351] 259 322 183 | E-mail: info@glups.pt | URL: www.glups.pt

   Se utilizarem estas funções para os vossos trabalhos,
   a Glups agradece que ao menos no retirem estas linhas de cdigo,
   por respeito e agradecimento ao trabalho das pessoas que a criaram
   e ao trabalho que vocs pouparam. Keep Cool. Peace ;)

*/

// endfold

// fold < notas >
/*	Notas:
	- Esta classe serve para controlar a apresentação de parte do interface do portal.
	- É responsável por receber pedidos do front-end (navegação, chamadas aos scripts via AJAX e receber respostas do AJAX;
	- Usa classes/métodos da superClasse Prototype.js
*/
// endfold


/*	+++++++++++++++++++++++++++++++++++++++++++++++
				CRIAÇÃO DA CLASSE 
	+++++++++++++++++++++++++++++++++++++++++++++++*/

// criamos uma variavel para representar a classe
var coreDivFlutuante = Class.create();

/*
*	@desc			Método 'prototype' da classe coreTmpl que define quais os métodos que torna público e disponíveis 
*					a quem instanciar esta classe
*	@retutn void
*/
coreDivFlutuante.prototype = {
	
	/*
	*	@desc			Método constructor 'prototype' da classe coreTmpl
	*					Define logo por defeito algumas variáveis privadas da classe
	*	@retutn void
	*/
	initialize: function(configBanner){ 
		
		this.bannerVertical = new Object();			// objecto da classe que vai ficar com as props do banner
		this._initBannerVertical(configBanner);
	},
	
	/*	++++++++++++++++++++++++++++++++++++++++++++++++++++
				SETTERS E GETTERS DE CONFIGS DA CLASSE 
		++++++++++++++++++++++++++++++++++++++++++++++++++++ */
	
		/* 
	* @desc				Método para controlar o banner vertical oscilante através do YPChaser
	*					Basta ser chamado num template que tenha o respectivo DIV, com um objecto com várias propriedades para desenhar o mapa:
						Para que a API funcione o site tem que estar registado no Google. Fazer registo em http://www.google.com/apis/maps/signup.html
	* @access			public
	* @param Array		Um array/objecto com as seguintes props:
						- sLayerId:
						- iTopOffset:
						- iSlideTime:
						- iCeiling:
						- iFloor
	* @return 			void
	*/
	setBannerVertical: function(configBanner){
		this._initBannerVertical(configBanner);
		/*
		var dadosBanner = '';
		for(var prop in configBanner){	dadosBanner += '\n-'+prop+': ' + configBanner[prop]; };
		alert(dadosBanner);
		*/
	},
	
	_initBannerVertical: function(configBanner) {
		
		this.bannerVertical.isIE 		= window.clientInformation ? true : false
		this.bannerVertical.isIEDTD 	= ((document.doctype && document.doctype.name.indexOf(".dtd")>-1) || document.compatMode == "CSS1Compat") ? true : false;
		this.bannerVertical.isN4 		= document.layers ? true : false
		this.bannerVertical.isN6 		= navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 5
		this.bannerVertical.isO5 		= navigator.userAgent.indexOf("Opera") != -1 && parseInt(navigator.appVersion) >= 4
		this.bannerVertical.registry 	= new Array( )
		this.bannerVertical.callRate 	= 10
		window.setInterval("divObj._bannerTimer()", this.bannerVertical.callRate);
		
		this.bannerVertical.chaserDiv	= null;
		this.bannerVertical.layerId		= configBanner['layer_id'];
		this.bannerVertical.topOffset	= configBanner['iTopOffset'];
		this.bannerVertical.slideTime	= configBanner['iSlideTime'];
		this.bannerVertical.ceiling		= configBanner['iCeiling'];
		this.bannerVertical.floor		= configBanner['iFloor'];
		this.bannerVertical.registry[this.bannerVertical.registry.length] = this.bannerVertical;
		
		/*
		//===========================================================
		// ypChaser class definition
		//===========================================================
		function ypChaser(sLayerId, iTopOffset, iSlideTime, iCeiling, iFloor) {
			this.chaserDiv = null
			this.layerId = sLayerId
			this.topOffset = iTopOffset
			this.slideTime = iSlideTime
			this.ceiling = iCeiling
			this.floor = iFloor
			ypChaser.registry[ypChaser.registry.length] = this
		}
		
		ypChaser.isIE = window.clientInformation ? true : false
		ypChaser.isIEDTD = ((document.doctype && document.doctype.name.indexOf(".dtd")>-1) || document.compatMode == "CSS1Compat") ? true : false;
		ypChaser.isN4 = document.layers ? true : false
		ypChaser.isN6 = navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 5
		ypChaser.isO5 = navigator.userAgent.indexOf("Opera") != -1 && parseInt(navigator.appVersion) >= 4
		ypChaser.registry = new Array( )
		ypChaser.callRate = 10
		window.setInterval("ypChaser.timer( )", ypChaser.callRate)
		
		ypChaser.timer = function() {
			for (var i = 0; chObj = this.registry[i]; i++) {
				if (!chObj.chaserDiv) chObj.attemptLoad()
				if (chObj.chaserDiv) chObj.main()
			}
		}
		
		ypChaser.prototype.attemptLoad = function() {
			var chDiv = null
			if (ypChaser.isN6 || ypChaser.isO5) chDiv = document.getElementById(this.layerId)
			else if (ypChaser.isIE) chDiv = document.all[this.layerId]
			else if (ypChaser.isN4) chDiv = document.layers[this.layerId]
			if (chDiv && chDiv != null) {
				this.chaserDiv = chDiv
			}
		}
		
		ypChaser.prototype.main = function( ){
			this.currentY = this.getCurrentY()
			var scrollTop = ypChaser.getWindowScroll() 
			var newTargetY = scrollTop + this.topOffset
			var floor = ypChaser.getDocumentHeight() - this.floor
			newTargetY = Math.max( newTargetY, this.ceiling)
			if (!ypChaser.isO5) newTargetY = Math.min(newTargetY, floor)
			if ( this.currentY != newTargetY ) {
				if ( newTargetY != this.targetY ) {
					this.targetY = newTargetY
					this.slideInit( )
				}
				this.slide( )
			}
		}
		
		ypChaser.prototype.slideInit = function( ){
			this.A = (this.targetY - this.currentY) / this.slideTime / this.slideTime
			this.startT = (new Date()).getTime()
			this.startP = this.getCurrentY()
			this.D = this.targetY - this.startP
		}
		
		ypChaser.prototype.slide = function( ){
			var elapsed = (new Date()).getTime() - this.startT
			if (elapsed < this.slideTime) {
				this.moveTo(this.D - (Math.round(Math.pow(this.slideTime - elapsed, 2) * this.A)) + this.startP)
			}
		}
		
		ypChaser.prototype.moveTo = function(ny) {
			if (ypChaser.isN4) this.chaserDiv.top = ny
			else this.chaserDiv.style.top = ny + "px"
		}
		
		ypChaser.prototype.getCurrentY = function() {
			var n = ypChaser.isN4 ? this.chaserDiv.top : parseInt(this.chaserDiv.style.top)
			return isNaN(n) ? 0 : n
		}
		
		ypChaser.getWindowScroll = function() {
			if (ypChaser.isIEDTD) {
				window.status=document.documentElement.scrollTop;
				return document.documentElement.scrollTop;
			}
			else if (ypChaser.isIE) return document.body.scrollTop;
			else return window.pageYOffset
		}
		
		ypChaser.getDocumentHeight = function() {
			if (ypChaser.isO5) return 0 // opera can't retreive this property.
			else if (ypChaser.isIEDTD) return Math.max(document.documentElement.scrollHeight, document.documentElement.offsetHeight)
			else if (ypChaser.isIE) return Math.max(document.body.scrollHeight, document.body.offsetHeight)
			else return window.document.height
		}
		
		*/
	},
	
	/*	++++++++++++++++++++++++++++++++++++++++++++++++++++
		  MÉTODO PARA CONTROLAR O BANNER LATERAL OSCILANTE 
		++++++++++++++++++++++++++++++++++++++++++++++++++++ */
	
	_bannerTimer: function() {
		for (var i = 0; chObj = this.bannerVertical.registry[i]; i++) {
			if (!chObj.chaserDiv) this._bannerAttemptLoad();
			if (chObj.chaserDiv) this._bannerMain();
		}
	},
	
	_bannerAttemptLoad: function() {
		var chDiv = null
		if (this.bannerVertical.isN6 || this.bannerVertical.isO5) chDiv = document.getElementById(this.bannerVertical.layerId);
		else if (this.bannerVertical.isIE) chDiv = document.all[this.bannerVertical.layerId];
		else if (this.bannerVertical.isN4) chDiv = document.layers[this.bannerVertical.layerId];
		if (chDiv && chDiv != null) {
			this.bannerVertical.chaserDiv = chDiv;
		}
	},
	
	_bannerMain: function(){
		this.bannerVertical.currentY = this.getCurrentY()
		var scrollTop = this.getWindowScroll() 
		var newTargetY = scrollTop + this.bannerVertical.topOffset
		var floor = this.getDocumentHeight() - this.bannerVertical.floor
		newTargetY = Math.max( newTargetY, this.bannerVertical.ceiling)
		if (!this.bannerVertical.isO5) newTargetY = Math.min(newTargetY, floor)
		if ( this.bannerVertical.currentY != newTargetY ) {
			if ( newTargetY != this.bannerVertical.targetY ) {
				this.bannerVertical.targetY = newTargetY
				this.slideInit()
			}
			this.slide()
		}
	},
	
	slideInit: function(){
		this.bannerVertical.A = (this.bannerVertical.targetY - this.bannerVertical.currentY) / this.bannerVertical.slideTime / this.bannerVertical.slideTime
		this.bannerVertical.startT = (new Date()).getTime()
		this.bannerVertical.startP = this.getCurrentY()
		this.bannerVertical.D = this.bannerVertical.targetY - this.bannerVertical.startP
	},
	
	slide: function(){
		//alert(this.bannerVertical.chaserDiv);
		var elapsed = (new Date()).getTime() - this.bannerVertical.startT
		if (elapsed < this.bannerVertical.slideTime) {
			this.moveTo(this.bannerVertical.D - (Math.round(Math.pow(this.bannerVertical.slideTime - elapsed, 2) * this.bannerVertical.A)) + this.bannerVertical.startP)
		}
	},
	
	moveTo: function(ny) {
		if (this.bannerVertical.isN4) this.bannerVertical.chaserDiv.top = ny
		else this.bannerVertical.chaserDiv.style.top = ny + "px"
	},
	
	getCurrentY: function() {
		var n = this.bannerVertical.isN4 ? this.bannerVertical.chaserDiv.top : parseInt(this.bannerVertical.chaserDiv.style.top)
		return isNaN(n) ? 0 : n
	},
	
	getWindowScroll: function() {
		if (this.bannerVertical.isIEDTD) {
			window.status=document.documentElement.scrollTop;
			return document.documentElement.scrollTop;
		}
		else if (this.bannerVertical.isIE) return document.body.scrollTop;
		else return window.pageYOffset
	},
	
	getDocumentHeight: function() {
		if (this.bannerVertical.isO5) return 0 // opera can't retreive this property.
		else if (this.bannerVertical.isIEDTD) return Math.max(document.documentElement.scrollHeight, document.documentElement.offsetHeight)
		else if (this.bannerVertical.isIE) return Math.max(document.body.scrollHeight, document.body.offsetHeight)
		else return window.document.height
	}
	
}
