$(function(){
    $(".w92").change(function(){
        replaceParam('order', this.value);
    });
    $(".conbox a").click(function(){
        replaceParam('cate_id', this.id);
    });
    $(".linebox_ul .brand a").click(function(){
        replaceParam('brand', this.title);
    });
    $(".linebox_ul .price a").click(function(){
        replaceParam('price', this.title);
    });
    $(".linebox_ul .region a").click(function(){
        replaceParam('region_id', this.title);
    });
    $(".attribute img").click(function(){
        dropParam(this.title);
    });
    $("#search_by_price").click(function(){
        replaceParam('price', $(this).siblings("input:first").val() + '-' + $(this).siblings("input:last").val());
    });
});

/* 替换参数 */
function replaceParam(key, value)
{
    var params = location.search.substr(1).split('&');
    var found  = false;
    for (var i = 0; i < params.length; i++)
    {
        param = params[i];
        arr   = param.split('=');
        pKey  = arr[0];
        if (pKey == 'page')
        {
            params[i] = 'page=1';
        }
        if (pKey == key)
        {
            params[i] = key + '=' + value;
            found = true;
        }
    }
    if (!found)
    {
        params.push(key + '=' + value);
    }
    location.assign(SITE_URL + '/index.php?' + params.join('&'));
}

/* 删除参数 */
function dropParam(key)
{
    var params = location.search.substr(1).split('&');
    for (var i = 0; i < params.length; i++)
    {
        param = params[i];
        arr   = param.split('=');
        pKey  = arr[0];
        if (pKey == 'page')
        {
            params[i] = 'page=1';
        }
        if (pKey == key)
        {
            params.splice(i, 1);
        }
    }
    location.assign(SITE_URL + '/index.php?' + params.join('&'));
}

