// JavaScript Document
var jsWinBox = {
	
	open : function(url,name,title,params){				
		try{
			
			/*verificar navegador*/
			var $browserName = (navigator.appName.indexOf('Netscape') > -1) ? 'NS' : 'IE';
			
			/*tamanho da janela*/
			if($browserName == 'NS'){
				winWidth = window.innerWidth;
				winHeight = window.innerHeight + 60;
				winSclTop = window.scrollY;
			}else{
				winWidth = document.documentElement.offsetWidth;
				winHeight = document.documentElement.offsetHeight + 60;
				winSclTop = document.documentElement.scrollTop;
			}

			/*dimensao do documento*/
			docWidth = document.body.clientWidth;
			docHeight = document.body.clientHeight;
	
			//Pelicula negra (modal)
			var cwOverLayer = document.createElement("div") ;
			with(cwOverLayer){
				setAttribute("id", "jsWinBox_OverLayer");
				setAttribute("class", "jsWinBox_OverLayer"); //IE
				className = "jsWinBox_OverLayer";
				style.height =  Math.max(winHeight, docHeight) + 'px';
			}
			if(!document.getElementById('jsWinBox_OverLayer'))document.body.appendChild(cwOverLayer);
			
			//Cria a moldura da janela
			var cwWind = document.createElement("div");
			with (cwWind){
				setAttribute("id", name);
				className = "jsWinBox_WinConteiner";			
			
				//Paramentros sao atribuidos atraves de classes CSS
				var stl = "";
				for(i=0; i<params.length; i++){
					stl += ("style." + params[i].replace(":","='") + "\';") ;
					eval(stl);
				}
			
				//ajusta largura
				if (cwWind.style.width.indexOf('px') > 0) getW = cwWind.style.width.replace('px','');
				//ajustar altura da janela entre FF e IE
				if (cwWind.style.height.indexOf('px') > 0) getH = cwWind.style.height.replace('px','');
				
				cwWind.style.position = 'absolute';
	

				//se alutura for informada...
				if (cwWind.style.top.length > 0){
					cwTop = cwWind.style.top;
				}else{
					cwTop = parseInt( ((winHeight/2) - (getH/2)) );						
					if (cwWind.style.top.indexOf('px') < 0) { cwWind.style.top = (cwTop + winSclTop) + 'px';}
				}
				
				if (cwWind.style.left.length > 0){
					cwLeft = cwWind.style.left;
				}else{
					cwLeft = parseInt( (winWidth/2) - (getW/2) );
					if (cwWind.style.left.indexOf('px') < 0) cwWind.style.left = cwLeft +	 'px';
				}
				
				//torna a janela transparente
				if($browserName == 'NS'){ cwWind.style.MozOpacity = .0; }else{ cwWind.style.filter = "alpha(opacity=0)";	}
			}
			
			if(!document.getElementById(name))document.body.appendChild(cwWind);
						
			//Barra de titulo
			cwWind.innerHTML += "<table class='jsWinBox_WinTBar'>" +
																"<tr>" +
																	"<td width='100%'>" + title + "</td>" +
																	"<td>"+
																		"<a href='javascript:void(0);' onclick=\"jsWinBox.close('" + name +"','_root');\" class='jsWinBox_TBarBtnClose' title='Fechar'>X</a>" +
																	"</td>"+ 
																"</tr>" +
															"</table>"
																								
			//Area onde carrega os conteudos
			w = cwWind.style.width;
			h = cwWind.style.height;
			h = (!IsIE() == true)?((h.replace('px','')) - 30) + 'px' : ((h.replace('px','')) - 28) + 'px'; 				
			cwWind.innerHTML += "<iframe name='jsWinBox_"+ name +"_frm' id='jsWinBox_"+ name +"_frm' src='"+ url +"' style='width:"+ w +";height:" + h + ";overflow:hidden' frameborder='no'/>"
			
			//exibe a janela graduativamento
			var shwAlpha = setInterval(
												function(){						
													if (i<=10){
														if($browserName == 'NS'){ 
															cwWind.style.MozOpacity = (i<10)?eval('.'+i):1; 
														}else{ 
															cwWind.style.filter = 'alpha(opacity=' + (i * 10) + ')';
														}														
													}else{
														clearInterval(shwAlpha)
													}													
													i++	;
												}
											,100);
			
			//acompanhar rolagem da tela
			window.onscroll = function(){
				if(document.documentElement.scrollTop){					
					scrTop = document.documentElement.scrollTop;
				}else{
					scrTop = window.scrollY;
				}
				scrTop = (typeof(scrTop)=='number')?scrTop:0;
				cwWind.style.top = (cwTop/2) + scrTop + 'px';				
			}	
		
		}catch(err){
			alert(err);
		}
		
	},
	
	resize : function(cwName, w, h, x, y){
		var wind = (document.getElementById(cwName)) ? document.getElementById(cwName) : window.parent.document.getElementById(cwName);
		var frm = wind.getElementsByTagName("IFRAME")[0];
		fh = (h.replace('px','') - 30) + 'px';
				
		if (w!=null) {
			wind.style.width = w  ;
			frm.style.width = wind.style.width;
		}
		if (h!=null){
			wind.style.height = h;
			frm.style.height = fh;
		}
		if (x!=null)wind.style.left = x;
		if (y!=null)wind.style.top = y;
	},
	
	close : function(cwName,level){		
		if (level == '_parent') {
			with (window.parent.document.body){
				removeChild(window.parent.document.getElementById('jsWinBox_OverLayer'));
				removeChild(window.parent.document.getElementById(cwName));
			}			
		}else{
			document.body.removeChild(document.getElementById('jsWinBox_OverLayer'));
			document.body.removeChild(document.getElementById(cwName));	
			
		}
		//window.parent.location.reload();		
	}
		
}
