$(document).ready(function() {

    var baseFile =window.location.pathname.match(/(([a-zA-Z0-9_]+\.php))/);

    if(baseFile == null || baseFile == 'undefined' || baseFile.length == 0) {
        baseFile = '/';
    }
    else {
        baseFile = '/'+baseFile[0];
    }

    $('a.search-more').click(function() {
        form = $('.advanced-search-form');
        if(form.css('display') == 'none') {
            form.slideDown("slow");
            $('input[name=wyszukiwarka]').val('zaawansowana');
            $(this).text('proste')
            $(this).addClass('search-more-open');
            $(this).removeClass('search-more-close');
        } else {
            form.slideUp("slow")
            $('input[name=wyszukiwarka]').val('prosta');
            $(this).text('zaawansowane');
            $(this).addClass('search-more-close');
            $(this).removeClass('search-more-open');
        }
    })

    city = $("input[id*=location_city_string]");
    if(city.length > 0) {
        city.suggest(baseFile+"/oferty/sugeruj-miasto",{
            createItems:function(txt) {
                if (typeof JSON!=='undefined' && typeof JSON.parse==='function')
                    return JSON.parse(txt);
                else
                    return eval('('+ txt +')');
            },
            formatItem:function(item,q) {
                return "<li>"+
                "<b>Miasto</b>: "+item.name+
                
                "</li>";
            },
            selectItemText:function(item) {
                return item.name;
            },
            onSelect: function(item) {
                $('input[id*=location_city_id]').val(item.id);
            }
        });

    }

    $('#my-search-form a.remove').click(function() {
       form = $('#my-search-form');
       $(this).parents('li').remove();
       form.submit();
    });

    /*$('ul.filters a').click(function() {
        //parent = $(this).parents('li');

        hidden = $(this).siblings(':hidden');
        text = $(this).text();
        //$(this).text('x');
        li = $('<li></li>').append(hidden).append($(this)).append('<span class="text">'+text+'</span>');
        $('#my-search-form ul').append(li);
        //parent.remove();

        $('#my-search-form').submit();
        //document.getElementById('my-search-form').submit();
        return false;
    });*/

    /*$('table#offers thead select').change(function() {
        name = $(this).attr('id');
        new_value = $(this).find(':selected').val();

        str = window.location.toString();

        if (str.indexOf("?") == -1)
        {
          separator = "?";
        }else {
          separator = "&";
        }

        re = new RegExp(name+"=.", "i");
        found = str.replace(re, name+"="+new_value);

        if (found == str)
        {
          window.location = str+separator+name+'='+new_value;
        }else {
          window.location = found;
        }
    });*/

    $('form#job_offer_form').submit(function() {
	  $(this).attr('action', $(this).attr('action')
		+"?search="+$('#job_offer_filters_search').val()
		+"&province="+$('#job_offer_filters_location_province_id').val()
		+"&category="+$('#job_offer_filters_categories_list').val()
		+"&contract="+$('#job_offer_filters_contract_id').val()
		+"&edu_level="+$('#job_offer_filters_edu_level_id').val()
		+"&publicated_from="+$('#job_offer_filters_publicated_from').val()
		+"&city="+$('#job_offer_filters_location_city').val()
		+"#search="+$('#job_offer_filters_search').val()
		+"&province="+$('#job_offer_filters_location_province_id').val()
		+"&category="+$('#job_offer_filters_categories_list').val()
		+"&contract="+$('#job_offer_filters_contract_id').val()
		+"&edu_level="+$('#job_offer_filters_edu_level_id').val()
		+"&publicated_from="+$('#job_offer_filters_publicated_from').val()
		+"&city="+$('#job_offer_filters_location_city').val()
	  );
	});
    $('form#training_offer_form').submit(function() {

      $(this).attr('action', $(this).attr('action')
        +"?search="+$('#training_offer_filters_search').val()
        +"&province="+$('#training_offer_filters_location_province_id').val()
        +"&category="+$('#training_offer_filters_categories_list').val()
        +"&publicated_from="+$('#training_offer_filters_publicated_from').val()
        +"&city="+$('#training_offer_filters_location_city').val()
        +"#search="+$('#training_offer_filters_search').val()
        +"&province="+$('#training_offer_filters_location_province_id').val()
        +"&category="+$('#training_offer_filters_categories_list').val()
        +"&publicated_from="+$('#training_offer_filters_publicated_from').val()
        +"&city="+$('#training_offer_filters_location_city').val()
      );
    });
});

