
var form_options = {
    beforeSubmit: function(formData, jqForm, form_options) {
        var $loader = $('#ajax_loader');
        var h = $loader.height(), w = $loader.width();
        $('.magic-box').fadeOut();
        $loader.css({position:'fixed',left:($(window).width()/2)-(w/2),top:($(window).height()/2) - (h/2)}).fadeIn();
    },
    success: function(responseText, statusText, xhr, $form) {
        $('#ajax_loader').fadeOut();
        $('#appcomment_form textarea').val('');
        $('div#appcomment').load('/talent/open/'+responseText+' #appcomment');
    }
}


$(function() {
	$('.hinter').hinter();
    $('.frozen').freezer('#footer');
    $('.hit-me').popbox('open');
    $('.mb-close').popbox('close');
});

/**
 * Freezer
 * "Freeze" (fixed position) and element unless it overlaps the lower_bound element.
 */ 
$.fn.freezer = function(lower_bound) {
    if(!this.length) return;
    (function(scroll_ends, start_point, height, froze) {
      $(window).scroll(function() {
          if($(window).scrollTop() + start_point + height > scroll_ends) {
              froze.css({'position': 'absolute', 'top': scroll_ends - height});
          }
          else {
              froze.css({'position': 'fixed', 'top': start_point});
          }
      });
    })( 
        $(lower_bound).offset()['top'],
        $(this).offset()['top'],
        $(this).outerHeight(),
        this
      );
}


$.fn.popbox = function(job) {
    $(this).click(function() {
        var $box = $('.magic-box');
        var h = $box.height(), w = $box.width();
        if(job=='open') $box.css({position:'fixed',left:($(window).width()/2)-(w/2),top:($(window).height()/2) - (h/2)}).slideDown();
        if(job=='close')$box.slideUp();
    });
}

/**
 * Show "title" attribute of any inputs in selection or children as a "hint"
 * overrides jQuery val() function to prevent hint being given as the input's value.
 *
 *  todo: Applies hard-coded styles
 *
 */ 
$.fn.hinter =  function() {
  var $selection;
  if(!($selection =  $(this).filter('input[title]')).length)
    if(!($selection = $(this).find('input[title]')).length)
      { return this;}

  function hint_on($t) {
      if($t.data('hint_on')) return;
      if($t.val()!='') return;
      $t.css({color:'#AABBCD', fontStyle:'normal'})
        .data('hint_on',true)
        .val($t.attr('title'));
  }
  function hint_off($t) {
    if(!$t.data('hint_on')) return;
    $t.css({color:'#222222', fontStyle:'normal'})
      .data('hint_on',false)
      .val('');
  }

  // browsers will persist input values on page refresh; blank them if found
  $selection.each(function() {
    if($(this).attr('title')==$(this).attr('value')) $(this).val('');
  });

  $selection.blur( function() { hint_on($(this)); })
            .focus( function() { hint_off($(this));})
            .blur(); // call blur to set intial hint

  // val() override
  if(!$.fn.orginal_val) {
    $.fn.orginal_val = $.fn.val;
    $.fn.val = function(val) {
      if(!(typeof(val)=='undefined')) return $(this).orginal_val(val);  
      out = $(this).orginal_val();
      if(out == $(this).attr('title')) return '';
      else return out;
    };
  }
  return this;
}

/**
 * Highlight (wrap with text-highlight span) keywords in text of each element in selection
 *  - note expects *text* not markup
 */ 
$.fn.highlighter = function(keywords) {
  regex = new RegExp('('+keywords.join('|')+')','ig');
  console.log(regex);
  $(this).each(function() {
    $(this).html($(this).text().replace(regex,'<span class="text-highlight">$1</span>'));
  });
}

$(function() {
  $('.rollover').each(function() {
     var rollover = $(this);
     rollover.css({position:'absolute'});
     (function(rollover) {
       rollover.parent().find('a').hover(
         function() {
          rollover.toggle();
         }
       )
     })(rollover);
  });
});

