function queryString(ji) {
  hu = window.location.search.substring(1);
  gy = hu.split("&");
  for (i=0;i<gy.length;i++) {
    ft = gy[i].split("=");
    if (ft[0] == ji) {
      return ft[1];
    }
  }
}

$(document).ready(function(){
  // Slide menu setup
  $('#nav ul').lavaLamp({
    fx:"linear",
    speed:300
  });
  // Tweak menu on mac
  var agt=navigator.userAgent.toLowerCase();
  if (navigator.appVersion.indexOf("Mac")!=-1 && agt.indexOf("firefox") != -1) {
    $('#nav ul').addClass('macnav');
  }
  if (navigator.appVersion.indexOf("Mac")!=-1 && agt.indexOf("safari") != -1) {
    $('#nav ul').addClass('macnavsaf');
  }

  // Header option rollovers
  $('#header .buttonGrey').hover(function() {
    $('#infobox-impactextra').hide();
    $('#infobox-myimpact').fadeIn(400, function() {
      $('#infobox-myimpact').delay(8000).fadeOut().stop();
    });
  });
  $('#header .loginpopup').hover(function() {
    $('#infobox-myimpact').hide();
    $('#infobox-impactextra').fadeIn(400, function() {
      $('#infobox-impactextra').delay(8000).fadeOut().stop();
    });
  });

  // Colorbox popups
  $("a[rel='staffpopup']").colorbox();
  $("a.loginpopup").colorbox({
    innerWidth:"530px",
    innerHeight:"265px",
    iframe:true
  });
  $("a.applypopup").colorbox({
    innerWidth:"530px",
    innerHeight:"543px",
    iframe:true
  });
  $("a.contactpopup").colorbox({
    innerWidth:"530px",
    innerHeight:"315px",
    iframe:true
  });
  $('.staffPopup').click(function() {
    id = '#' + $(this).attr('id').replace("popup_", "staff_");
    $(id).trigger('click');
  });

  // Trigger the popup if a login url is in the querystring
  if (queryString("loginpage") != undefined) {
    $("a.loginpopup").trigger('click');
  }
  if (queryString("applypage") != undefined) {
    $("a.applypopup").trigger('click');
  }

  // Rotators
  if (navigator.appName == "Microsoft Internet Explorer") {
    $('ul#bulletins,ul#testimonials').quote_rotator({fade_speed:0});
  } else {
    $('ul#bulletins,ul#testimonials').quote_rotator();
  }
  $('ul.extralinks').quote_rotator();

  // Form placeholders
  $('input[name="keyword"]').attr('placeholder', 'Warehouse, Office etc');
  $('input[name="keyword"]').placeholder();

  $('#newsletteremail').attr('placeholder', 'Email Address');
  $('#newsletteremail').placeholder();

  $('#location').attr('placeholder', 'E.g. NN1 4HX');
  $('#location').placeholder();

  // Toggle search buttons
  $('#searchtoggle .buttonBlue').click(function() {
    $('#searchtoggle .buttonBlue').css('background-position', 'top left');
    $('#searchtoggle .buttonGreen').css('background-position', 'top right');
    $('#searchtoggle').attr('action', vrootdir+'/jobs');
    $('.advSearchJobs').show();
    $('.advSearchCands').hide();
  });
  $('#searchtoggle .buttonGreen').click(function() {
    $('#searchtoggle .buttonBlue').css('background-position', 'bottom left');
    $('#searchtoggle .buttonGreen').css('background-position', 'bottom right');
    $('#searchside').attr('action', vrootdir+'/candidates');
    $('.advSearchJobs').hide();
    $('.advSearchCands').show();
  });

  // Seo slider block
  $('#aboutblock .link').click(function() {
    $('#aboutblock .slide').slideToggle(200);
    $('#aboutblock .link').toggleClass('uparrow');
    return false;
  });
  
  // Newsletter ajax
  $("#newslettersignup").submit(function() {
    $("#newslettersignup img").show();
    
    var dataStr = $("#newslettersignup").serialize();
    $.ajax({
      type: "POST",
      url: vrootdir + "/includes/newsletter-signup.php",
      data: dataStr,
      success: function(response){
        $('#newsletterInvalidEmail,#newsletterNoEmail,#newsletterAlreadySignedup,#newsletterSuccess,#newslettersignup img').hide();

        if (response == 'newsletterSuccess') {
          $('#newslettersignup').hide();
        }

        $('#'+response).fadeIn();
      }
    });
    return false;
  });

  // Shortlist ajax
  $(".linkShortlistJob").click(function() {
    var dataStr = '';
    if ($(this).hasClass('linkShortlistRemove')) {
      $(this).removeClass('linkShortlistRemove');
      $(this).html('Save to My Shortlist');
      dataStr = 'jobid='+$(this).attr('rel')+'&action=remove';
    } else {
      $(this).addClass('linkShortlistRemove');
      $(this).html('Remove from My Shortlist');
      dataStr = 'jobid='+$(this).attr('rel')+'&action=add';
    }
      
    $.ajax({
      type: "POST",
      url: vrootdir + "/includes/jobs-shortlist.php",
      data: dataStr
    });
    return false;
  });
  $(".linkShortlistCandidate").click(function() {
    var dataStr = '';
    if ($(this).hasClass('linkShortlistRemove')) {
      $(this).removeClass('linkShortlistRemove');
      $(this).html('Save to My Shortlist');
      dataStr = 'candidateid='+$(this).attr('rel')+'&action=remove';
    } else {
      $(this).addClass('linkShortlistRemove');
      $(this).html('Remove from My Shortlist');
      dataStr = 'candidateid='+$(this).attr('rel')+'&action=add';
    }

    $.ajax({
      type: "POST",
      url: vrootdir + "/includes/candidates-shortlist.php",
      data: dataStr
    });
    return false;
  });
  $(".linkEmail").click(function() {
    var dataStr = '';
    $(this).html('Email Alert Added');
    dataStr = 'jobid='+$(this).attr('rel');

    $.ajax({
      type: "POST",
      url: vrootdir + "/includes/email-alerts.php",
      data: dataStr
    });
    return false;
  });

  // Switch Business/Candidate help content
  $("input[name=switchContent]").change(function() {
    if ($("input[name=switchContent]:checked").attr('id') == 'switchCandidate') {
      $("#contentBusiness").hide();
      $("#contentCandidate").fadeIn();
    } else {
      $("#contentCandidate").hide();
      $("#contentBusiness").fadeIn();
    }
  });

  // Switch Business/Candidate homepage content
  $(".headingColHome strong").click(function() {
    $(".busCandContentC").hide();
    $(".busCandContentB").fadeIn();
    $('.headingColHomeRight').addClass('headingColHomeLeft');
    $('.headingColHomeRight').removeClass('headingColHomeRight');
    $('.businessesCandidates').addClass('businessesCandidatesGreen');
  });
  $(".headingColHome span").click(function() {
    $(".busCandContentB").hide();
    $(".busCandContentC").fadeIn();
    $('.headingColHomeLeft').addClass('headingColHomeRight');
    $('.headingColHomeLeft').removeClass('headingColHomeLeft');
    $('.businessesCandidates').removeClass('businessesCandidatesGreen');
  });

  // Switch Business/Candidate registration form
  $(".switchRegister input[name=client]").change(function() {
    switchRegister();
    $('html,body').animate({
      scrollTop: $('.fadeBlueLong').offset().top - 15
    }, 400);

    $('#wrapper').css('margin', '0px auto');
  });
  
  $('#registerNext').click(function() {
    if ($("input[name=client]:checked").attr('id') != undefined) {
      $(this).hide();    
      $('.checkBeforeBusiness').remove();
      $('.checkBeforeCandidate').remove();
      $('#registerButton').show();
      $('.registrationFormDiv').slideDown();
      $('html,body').animate({
        scrollTop: $('.fadeBlueLong').offset().top - 15
      }, 400);
    } else {
      alert('Please select if you are a Business or Candidate');
    }
    return false;
  });

  // Switch contact info between Northampton and Corby
  $("#contabNorthampton").click(function() {
    $("#contabContentCorby").hide();
    $("#contabContentNorthampton").fadeIn();
    
    $('#contabNorthampton,#contabCorby').removeClass('contabOff');
    $('#contabCorby').addClass('contabOff');
    return false;
  });
  $("#contabCorby").click(function() {
    $("#contabContentNorthampton").hide();
    $("#contabContentCorby").fadeIn();
    if (!map2) {
      gMapInitialize2();
    }
    
    $('#contabNorthampton,#contabCorby').removeClass('contabOff');
    $('#contabNorthampton').addClass('contabOff');
    return false;
  });
  
});

function switchRegister() {
  if ($("input[name=client]:checked").attr('id') == 'clientY') {
    $(".fadeBlueLong").addClass('fadeGreenLong');
    $(".form50").show();
    $(".fadeRed").hide();
    $(".jobalerts").hide();
    $('#company').addClass('req');
    $('#jobtitle').addClass('req');
    $('#cvupload').removeClass('req');
    $('label[for="cvupload"]').html('Job Specification:<i>*</i>');
        
    $('.checkBeforeBusiness').show();
    $('.checkBeforeCandidate').hide();
  } else {
    $(".fadeBlueLong").removeClass('fadeGreenLong');
    $(".form50").hide();
    $(".fadeRed").show();
    $(".jobalerts").show();
    $('#company').removeClass('req');
    $('#jobtitle').removeClass('req');
    $('#cvupload').addClass('req');
    $('label[for="cvupload"]').html('Upload your CV:');
        
    if ($("input[name=client]:checked").attr('id') != undefined) {
      $('.checkBeforeBusiness').hide();
      $('.checkBeforeCandidate').show();
    }
  }
}

function resizeColorbox (newheight) {
  $.colorbox.resize({
    innerHeight:newheight+"px"
  });
}
