/* Author: 

*/

$(document).ready(function(){

  // add first and last classes to all lists, table rows, table heads and table cells in the DOM
  $('.ie7 ul li:first-child, .ie8 ul li:first-child, .ie7 table tr:first-child, .ie8 table tr:first-child, .ie7 tr td:first-child, .ie8 tr td:first-child, .ie7 tr th:first-child, .ie8 tr th:first-child').addClass("first");
  $('.ie7 ul li:last-child, .ie8 ul li:last-child, .ie7 table tr:last-child, .ie8 table tr:last-child, .ie7 tr td:last-child, .ie8 tr td:last-child, .ie7 tr th:last-child, .ie8 tr th:last-child').addClass("last");

  $('#specialism').hide();

  $("#category").selectSubcategory({
    url: 'http://www.abilitypeople.com/search/search_helper_type_segment/',
    subcategoryid: 'job_type_category'
  });

  $("#category").change(function() {
    var sector=$("#category option:selected").text();
    if (sector !== "Engineering")
    {$('#specialism').hide();}
    else
    {$('#specialism').show();}
  });

  $("#category").selectSubcategory({
    url: 'http://www.abilitypeople.com/search/search_helper_specialism_segment/',
    subcategoryid: 'specialism'
  });

  frm_location = $("#location").change();
  frm_category = $("#category").change();
  frm_specialism = $("#specialism").change();
  frm_job_type_category = $("#job_type_category").change();

  $('#job_form').submit(function() {

    frm_location = frm_location.val().replace("&&", "");
    if (frm_location !== "") {
      frm_location = frm_location + "/";
    }
    else
    {
      frm_location = "all" + "/";
    }

    frm_category = frm_category.val().replace("&&", "");
    if (frm_category !== "") {
      frm_category = frm_category + "/";
    }
    else
    {
      frm_category = "all" + "/";
    }

    frm_specialism = frm_specialism.val().replace("&&", "");
    if (frm_specialism !== "" && frm_specialism !== "undefined") {
      frm_specialism = frm_specialism + "/";
    }
    else
    {
      frm_specialism = "all" + "/";
    }

    frm_job_type_category = frm_job_type_category.val().replace("&&", "");
    if (frm_job_type_category !== "" && frm_job_type_category !== "undefined") {
      frm_job_type_category = frm_job_type_category + "/";
    }
    else
    {
      frm_job_type_category = "all" + "/";
    }

    var categories_url = $("form[name='search_form']").attr("action") + frm_location + frm_category + frm_specialism + frm_job_type_category;
    $("form[name='search_form']").attr("action", categories_url);
  });
  
  if ($('input#country-select-save').length > 0) {
    var site = $.cookie('default_site');
    if (site != null && site != undefined && site != '') {
      window.location = site;
    }
    
    $('a#link-au').click(function(e) {
      if ($('input#country-select-save').is(':checked')) {
        $.cookie('default_site', 'http://www.abilitypeople.com.au', {expires: 365});
      }
    });
    
    $('a#link-uk').click(function() {
      if ($('input#country-select-save').is(':checked')) {
        $.cookie('default_site', 'http://www.abilitypeople.co.uk', {expires: 365});
      }
    });
  }

});
