/*
Descrição: Funcções diversas javascript
Criado em: 22/03/2009
Autor: Hédi Carlos Minin - hedicarlos@gmail.com
*/

var key_pressed = 0;
var mouse = {"x":0,"y":0};

function getEvent(e){
	if(!e){ var e = window.event; }
	return e;
}

function setOpacity(obj, opacidade) { 
    obj.style.opacity = (opacidade / 100); 
    obj.style.MozOpacity = (opacidade / 100); 
    obj.style.KhtmlOpacity = (opacidade / 100); 
    obj.style.filter = 'alpha(opacity=' + opacidade + ')'; 
}

/*
function fade(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 
*/

/*
function moveMouse(e) {
	var e = getEvent(e);
	if(e.pageX || e.pageY){
		mouse_x = e.pageX;
		mouse_y = e.pageY;
	}else if(e.clientX || e.clientY){
		mouse_x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		mouse_y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
}
*/

function getMousePosition(e){
    if(document.all){
          mouse.x = event.clientX;
          mouse.y  = event.clientY;
    } else {
          mouse.x = e.clientX;
          mouse.y = e.clientY;
    }
}

function createElement(element){
	this.element = element;
	this.parent = document.body;
	this.id = false;
	this.title = false;
	
	this.Create = function(){
		var elem = document.createElement(this.element);	
		if(this.id){ 
			elem.setAttribute('id',this.id); 
		}
		if(this.title){ 
			elem.setAttribute('title',this.title); 
		}
		this.parent.appendChild(elem);	
		return elem;
	}
}

function removeElement(parent,obj){
	if(parent == false){ parent = document.body; }
	parent.removeChild(obj);
}

function addEvent(obj,evento,funcao){	
	if(document.addEventListener){
		obj.addEventListener(evento, funcao, true);	
	}else{ 
		obj.attachEvent('on' +evento,funcao);	
	}
}

function removeEvent(obj,evento,funcao){
	if(document.removeEventListener){
		obj.removeEventListener(evento, funcao, true);	
	}else{ 
		obj.detachEvent('on' +evento,funcao);	
	}	
}


function disableTextSelect(){
	document.onmousedown = function(){ return false; }	
    document.onselectstart = function(){ return false; }		
}

function enableTextSelect(){
	document.onmousedown = function(){ return true; }	
    document.onselectstart = function(){ return true; }		
}

function windowFocus(){
	if((document.all ) && (!window.opera)){
		document.onfocusout = function(){}
		document.onfocusin = function(){}
	}else{	
		window.onblur = function(){}
		window.onfocus = function(){}
	}	
}

function getPosition(obj) {
	var curleft = 0;
	var curtop = 0;
	var curheight = obj.offsetHeight;
	var curwidth = obj.offsetWidth;
	if(obj.offsetParent) {
		do{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}while(obj = obj.offsetParent);
	}	
	return {"left":curleft,"top":curtop,"height":curheight,"width":curwidth};
}

function getBodySize(){
	var user_y = document.documentElement.clientHeight;
	var user_x = document.documentElement.clientWidth;
	
	if (window.opera){ 
		user_y = document.body.clientHeight;
		user_x = document.body.clientWidth;
	}
	return {"width":user_x,"height":user_y};
}

function getBodyScroll(){
	
	var scroll_height = document.documentElement.scrollHeight;
	var scroll_width = document.documentElement.scrollWidth;
	var scroll_top = document.documentElement.scrollTop;
	
	if (window.opera){ 
		scroll_height = document.body.scrollHeight;
		scroll_width = document.body.scrollWidth;
		scroll_top = document.body.scrollTop;
	}
	return {"width":scroll_width,"height":scroll_height,"top":scroll_top};
}

function createBackgroundLayer(opacity, operation){
	var body_size = getBodySize();
	var body_scroll = getBodyScroll();
	
	if(!document.getElementById('background_layer')){
		var back_layer = document.createElement('div');

		back_layer.setAttribute('id','background_layer'); 
		document.body.appendChild(back_layer);
	}	
	
	var back_layer = document.getElementById('background_layer');
	
	
	var x =  body_scroll.width >  body_size.width ? body_scroll.width : body_size.width;
	var y =  body_scroll.height >  body_size.height ? body_scroll.height : body_size.height;
	
	back_layer.style.height = y+ 'px';		
	back_layer.style.width = x+ 'px';
	
	if(operation != false){
		back_layer.onclick = operation;
	}
	
	setOpacity(back_layer, opacity);
	back_layer.style.display = 'block';
}

function closeBackgroundLayer(){
	if(document.getElementById('background_layer')){
		document.getElementById('background_layer').style.display = 'none';	
	}
}

function flashControl(id){
	this.flash = document.getElementById(id);
	
	this.Play = function(){
		this.flash.Play();	
	}
	
	this.Rewind = function(){
		this.flash.Rewind();	
	}

	this.Stop = function(){
		this.flash.StopPlay();	
	}	
	
	this.Pause = function(){
		this.flash.PausePlay();	
	}
	//TotalFrames()
	//SetVariable( variableName, value )
	//PercentLoaded() 
	//IsPlaying()
	//GotoFrame( frameNumber ) 
}

function fadeOut(id, time) {
	target = document.getElementById(id);
	var alpha = 100;
	var timer = time;
	var i = setInterval(
			function() {
				if (alpha <= 0){
					clearInterval(i);
				}
				setOpacity(target, alpha);
				alpha -= 5;
			}, timer);
}

function fadeIn(id, time) {
	target = document.getElementById(id);
	var alpha = 0;
	var timer = time;
	var i = setInterval(
			function() {
				if (alpha >= 100){
					clearInterval(i);
				}
				setOpacity(target, alpha);
				alpha += 5;
			}, timer);
}


function getKeyPressed(e){
	if(document.all){
		key_pressed = window.event.keyCode;	
	}else{
		key_pressed = e.keyCode;	
	}	
}

addEvent(document,'mousemove',getMousePosition);
addEvent(document,'keydown',getKeyPressed);