$(function(){
    /* 显示全部分类 */
    $("#show_category").click(function(){
        $("ul[ectype='ul_category'] li").show();
        $(this).hide();
    });
    
    /* 显示全部品牌 */
    $("#show_brand").click(function(){
        $("ul[ectype='ul_brand'] li").show();
        $(this).hide();
    });
    
    /* 自定义价格区间 */
    $("#set_price_interval").click(function(){
        $("ul[ectype='ul_price'] li").show();
        $(this).hide();
    });
    
    /* 显示全部地区 */
    $("#show_region").click(function(){
        $("ul[ectype='ul_region'] li").show();
        $(this).hide();
    });

    /* 筛选事件 */
    $("ul[ectype='ul_category'] a").click(function(){
        replaceParamKeyword('cate_id', this.id, this.name);

    });
    $("ul[ectype='ul_brand'] a").click(function(){
        //replaceParam('brand', this.title);
		replaceParamKeyword('brand', this.title, this.name);
    });
    $("ul[ectype='ul_price'] a").click(function(){
        replaceParam('price', this.title);
		//replaceParamKeyword('price', this.title, this.name);
    });
    $("#search_by_price").click(function(){
      	replaceParam('price', $(this).siblings("input:first").val() + '-' + $(this).siblings("input:last").val());
    });
    $("ul[ectype='ul_region'] a").click(function(){
        replaceParam('region_id', this.title);
    });
    $("li[ectype='li_filter'] img").click(function(){
        dropParam(this.title);
    });
    $("[ectype='order_by']").change(function(){
        replaceParam('order', this.value);
    });
    
    /* 下拉过滤器 */
    $("li[ectype='dropdown_filter_title'] a").click(function(){
        var jq_li = $(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)
    });

    /* 显示方式 */
    $("[ectype='display_mode']").click(function(){
        $("[ectype='current_display_mode']").attr('class', $(this).attr('ecvalue'));
        $.setCookie('goodsDisplayMode', $(this).attr('ecvalue'));
    });
});

/** 打开/关闭过滤器 Open/Close filter
 *  参数 Parameter filter 过滤器   brand | price | region
 *  参数 status 目标状态 on | off
 */
function switch_filter(filter, status)
{
    $("li[ectype='dropdown_filter_title']").attr('class', 'normal');
    $("li[ectype='dropdown_filter_title'] img").attr('src', downimg);
    $("div[ectype='dropdown_filter_content']").hide();

    if (status == 'on')
    {
        $("li[ectype='dropdown_filter_title'][ecvalue='" + filter + "']").attr('class', 'active');
        $("li[ectype='dropdown_filter_title'][ecvalue='" + filter + "'] img").attr('src', upimg);
        $("div[ectype='dropdown_filter_content'][ecvalue='" + filter + "']").show();
    }
}

/* 替换参数 Change Parameter*/ 
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('&'));
	
}

// *** This function will be used in search.app.php to remove the Keyword field fromm URL
function replaceParamKeyword(key, value,category_name)
{
	var params = location.search.substr(1).split('&');
    var found  = false;
	var flag= 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=='keyword')
			{
				flag=true;
			}
        if (pKey == key)
        {
			params[i] = key + '=' + value;
            found = true;
        }
    }
    if (!found)
    {
        params.push(key + '=' + value);
    }
	var j=0;
	paramnew=new Array();	
	for (var i = 0; i < params.length; i++)
    {
		param = params[i];
        arr   = param.split('=');
        pKey  = arr[0];
		if(pKey!='keyword')
			{
				paramnew[j]=param;
				j++;
			}
	}
	//location.assign(SITE_URL + '/index.php?' + paramnew.join('&'));
	location.assign(SITE_URL + '/cd_' + value + '~' + category_name + '.html');
}


/* 删除参数 Delete Parameter */ 
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('&'));
}
