jQuery(function(){
    /* 显示全部分类 */
    jQuery("#show_category").click(function(){
        jQuery("ul[ectype='ul_category'] li").show();
        jQuery(this).hide();
    });
    
    /* 显示全部品牌 */
    jQuery("#show_brand").click(function(){
        jQuery("ul[ectype='ul_brand'] li").show();
        jQuery(this).hide();
    });
    
    /* 自定义价格区间 */
    jQuery("#set_price_interval").click(function(){
        jQuery("ul[ectype='ul_price'] li").show();
        jQuery(this).hide();
    });
    
    /* 显示全部地区 */
    jQuery("#show_region").click(function(){
        jQuery("ul[ectype='ul_region'] li").show();
        jQuery(this).hide();
    });

    /* 筛选事件 */
    jQuery("ul[ectype='ul_category'] a").click(function(){
        replaceParam('cate_id', this.id);
        return false;
    });
    jQuery("ul[ectype='ul_brand'] a").click(function(){
        replaceParam('brand', this.title);
        return false;
    });
    jQuery("ul[ectype='ul_price'] a").click(function(){
        replaceParam('price', this.title);
        return false;
    });
    jQuery("#search_by_price").click(function(){
        replaceParam('price', jQuery(this).siblings("input:first").val() + '-' + jQuery(this).siblings("input:last").val());
        return false;
    });
    jQuery("ul[ectype='ul_region'] a").click(function(){
        replaceParam('region_id', this.title);
        return false;
    });
    jQuery("li[ectype='li_filter'] img").click(function(){
        dropParam(this.title);
        return false;
    });
    jQuery("[ectype='order_by']").change(function(){
        replaceParam('order', this.value);
        return false;
    });
    
    /* 下拉过滤器 */
    jQuery("li[ectype='dropdown_filter_title'] a").click(function(){
        var jq_li = jQuery(this).parents("li[ectype='dropdown_filter_title']");
        var status = jq_li.find("img").attr("src") == upimg ? 'off' : 'on';
        switch_filter(jq_li.attr("ecvalue"), status)
    });

    /* 显示方式 */
    jQuery("[ectype='display_mode']").click(function(){
        jQuery("[ectype='current_display_mode']").attr('class', jQuery(this).attr('ecvalue'));
        $.setCookie('goodsDisplayMode', jQuery(this).attr('ecvalue'));
    });
});

/** 打开/关闭过滤器
 *  参数 filter 过滤器   brand | price | region
 *  参数 status 目标状态 on | off
 */
function switch_filter(filter, status)
{
    jQuery("li[ectype='dropdown_filter_title']").attr('class', 'normal');
    jQuery("li[ectype='dropdown_filter_title'] img").attr('src', downimg);
    jQuery("div[ectype='dropdown_filter_content']").hide();

    if (status == 'on')
    {
        jQuery("li[ectype='dropdown_filter_title'][ecvalue='" + filter + "']").attr('class', 'active');
        jQuery("li[ectype='dropdown_filter_title'][ecvalue='" + filter + "'] img").attr('src', upimg);
        jQuery("div[ectype='dropdown_filter_content'][ecvalue='" + filter + "']").show();
    }
}

/* 替换参数 */
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)
    {
        value = transform_char(value);
        params.push(key + '=' + value);
    }
	if (re_on)
	{
		location.assign(SITE_URL + location.pathname +'?' + params.join('&'));
	}
	else {
		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);
        }
    }

	if (re_on)
	{
		location.assign(SITE_URL + location.pathname +'?' + params.join('&'));
	}
	else {
		location.assign(SITE_URL + '/index.php?' + params.join('&'));
	}
}

