var Jobs = {
  /* taken from prototype.js */
  Browser: {
    IE:     !!(window.attachEvent &&
      navigator.userAgent.indexOf('Opera') === -1),
    Opera:  navigator.userAgent.indexOf('Opera') > -1,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && 
      navigator.userAgent.indexOf('KHTML') === -1,
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
  },

  proxy: 'http://www.hulu.com/jobs/proxy',

  getSessionId: function (src) {
    var start = src.indexOf(';jsessionid');
    var end = src.indexOf('?');
    if (start != -1 && end > start)
      src = src.substring(start, end);
    else
      src = '';
    return src;
  },

  TBE_attachments: function () {
    document.TBE_theForm.act.value="attachments";
    document.TBE_theForm.action='http://tbe.taleo.net/NA12/ats/careers/Attachments.jsp' + Jobs.getSessionId(document.TBE_theForm.action) + '?org=HULU&cws=1';
    document.TBE_theForm.submit();
    return false;
  },

  TBE_removeAtt: function (attachmentNo) {
    document.TBE_theForm.act.value="removeAtt";
    document.TBE_theForm.attNo.value=attachmentNo;
    document.TBE_theForm.action='http://tbe.taleo.net/NA12/ats/careers/apply.jsp' + Jobs.getSessionId(document.TBE_theForm.action) + '?org=HULU&cws=1';
    document.TBE_theForm.submit();
    return false;
  },

  sendMessage: function () {
    // hack to set submit button's style and remove <br> before table and make <hr> align
    // It's jquery code since taleo uses jquery
    if (location.href.indexOf('http://tbe.taleo.net') != -1) {
      if (location.href.indexOf('#previous') != -1) {
          var bs = $("b:contains('Previous Applicants')");
          if (bs.size() > 0) {
            var prevTR = $(bs[0]).parent().parent().parent();
            var children = prevTR.parent().children();
            for (var i = 1; i < children.size() && children[i] != prevTR[0]; i++) {
              $(children[i]).remove();
            }
            //IE needs to remove another time
            children = prevTR.parent().children();
            for (var i = 1; i < children.size() && children[i] != prevTR[0]; i++) {
              $(children[i]).remove();
            }
          }
      }
      var inputs = $('input');
      for (var i = 0; i < inputs.size(); i++) {
        var e = inputs[i];
        if (e.type == 'submit') {
          $(e).addClass('submit');
          if(e.value == 'Search' || e.value == 'Login') {
            if(i > 0) {
              var left = $(inputs[i - 1]).offset().left - 4;
              $(e).css("marginLeft", left + "px");
            }
            else
              $(e).addClass('marginleft');
          }
        }
      }
      $("table:first").prevAll("br").remove();
      
      // Hack to change /jobs/what_defines_hulu to /jobs/load_what_defines_hulu when in Hulu iframe.
      // This code should only be loaded when within a Hulu iframe.
      var hulu_link = $("a:contains('What Defines Hulu')");
      for (var i = 0; i < hulu_link.size(); i++) {
        var hl = hulu_link[i];
        hl.href = "http://www.hulu.com/jobs/load_what_defines_hulu";
      }
      
      if(Jobs.Browser.IE && (location.href.indexOf('apply.jsp') != -1 || location.href.indexOf('applicantEdit.jsp') != -1)) {
        $("br:last").remove();
        $("br:last").remove();
        var as = $("a:contains('Add Resume')");
        if (as.size() > 0) {
          var a = as[0];
          $(a).unbind('click');
          $(a).attr('onclick','');
          $(a).bind('click',Jobs.TBE_attachments);
        }
        as = $("a:contains('Remove')");
        for(var i = 0; i < as.size(); i++) {
          var a = as[i];
          $(a).unbind('click');
          $(a).attr('onclick','');
          // we can't simply use i here.
          if (i == 0) $(a).bind('click', function() { Jobs.TBE_removeAtt(0);});
          else if (i == 1) $(a).bind('click', function() { Jobs.TBE_removeAtt(1);});
          else if (i == 2) $(a).bind('click', function() { Jobs.TBE_removeAtt(2);});
          else if (i == 3) $(a).bind('click', function() { Jobs.TBE_removeAtt(3);});  
        }
      }
      if(Jobs.Browser.IE && location.href.indexOf('Attachments.jsp') != -1) {
        var form = $("form:first");
        if (form.size() > 0) {
          form = form[0];
          var param = Jobs.getSessionId(window.location.href);
          var action = form.action;
          var index = action.indexOf('?');
          if (index != -1) action = action.substring(0, index) + param + action.substring(index);
          form.action = action;
        }
      }
      if(Jobs.Browser.IE) $("tr:eq(1) > td > hr").css("marginTop","-9px");
      else $("tr:eq(1) > td > hr").css("marginTop","-2px");
    }

    var height;
    if (Jobs.Browser.IE) height = document.body.scrollHeight;
    else if (Jobs.Browser.WebKit) height = document.getElementById('bottom-div').offsetTop;
    else height = document.body.offsetHeight;
    
    if (Jobs.Browser.IE || Jobs.Browser.Gecko || (Jobs.Browser.WebKit && !window.postMessage)) { // IE, FF and Safari 3
      // Create a new hidden iframe.
      var el = document.createElement("iframe");
      el.style.position = "absolute";
      el.style.visibility = "hidden";
      el.style.top = el.style.left = "0";
      el.style.width = el.style.height = "0";
       // Listen for the onload event.
      if (el.addEventListener) {
        el.addEventListener("load", function () {
          // First, remove the event listener or the iframe
            // we intend to discard will not be freed...
          // Discard the iframe...
          setTimeout(function () {
              document.body.removeChild(el);
          }, 1000);
      },false);}
      else {
        el.attachEvent("onload", function () {
          // First, remove the event listener or the iframe
          // we intend to discard will not be freed...
          // Discard the iframe...
          setTimeout(function () {
              document.body.removeChild(el);
          }, 1000);
        });
      }
  
      // Compose the message...
      s = "height=" + escape(height) + "&src=" + escape(location);
      // Set its src first...
      el.src = Jobs.proxy + "#" + s;
      // ...and then append it to the body of the document.
      document.body.appendChild(el);
    }
    else {
     var msg = "height=" + escape(height) + "&src=" + escape(location);
     if(window.postMessage) { // HTML 5 Standard
       window.parent.postMessage(msg, "http://www.hulu.com");
     }
     else if(window.document && window.document.postMessage) { // Opera 9
       window.parent.document.postMessage(msg, "http://www.hulu.com");
     }
    }
  }
};
