function sizethis() 
{	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
}


var dhtmlgoodies_slideSpeed = 15;	// Higher value = faster
var dhtmlgoodies_timer = 50;	// Lower value = faster

var objectIdToSlideDown = false;
var dhtmlgoodies_activeId = false;
var dhtmlgoodies_slideInProgress = false;
function showHideContent(e,inputId)
{

	if(dhtmlgoodies_slideInProgress)return;
	dhtmlgoodies_slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('dhtmlgoodies_a' + numericId);

	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(dhtmlgoodies_activeId &&  dhtmlgoodies_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(dhtmlgoodies_activeId,(dhtmlgoodies_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,dhtmlgoodies_slideSpeed);
		}
	}else{
		slideContent(numericId,(dhtmlgoodies_slideSpeed*-1));
		dhtmlgoodies_activeId = false;
	}	
}

function slideContent(inputId,direction)
{	
	var obj =document.getElementById('dhtmlgoodies_a' + inputId);
	var contentObj = document.getElementById('dhtmlgoodies_ac' + inputId);
	
	width = obj.clientWidth;
	height = obj.clientHeight;
	
	if(width==0) width = obj.offsetWidth;
	if(height==0) height = obj.offsetHeight;
	
	height = height + direction;
	
	rerunFunction = true;
	rerunFunction2 = true;
	
	if(width>contentObj.offsetWidth){
		width = contentObj.offsetWidth;
		rerunFunction2 = false;
	}
	
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	
	if(width<=1){
		width = 1;
		rerunFunction2 = false;
	}
	
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.width = width + 'px';
	obj.style.height = height + 'px';
	
	var leftPos = width - contentObj.offsetWidth;
	var topPos = height - contentObj.offsetHeight;
	
	if(topPos>0) topPos=0;
	if(leftPos>0) leftPos=0;
	
	contentObj.style.top = topPos + 'px';
	contentObj.style.left = leftPos + 'px';
	if(topPos==0) document.getElementById('sldg').src="http://192.168.1.211/dev/amorim_montenegro/templates/bt_slide.png";
	else if(topPos=="-129") document.getElementById('sldg').src="http://192.168.1.211/dev/amorim_montenegro/templates/bt_slide2.png";
	
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.display='block';
				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);				
			}else{
				dhtmlgoodies_slideInProgress = false;
			}
		}else{
			dhtmlgoodies_activeId = inputId;
			dhtmlgoodies_slideInProgress = false;
		}
	}
}



function initShowHideDivs()
{
	var sz = sizethis();
	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='dhtmlgoodies_question'){
			divs[no].onclick = showHideContent;
			divs[no].id = 'dhtmlgoodies_q'+divCounter;
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			answer.id = 'dhtmlgoodies_a'+divCounter;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px';
			contentDiv.style.left = 0 - contentDiv.offsetWidth + 'px'; 	
			contentDiv.style.width = sz[0] + "px";
			contentDiv.style.height = "130px";
			contentDiv.className='dhtmlgoodies_answer_content';
			contentDiv.id = 'dhtmlgoodies_ac' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';
			
			divCounter++;
		}		
	}	
}




carregando = '';
function carrega_pagina(url,destino)
{
	try
	{
		var ajax = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	catch(e)
	{
		try
		{
			var ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex) 
		{
			try
			{
				var ajax = new XMLHttpRequest();
			}
			catch(exc)
			{
				alert("Esse browser não tem recursos para uso do Ajax");
				ajax = null;
			}
		}
	}
    if(ajax)
    {
		var idCentral=document.getElementById(destino);
	    ajax.open("GET", url, true);
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                
        ajax.onreadystatechange = function() 
		{            
			if(ajax.readyState == 4)
        	{
            
				var texto = ajax.responseText;
			    texto = texto.replace(/\+/g," ");			
				texto = unescape(texto);
				idCentral.innerHTML = texto;
				extraiScript(texto);

				
      		}
        }
        ajax.send(null);
    }
}

function extraiScript(texto)
{
        var ini = 0;
        // loop enquanto achar um script
        while (ini!=-1){
                // procura uma tag de script
                ini = texto.indexOf('<script', ini);
                // se encontrar
                if (ini >=0){
                        // define o inicio para depois do fechamento dessa tag
                        ini = texto.indexOf('>', ini) + 1;
                        // procura o final do script
                        var fim = texto.indexOf('</script>', ini);
                        // extrai apenas o script
                        codigo = texto.substring(ini,fim);
                       
                        novo = document.createElement("script")
                        novo.text = codigo;
                        document.body.appendChild(novo);
                }
        }
}



function slideIt(tipo)
{
	
	var slidingDiv = document.getElementById("push");
	var stopPosition = 0;
	var beginPosition = -40;

	if ((parseInt(slidingDiv.style.top) < stopPosition) && tipo=="down")
	{
		slidingDiv.style.top = parseInt(slidingDiv.style.top) + 10 + "px";
		setTimeout(function(){slideIt(tipo)}, 1);		
	}	
	if ((parseInt(slidingDiv.style.top) >= stopPosition) && tipo=="down")
	{ 
		document.getElementById('down').style.display="none";	
		document.getElementById('up').style.display="block";	
		//clearInterval(intervalo);
	}
	
	if (parseInt(slidingDiv.style.top)>beginPosition && tipo=="up")
	{	
		slidingDiv.style.top =  parseInt(slidingDiv.style.top) - 10 + "px";
		setTimeout(function(){slideIt(tipo)}, 1);
	}
		if ((parseInt(slidingDiv.style.top) <= beginPosition) && tipo=="up")
	{ 
		document.getElementById('up').style.display="none";	
		document.getElementById('down').style.display="block";
		
	}
}

var control_width = 780;
var eachdivwidth = Math.ceil(control_width/100);
var bar_div_width =  16;


scrollStep=5;
scrollStep2="";
timerLeft=""
timerRight=""

function toLeft(id,tamanho)
{
	document.getElementById(id).scrollLeft=0
	document.getElementById('rs').value = 1;
	setcolor(1,1,tamanho)
}

function toRight(id,tamanho)
{
	document.getElementById(id).scrollLeft=document.getElementById(id).scrollWidth
	document.getElementById('rs').value = 1;
	setcolor(100,1,tamanho)
}

function scrollDivLeft(id,tamanho)
{ //dir	
  clearTimeout(timerRight);
  document.getElementById(id).scrollLeft+=scrollStep
  timerRight=setTimeout("scrollDivLeft('"+id+"','"+tamanho+"')",20)
  scrollStep2 = Math.floor((document.getElementById(id).scrollLeft*100)/parseInt(tamanho));
  
}

function scrollDivRight(id,tamanho)
{//esq

  clearTimeout(timerLeft);
  document.getElementById(id).scrollLeft-=scrollStep
  timerLeft=setTimeout("scrollDivRight('"+id+"','"+tamanho+"')",20)
  scrollStep2 = Math.floor((document.getElementById(id).scrollLeft*100)/parseInt(tamanho));    
}

function stopMe(tamanho)
{
  clearTimeout(timerRight);
  clearTimeout(timerLeft);
  //document.getElementById('rs').value = 1;
  //setcolor(scrollStep2,0,tamanho);
  //document.getElementById('debug').innerHTML = scrollStep2;
}

function scrollDivLeft2(id)
{ //dir	
	clearTimeout(timerRight);
	document.getElementById(id).scrollLeft+=scrollStep
	timerRight=setTimeout("scrollDivLeft2('"+id+"')",10)
	//document.getElementById('debug').innerHTML = document.getElementById(id).scrollLeft;  
}

function scrollDivRight2(id)
{//esq

	clearTimeout(timerLeft);
	document.getElementById(id).scrollLeft-=scrollStep
	timerLeft=setTimeout("scrollDivRight2('"+id+"')",10)
	//document.getElementById('debug').innerHTML = document.getElementById(id).scrollLeft;
}


function CreateController(tamanho)
{ 
	var control_sting = "";
	//document.getElementById('debug').innerHTML = control_width+" - "+eachdivwidth+" - "+bar_div_width;

	control_sting = control_sting+"<div style='height:4px;cursor:pointer; width:810px; position:absolute;' onmousedown=\"rsvalue('1')\" onmouseup=\"rsvalue('0')\">";

	for(var i = 1 ;i<=100; i++)
	{  	
		control_sting = control_sting+"<div id='tde"+i+"' onmouseover=\"setcolor('"+i+"','0','"+tamanho+"')\"";		
		control_sting = control_sting+" style='float:left;height:4px;position:relative;top:0px; width:"+(eachdivwidth)+"px;background-color:#f2f2f2;'";		
		control_sting = control_sting+"><div style='position:relative;margin-top:3px; top:-2px; border-top:2px solid #666666;width:"+(eachdivwidth)+"px;'>";
		control_sting = control_sting+"</div></div>";
	}
	
	 control_sting = control_sting+"</div>";
	 document.getElementById('controldiv').innerHTML = control_sting;
	 document.getElementById('rs').value = 1;
	 setcolor(1,1,tamanho);
}

function rsvalue(x){ document.getElementById('rs').value=x;}

function setcolor(goti,primeiro,tamanho)
{
	if(document.getElementById('rs').value == 1)
	{		
		for(var i = 1 ;i<=100; i++)
		{						
			document.getElementById('tde'+i).style.backgroundColor='#f2f2f2';
			document.getElementById('tde'+i).style.width = (eachdivwidth)+'px';
		}
		//document.getElementById('debug').innerHTML = control_width+" - "+eachdivwidth+" - "+bar_div_width;
		document.getElementById('tde'+goti).style.backgroundColor = "#9999cc";
		document.getElementById('tde'+goti).style.width = bar_div_width+'px';
		
		changeimageSIze(goti,tamanho);
		
		if(primeiro==1)
		{ 
			document.getElementById('rs').value = 0;
			primeiro=0;
		}
	}
}

function mais_padding(tamanho,id)
{
	scrollStep2xx = Math.floor((parseInt(tamanho) * 100) / 810) ;
	if(scrollStep2xx>100) scrollStep2xx=100;
	if(scrollStep2xx<1) scrollStep2xx=1;
	setcolor(scrollStep2xx,0,id);
	//document.getElementById('debug').innerHTML = scrollStep2xx+" - "+id;
}

function getXOffset(e) {
if (typeof e.offsetX != 'undefined')
return e.offsetX;
else if (typeof e.pageX != 'undefined')
return e.pageX - e.target.offsetLeft;
}

function getYOffset(e) {
if (typeof e.offsetY != 'undefined')
return e.offsetY;
else if (typeof e.pageY != 'undefined')
return e.pageY - e.target.offsetTop;
}

function displayOffsets(e, tamanho) {
e = (e) ? e : window.event;
//window.status = 'x: ' + getXOffset(e) + ' y: ' + getYOffset(e);
mais_padding(getXOffset(e),tamanho);
//document.getElementById('debug').innerHTML = 'x: ' + getXOffset(e) ;
}


function changeimageSIze(goti,tamanho) 
{
	scrollStep = Math.floor((parseInt(tamanho)/100)*goti);
	document.getElementById("container").scrollLeft=scrollStep;
}

function last_pos(goti) 
{  	
	document.getElementById("pos_t").value=document.getElementById("container").scrollLeft;
}
