var my_key = "U5BBZ-AYAWW-UGFRR-O7OX6-CJ3QH-PLFAS";//应用接口key
var my_app = "yubin";//应用名称
var city = '';
var storage = window.sessionStorage;
var geolocation = new qq.maps.Geolocation(my_key, my_app);
//获取当前城市
function get_city(){
    if(storage){
        /*if(storage.getItem('city')){
            return false;
        }*/
        geolocation.getLocation(function(position){
            var city = position.city;
            storage.setItem('city', city);        
        });    
    }    
}

//获取当前地址
function get_address(){
    if(storage){
        /*if(storage.getItem('nowplace')){
            $('#nowplace').val(storage.getItem('nowplace'));
            return false;
        }*/
        geolocation.getLocation(function(position){
            //城市
            var city = position.city;
            storage.setItem('city', city);
            var url = "address.php?location="+ position.lat+","+position.lng;
            $.ajax({
                type : "get", //不支持post方式跨域的
                url : url, //跨域请求的URL
                dataType : "jsonp",
                jsonpCallback:"set_address"
            });    
        });   
    }  
}

//设置当前地址
function set_address(data){
    if(data.status){
        alert(data.status);
        return;
    }
    var address;
    if(/^.*[0-9]{1,}号$/.test(data.result.address)){
        address = data.result.address; 
    }
    var near = data.result.pois[0];
    address = near.address;
	//alert(address);
    if(storage){
        storage.setItem('nowplace', address)   
    }
    $('#nowplace').val(address);
}


//获取当前地址
function get_qqmap_address(){
    if(storage){
        geolocation.getLocation(function(position){
            //城市
            var city = position.city;
            storage.setItem('city', city);
			storage.setItem('lat', position.lat);
			storage.setItem('lng', position.lng);
            var url = "address.php?location="+ position.lat+","+position.lng;
            $.ajax({
                type : "get", //不支持post方式跨域的
                url : url, //跨域请求的URL
                dataType : "jsonp",
                jsonpCallback:"set_qq_address"
            });    
        });   
    }  
}

//设置当前地址
function set_qq_address(data){
    if(data.status){
        alert(data.status);
        return;
    }
    var address;
    if(/^.*[0-9]{1,}号$/.test(data.result.address)){
        address = data.result.address; 
    }
    var near = data.result.pois[0];
    address = near.address;
    if(storage){
        storage.setItem('nowplace', address)   
    }
	var city = storage.getItem('city');
	var lat = storage.getItem('lat');
	var lng = storage.getItem('lng');

	init_address(city,lat,lng,address);
	//alert('lat='+lat+' lng='+lng);
    //$('#nowplace').val(address);

}



//计算价格
function calculate_price(){
    return false;
    if($('#nowplace').val() != '' && $('#toplace').val() != ''){
        $('#price-div').removeClass('hide');
        $('#price-div').addClass('center');
    }else{
        $('#price-div').addClass('hide');
        $('#price-div').removeClass('center');   
    }
    $('#price').html('33.3');
}

//参数值
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

//显示时间设置
function show_time(obj){
    if($(obj).is(":checked")){
        $('#showtime').addClass('hide');
    }else{
        $('#showtime').removeClass('hide');
    }
}

//获取车型配置
function get_config_car_type(){
    $.post('get_config.php', {type:'car_type'}, function(data){
        var html = "<option value='0'>请选择</option>";
        if(data.code){
            $.each(data.desc, function(k, v){
                html += "<option value='"+ v +"'>"+ v +"</option>";
            });
        }
        $('#car').html(html);
    }, 'json');
}

//获取机场配置
function get_config_airport(){
    $.post('get_config.php', {type:'airport'}, function(data){
        var html = "<option value='0'>请选择</option>";
        if(data.code){
            $.each(data.desc, function(k, v){
                html += "<option value='"+ v +"'>"+ v +"</option>";
            });
        }
        $('#airport').html(html);
    }, 'json');
}

//获取物品配置
function get_config_good(){
    $.post('get_config.php', {type:'good'}, function(data){
        var html = "<option value='0'>请选择</option>";
        if(data.code){
            $.each(data.desc, function(k, v){
                html += "<option value='"+ v +"'>"+ v +"</option>";
            });
        }
        $('#good').html(html);
    }, 'json');   
}

function change_nowplace(){
    var address = $('#nowplace').val();
    address_list(address);
}

function address_list(address){
    var url = "search.php?city="+ city +'&keyword='+ address;
    $.getJSON(url, function(data){
        if(data.status){
            alert(data.status);
            return;
        }
        var html = ''
        $.each(data.data, function(k,v){
            html += '<li class="ui-border-b" onclick="my_close(\''+ v.address +'\',\''+ v.location.lat +'\',\''+ v.location.lng +'\')">\
                        <div class="ui-list-info">\
                            <h4 class="ui-nowrap">'+ v.title +'</h4>\
                            <p class="ui-nowrap">'+ v.address +'</p>\
                        </div>\
                    </li>';
        });
        $('#address-list').html(html);
    });
}

function my_close(val,lat,lng){
	var gourl = $("#urls").val();
	var mode = $("#mode").val();
	var dizhi = $("#dizhi").val();
    if(storage){
		if(mode=='nowplace' || dizhi=='nowplace'){
			if(val == 0){
				storage.setItem('nowsearrch', $('#nowplace').val());
				storage.setItem('nowplace', '');
				storage.setItem('fromlat', '');    
				storage.setItem('fromlng', '');  
			}else{
				storage.setItem('nowsearrch', '');
				storage.setItem('nowplace', val); 
				storage.setItem('fromlat', lat);    
				storage.setItem('fromlng', lng);  
			}
		}else if(mode=='toplace'){
			if(val == 0){
				storage.setItem('tosearrch', $('#nowplace').val());
				storage.setItem('toplace', '');    
				storage.setItem('tolat', '');    
				storage.setItem('tolng', '');    
			}else{
				storage.setItem('toplace', val); 
				storage.setItem('tolat', lat);    
				storage.setItem('tolng', lng);  
			}
		}else if(gourl=='add_wiring'){
			storage.setItem('nowplace', val); 
			storage.setItem('fromlat', lat);    
			storage.setItem('fromlng', lng);  
		}
    }
	gourl = gourl.replace('GLORYDEFINE','&');

	if(gourl=='store_add'){
		window.location.href = "store.php?act="+gourl+"&mode="+mode;
	}else if(gourl=='expose_park'){
		window.location.href = "civil.php?act="+gourl+"&dizhi=nowplace";
	}else if(gourl=='expose_add'){
		window.location.href = "civil.php?act="+gourl+"&dizhi=nowplace";
	}else if(gourl=='add_wiring'){
		window.location.href = "village.php?act="+gourl+"&wiring_id="+mode;
	}else{
		window.location.href = "travel.php?act="+gourl+"&mode="+mode;
	}
    
}
