var CDIR='../';
function balao(id, alvo, mensagem, tempo, invertido) {
	alvo = $(alvo);
	if($(id)) {
		if($(id).timeOutRemove){
			clearTimeout($(id).timeOutRemove);
			$(id).timeOutRemove = window.setTimeout(function() { if($(id)) Element.remove($(id)) }, tempo);
		}
		return;
	}
	if(!alvo)
		return;
	var balao = document.createElement('div');
	var seta = document.createElement('div');
	Event.observe(balao, 'mousedown', function remove() { Element.remove(balao) });
	balao.setAttribute('id', id);
	var offsets = alvo.positionedOffset();
	var top = offsets[1];
	var left = offsets[0];
	Element.setStyle(balao, {
		position: 'absolute',
		fontSize:'11px',
		top: (invertido?top+45:top)+'px',
		left: left+'px',
		padding: '3px',
		border: 'black solid 1px',
		backgroundColor: '#FFFFAE',
		textAlign: 'left',
		webkitBoxShadow: '0px 0px 3px #FFF',
		mozBoxShadow: '0px 0px 3px #FFF',
		borderRadius: '3px',
		MozBorderRadius: '3px',
		webkitBorderRadius: '3px',
		lineHeight:'14px',
		zIndex:'90000',
		color: 'black'});
	balao.innerHTML = mensagem;
	Element.setStyle(seta, {position: 'absolute', width: '9px', height: '9px', background: 'url('+CDIR+'base/imagens/'+(invertido?'balao_seta_invertida':'balao_seta')+'.gif)'});
	balao.appendChild(seta);
	alvo.parentNode.appendChild(balao);
	var larguraAlvo = alvo.getDimensions().width;
	var alturaAlvo = alvo.getDimensions().height;
	var larguraBalao = balao.getDimensions().width;
	var alturaBalao = balao.getDimensions().height;
	Element.setStyle(balao, {top: (parseInt(balao.style.top)-alturaBalao-3)+'px'});
	Element.setStyle(seta, {top: (invertido?(-8):(alturaBalao-3))+'px'});
	Element.setStyle(seta, {left: '2px'});
	if(tempo) {
		$(id).timeOutRemove = window.setTimeout(function() { if($(id)) Element.remove($(id)) }, tempo);
	}
	alvo.observe('mousedown', function(){
		if($(id))
			Element.remove($(id));
	}).observe('keydown', function(){
		if($(id))
			Element.remove($(id));
	});
};

function ajax(r,p,cb,eb,json){
	if(Object.isString(p)){
		var newp = new Object();
		p = p.split('&');
		p.each(function(v){
			v = v.split('=');
			newp[unescape(v[0])] = unescape(v[1]);
		});
		p = newp;
	}
	return new Ajax.Request('ajax.php', {
		parameters:Object.extend({i:window.plid,r:r,json:json?1:0},p),
		onSuccess:function(res){
			// alert(res.responseText);
			if(cb)
				return cb(res['response'+(json?'JSON':'Text')],p,r);
		},
		onFailure: function(res){
			if(!eb)
				alert('AJAX ERROR JS: '+res.statusText);
			else
				eb(res,p,r);
		}
	});
}

function ajaxjs(r,p,cb,eb){
	return ajax(r,p,cb,eb,true);
}

function mostra_telefone(e,t,v){
	$(e).replace('<strong>'+t+'</strong>');
	ajaxjs('telefone', {veiculo:v});
}

function carrega_select(url, select, alvo, param, cback){
	if(!select.value)
		return false;
	alvo=$(alvo);
	ajax(url, param, function(res){
		alvo.update(alvo[0]);
		alvo.insert(res);
		alvo.selectedIndex=0;
		if(alvo[1])
			alvo.disabled=false;
		else
			alvo.disabled=true;
	});
}

function carregaFotoMiniatura(el, img, v){
	if(!v)
		return;
	el = $(el), imgEl = el.up().previous().down();
	if(!el.hasClassName('ativo')){
		el.addClassName('ativo').siblings().invoke('removeClassName', 'ativo');
		imgEl.show();
		if(!imgEl.loadings)
			imgEl.loadings = 0;
		imgEl.loadings++;
		imgEl.insert(Builder.node('img', {width:'0', height:'0', className:'fl', src:img}).observe('load', function(){
			imgEl.loadings--;
			if(imgEl.loadings<=0)
				imgEl.hide().up().setStyle({'background-image':'url('+img+')'}).down('.zoom', el.previousSiblings().length).addClassName('ativo').siblings().invoke('removeClassName', 'ativo')
			this.remove();
		}));
	}
}

function newsletter(email, nome){
	ajax('newsletter', {email:email, nome:nome}, eval);
};

function newsletter_consorcio(){
	ajax('newsletter', $('formulario_newsletter').serialize(), eval);
};

function geraBannerFlash(mov, home){
	return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+(home?809:690)+'" height="'+(home?402:180)+'" align="middle">'+
		'<param name="movie" value="../imagens/'+(home?'grupo_banner':'banner')+'/'+mov+'.swf" />'+
		'<param name="quality" value="high" />'+
		'<param name="bgcolor" value="#ffffff" />'+
		'<param name="play" value="true" />'+
		'<param name="loop" value="false" />'+
		'<param name="wmode" value="transparent" />'+
		'<param name="scale" value="showall" />'+
		'<param name="menu" value="false" />'+
		'<param name="devicefont" value="false" />'+
		'<param name="salign" value="" />'+
		'<param name="allowScriptAccess" value="sameDomain" />'+
		(!Prototype.Browser.IE?'<object type="application/x-shockwave-flash" data="../imagens/'+(home?'grupo_banner':'banner')+'/'+mov+'.swf" width="'+(home?809:690)+'" height="'+(home?402:180)+'">'+
		'<param name="movie" value="../imagens/'+(home?'grupo_banner':'banner')+'/'+mov+'.swf" />'+
		'<param name="quality" value="high" />'+
		'<param name="bgcolor" value="#ffffff" />'+
		'<param name="play" value="true" />'+
		'<param name="loop" value="false" />'+
		'<param name="wmode" value="transparent" />'+
		'<param name="scale" value="showall" />'+
		'<param name="menu" value="false" />'+
		'<param name="devicefont" value="false" />'+
		'<param name="salign" value="" />'+
		'<param name="allowScriptAccess" value="sameDomain" />':'')+
		'<a href="http://www.adobe.com/go/getflash">'+
			'<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Obter Adobe Flash Player" />'+
		'</a>'+
		(!Prototype.Browser.IE?'</object>':'')+
	'</object>';
}

function carrega_marcas(checkbox){
	if(checkbox.checked){
		carrega_select('carrega_marca', checkbox, 'marca', {categoria:checkbox.value});
		$('modelo').update($('modelo')[0]).disable().selectedIndex=0;
	}
}

var baid=1, mtimer;
function ativa_banner_timer(){
	clearTimeout(mtimer);
	mtimer = setTimeout(function(){
		ativa_banner($('bnct_'+(baid+1)), baid+1, false, true);
	}, 10000);
}

var ativa_banner = function(botao, id, home, fc){

	clearTimeout(mtimer);

	if(botao) {
		botao = $(botao);
		
		if(banner_atividade>0||botao.hasClassName('ativo'))
			return;
		
		banner_atividade=true;
		botao.siblings().invoke('removeClassName', 'ativo');
		botao.addClassName('ativo');
		
	} else if(fc){
		baid=0;
		if(!$('bnct_'+(baid+1)))
			return false;
		ativa_banner($('bnct_'+(baid+1)), baid+1, false, true);
		return false;
	}
	
	var novoBannerAtivo = $('mbanner_'+wbanners[id-1]);
	if(!novoBannerAtivo)
		$('banners_area').insert(novoBannerAtivo = Builder.node('div', {id:'mbanner_'+wbanners[id-1], className:'item'}).update(geraBannerFlash(wbanners[id-1], home)));
	
	var bannersDesativados = novoBannerAtivo.siblings().select(Element.visible);
	var params = {duration:.3, afterFinish:function(){banner_atividade--;}};
	
	banner_atividade = bannersDesativados.length+1;
	bannersDesativados.invoke('fade', params);
	novoBannerAtivo.appear(params);
	
	baid = id;
	ativa_banner_timer();
	
}, banner_atividade=0;

ativa_banner_timer();

function controle_newsletter(el, io){
	el=$(el);
	el[(io?'add':'remove')+'ClassName']('ativo');
	if(Prototype.Browser.IE&&parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==7)
		el.up().previous().setStyle('height:'+(io?'152':'184')+'px');
}

function abre_proposta_home(cid,cnm,cpc,clj){
	$('proposta_p').insert('<input name="id" type="hidden" value="'+cid+'"/>');
	$('proposta_area').down('.vName').update(cnm);
	$('proposta_area').down('.foto').setStyle({backgroundImage:'url('+cpc+')'});
	$('proposta_area').down('.loja').src = 'imagens/logo/'+clj+'.jpg';
	$('proposta_area').show();
}
