$(document).ready(function(){
	/**
	 * Função para exibir o conteúdo dos menus da lista de exames e procedimentos
	 * @author Ítalo Santiago
	 * @since 12/09
	 * @param {Object} this
	 */
    $('.menuExames a').click(function(){
		//pega a classe do link clicado
		var item_exame = $(this).attr('class');
		
		$('.conteudoexames').show();
		$('.conteudoexames div').not('.lista_preparos').hide();
		$('.conteudoexames div.'+item_exame).show();
		$('.voltarExames').show();
        
        return false
    });
    
    
    function nl2br (str, is_xhtml) {
        
        var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';
     
        return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
    }
    
    $('.listaprocedimentos dd a').click(function(){
    	
    	var url = $(this).attr('href');
    	
    	$.ajax({
    		  url		: url,
    		  success	: function(data) {
    			var html = nl2br(data, false);
    			alert(html)
    		  }
    	});
    	
    	return false;
    	
    })
	
    $('.listaconvenios a').click(function(){
		if($(this).attr('class') == 'convenio_vazio') return false;
        $('.listaTodosConvenios div').hide().parent().find( '.' + this.className ).show()
        return false
    });	
	
	$('.mapa a').click(function(){
		
		if($(this).attr('id') == 'Unidade1'){
			var mapa = '<div id="mapa"><iframe width="425" height="320" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com.br/maps?f=q&amp;source=s_q&amp;hl=pt-BR&amp;geocode=&amp;q=Padre+Rolim,+101+-+S%C3%A3o+Lucas&amp;sll=-14.179186,-50.449219&amp;sspn=104.764959,158.027344&amp;ie=UTF8&amp;hq=&amp;hnear=R.+Padre+Rolim,+101+-+Santa+Efig%C3%AAnia,+Belo+Horizonte+-+MG,+30130-090&amp;ll=-19.92835,-43.922235&amp;spn=0.004135,0.005&amp;z=17&amp;iwloc=A&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com.br/maps?f=q&amp;source=embed&amp;hl=pt-BR&amp;geocode=&amp;q=Padre+Rolim,+101+-+S%C3%A3o+Lucas&amp;sll=-14.179186,-50.449219&amp;sspn=104.764959,158.027344&amp;ie=UTF8&amp;hq=&amp;hnear=R.+Padre+Rolim,+101+-+Santa+Efig%C3%AAnia,+Belo+Horizonte+-+MG,+30130-090&amp;ll=-19.92835,-43.922235&amp;spn=0.004135,0.005&amp;z=17&amp;iwloc=A" style="color:#0000FF;text-align:left">Exibir mapa ampliado</a></small></div>';			
		}else{
			var mapa = '<div id="mapa"><iframe width="425" height="320" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com.br/maps?f=q&amp;source=s_q&amp;hl=pt-BR&amp;geocode=&amp;q=Rua+Cear%C3%A1,+505+-+Santa+Efig%C3%AAnia&amp;sll=-19.928349,-43.92223&amp;sspn=0.007242,0.009645&amp;ie=UTF8&amp;hq=&amp;hnear=R.+Cear%C3%A1,+505+-+Santa+Efig%C3%AAnia,+Belo+Horizonte+-+MG,+30150-310&amp;ll=-19.925647,-43.92658&amp;spn=0.003429,0.00456&amp;z=17&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com.br/maps?f=q&amp;source=embed&amp;hl=pt-BR&amp;geocode=&amp;q=Rua+Cear%C3%A1,+505+-+Santa+Efig%C3%AAnia&amp;sll=-19.928349,-43.92223&amp;sspn=0.007242,0.009645&amp;ie=UTF8&amp;hq=&amp;hnear=R.+Cear%C3%A1,+505+-+Santa+Efig%C3%AAnia,+Belo+Horizonte+-+MG,+30150-310&amp;ll=-19.925647,-43.92658&amp;spn=0.003429,0.00456&amp;z=17" style="color:#0000FF;text-align:left">Exibir mapa ampliado</a></small></div>';						
		}
		
		$('body').append(mapa);
		$("#mapa").dialog({
			modal		: true,
			width		: 465,
			height		: 410,
			title		: 'Mapa',
			resizable	: false,
			draggable	:false,
			close		: function(event, ui){
				$("#mapa").dialog( 'destroy' );
				$("#mapa").remove();
			}
		});
		return false;
	});
	
	$('.box_preparo').click(function(){
		
		var preparo = $(this).attr('id');
		
		var box = '<div id="dialog">'+$('.'+preparo).html()+'</div>';
		
		$('body').append(box);
		$("#dialog").dialog({
			modal		: true,
			width		: 465,
			height		: 410,
			title		: 'Preparos',
			resizable	: false,
			draggable	:false,
			close		: function(event, ui){
				$("#dialog").dialog( 'destroy' );
				$("#dialog").remove();
			}
		});
		return false;
	});
	
});

function salvaAtendimento(idForm){
						
	var BASE_URL = 'http://www.imrad.com.br/novo/';
	
	$.ajax({
		type: "POST",
		url: BASE_URL+"salvar.php",
		data: $(idForm).serialize(),
		success: function(msg){
			alert( msg );
		}
	});
}


/**
 * alert
 * sobrescreve o alert padrão por um dialog
 *
 * @author	Luiz Tanure
 * @param	String mensagem
 * @return	Boolean
 *
 */
function alert(msg){

	var divDialog = '<div id="dialog" style="font-size:12px; padding:15px;">'+msg+'</div>';

	$('body').append(divDialog);

	$("#dialog").dialog({
		modal		: true,
		title		: 'Preparo',
		resizable	: false,
		draggable	:false,
		close		: function(event, ui){
			$("#dialog").dialog( 'destroy' );
			$("#dialog").remove();
		}
	});
	return false;

}


