// JavaScript site C&A Imoveis
// Danniel Magno <dennyloko@gmail.com>

var mv = 0 ;
var pm = 0 ;
var timer;
var mainflag = '';
var prevflag = '';
var popflag = '';

function roller(id, flag, pm){
	if($Peg('cboxOverlay').style.display == 'none'){
		if(flag == 'l' && mv < 0){
			mv+=1;
		}else if(flag == 'r' && mv >= pm*-1){
			mv-=1;
		}
		
		$Peg(id).style.marginLeft=mv+'px';
	}
}

function selectChange(id, src){
	$Peg(id).src=$Peg('imovel_img').src;
	$Peg('imovel_img').src=src;
	//$Peg('imovel_img').onclick='popUp('+src+')';
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,left = 412,top = 134');");
}

function checaNavIE(){
	if(navigator.userAgent.indexOf("MSIE") != -1) { 
		return true; 
	}else{
		return false;
	}
}

function sombraFade(type, to, now){
	type = (type==undefined) ? 'in' : type;
	to = (to==undefined) ? 80 : to;
	now = (now==undefined) ? 0 : now;
	
	var sombra = document.getElementById('sombra');
	var tfade = 20;
	
	if(type == 'in'){
		//alert(now);
		if(checaNavIE()){
			sombra.style.background='url(gfx/rgba.php?r=0&g=0&b=0&a='+now+') repeat';
		}else{
			sombra.style.background='rgba(0, 0, 0, 0.'+now+')';
		}
		
		if(now < to){
			now=now+10;
			setTimeout('sombraFade("'+type+'", '+to+', '+now+')', tfade);
		}
	}else if(type == 'out'){
		if(checaNavIE()){
			sombra.style.background='url(gfx/rgba.php?r=0&g=0&b=0&a='+now+') repeat';
		}else{
			sombra.style.background='rgba(0, 0, 0, 0.'+now+')';
		}
		
		if(now > to){
			now=now-10;
			setTimeout('sombraFade("'+type+'", '+to+', '+now+')', tfade);
		}
	}
}

function sombra(){
	var corpo = document.getElementsByTagName('body')[0];
	var sombra = document.getElementById('sombra');
	
	if(sombra.style.display == 'none' || sombra.style.display == ''){
		// Parando o carrosel de lotes
		mainflag='';
		
		// Mostrando a sombra e aplicando o efeito de fade
		sombra.style.display = 'block';
		sombraFade('in', 80, 0);
	}else{
		// Retirando a sombra e aplicando o efeito de fade
		sombraFade('out', 0, 80);
		setTimeout(sombra.style.display = 'none', 30);
		
		// Iniciando o carrosel de lotes
		mainflag=prevflag;
	}
	
	if(corpo.style.overflow == 'auto' || corpo.style.overflow == '')
		corpo.style.overflow='hidden';
	else
		corpo.style.overflow='auto';
}

function checaSombra(){
	var sombra = document.getElementById('cboxOverlay');
	
	if(sombra.style.display == 'none' || sombra.style.display == '')
		return false;
	else
		return true;
}

function abreImovel(id){
	if(id == undefined){
		alert("ERRO!\nPara ver um imovel, precisamos do ID do mesmo.\nContacte a imobiliaria e comunique este problema, por favor.");
	}else{
		// Ativando a sombra e mostrando a janela
		//sombra();
		$Peg('imovelInfo').style.display='block';
		$Peg('imovel_iframe').src='include/imoveis.php?method=individual&id='+id;
		
		// Mostra o imovel na janela
		// Ver com o Pedro por que que esta travando o setInterval
		//$Aja2('include/blank2.html', 'GET', null, 'imovelInfoConteudo', 'gfx/load.gif', null);
	}
}

function abreImovelAdmin(id){
	if(id == undefined){
		alert("ERRO!\nPara ver um imovel, precisamos do ID do mesmo.\nContacte a imobiliaria e comunique este problema, por favor.");
	}else{
		// Ativando a sombra e mostrando a janela
		sombra();
		$Peg('imovelInfo').style.display='block';
		$Peg('imovel_iframe').src='src/imoveis.php?do=edit&id='+id;
		
		// Mostra o imovel na janela
		// Ver com o Pedro por que que esta travando o setInterval
		//$Aja2('include/blank2.html', 'GET', null, 'imovelInfoConteudo', 'gfx/load.gif', null);
	}
}

function horizontal(){
	var navItems = document.getElementById("menu_dropdown").getElementsByTagName("li");
	
	for(var i=0; i< navItems.length; i++){
		if(navItems[i].className == "submenu " || navItems[i].className == "submenu selected"){
			if(navItems[i].getElementsByTagName('ul')[0] != null){
				navItems[i].onmouseover=function() {this.getElementsByTagName('ul')[0].style.display="block";}
				navItems[i].onmouseout=function() {this.getElementsByTagName('ul')[0].style.display="none";}
			}
		}
	}
}

var cont = 0;
function addFiles(id){
	var fileINPUT = document.createElement('input');
	var fileBR = document.createElement('br');
	
	fileINPUT.setAttribute('type', 'file');
	fileINPUT.setAttribute('name', 'imovel_fotos[]');
	fileINPUT.setAttribute('id', 'imovel_fotos_'+cont);
	fileINPUT.setAttribute('size', '45');
	fileINPUT.setAttribute('onchange', 'if(this.id.length > 0){addFiles(\''+id+'\')}');
	
	$Peg(id).appendChild(fileBR);
	$Peg(id).appendChild(fileINPUT);
	cont++;
}

function imovelPrincipal(foto, id, fc){
	for(var i=0; i<fc; i++){
		$Peg('foto_'+i).style.border='none';
	}
	
	$Peg('fotoprincipal').value=foto;
	$Peg(id).style.border='solid 2px crimson';
}

function delFoto(foto, imovel, principal, id){
	var url = 'delfoto.php?foto='+foto+'&imovel='+imovel+'&idfoto='+id;
	
	if(foto == principal){
		if(confirm('A foto que pretende excluir, é a principal do imóvel. Deseja prosseguir?')){
			return $Aja(url+'&principal=1', 'GET', null, null, null, 'json');
		}
	}else{
		if(confirm('Tem certeza que deseja excluir essa foto?')){
			return $Aja(url, 'GET', null, null, null, 'json');
		}
	}
}

/*
 *
 * IEContentLoaded.js
 *
 * Author: Diego Perini (diego.perini at gmail.com) NWBOX S.r.l.
 * Summary: DOMContentLoaded emulation for IE browsers
 * Updated: 05/10/2007
 * License: GPL/CC
 * Version: TBD
 *
 */

// @w	window reference
// @fn	function reference
function IEContentLoaded (w, fn) {
	var d = w.document, done = false,
	// only fire once
	init = function () {
		if (!done) {
			done = true;
			fn();
		}
	};
	// polling for no errors
	(function () {
		try {
			// throws errors until after ondocumentready
			d.documentElement.doScroll('left');
		} catch (e) {
			setTimeout(arguments.callee, 50);
			return;
		}
		// no errors, fire
		init();
	})();
	// trying to always fire before onload
	d.onreadystatechange = function() {
		if (d.readyState == 'complete') {
			d.onreadystatechange = null;
			init();
		}
	};
}
