// JavaScript Document

function menuOver(b,o){
	if(b){
		// change img
		alert(b+", "+o);
		document.getElementById(o).src="img/menu_"+o+".png";
	}
}



// show content

var page=0;

function showContent(p,s){
	// loading
	loading_img(1);
	page=p;
	if(s==undefined){
		i="";
	}else{
		i="_item";
	}
	//changeLine();
	
	// stop slideshow
	/*
	if(page!=0){
		var bg=document.getElementById('slideshow0');
		var fg=document.getElementById('slideshow1');
		if (bg.timer) window.clearTimeout(bg.timer);
		if (fg.timer) window.clearTimeout(fg.timer);
	}
	*/
	
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById("content").innerHTML=xmlhttp.responseText;
						
			if(page==0){
				RunSlideShow('slideshow1','slideshow0',slides,5);
			}else if(page==2&&i=="_item"){
				eval(document.getElementById("js2").innerHTML);
			}else if(page==3){
				eval(document.getElementById("js3").innerHTML);
			}else if(page==4){
				initialize();
			}
			
			// linha titulo
			changeLine();
			// titulo
			changeTitle(aPages[page]);
			// loading
			loading_img(0);
			
			align();
		}
	}
	xmlhttp.open("GET",p+i+".asp?id="+s,true);
	xmlhttp.send();
}


function changeTitle(t){
	if(page==0){
		document.getElementById('titulo_p').innerHTML="";
		document.getElementById('titulo_p').style.visibility='hidden';
		
		document.getElementById('quad_img').style.height='140px';
	}else{
		document.getElementById('titulo_p').innerHTML=t;
		document.getElementById('titulo_p').style.visibility='visible';
		
		document.getElementById('quad_img').style.height='220px';
		//alert(document.getElementById('quad_img').style.height);
	}
}


function changeLine(){
	if(page==0){
		document.getElementById('title_line').style.borderBottom="1px solid #bababa";
	}else{
		document.getElementById('title_line').style.borderBottom="transparent";
	}
}


function loading_img(b){
	if(b){
		// liga
		document.getElementById('loading_img').src="img/loading4.gif";
	}else{
		document.getElementById('loading_img').src="img/spacer.gif";
	}
}


aObras = new Array();
iObra=0;
function showObra(b,i){
	if(b){
		iObra=i;
//		alert(iObra);
		document.getElementById('obra').src="images/"+aObras[iObra][1];
		document.getElementById('legenda').innerHTML=aObras[iObra][2];
	}else{
		// nothing
	}
}


// image loader 
var image = new Image();
image.onload = function() {
    // always called
    getImgSize();
};

function getImgSize(imgSrc){
	var imgheight = image.height;
	var imgwidth = image.width;
	//alert ('The image size is '+width+'*'+height);
	
	// resize viewer
	document.getElementById('viewer').style.width = imgwidth+(2*30)+'px';
	document.getElementById('viewer').style.height = imgheight+(2*30)+'px';
	// aliign viewer
	alignGallery();
}


aPhotos = new Array();
iPhoto=0;
iGallery=null;
function openGallery(b,n,img){
	if(b){
		// open
		alignGallery();
		document.getElementById('viewer').style.visibility="visible";
		document.getElementById('fader').style.visibility="visible";
		iPhoto=img-1;
		iGallery=n;
		openPhoto(1);
		
		// scroll watch
		window.onscroll=function (){
			//alert("scroll");
			openGallery(0);
		}
	}else{
		// close
		document.getElementById('viewer').style.visibility="hidden";
		document.getElementById('fader').style.visibility="hidden";
		iPhoto=0;
		iGallery=null;
		
		// scroll watch
		window.onscroll="";
	}
}

function openPhoto(b){
	if(b){
		if(iPhoto+1==aPhotos[iGallery].length){
			iPhoto = 0;
		}else{
			iPhoto = iPhoto+1;
		}
	}else{
		if(iPhoto==0){
			iPhoto = aPhotos[iGallery].length+1;
		}else{
			iPhoto = iPhoto-1;
		}
	}
	image.src = "images/"+aPhotos[iGallery][iPhoto];
	document.getElementById('photo').src="images/"+aPhotos[iGallery][iPhoto];
	//alert(aPhotos[iPhoto]);
}

function alignGallery(){

	var lmt = document.getElementById('viewer');
	var container = document.documentElement;

	if(lmt && container){
	    var containerWidth;
	    var containerHeight;
	    if (container.innerWidth){
            containerWidth = container.innerWidth;
            containerHeight = container.innerHeight;
		}else{
            containerWidth = container.clientWidth;
            containerHeight = container.clientHeight;
		}
		
	    var lmtWidth;
	    var lmtHeight;
	    if (lmt.innerWidth){
            lmtWidth = lmt.innerWidth;
            lmtHeight = lmt.innerHeight;
		}else{
            lmtWidth = lmt.offsetWidth;
            lmtHeight = lmt.offsetHeight;
		}
		
		var x = Math.ceil((containerWidth - lmtWidth) / 2);
		var y = Math.ceil((containerHeight - lmtHeight) / 2);
		if(y<0){
			y = 0;
		}
		if(x<0){
			x = 0;
		}
		
		// adjust gallery
		yoffset=document.all? iebody.scrollTop : pageYOffset
		document.getElementById('fader').style.top = yoffset + "px";
		
		//lmt.style.position = "relative";
		lmt.style.left = x + "px";
		lmt.style.top = (y+yoffset) + "px";
		
	}
}


