﻿/*****************************************************************************
 * AlwaysVisible.js
 * por Wiliam Junio, dezembro de 2009
 *****************************************************************************/


AV_H_LEFT   = 1
AV_H_CENTER = 2
AV_H_RIGHT  = 3

AV_V_TOP    = 1
AV_V_CENTER = 2
AV_V_BOTTOM = 3

var AlwaysVisible = function() {	
	var __av_lastScrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
	var __av_lastScrollTop = window.pageYOffset || document.documentElement.scrollTop;

	var __av_controls = new Array();

	var __AV_SCROLL_THRESHOLD = 1;
    var __AV_DETECT_SCROLL_INTERVAL = 100;
    
	/**************************************************************/
	/* Rotinas de posicionamento da janela, documento e elementos */
	/**************************************************************/

	var GetAbsoluteX = function(el) {
		var x = 0;
		
		while (el) {
			x += el.offsetLeft;
			el = el.offsetParent;
	 	}

		return x;
	}

	var GetAbsoluteY = function(el) {
		var y = 0;
		
		while (el) {
			y += el.offsetTop;
			el = el.offsetParent;
	 	}

		return y;
	}

	var GetWindowWidth = function() {
		if (window.innerWidth)
			return window.innerWidth;
		else if (document.documentElement.clientWidth)
			return document.documentElement.clientWidth;
		else
			return 0;
	}

	var GetWindowHeight = function() {
		if (window.innerHeight)
			return window.innerHeight;
		else if (document.documentElement.clientHeight)
			return document.documentElement.clientHeight;
		else
			return 0;
	}

	var GetWindowScrollPosX = function() {
		if (window.pageXOffset)
			return window.pageXOffset;
		else if (document.documentElement.scrollLeft)
			return document.documentElement.scrollLeft;
		else
			return 0;
	}

	var GetWindowScrollPosY = function() {
		if (window.pageYOffset)
			return window.pageYOffset;
		else if (document.documentElement.scrollTop)
			return document.documentElement.scrollTop;
		else
			return 0;
	}
	
	/**************************************************************/
	/* Tipos                                                      */
	/**************************************************************/
	
	var AlwaysVisibleControl = function(element, hPosition, vPosition) {
	    this.element = element;
	    this.hPosition = hPosition;
	    this.vPosition = vPosition;
	    
	    this.RefreshPosition = function() {
	        // Torna elemento visível
	        //el.style.display = "";
	        //el.style.visibility = "visible";
	        this.element.style.position = "absolute";
	        
	        // Posição horizontal
	        var x = 0;
	        if (this.hPosition == AV_H_LEFT)
	            x = GetWindowScrollPosX();
	        else if (this.hPosition == AV_H_CENTER)
	            x = GetWindowScrollPosX() + (GetWindowWidth() - this.element.offsetWidth) / 2;
	        else if (this.hPosition == AV_H_RIGHT)
	            x = GetWindowScrollPosX() + GetWindowWidth() - this.element.offsetWidth;

	        // Posição vertical
	        var y = 0;
	        if (this.vPosition == AV_V_TOP)
	            y = GetWindowScrollPosY();
	        else if (this.vPosition == AV_V_CENTER)
	            y = GetWindowScrollPosY() + (GetWindowHeight() - this.element.offsetHeight) / 2;
	        else if (this.vPosition == AV_V_BOTTOM)
	            y = GetWindowScrollPosY() + GetWindowHeight() - this.element.offsetHeight;
	            
	                 
	        this.element.style.left = x + "px";
	        this.element.style.top = y + "px";
	    }
	}

	/***********************************************************/
	/* Rotinas de detecção de eventos e atualização de estados */
	/***********************************************************/

	var RefreshControlPositions = function() {
	}
	
	var AlwaysVisibleDetectScroll = function() {
		if ((Math.abs(GetWindowScrollPosX() - __av_lastScrollLeft) >= __AV_SCROLL_THRESHOLD) 
		|| (Math.abs(GetWindowScrollPosY() - __av_lastScrollTop) >= __AV_SCROLL_THRESHOLD)) {
			__pac_lastScrollLeft = GetWindowScrollPosX();
			__pac_lastScrollTop = GetWindowScrollPosY();
			
            for (var i=0; i < __av_controls.length; i++)
                __av_controls[i].RefreshPosition();
		}
	}	
	
	this.AddControl = function(elementId, hPosition, vPosition) {
	    el = document.getElementById(elementId);
	    __av_controls[__av_controls.length] = new AlwaysVisibleControl(el, hPosition, vPosition); 
	    __av_controls[__av_controls.length - 1].RefreshPosition();
	}
	
    // Inicia processo para detecção de scroll na janela
    setInterval(AlwaysVisibleDetectScroll, __AV_DETECT_SCROLL_INTERVAL);  				
}


