var REGASST = {};

/* -----------------------------------*/
/* -------->>> UTILITIES <<<----------*/
/* -----------------------------------*/

REGASST.Utils = {

};

/* -----------------------------------*/
/* ---------->>> GLOBAL <<<-----------*/
/* -----------------------------------*/

REGASST.Global = {
   
};

/* -----------------------------------*/
/* ---------->>> EVENTS <<<-----------*/
/* -----------------------------------*/
REGASST.Events = {
   registrationAssistant: function(o){
      o.preventDefault();
      var crn;
      var index;
      var term;
      var str;
	
      if($(this).hasClass('active')){
         //open up the registration assistant
         REGASST.Dom.toggleScheduleOverlay();
      }else{
         str = $(this).attr('rel');
         index = str.indexOf('_');
         crn = str.substr(0,index);
         term = str.substr(index+1);
         $(this).addClass('active');
         $(this).parents('div.expandable:first').prev('.expander:first').addClass('addedToSchedule');         
         //set cookie
         var sc = $.cookie('sjcc_schedule');
         if(sc){
            sc += ':' + crn;
         }else{
            sc = (sc == null) ? '' : sc ;
            sc += crn;
         }
         $.cookie('sjcc_schedule', sc, { expires: 365});
         $.post("/sites/default/themeglobal/includes/schedule-assistant-add.php", { crn: crn, term: term } );
		}
      if($('#scheduleOverlay').length == 0){
         REGASST.Dom.createScheduleOverlayMarkup(true);
      }
   }
};

/* -----------------------------------*/
/* ----->>> DOM MANIPULATION <<<------*/
/* -----------------------------------*/

REGASST.Dom = {
   createScheduleOverlayMarkup: function(isFirst){
      if($('#scheduleHolder').length == 0){
         if($('#scheduleOverlay').length == 0){
            $('body').prepend('<div id="scheduleOverlay"></div>');
         }
         $.get('/sites/default/themeglobal/includes/regasst.php', function(data){
            $('body').prepend(data);
            $('a.scheduleBtn:first').bind('click', REGASST.Dom.toggleScheduleOverlay);
            $('a.scheduleBtn:first').hover(
               function(){
                  $(this).children('.mid:first').stop().animate({
                     height: 106
                  }, 150);
                  $(this).parent().css('z-index', 110);
               }, 
               function(){
                  $(this).children('.mid:first').stop().animate({
                     height: 0
                  }, 150);
                  $(this).parent().css('z-index', 101);
               }
            );
            $('#scheduleOverlay').bind('click', REGASST.Dom.toggleScheduleOverlay);
            if(isFirst){
               REGASST.Dom.showAlert('This class has been added to the schedule assistant. You can view and edit the classes you\'ve added by clicking the "my schedules" tab on the right side of the page. To complete the registration process click the "Register (S.O.S.)" button.<br/><img id="helpImage" src="/sites/default/themeglobal/images/regasst/help-image.gif" alt="schedule tab image" />');
            }
         });
      }
   },
   
   printSchedule: function(o){
      var cterm = $(this).attr('rel') ;      

	if ( cterm.length > 0 ) {
		if ( $("#term_" + cterm).length > 0 ) {
       			$("#term_" + cterm).printArea();
		}
	}
	else {
	$('#semesterNav li a').each(function(i, el){
	var _el = $(el);
	cterm = _el.attr('rel');
		if ( $("#term_" + cterm).length > 0 ) {
       			$("#term_" + cterm).printArea();
		}
	});
	}
   },
   
   clearSchedule: function(o){
      var cterm = $(this).attr('rel') ||  $('li.first-term a').attr('rel');      
       $.post("/sites/default/themeglobal/includes/schedule-assistant-remove.php", { clear: 'true', term: cterm }, function(){
          $('#scheduleAssistant .classRow').remove();
         }
       );
   },
   
   showAlert: function(message){
      if($('#alertHolder').length > 0){
         REGASST.Dom.closeAlert();
      }
      $('body').prepend("<div id='alertHolder'><div id='alertBody'><div id='alertInsert'></div><a href='#' class='regBtn' id='alertClose'><span>OK</span></a></div></div>");
      $('#alertClose').bind('click', REGASST.Dom.closeAlert);
      REGASST.Dom.alignAlert();
      if($('#scheduleOverlay').length == 0){
         $('body').prepend('<div id="scheduleOverlay"></div>');
      }
      $('#scheduleOverlay').css('display', 'block');
      $('#alertInsert').append(message);
      $(window).unbind('resize', REGASST.Dom.alignAlert);
      $(window).unbind('scroll', REGASST.Dom.alignAlert);
      $(window).bind('resize', REGASST.Dom.alignAlert);
      $(window).bind('scroll', REGASST.Dom.alignAlert);
   },
   
   alignAlert: function(){
      var _top = ($(window).height() / 2) - ($('#alertHolder').height() / 2);
      _top += $(window).scrollTop();
      $('#alertHolder').css('top', _top);
   },
   
   closeAlert: function(o){
      if(o){
         o.preventDefault();
      }
      $(window).unbind('resize', REGASST.Dom.alignAlert);
      $(window).unbind('scroll', REGASST.Dom.alignAlert);
      $('#alertHolder').remove();
      $('#scheduleOverlay').css('display', 'none');
   },
   
   switchTabs: function(o){
      o.preventDefault();
      $('#printSchedule').attr('rel', $(this).attr('rel')); 
      $('#clearSchedule').attr('rel', $(this).attr('rel')); 
      var _ahref = $('#semesterNav li.active:first a').attr('href');
      var activeTab = _ahref.substr(_ahref.indexOf('#'));
      $(activeTab).css('display', 'none');
      $('#semesterNav li.active:first').removeClass('active');
      $(this).parent().addClass('active');
      var _href = $(this).attr('href');
      var newTab = _href.substr(_href.indexOf('#'));
      $(newTab).css('display', 'block');
   },
   
   toggleScheduleOverlay: function(o){
      if(o){
         o.preventDefault();
      }
      if($('#alertHolder').length > 0 ){
         return false;
      }
      if(REGASST.Global.scheduleOverlayActive){
         $('#scheduleHolder').stop().animate({
            width: 0
         }, function(){
            $('#scheduleBody').html('');
            $('#scheduleSpinner').css('display', 'none');
         });
         $('#scheduleHolder').removeClass('activeSidePanel');
         $('#scheduleOverlay').css('display', 'none');
         REGASST.Global.scheduleOverlayActive = false;
      }else{
         $('#scheduleHolder').stop().animate({
            width: 962
         });
         $('#scheduleHolder').addClass('activeSidePanel');
         //get ajax schedule assistant
         $('#scheduleSpinner').css('display', 'block');
         var sc = $.cookie('sjcc_schedule');         
         sc_arry = sc.split(':');
         $.makeArray(sc_arry);
         $('#scheduleBody').load('/sites/default/themeglobal/includes/schedule-assistant.php', {'ids[]' : sc_arry},  function(){
            $('#scheduleSpinner').css('display', 'none');
            var _el;
            var id;
            $('#semesterNav li a').each(function(i, el){
               _el = $(el);
               _el.bind('click', REGASST.Dom.switchTabs);
               //remove any active tabs and hide all
               _el.parent().removeClass('active');
               var tab = _el.attr('href').substr(_el.attr('href').indexOf('#'));
               $(tab).css('display', 'none');
            });
            
            //find first tab with content and make it active
            $('#semesterNav li a').each(function(i, el){
               _el = $(el);
               var found = false;
               var tab = _el.attr('href').substr(_el.attr('href').indexOf('#'));
                if($(tab).children('.classRow').length){
                   $(tab).css('display', 'block');
                   _el.parent().addClass('active');
                   return false;//break loops
                }
            });
            $('#printSchedule').each(function(i, el){
                  $(el).bind('click', REGASST.Dom.printSchedule);
            });
            
            $('#clearSchedule').each(function(i, el){
                  $(el).bind('click', REGASST.Dom.clearSchedule);
            });
            $('#scheduleAssistant a.closeBtn').each(function(i, el){
               $(el).bind('click', REGASST.Dom.removeClass);
            });
            $('#scheduleAssistant div.scrollPane').each(function(i, el){
               var el = $(el);
               if(el.height() > 348){
                  el.css({
                     'height': '348px'
                  });
               }
            });
         });
         $('#scheduleOverlay').css('display', 'block');
         REGASST.Global.scheduleOverlayActive = true;
      }
   },
   
   removeClass: function(o){
      o.preventDefault();
      var _rel = $(this).attr('rel');
      var i = _rel.indexOf('_');
      var term = _rel.substr(0,i);
      var crn = _rel.substr(i+1);
      var sc = $.cookie('sjcc_schedule');
      var rel = _rel.slice(i+1);
      var sc = sc.replace(rel, '');
      if(sc.lastIndexOf(':') == (sc.length - 1)){
         sc = sc.slice(0, sc.length -1);
      }
      $(this).parent('.classRow').remove();
      //remove any active added to schedule buttons and remove the addedtoschedule class from the parent
      var link = $("a[rel^='"+rel+"']:first");
      link.removeClass('active');
      var _parent = link.parents('div.expandable:first').prev('.expander:first');
      if(link.parents('div.expandable:first').contents().find('a.active').length == 0){
         _parent.removeClass('addedToSchedule');
      }
      //update the cookie
      $.cookie('sjcc_schedule', sc, { expires: 365});
      $.post("/sites/default/themeglobal/includes/schedule-assistant-remove.php", { crn: crn, term: term } );
      
   }
};


REGASST.Inits = {
   init: function(){
      REGASST.Inits.regAsstSetup();
   },
   
   regAsstSetup: function(){
      $('a.regasst').each(function(i, el){
         $(el).bind('click', REGASST.Events.registrationAssistant);
      });
      var sc = $.cookie('sjcc_schedule');
      var sc_arry = [];
      if(sc){
         REGASST.Dom.createScheduleOverlayMarkup();
         sc = (sc == null) ? '' : sc ;
         sc_arry = sc.split(':');
         $.makeArray(sc_arry);
         var _link;
         var _parent;
         $.each(sc_arry, function(i, rel){
            _link = $("a[rel^='"+rel+"']:first");
            _link.addClass('active');
            _parent = _link.parents('div.expandable:first').prev('.expander:first');
            _parent.addClass('addedToSchedule');
         });
         $.post("/sites/default/themeglobal/includes/schedule-assistant-add.php", { 'name[]': sc_arry } );
      }
   }
};

/* -----------------------------------*/
/* ----->>> ONLOAD FUNCTIONS <<<------*/
/* -----------------------------------*/

$(document).ready(function () {
    REGASST.Inits.init();
});


