// JavaScript Document
$(document).ready(function(){
    $('head').append('<script src="js/mask.js" type="text/javascript"></script>');
})

var last_cep = 0;
var address;
var lat;
var lng;
var wsconf;
function wscep(conf)
{
    //parametros padrao true
    if(!conf){
        conf = {
            'auto': true,
            'map' : '',
            'wsmap' : ''
        };
    }
    wsconf = conf;
    //evento keyup no campo cep opcional
    if(wsconf.auto == true){
        $('#Cep').live('keyup',function(){
            var cep = $.trim($('#Cep').val()).replace('_','');
            if(cep.length >= 9){
                if(cep != last_cep){
                    busca();
                }
            }
        });         
    }else{
        var btn_busca = '<div class="input-append input-prepend"><span class="add-on">CEP</span>';
        btn_busca += '<input id="cep" name="cep" style="width:139px!important" type="text" maxlength="9" placeholder="Informe o CEP" />';
        btn_busca += '<button class="btn btn_handler" type="button">Busca</button></div>';
        $('.cep-label').replaceWith(btn_busca)
        $('.btn_handler').live('click',function(){
            busca();
        })
    }    
    $('#Cep').mask('99999-999');    
}
//busca o cep
function busca(){
    var cep = $.trim($('#Cep').val());
    var url = 'https://clareslab.com.br/ws/cep/json/'+cep+'/';    
    if ($.browser.msie) {
        var url = 'ie.php';    
    }
    $.post(url,{
        cep:cep
    },
    function (rs) {
	console.log(rs)
        if(rs != 0){
			rs = $.parseJSON(rs);
            address = rs.endereco + ', ' + rs.bairro + ', ' + rs.cidade + ', ' + ', ' + rs.uf;
            if(wsconf.map != '' ){
                setMap(wsconf.map);
            }
            $('#Rua').val(rs.endereco);
            $('#Bairro').val(rs.bairro);
            $('#Cidade').val(rs.cidade);
            $('#UF').val(rs.uf);
            $('#Cep').removeClass('invalid');
            $('#Num').focus();
            $('#Num').live('change',function(){
                address = rs.endereco + ', ' + $('#Num').val() + ', ' + rs.bairro + ', ' + rs.cidade + ', ' + ', ' + rs.uf;    
                if(wsconf.map != ''){
                    setMap(wsconf.map);
                }
            })
            last_cep = cep;
        }
        else{
            $('#Cep').addClass('invalid');    
            $('#Cep').focus();  
            last_cep = 0;
        }
    })    
}
 
function wsmap(cep,num,elm)
{
    var url = 'https://clareslab.com.br/ws/cep/json/'+cep+'/';    
    if ($.browser.msie) {
        var url = 'ie.php';    
    }    
    $.post(url,{
        cep:cep
    },
    function (rs) {
        if(rs != 0){
        rs = $.parseJSON(rs);
            address = rs.endereco + ', ' + num + ', ' + rs.bairro + ', ' + rs.cidade + ', ' + ', ' + rs.uf;
            setMap(elm);
        }
    })
}
function setMap(elm)
{
    GMaps.geocode({
        address: address,
        callback: function(results, status) {            
            if (status == 'OK') {
                //console.log(elm);
                $('#'+elm).show();
                var latlng = results[0].geometry.location;
                lat = latlng.lat();
                lng = latlng.lng()
                map = new GMaps({
                    div: elm,
                    lat: lat,
                    lng: lng,
                    scrollwheel: false,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    streetViewControl: true,
                    zoom: 14
                })
                map.addMarker({
                    lat: lat,
                    lng: lng,
                    title: address,
                    infoWindow: {
                        content: '<p>'+address+'</p>'
                    }
                });
                map.setCenter(lat, lng);
            }
        }
    });   
     
}
