﻿jQuery.fn.setPostType = function()
{
  $('.posType').hide();
  var posTypeSelected = this.val();
  if(posTypeSelected == 'apt') 
  {
    $('.apt').show();
  } 
  else if(posTypeSelected == 'fac') 
  {
    $('.fac').show();
  }
  else if(posTypeSelected == 'exec') 
  {
    $('.exec').show();
  };
  return this; 
};

$(document).ready(function() {
  // tooltip to describe fields on form
  $('.toolTip').hover(
    function() {
    this.tip = this.title;
    $(this).append(
     '<div class="toolTipWrapper">'
        +'<div class="toolTipTop"></div>'
        +'<div class="toolTipMid">'
          +this.tip
        +'</div>'
        +'<div class="toolTipBtm"></div>'
      +'</div>'
    );
    this.title = "";
    this.width = $(this).width();
    $(this).find('.toolTipWrapper').css({'top': -150 + 'px', 'left': 10 + 'px'})
    $('.toolTipWrapper').fadeIn(200);
  },
    function() {
      $('.toolTipWrapper').fadeOut(100);
      $(this).children().remove();
        this.title = this.tip;
      }
  );
  // table paging
  $('a.newpg').live('click',(function() 
  {
    var url = $(this).attr('href') + "&random=" + Math.random()*99999;
    $('.grid').load(url);
    return false;
  }));
  // table sorting
  $('a.sortlink').live('click',(function()
  {
    $('.grid').load($(this).attr('href'));
    return false;
  }));  
  // results of posType selection 
  $('.posTypeSelector').change(function() 
  {
    $(this).setPostType();
  });  
  // all campus click then click or unclick all campuses
  $('.cmpall').click(function()
  {
    if(this.checked)
    {
      $('.oahu').attr('checked', true);
      $('.outerisle').attr('checked', true);
      $('.oahuall').attr('checked', '');      
    }
    else
    {
      $('.oahu').attr('checked', '');
      $('.outerisle').attr('checked', '');      
    }
  });
  // oahu all campus click then click or unclick oahu campuses
  $('.oahuall').click(function()
  {
    if(this.checked)
    {
      $('.oahu').attr('checked', true);
      $('.outerisle').attr('checked', '');
      $('.cmpall').attr('checked', '');      
    }
    else
    {
      $('.oahu').attr('checked', '');      
    }
  });
  // single oahu unclick then unclick oahu all and all campuses
  $('.oahu').click(function()
  {
    if(!this.checked)
    {
      $('.oahuall').attr('checked', '');
      $('.cmpall').attr('checked', '');      
    }    
  });
  // single outerisle unclick then unclick all campuses
  $('.outerisle').click(function()
  {
    if(!this.checked)
    {
      $('.cmpall').attr('checked', '');      
    }    
  });
  // discall click then click or unclick disciplines
  $('.discall').click(function()
  {
    if(this.checked)
    {
      $('.disc').attr('checked', true);          
    }
    else
    {
      $('.disc').attr('checked', '');      
    }
  });
  // single disc unclick then unclick discall campuses
  $('.disc').click(function()
  {
    if(!this.checked)
    {
      $('.discall').attr('checked', '');      
    }    
  });
  // intial set up for apt, exec, fac
  $('.posTypeSelector').setPostType(); 
  // ad preview section
  var $adpreview = $('.adpreview');
  var $previewinfo = $('<div id="previewInfo"></div>').appendTo('body');
  var positionPreviewInfo = function(event) 
  {
    var windowHeight = $(window).height();
    var previewInfoHeight = $previewinfo.height();
    var windowScrollTop = $(window).scrollTop();
    var posX = event.pageX;
    var posY = event.pageY;
    var tPosX = posX + 20;
    var tPosY;
    if((posY - previewInfoHeight) < windowScrollTop)
    {
        tPosY = posY + 15;
    }
    else if((posY + previewInfoHeight) >= ((windowHeight + windowScrollTop) - 100))
    {
        tPosY = posY - (previewInfoHeight + 15);
    }
    else
    {
        tPosY = posY + 15;
    }
    $previewinfo.css({top: tPosY, left: tPosX});
  };
  var showPreviewInfo = function(event)
  {
    var urlValue = event.target;
    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "adpreview.asmx/GetAdPreview",
      data: "{'url':'" + urlValue + "'}",
      dataType: "json",
      success: function(msg) {
      $previewinfo.html(msg.d).show();
    }

    });
    //$previewinfo.text(html).show();
    positionPreviewInfo(event); 
  };
  var hidePreviewInfo = function()
  {
    $previewinfo.hide();
  };
  $adpreview.live('mouseover', function(event)
  {
    showPreviewInfo(event);
  });
  /*
  $adpreview.live('mousemove', function(event)
  {
    positionPreviewInfo(event);
  }); 
  */
  $('.closeButton').live('click', function()
  {
    hidePreviewInfo();
  });
  var posnoRegex;
  
  $('form :input').blur(function() 
  {
    var $parentDiv = $(this).parents('div:eq(1)');
    $parentDiv.removeClass('error-warning').prev('span.error-message').remove();
    if((/_txtPosno$/).test(this.id))  
    {      
      if(this.value != '' && !/^[a-zA-Z0-9]{3,8}$/.test(this.value))
      {
        var errorMessage = 'The position number contains only numbers and letters. You'
                    + ' must enter between 3 and 8 characters.';
        $('<span></span>')
          .addClass('error-message')
          .text(errorMessage)
          .insertBefore($parentDiv);
        $parentDiv.addClass('error-warning');
      }      
    }
    if((/_txtKeyword$/).test(this.id))  // className if better
    {
      if(this.value != '' && !/^[a-zA-Z0-9 "]{3,60}$/.test(this.value))
      var errorMessage = 'The keyword search contains only letters, numbers, spaces and quotation marks and must be between 3 and 60 characters.';
      $('<span></span>')
          .addClass('error-message')
          .text(errorMessage)
          .insertBefore($parentDiv);
        $parentDiv.addClass('error-warning');
    }    
  });
  $('form').submit(function()
  {
    $('#submit-message').remove();
    $(':input.tvalidate').trigger('blur');
    
    var numWarnings = $('.error-warning', this).length;
    if(numWarnings)
    {
      var fieldList = [];
      $('.error-warning').each(function()
      {
        if((/_posnoRow$/).test(this.id))
        {
          fieldList.push('Position Number');
        }
        else if((/_keywordRow$/).test(this.id))
        {
          fieldList.push('Keyword Search');
        }
      });
      var fieldMessage;  
      if(numWarnings == 1)
      {
        fieldMessage = 'Please correct error with the following field:<br />';        
      }
      else
      {
        fieldMessage = 'Please correct error with the following ' + numWarnings + ' fields:<br />';        
      }  
      $('<div></div>')
      .attr({
        'id': 'submit-message',
        'class': 'error-warning'
      })
      .append(fieldMessage)
      .append('&bull; ' + fieldList.join('<br />&bull; '))
      .insertBefore('.ohrbutton');        
      
      return false;
    }
    
  }); 
});
