/*
 * @author	Nicholas Almeida, www.nicholasalmeida.com
 * @version	1.0
*/
pImg('..\/img\/bg.png');
pImg('..\/img\/img_loader.gif')
function hSelects(){for(var i=0;i<gTag('select').length;i++) gTag('select')[i].style.visibility = 'hidden';return false;}
function sSelects(){for(var m=0;m<gTag('select').length;m++) gTag('select')[m].style.visibility = 'visible';return false;}
function winW(){return window.innerWidth ? window.innerWidth : document.documentElement.offsetWidth;}
function winH(){return window.innerHeight ? window.innerHeight : document.documentElement.offsetHeight;}
function winHPos(){return document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;}
var tmpImgLoader;
var imagemPop;
var loaderPersId;
function openBox(e, o){
	o = o || false;
	if(e == ''){
		createElm('div','boxPopDefault');
		var boxPop = gElm('boxPopDefault');
		boxPop.style.padding = '10px';
		boxPop.style.backgroundColor = '#ffffff';
		boxPop.style.border = '2px solid lightgray';
		e = 'boxPopDefault';
	}
	if (!is.ie5){
		createElm('div','openBoxPopBg');
		var bgBoxStyle = gElm('openBoxPopBg').style;
		bgBoxStyle.position = 'absolute';
		if(is.ie){bgBoxStyle.background = '#ffffff';bgBoxStyle.filter = 'Alpha(Opacity=50)';}else{bgBoxStyle.background = 'url(../img/bg.png)';};
		bgBoxStyle.zIndex = 10;
	}
	sDiv(e);
	if(o.img){
		var dc = (o.img.closeImg == undefined) ? true : false;
		var fx = o.img.fx || false;

		var imgId = gElm(e);
		/*
			img{
				id:'idDaImagem'
				src:'../img/imagem.jpg'
				dontCloseImg: true or false
			}
		*/
		hDiv(e);
		//cria imagem
		if(!gElm('imgTarget')){
			var img = document.createElement('img');
			img.setAttribute('id','imgTarget');
			imgId.appendChild(img);
		}
		//cria loader
		if(!o.img.loader){
			if(!gElm('boxImgLoader')){
				createElm('div','boxImgLoader');
				var boxLoader = gElm('boxImgLoader');
				boxLoader.style.width = '280px';
				boxLoader.style.height = '132px';
				boxLoader.style.backgroundColor = '#F1F1F1';
				boxLoader.innerHTML = '<p style="margin:50px 100px;text-align:center;"><img src="../img/img_loader.gif" alt="carregando" title="" /><p>';
			}
		}else{
			loaderPersId = gElm(o.img.loader).id
			sDiv(loaderPersId);
		}
		imagemPop = new Image();
		imagemPop.src = o.img.src;
		tmpImgLoader = setInterval(function(){
			if (imagemPop.complete) {
				var tmpImg = gElm('imgTarget');
				clearInterval(tmpImgLoader);
				if(gElm('boxImgLoader'))delElm('boxImgLoader');
				if(gElm(loaderPersId))hDiv(loaderPersId);
				tmpImg.src = o.img.src;
				tmpImg.title = 'Clique na imagem para fechá-la.';
				imgId.style.top = '-5000px';
				imgId.style.left = '-5000px';
				sDiv(e);
				alignBox(e, o);
				if(dc){
					gElm('imgTarget').style.cursor = 'pointer';
					addEvent(gElm('imgTarget'),'click',function(){closeBox(e);});
				}else{
					gElm('imgTarget').setAttribute('onclick',false);
				}
				delete tmpImg;
			}
		}, 50);
	}
	alignBox(e, o);
	if(o.onFinish){
		eval(o.onFinish)();
	}
	//if (is.ie)hSelects();
	window.onscroll = function(){alignBox(e, o)};
	window.onresize = function(){alignBox(e, o)};
}
function alignBox(e, o){
	/*{
		vAlign:'top' or 'middle' or 'bottom' or Number (top) //middle
		hAlign:'left' or 'center' or 'right' or Number (left) //center
		fix: true or false //false
		dontClose: true or false //false
		img:{id:'idBox',src:'img.jpg'}
	}*/
	o = o || false;
	var va = o.vAlign || 'middle';
	var ha = o.hAlign || 'center';
	var fx = o.fix || false;
	var cls = o.dontClose || false;
	var bgBox = gElm('openBoxPopBg');
	var w = gElm(e).offsetWidth;
	var h = gElm(e).offsetHeight;
	gElm(e).style.position = 'absolute';
	gElm(e).style.zIndex = bgBox.style.zIndex+2;
	bgBox.style.width = '100%';
	bgBox.style.height = winH() +'px';
	bgBox.style.top = winHPos() +'px';
	if(gElm('boxImgLoader') || gElm(loaderPersId)){
		var boxLoader2 = gElm('boxImgLoader') || gElm(loaderPersId);
		if(boxLoader2 == gElm('boxImgLoader'))sDiv('boxImgLoader');
		var ww = boxLoader2.offsetWidth;
		var hh = boxLoader2.offsetHeight;
		boxLoader2.style.position = 'absolute';
		boxLoader2.style.top = (winHPos()+(winH()/2)-(hh/2))+'px';
		boxLoader2.style.marginLeft = '50%';
		boxLoader2.style.left = (-(ww/2))+'px';
		boxLoader2.style.zIndex = bgBox.style.zIndex+1;
	}
	if(isNaN(va)){
		switch(va){
			case 'top':
				gElm(e).style.top = winHPos()+'px';
				break;
			case 'middle':
				gElm(e).style.top = (winHPos()+(winH()/2)-(h/2))+'px';
				break;
			case 'bottom':
				gElm(e).style.top = (winHPos()+winH()-h)+'px';
				break;
			default:
				alert('bug: o parâmetro "'+ va +'" não existe')
		}
	}else{
		if(fx){
			gElm(e).style.top = va+'px';
		}else{
			gElm(e).style.top = winHPos()+va+'px';
		}
	}
	if(isNaN(ha)){
		switch(ha){
			case 'left':
				gElm(e).style.marginLeft = 0;
				gElm(e).style.left = 0;
				break;
			case 'center':
				gElm(e).style.marginLeft = '50%';
				gElm(e).style.left = (-(w/2))+'px';
				break;
			case 'right':
				gElm(e).style.marginLeft = 0;
				gElm(e).style.left = 'auto';
				gElm(e).style.right = 0;
				break;
			default:
				alert('bug: o parâmetro "'+ ha +'" não existe')
		}
	}else{
		gElm(e).style.marginLeft = 0;
		gElm(e).style.left = va+'px';
	}
	if(!cls){
		addEvent(bgBox,'click',function(){closeBox(e);});
	}
}
function closeBox(e){
	imagemPop = null;
	clearInterval(tmpImgLoader);
	if(gElm('imgTarget'))delElm('imgTarget');
	if(gElm('boxImgLoader'))delElm('boxImgLoader');
	if(gElm(loaderPersId))hDiv(loaderPersId);
	if(gElm(e)){
		if(gElm('openBoxPopBg')){
			delElm('openBoxPopBg');
			//if(is.ie) sSelects();
		}
		hDiv(e);
	}
	window.onscroll = null;
	window.onresize = null;
}
