
  $(document).ready(function() {

    var query = '';
    if(location.toString().indexOf("?") == -1)
        query = '?';
    else
        query = location.toString().substring(location.toString().indexOf("?"))+'&';
  });


/**
 * Filtrowanie
 * @param form id
 * @param bool wez url_target z action form'a
 */
function applyFilter(form,action_target)
{
    var query = '';
    var path = '';
    if(location.toString().indexOf("?") == -1)
    {
        query = '?';
        path = location.toString();
    }
    else
    {
        query = location.toString().substring(location.toString().indexOf("?"))+'&';
        path = location.toString().substring(0,location.toString().indexOf("?"));
    }
    if(action_target)
    {
        path = $('#'+form).attr('action');
    }

    $('.filter_form').each(function(index,item)
    {
        //alert($(this).html());
        $(this).find('input').each(function(index) {
            if($(this).attr('type')=='radio' && $(this).attr('checked'))
            {
                var filter = $(this).attr('name');
                if(query.indexOf(filter+'=')>=0)
                {
                    var part1 = query.substring(0,query.indexOf(filter+'='));
                    query = part1+filter+'='+$(this).val()+query.substring(query.indexOf('&',query.indexOf(filter+'=')));
                }
                else
                    query += filter+'='+$(this).val()+'&';
            }
            if($(this).attr('type')=='text')
            {
                var filter = $(this).attr('name');
                if(query.indexOf(filter+'=')>=0)
                {
                    var part1 = query.substring(0,query.indexOf(filter+'='));
                    query = part1+filter+'='+$(this).val()+query.substring(query.indexOf('&',query.indexOf(filter+'=')));
                }
                else
                    query += filter+'='+$(this).val()+'&';
            }
            if($(this).attr('type')=='checkbox')
            {
                var filter = $(this).attr('name')+'='+$(this).val();
                if(query.indexOf(filter+'&')>=0 && !$(this).attr('checked'))
                {
                    var part1 = query.substring(0,query.indexOf(filter+'&'));
                    query = part1+query.substring(query.indexOf('&',query.indexOf(filter+'&'))+1);
                }
                else if(query.indexOf(filter+'&')<0 && $(this).attr('checked'))
                {
                    query += filter+'&';
                }
            }
        });
        $(this).find('select').each(function(index) {
            var filter = $(this).attr('name');
            if(query.indexOf(filter+'=')>=0)
            {
                var part1 = query.substring(0,query.indexOf(filter+'='));
                query = part1+filter+'='+$(this).val()+query.substring(query.indexOf('&',query.indexOf(filter+'=')));
            }
            else
                query += filter+'='+$(this).val()+'&';
        });

    });
    //alert(path+query.substring(0,query.length-1));
    window.location = path+query.substring(0,query.length-1);
}

function clearFilter(form)
{
    var query = '';
    var path = '';
    if(location.toString().indexOf("?") == -1)
    {
        query = '?';
        path = location.toString();
    }
    else
    {
        query = location.toString().substring(location.toString().indexOf("?"))+'&';
        path = location.toString().substring(0,location.toString().indexOf("?"));
    }

    $('#'+form+' input').each(function(index) {
        var filter = $(this).attr('name')+'='+$(this).val();
        if(query.indexOf(filter+'&')>=0)
        {
            var part1 = query.substring(0,query.indexOf(filter+'&'));
            query = part1+query.substring(query.indexOf('&',query.indexOf(filter+'&'))+1);
        }
    });
    //alert(path+query.substring(0,query.length-1));
    window.location = path+query.substring(0,query.length-1);
}

function get_scroll_position()
{
    return window.pageYOffset || document.documentElement.scrollTop || 0;
}

function clearAllFilters()
{
    if(location.toString().indexOf("?") >= 0)
        window.location = location.toString().substring(0,location.toString().indexOf("?"));
}

/**
 * Sortowanie
 */
function sortBy(by)
{
    var query = '';
    var path = '';
    if(location.toString().indexOf("?") == -1)
    {
        query = '?';
        path = location.toString();
    }
    else
    {
        query = location.toString().substring(location.toString().indexOf("?"))+'&';
        path = location.toString().substring(0,location.toString().indexOf("?"));
    }
    if(by=='name' && $('#sort_name').hasClass('desc'))
        by+='1';
    else if(by=='name' && $('#sort_name').hasClass('asc'))
        by+='2';
    else if(by=='price' && $('#sort_price').hasClass('desc'))
        by+='1';
    else if(by=='price' && $('#sort_price').hasClass('asc'))    
        by+='2';
    if(query.indexOf('sort=')>=0)
    {
        var part1 = query.substring(0,query.indexOf('sort='));
        query = part1+'sort='+by+query.substring(query.indexOf('&',query.indexOf('sort=')));
    }
    else
        query += 'sort='+by+'&';
    window.location = path+query.substring(0,query.length-1);
}

