function loadFlash() {
  // does nothing.  overwrite to load SWFObject on pages using Flash
}

$(document).ready(function() {
  $('a[href^=/popup/]').click(function() {
    openPopupWindow(this.href, 600, 388);
    return false;
  });

  $('a.popup').click(function() {
    openPopupWindow(this.href, 600, 388);
    return false;
  });

  $(".list-item").hover(function() {
    $(this).addClass("list-item-over");
  }, function() {
    $(this).removeClass("list-item-over");
  });
});

function togglePanel(el) {
  if(typeof el == "string")
    el = document.getElementById(el);
  
  $btn = $(el);
  pid = el.id.replace('btn', 'panel');

  if(!document.getElementById(pid)) {
    $btn.after('<div id="'+pid+'" class="dropdown"><div class="bd"><div class="wait"></div></div></div>');
    href = $btn.attr("href");
    if(href.indexOf("?") != -1) {
      href = href.replace("?", ".jsp?");
    } else {
      href += ".jsp";
    }
    if('https:' == document.location.protocol) {
      href = "/secure/pod.jsp?url=" + href.replace("?", "&");
    }

    if(console && console.log)
      console.log("href = " + href);
    $("#" + pid + " .bd").load(href);
  }
  $panel = $("#" + pid);
  $btn.toggleClass('long-btn-hover');
  $panel.slideToggle('fast');
  if(_gaq)
    _gaq.push(['_trackEvent', el.href, 'Click', window.location]);
  return false;
}

function changeLocation(el) {
  if($(el).val() == '4') {
    el.form.action = '/us/jobs';
    $('#search-states', el.form).show();
  } else {
    el.form.action = '/international/jobs';
    $('#search-states', el.form).hide();
  }
}

function post(frm, url) {
  if(validate(frm)) {
    $.ajax({
      type: 'POST',
      url: url || '/blank.jsp',
      data: $(frm).serialize(),
      success: function(data) {
        //$(frm).parent('.bd').html('<h2>Thank You</h2>Your information has been submitted.');
        $(frm).parent('.bd').html(data
          + '<a href="#" onclick="return togglePanel(\''+ frm.id.replace('form', 'btn')
          +'\')" style="float:right;margin-top:10px;"><img src="/assets/images/cross-grey.png" alt="X" /></a>');

        if(_gaq)
          _gaq.push(['_trackEvent', frm.id, 'Submit', window.location]);
      }
    });
    return false;
  }
  return false;
}


function referAFriend(frm) {
  if(validate(frm)) {
    frm.action = "mailto:" + frm.colleague_email.value
               + "?subject=I thought you might like this"
               + "&body=" + window.location;
    frm.submit();
    //$(frm).parent('.bd').html('<h2>Thank You</h2>Your information has been submitted.');
    if(_gaq)
      _gaq.push(['_trackEvent', frm.id, 'Submit', window.location]);
  }
  return false;
}

function doPodHover(el, out) {
  if(out) {
    $("#" + el.id + "-hover").effect('slide', {direction: 'right', mode: 'hide'});
    //$(el).css('margin-right', '0');
    $(el).animate({
      right: '+=5'
    }, 'fast');

  } else {
    $("#" + el.id + "-hover").effect('slide', {direction: 'right', mode: 'show'});
    //$(el).css('margin-right', '-2px');
    $(el).animate({
      right: '-=5'
    }, 'fast');
  }
}

function validateApplication(frm) {
  return validate(frm, $('.required', frm));
}

function validate(frm, reqs) {
  if(!reqs)
    reqs = $('.required:visible', frm);
  var $first = 0;
  var errors = 0;
  for(i=0; i<reqs.length; i++) {
    $req = $(reqs[i]);
    if(!hasValue($req)) {
      if(!$first) $first = $req;
      $req.addClass('error').change(function() {
        if(hasValue($(this))) {
          $(this).removeClass('error');
        }
      });
      errors++;
    }
  }

  if(errors) {
    alert('Please answer all the required questions.');
    tab = $first.parents(".ui-tabs-panel");
    if(tab) {
      tab.parents(".ui-tabs").tabs('select', tab.attr('id'));
    }
    $first.focus();
    return false;
  }
  return true;
}

function hasValue($req) {
  r = false;
  type = $req.attr('type');
  if(type == 'checkbox' || type == 'radio')
    r = $('input[name='+$req.attr('name')+']:checked').length != 0;
  else
    r = $req.val() != '';

  return r;
}

function openPopupWindow(url, width, height, scrolling, name) {
  if (!width) width = 350;
  if (!height) height = 350;
  if (!scrolling) scrolling = "no"; 
  if (!name) name = "win"+Math.round(Math.random()*1000); 
  
  features = "width="+width+","
           + "height="+height+","
           + "toolbar=no,"
           + "location=no,"
           + "status=no,"
           + "menubar=no,"
           + "scrollbars="+scrolling+","
           + "top="+((window.screen.height-50)-height)/2+","
           + "left="+(window.screen.width-width)/2;
           //+ "top=50,"//+(window.screen.height-height)/2+","
           //+ "left=50";//+(window.screen.width-width)/2;
  window.open(url,'_blank',features);
}

function openNewsletter(u) {
  openPopupWindow(u, 900, $(window).height(), 'yes', 'newsletterWin');
}

function getInspired() {
  openPopupWindow(
    'http://issuu.com/gmedical/docs/get_inspired?mode=embed&layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&showFlipBtn=true',
    $(window).width()-100,
    $(window).height()-50
  );
}

/**
 * Date Selection Functions
 */
  function updateDate(el) {
    frm = el.form;
    n = el.name.substring(0, el.name.lastIndexOf("_"));
    val = '';
    if(frm[n+'_year']) {
      val += formatDate(n);
    }
    if(frm[n+'_hour']) {
      if(val.length > 0)
        val += ' ';
      val += formatTime(n);
    }
    frm[n].value = val;
  }

  function formatDate(s) {
    return v(s+'_year') + "-" + v(s+'_month') + "-" + v(s+'_day');
  }

  function formatTime(s) {
    h = parseInt(v(s+'_hour'));
    if(v(s+'_AmPm') == 'PM')
      h += 12;
    return h + ":" + v(s+'_min') + ":00";
  }

  function v(n) {
    el = document.mainForm[n];
    return el.options[el.selectedIndex].value;
  }

