// JavaScript Document (capaz, huaheuhauehuhea......)
function PopUp(){
	this.loadImg = function(src, label){
		var img = new Image();
		
		if(document.getElementById('aw_molde')){
			document.body.removeChild(document.getElementById('aw_molde'));
		}
		//
		var bg = document.createElement("div");
		bg.setAttribute("id" , "aw_background");
		bg.style.top = ((document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop) + "px";
		bg.style.left = ((document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft) + "px";
		bg.style.width = "100%";
		bg.style.height = "100%";
		//
		var loading = document.createElement("div");
		loading.setAttribute("id" , "aw_loading");
		loading.appendChild(document.createTextNode("Aguarde..."));
		loading = centralize(loading, 200, 21);
		//
		var close = document.createElement("div");
		close.setAttribute("id" , "aw_close");
		close.appendChild(document.createTextNode("fechar"));
		close.onclick = bg.onclick = function(){
			try{
				document.body.removeChild(document.getElementById('aw_molde'));
				document.body.removeChild(document.getElementById('aw_background'));
				document.body.style.overflowX = "visible";   
				document.body.style.overflowY = "visible";
				showSelect();
				//showSWF();
			}catch(e){}
		}
		//
		var molde = document.createElement("div");
		molde.setAttribute("id" , "aw_molde");
		molde.onmouseover = function(){
			try{
				document.getElementById('aw_close').style.display = 'block';
			}catch(e){}
		}
		molde.onmouseout = function(){
			try{
				document.getElementById('aw_close').style.display = 'none';
			}catch(e){}
		}
		//
		molde.appendChild(img);
		molde.appendChild(close);
		if(label){
			var n_label = document.createElement("div");
			n_label.setAttribute("id" , "aw_label");
			n_label.appendChild(document.createTextNode(label));
			molde.appendChild(n_label);
		}
		//
		hideSelect();
		//hideSWF();
		document.body.style.overflowX = "hidden";
		document.body.style.overflowY = "hidden";
		//document.body.appendChild(bg);
		document.body.appendChild(loading);
		//
		img.onload = function(){
			molde = centralize(molde, img.width, img.height);
			document.body.appendChild(molde);
			document.body.removeChild(document.getElementById("aw_loading"));
		}
		img.src = src;
	}
	
	
	//alpha
	//tenta linkar
	this.init = function(){
		var elements = document.getElementsByTagName("a");
		for(i=0 ; i<elements.length ; i++){
			if(elements[i].rel=='imgbox'){
				var img = elements[i].childNodes;
				var label = '';
				if(img[0]){
					label = (img[0].title) ? img[0].title : ((img[0].label) ? img[0].label : '') ;
				}
				elements[i].href = "javascript:imgbox('"+elements[i].href+"' , '"+label+"')";
			}
		}
	}
	
	
	//esconde os swf
	function hideSWF(){
		var swfs = document.getElementsByTagName("object");
		for(i=0 ; i<swfs.length ; i++){
			swfs[i].style.display = 'none';
		}
	}
	
	
	//exibe os swf
	function showSWF(){
		var swfs = document.getElementsByTagName("object");
		for(i=0 ; i<swfs.length ; i++){
			swfs[i].style.display = 'block';
		}
	}
	
	
	//retorna um array com as imagens em galeria
	function getGalery(){
		var imgs = document.anchors;
		var galery = new Array();
		var count = 0;
		for(i=0 ; i<imgs.length ; i++){
			alert(document.anchors[i].rel);
			if(document.anchors[i].rel=="[imgbox]"){
				galery[count] = document.anchors[i];
				count++;
			}
		}
		return galery;
	}
	
	//retorna um elemento centralizado
	function centralize(el, width, height){
        var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
        var left = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
        var size = getPageSize();
        var width = Math.ceil(width/2);
        var height = Math.ceil(height/2);
        el.style.top = ((size[3]/2)+top-height)+'px';
        el.style.left = ((size[2]/2)+left-width)+'px';
		return el;
	}

	//retorna as medidas da pagina
	function getPageSize(){
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			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
			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 = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return(arrayPageSize);
	}
	
	
	//se tá no IE, retorna true
	function isIE(){
		if(navigator.userAgent.toString().indexOf("IE") >= 0){
			return true;
		}else{
			return false;
		}
	}
	
	//esconde os selects de uma pagina
	function hideSelect(){
		if(isIE()){
			for(i=0 ; i<document.forms.length ; i++){
				for(j=0 ; j<document.forms[i].elements.length ; j++){
					try{
						if(document.forms[i].elements[j].type.indexOf('select')>=0){
							document.forms[i].elements[j].style.display='none';
						}
					}catch(e){}
				}
			}
		}
	}
	
	
	//exibe os selectes de uma pagina
	function showSelect(){
		if(isIE()){
			for(i=0 ; i<document.forms.length ; i++){
				for(j=0 ; j<document.forms[i].elements.length ; j++){
					try{
						if(document.forms[i].elements[j].type.indexOf('select')>=0){
							document.forms[i].elements[j].style.display='block';
						}
					}catch(e){}
				}
			}
		}
	}
	
}


this.imgbox = function(src, label){
	var p = new PopUp();
	p.loadImg(src, label);
}