/*
script desenvolvido por Luis Carlos de Brito - luis@pevermelho.art.br

caso venha adquirir este script por outro meio senao atraves do autor, favor manter o credito do criadore
este script pode ser utilizado, modificado, publicado, utilizado sem restricoes desde que mantido os credidos dos criadores

##########------- tive muito trabalho para fazer este funcionar, respeite meu trabalho e conhecimento -------##########

*/
var ie=ie6=ie7=ie8=false;
var obj_flash;

function init() {	
	if(window.navigator.appVersion.indexOf('MSIE 6.')!=-1) ie6=true;
	if(window.navigator.appVersion.indexOf('MSIE 7.')!=-1) ie7=true;
	if(window.navigator.appVersion.indexOf('MSIE 8.')!=-1) ie8=true;
	window.navigator.appName=='Microsoft Internet Explorer' ? ie=true : ie=false;
	$('#contato').click(function(){ showModalDiv('dv_contato')});
	
	res=window.screen.width+'x'+window.screen.height;	
	if(!getCookie('resolucao')) {
		 setCookie('resolucao',res);
		 loadURL('none','','registrarResolucao&resolucao='+res);
	}
	doFeed();
	/*if(document.referrer.indexOf('geracaoveiculos')==-1) {
		get('popup').style.left=(window.screen.width-500)/2+'px'
		get('body').style.opacity=0.6;
		//if(ie) get('site').style.filter='alpha(opacity=60)';
		var tmp=new Image();
		tmp.onload=function(){$('#popup').css('background-image','url(imagens/pop_up_3.png)'); $('#popup').slideToggle(700);}
		tmp.src='imagens/pop_up_3.png';
		window.setTimeout(function(){fecharPopup();},10000);
	}*/
}

function setCookie(nome, valor) { document.cookie=nome+'='+valor; }

function getCookie(nome) {
	var start=document.cookie.lastIndexOf(nome+'=');
	if(start==-1) return false;
    var end=document.cookie.indexOf(';',start);
    if(end!=-1) 
    	return document.cookie.substring(start + nome.length+1, end);
    else 
    	return document.cookie.substring(start + nome.length+1, document.cookie.length);
}

function loadData(form) {
  form = get(form);
  var n_fields = form.length;
  var dados = '';
  var types1 = 'text hidden select-one textarea password';
  var types2 = 'checkbox radio';
  var types3 = 'button submit';
  
  for(var n=0; n < n_fields; n++) {
     if(types3.indexOf(form.elements[n].type)==-1) {
     	if(form.elements[n].getAttribute('obrigatorio')=='sim' && form.elements[n].value=='') {
     		$.growlUI('',form.elements[n].getAttribute('msg'));
     		form.elements[n].focus();
     		return false;
     	}
	  	else {
		  	if(types1.indexOf(form.elements[n].type)!=-1)
		     	dados+=form.elements[n].id+'='+ form.elements[n].value +'&';
		  	else{
		  		if(types2.indexOf(form.elements[n].type)!=-1) {
		  			if(form.elements[n].checked)
		  				dados+= form.elements[n].id+'='+form.elements[n].value+'&';
		  		}
	  		}
	  	}
   	}
  } 
  dados = dados.substr(0, dados.length-1);
  return dados;
}

function showModalDiv(id) {
	$.blockUI({ message: $('#'+id) });
	$('.blockOverlay').click($.unblockUI);
}

function fecharPopup() {
	get('popup').style.display='none';
	get('body').style.opacity=1;
	//if(ie) get('site').style.filter='alpha(opacity=100)';
}

function insertSWF(file, w, h, params) {
	insert = new Flash(file, '', w, h,'');
	insert.addParameter('wmode', 'transparent'); 
	insert.addParameter('showMenu', 'false'); 
	
	if(params.length>2) {
		tmp=params.split(',');
		for(i in tmp) {
			param_value = tmp[i].split('=');
			insert.addVariable(param_value[0]+'', param_value[1]+'');
		}
	}
	return insert.toString();
}

function showHideDiv(id) {
	$('._h').css({display:'none'});
	$("._v").css({display:'block'});
	$('#noticia0'+id).css({display:'none'});
	$('#noticia1'+id).fadeIn(1000);
}

function ucwords(str) {
	data = str.split(' ');
	res=' '
	for(i in data)
		res+=data[i].substr(0,1).touppercase + data[i].substr(1)+'  '
	return res;
}

function aleatorio(inferior,superior){
    aleat = Math.random() * (superior - inferior)
    aleat = Math.floor(aleat)
    return parseInt(inferior) + aleat
}

function addListener(el, event, func) {
	try {
		el.addEventListener(event, func, false);
	} catch (e) {
		try {
			el.detachEvent('on'+ event, func);
			el.attachEvent('on'+ event, func);
		} catch (e) {}
	}
}

function enviarForm() {
	if(get('filtro_marca')==null) {window.location.href='?pg=estoque'}
	else {document.frm_pesquisa.submit();}
}

function doNavDepo() {
	window.setTimeout(function(){$('.nav').empty();
	$('#dv_depoimento').after('<div class="nav"><a id="prev" href="#">Anterior</a> <a id="next" href="#">Próximo</a></div>').cycle({ 
    fx:     'fade', 
    speed:  'fast', 
    timeout: 0,
	prev:'#prev',
	next:'#next'});},1000);
}

function doFeed() {
	$.jGFeed('http://carroonline.terra.com.br/rss.asp',
	function(feeds){
	  if(!feeds){
	    return false;
	  }
	  for(var i=0; i<3; i++){
	    var entry = feeds.entries[i];
	    $('#dv_noticias').append('<a href="'+entry.link+'" target="_blank"><span class="_bold _fs14">'+entry.title+'</span><br>'+entry.content+'</a><br><br>');
	  }
	  $('#dv_noticias').append('<b>Fonte: <a style="color:#eaaf00" href="http://saude.terra.com.br/" target="_blank">Terra - Carros</a></b>');
	}, 10);	
}

function carregarFoto(id,img) {
	ie ? get(id).style.filter='alpha(opacity=50)':get(id).style.opacity=0.5;
	get(id).onload=function(){ie ? get(id).style.filter='alpha(opacity=100)':get(id).style.opacity=1;}
	get(id).src='upload/'+img;
}

addListener(window, 'load', init);
