/* 位置多级选择相关函数，适合店铺搜索页
 * multi-level selection
 */


/* 地区选择函数 */
function locateInit(divId)
{
    $("#" + divId + " > select").change(locateChange); // select的onchange事件
    $("#" + divId + " > input:button[class='edit_locate']").click(locateEdit); // 编辑按钮的onclick事件
}

function locateChange()
{
    // 删除后面的select
    $(this).nextAll("select").remove();
	// 删除已选择的store_num 值
    $(this).siblings(".mls_id").attr("value","0");

    // 计算当前选中到id和拼起来的name
    var selects = $(this).siblings("select").andSelf();
    var id = 0;
    var names = new Array();
    for (i = 0; i < selects.length; i++)
    {
        sel = selects[i];
        if (sel.value > 0)
        {
            id = sel.value;
            name = sel.options[sel.selectedIndex].text;
            names.push(name);
        }
    }


    $(".mls_id").val(id);

    // ajax请求下级地区
    if (this.value > 0)
    {
        var _self = this;
        var url = SITE_URL + '/index.php?app=mlselection&type=locate';
        $.getJSON(url, {'pid':this.value}, function(data){
            if (data.done)
            {
                if (data.retval.length > 0)
                {
                    $("<select><option value='0'>" + lang.select_pls + "</option></select>").change(locateChange).insertAfter(_self);
                    var data  = data.retval;
                    for (i = 0; i < data.length; i++)
                    {
                        $(_self).next("select").append("<option value='" + data[i].locate_id + "'>" + data[i].locate_name + "</option>");
                    }
                }
            }
            else
            {
                alert(data.msg);
            }
        });
    }
}

function locateEdit()
{
    $(this).siblings("select").show();
    $(this).siblings("span").andSelf().hide();
	//在我的店铺点击修改位置属性时,设置当前页面locate_id为0
	//Date:2009.10.25 by Ian
    $(this).siblings(".mls_id").attr("value","0");

}


