var enable_controls = false;
var larguraConteudo;
var larguraJanela;

function scroll_init(id, largura, altura, controles) {
	var tag = _get(id);	
	if (tag) {
		
		var div1 = document.createElement('div');	
		var div2 = document.createElement('div');	
		div1.style.width = largura + 'px';
		div1.style.height = altura + 'px';
		div1.style.overflow = 'hidden';	
		div1.appendChild(div2);	
		if (tag.hasChildNodes()) {
			while (tag.hasChildNodes())
				div2.appendChild(tag.removeChild(tag.firstChild))
			tag.appendChild(div1);
		}
		else {
			tag.parentNode.replaceChild(div1, tag);
			div2.appendChild(tag);
		}
		
		larguraConteudo = div1.scrollWidth;
		larguraJanela = largura;
				
		tag.style.display = '';
//		if (controles && largura >= (window.opera ? 2000 : div2.scrollWidth) && altura >= div2.scrollHeight) {
		if (controles && larguraConteudo <= larguraJanela) { //&& altura >= div2.style.height) {
			controles = controles.split(/\W/);
			for (var i in controles)
//				_get(controles[i]).style.visibility = 'visible';
				_get(controles[i]).style.visibility = 'hidden';
		}
	}
}

function scroll_start(id, horizontal, vertical) {
//	if (enable_controls) {

	var tag = _get(id);
	var div1, div2;

	if (tag.hasChildNodes())

		div2 = (div1 = tag.firstChild).firstChild;
	else
		div1 = (div2 = tag.parentNode).parentNode;


//	var largura = (window.opera ? 2000 : div2.scro1lWidth) - parseInt(div2.style.width);
//	var altura = div1.scrollHeight - parseInt(div2.style.height);
	
	var altura = div1.scrollHeight;

	if (tag.scroll_timer) window.clearInterval(tag.scroll_timer);
	tag.scroll_timer = window.setInterval(
		function() {
			var esquerda = parseInt(div2.style.marginLeft) || 0;
			var topo = parseInt(div2.style.marginTop) || 0;
			
		/*	if (largura < 0 || esquerda - horizontal >= 0)
				horizontal = esquerda;
			else if (largura + esquerda - horizontal < 0)
				horizontal = largura + esquerda;
							
			if (altura < 0 || topo - vertical >= 0)
				vertical = topo;
			else if (altura + topo - vertical < 0)
				vertical = altura + topo;
		*/	
		
			//alert(larguraJanela);
			//alert(larguraConteudo);
		
			if (horizontal > 0){ // botao da direita
				if (Math.abs(esquerda - horizontal) >= (larguraConteudo - larguraJanela)){
					div2.style.marginLeft = -(larguraConteudo - larguraJanela) + 'px';
				} else {
					div2.style.marginLeft = esquerda - horizontal + 'px';
				}
			} else if (horizontal < 0){
				if (esquerda - horizontal >= 0){
					div2.style.marginLeft = 0 + 'px';
				} else {
					div2.style.marginLeft = esquerda - horizontal + 'px';
				}
			}
			
			div2.style.marginTop = topo - vertical + 'px';
		}, 100);
//	}
}

function scroll_stop(id) {
	var tag = _get(id);
	if (tag.scroll_timer) window.clearInterval(tag.scroll_timer);
	tag.scroll_timer = '';
}

function _get(id) {
	return document.getElementById && document.getElementById(id) || document.all && document.all[id];
}











var enable_controls_edc = false;
var larguraConteudoEdc;
var larguraJanelaEdc;

var alturaConteudoEdc;
var alturaJanelaEdc;


function scroll_init_Edc(idEdc, larguraEdc, alturaEdc, controlesEdc) {
	var tagEdc = _get_edc(idEdc);	
	if (tagEdc) {
		
		var div1Edc = document.createElement('div');	
		var div2Edc = document.createElement('div');	
		div1Edc.style.width = larguraEdc + 'px';
		div1Edc.style.height = alturaEdc + 'px';
		div1Edc.style.overflow = 'hidden';	
		div1Edc.appendChild(div2Edc);
		if (tagEdc.hasChildNodes()) {
			while (tagEdc.hasChildNodes())
				div2Edc.appendChild(tagEdc.removeChild(tagEdc.firstChild))
			tagEdc.appendChild(div1Edc);
		}
		else {
			tagEdc.parentNode.replaceChild(div1Edc, tagEdc);
			div2Edc.appendChild(tagEdc);
		}
		
		alturaConteudoEdc = div1Edc.scrollHeight;
		alturaJanelaEdc = alturaEdc;
				
		tagEdc.style.display = '';
//		if (controlesEdc && larguraEdc >= (window.opera ? 2000 : div2Edc.scrollWidth) && alturaEdc >= div2Edc.scrollHeight) {
		if (controlesEdc && alturaConteudoEdc <= alturaJanelaEdc) { //&& alturaEdc >= div2Edc.style.height) {
			controlesEdc = controlesEdc.split(/\W/);
			for (var i in controlesEdc)
//				_get_edc(controlesEdc[i]).style.visibility = 'visible';
				_get_edc(controlesEdc[i]).style.visibility = 'hidden';
		}
	}
}

function scroll_start_edc(idEdc, horizontalEdc, verticalEdc) {
//	if (enable_controls_edc) {

	var tagEdc = _get_edc(idEdc);
	var div1Edc, div2Edc;

	if (tagEdc.hasChildNodes())

		div2Edc = (div1Edc = tagEdc.firstChild).firstChild;
	else
		div1Edc = (div2Edc = tagEdc.parentNode).parentNode;


//	var largura = (window.opera ? 2000 : div2Edc.scro1lWidth) - parseInt(div2Edc.style.width);
//	var alturaEdc = div1Edc.scrollHeight - parseInt(div2Edc.style.height);
	
	var alturaEdc = div1Edc.scrollHeight;

	if (tagEdc.scroll_timer) window.clearInterval(tagEdc.scroll_timer);
	tagEdc.scroll_timer = window.setInterval(
		function() {
			var esquerdaEdc = parseInt(div2Edc.style.marginLeft) || 0;
			var topoEdc = parseInt(div2Edc.style.marginTop) || 0;
			
		/*	if (largura < 0 || esquerdaEdc - horizontalEdc >= 0)
				horizontalEdc = esquerdaEdc;
			else if (largura + esquerdaEdc - horizontalEdc < 0)
				horizontalEdc = largura + esquerdaEdc;
		*/					
			if (alturaEdc < 0 || topoEdc - verticalEdc >= 0)
				verticalEdc = topoEdc;
			else if (alturaEdc + topoEdc - verticalEdc < 0)
				verticalEdc = alturaEdc + topoEdc;
			
		
			//alert(larguraJanelaEdc);
			//alert(larguraConteudoEdc);
		
        
            if (verticalEdc > 0){ // botao da direita
				if (Math.abs(topoEdc - verticalEdc) >= (alturaConteudoEdc - alturaJanelaEdc)){
					//alert("topoEDC = "+topoEdc+" alturaConteudoEdc = "+alturaConteudoEdc+" alturaJanelaEdc = "+alturaJanelaEdc+" operação = "+(alturaConteudoEdc - alturaJanelaEdc));
					div2Edc.style.marginTop = -(alturaConteudoEdc - alturaJanelaEdc) + 'px';
				} else {
					
					div2Edc.style.marginTop = topoEdc - verticalEdc + 'px';
				}
			} else if (verticalEdc < 0){
				if (topoEdc - verticalEdc >= 0){
					div2Edc.style.marginTop = 0 + 'px';
				} else {
					div2Edc.style.marginTop = topoEdc - verticalEdc + 'px';
				}
			}
        
        
            //alert('Nao funciona');
            
			if (horizontalEdc > 0){ // botao da direita
				if (Math.abs(esquerdaEdc - horizontalEdc) >= (larguraConteudoEdc - larguraJanelaEdc)){
					div2Edc.style.marginLeft = -(larguraConteudoEdc - larguraJanelaEdc) + 'px';
				} else {
					div2Edc.style.marginLeft = esquerdaEdc - horizontalEdc + 'px';
				}
			} else if (horizontalEdc < 0){
				if (esquerdaEdc - horizontalEdc >= 0){
					div2Edc.style.marginLeft = 0 + 'px';
				} else {
					div2Edc.style.marginLeft = esquerdaEdc - horizontalEdc + 'px';
				}
			}
			
			//div2Edc.style.marginTop = topoEdc - verticalEdc + 'px';
		}, 100);
//	}
}

function scroll_stop_edc(idEdc) {
	var tagEdc = _get_edc(idEdc);
	if (tagEdc.scroll_timer) window.clearInterval(tagEdc.scroll_timer);
	tagEdc.scroll_timer = '';
}

function _get_edc(idEdc) {
	return document.getElementById && document.getElementById(idEdc) || document.all && document.all[idEdc];
}

