$(document).ready(function() {

// external links

$('a[rel="external"]').click( function() {
    window.open( $(this).attr('href') );
    return false;
});
$('a[rel="popup"]').click( function() {
    window.open( $(this).attr('href'),'','width=772,height=390,resizable=0,scrollbars=1,location=0,toolbar=0,menubar=0' );
    return false;
});

// video play button
$('.multi-vid .vid').append('<img class="btn-play" src="/assets/styles/images/buttons/play-sm.png" alt="" />');

}); // document.ready

$(document).ready(function() {

  // Video Shadowbox
  if (typeof Shadowbox !== 'undefined') {
    $('a[href$=".mov"], a[href$=".flv"]').attr({
      rel: function() {
        if (this.href.indexOf('top-5') > -1) {
          return 'shadowbox;width=732;height=252';
        } else {
          return 'shadowbox;width=528;height=392';
        }
      },
      title: 'click to close the video'
    });

    var sboptions = {
      assetURL:  '/assets/scripts/shadowbox/',
      loadingImage: 'styles/images/loading.gif',
      overlayBgImage: 'styles/images/overlay-85.png',
      flvPlayer: 'flvplayer.swf',
      keysClose:          ['c', 27]
    };

    // ie6 only options
    if ($.browser.msie === true && parseInt($.browser.version,10) < 7) {
      sboptions.onFinish = function() {
        $('#shadowbox_nav_close')
        .find('a').ifixpng().end()
        .css('left', '-20px');
      };
    }

    Shadowbox.init(sboptions);
  }
  // biglinks
  $('.media-block .vid').biglinks({preventDefault: true});
  $('body.news-listing .article').biglinks();

  // add frame
  $('body.news-read .article img').addClass('framed-alt');

  // add top class if img is first in p
  $('body.news-read .article p').filter(function() {
    var firstKid = this.firstChild,
        firstImage = false;
    if (firstKid.nodeType == '3' && firstKid.nodeValue.replace(/\s/g,'') == '') {
      firstImage = firstKid.nextSibling && firstKid.nextSibling.nodeName == 'IMG';
    } else {
      firstImage = firstKid.nodeName == 'IMG';
    }
    return firstImage;
  }).children('img:first').addClass('top');

});

$(document).ready(function() {
  $('.home-vids a').append('<img class="video-play-btn" src="/assets/styles/images/buttons/play-sm.png" alt="" />');
});

$(document).ready(function() {
  if (typeof $.fn.cycle !== 'undefined') {
    $('.landing-ads .ad-1').cycle({
      timeout:  6000
    });
    $('.landing-ads .ad-2').cycle({
      delay: 3000,
      timeout:  6000
    });
  }
});

// programs list
$(document).ready(function() {
  if ( (/(professional|certificates|associate|bachelor)/i).test($('#nav_cat_archive > li > strong').text()) ) {
    $('#nav_cat_archive').addClass('col').after('<ul id="nav_cat_archive2" class="col programs-list last"></ul>')
      .children().each(function() {
        var cat = $('strong:first', this).text();
        if ( !(/(professional|certificates|associate|bachelor)/i).test(cat) ) {
          $(this).appendTo('#nav_cat_archive2');
        }
    });
  }
});

$(document).ready(function() {
  // prettier headings with js
  $('body.audience h3').addClass('group').wrapInner('<span></span>');
});

/** =request info form
************************************************************/
$(document).ready(function() {
  var $program = $('#program'),
      $linkedOptions = $program.find('option'),
      $degree = $('#degree'),
      rrTxt = '';

  // bail out if no program field
  if ( !$program.length ) {
    return;
  }
  // avoid errors if tinyvalidate not loaded
  if (typeof $.tinyvalidate == 'undefined') {
    $.tinyvalidate = {rules:{requiredradio:{text:'at least one option must be selected'}}};
  } else {
    rrTxt = $.tinyvalidate.rules.requiredradio.text;
  }

  $program.bind('change', function(event) {
    var txt = '',
        rn = false;

    // special behavior for Nursing
    if ($(this).val() == 'Bachelor of Science in Nursing') {
      rn = true;
    }
    txt = rn ? '<div style="text-align:left;">Thanks for your interest. Currently, you must be a Registered Nurse to qualify for the B.S. in Nursing program. If you would like to inquire about another program, please select from the menu above and submit this form.</div>' : rrTxt;
    $.tinyvalidate.rules.requiredradio.text = txt;
    $('#rn').toggleClass('hide', !rn);
  });

  // more special behavior for Nursing
  $('input[name="registered_nurse"]').bind('change', function(event) {
    $('#rn_yes').trigger('blur');
  });
  // if the degree field is "accessibly" hidden, update it based on the program selected
  if ( $degree.parent().is('.js-access') ) {

    $program.change(function() {
      var val = $(this).find('option:selected').attr('class') || '';
      var f = val.charAt(0).toUpperCase();
      var optVal = f+val.substr(1);
      $degree.val(optVal);
    }).trigger('change');

  // if the degree field is visible
  } else {
    $degree.change(function() {
      var degreeId = $degree.find('option:selected').attr('id'),
          $filteredPrograms = degreeId ? $linkedOptions.filter('.'  + degreeId) : '';

      $program
      .html($filteredPrograms)
      .trigger('change');
    });

  }



  /**Filter out some #degree options and #program options
   *based on existence of GET params
   **********************************************************/
  //Plugin to capture get vars
  $.extend({
      getGetParams:   function(){
          var params = {}, param;
          var nvPairs = window.location.search.slice(1).split('&');
          for(var i=0; i<nvPairs.length; i++)
          {
              param = nvPairs[i].split('=');
              params[param[0]] = param[1];
          }
          return params;
      },
      getGetParam:    function(name){
          var params = $.getGetParams();
          return params[name];
      },
      getPathParams:  function(arr){
          var params = {},
              param,
              path = window.location.pathname.replace(/^\//,'');
          var pieces = path.split('/');

          if (arr) {
            return pieces;
          }

          for(var i=0; i<pieces.length; i=i+2) {
              param = [pieces[i], pieces[i+1]];
              params[param[0]] = (param[1] === undefined) ? param[0] : param[1];
          }
          return params;
      }
  });

  $.extend({
    manipulateDegreesPrograms:  function(selectedDegreeValue, selectedProgramValue, lowestEduLevel) {
      if (!selectedDegreeValue) {
        return;
      }
      if (lowestEduLevel) {
        var $eduLevels = $('#edu_' + lowestEduLevel).nextAll().andSelf();
        $('#educationlevel').html($eduLevels);
      }
      $('#degree').val(selectedDegreeValue);
      $('#program').val(selectedProgramValue);

      $('#degree, #program').trigger('change');
    }
  });

  var formType = $.getGetParam('formType'),
      pathParams = $.getPathParams(),

      specials = {
        'dba': ['Doctorate', 'Doctor of Business Administration', 'master'],
        'accelerated': ['Bachelor', 'Bachelor of Business Administration - Accelerated Program', 'associate'],
        'nursing': ['Bachelor', 'Bachelor of Science in Nursing']
      },
      exclude = false;

  $.each(pathParams, function(key, val) {
    if ( (key in specials) || formType === key ) {
      exclude = true;
      $.manipulateDegreesPrograms.apply(this, specials[key]);
    }

  });

  if (!exclude) {

    $('#program').change(function() {
      $('div.program.active').removeClass('active');
      $('div.program').filter(function() {
        return $(this).children('h3').text() == $('#program').val();
      }).addClass('active');
    }).trigger('change');

  }

});

$(document).ready(function() {
  // homepage getting started tabs
  var $tpanel = $('div.tabs-panel');

  $tpanel.slice(1).hide();
  $tpanel.removeClass('js-hide');

  var $tabLinks = $('.tabs-nav a');
  $tabLinks.click(function() {
    var panel = this.hash;
    if ( !$(panel).is(':visible') ) {
      $tabLinks.removeClass('active');
      $(this).addClass('active');
      $('div.tabs-panel:visible').fadeOut(200, function() {
        $(panel).fadeIn();
      });
    }
    return false;
  }).filter(':first').addClass('active');
});

