
$(document).ready(

  function() { // Wait until the DOM is loaded, then go nuts

    var exploder6 = false;

    if ( $.browser.msie && document.all ) { exploder6 = true; }

    if ($('#rightColumn').show());

    $('#sortable-table').tableSorter({

      stripingRowClass: ['even','altRow'],   // Class names for striping supplyed as a array.
      stripeRowsOnStartUp: true,             // Strip rows on tableSorter init.
      sortClassAsc: 'asc',
      sortClassDesc: 'desc',
      headerClass: 'tableSortHeaders'

    });

    $("#sortable-table2").tablesorter({

        // pass the headers argument and assing a object

        headers: {

            0: {
                sorter: false
            },

            5: {
                sorter: false
            },

            6: {
                sorter: false
            },

            7: {
                sorter: false
            },

            8: {
                sorter: false
            }
        },

        cssHeader: 'headerSort'

     });

    $('#saveAlertDT').css('display','inline');
    $('#saveAlertDD').css('display','inline');

    $('#saveAlertDD form').bind('submit',

      function(event) {

        event.preventDefault();

        ajaxSaveSearch();

      }

    );

    if ($('.findList')) { // "Find Learning" list tables

      $('.findList tr').mouseover( function() { $(this).addClass('findHighlightRow'); } );

      // Make header styles stick onmouseover

      $('.findList th').css( { 'background-color' : 'white', 'color' : 'black' } );
      $('.findList th a').css('color','black');

      $('.findList tr').mouseout( function() { $(this).removeClass('findHighlightRow'); } );

      $('.findList tr:nth-child(even)').addClass('altRow');

    }

    if ($('.engagelist')) { // "Engage in Learning" list tables

      $('.engageList tr').mouseover( function() { $(this).addClass('engageHighlightRow'); } );

      $('.engageList th').css( { 'background-color' : 'white', 'color' : 'black' } );
      $('.engageList th a').css('color','black');

      $('.engageList tr').mouseout( function() { $(this).removeClass('engageHighlightRow'); } );

      $('.engageList tr:nth-child(even)').addClass('altRow');

    }

    if ($('.post')) { // engage forum posts are separate tables so need their own class

      $('.post tr:nth-child(odd)').addClass('altRow');

    }

    if ($('.adminList')) { // "admin" list tables

      $('.adminList tr').mouseover( function() { $(this).addClass('adminHighlightRow'); } );

      // Make header styles stick onmouseover

      $('.adminList th').css( { 'background-color' : 'white', 'color' : 'black' } );
      $('.adminList th a').css('color','black');

      $('.adminList tr').mouseout( function() { $(this).removeClass('adminHighlightRow'); } );

      $('.adminList tr:nth-child(even)').addClass('altRow');

    }

    if ($('#forum')) { // "Engage in Learning" forum styling

       $('#forum.fourColumns th:nth-child(2)').css('text-align', 'center');
       $('#forum.fourColumns th:nth-child(3)').css('text-align', 'center');

       $('#forum.fourColumns td:nth-child(2)').css('text-align', 'center');
       $('#forum.fourColumns td:nth-child(3)').css('text-align', 'center');

       $('#forum.fiveColumns th:nth-child(2)').css('text-align', 'center');
       $('#forum.fiveColumns th:nth-child(3)').css('text-align', 'center');
       $('#forum.fiveColumns th:nth-child(4)').css('text-align', 'center');

       $('#forum.fiveColumns td:nth-child(2)').css('text-align', 'center');
       $('#forum.fiveColumns td:nth-child(3)').css('text-align', 'center');
       $('#forum.fiveColumns td:nth-child(4)').css('text-align', 'center');

    }

    if ($('#eventDetails')) {

       $('#signMeUpNow').empty();

       $('#signMeUpNow').append('<button type="button" onclick="window.location=\'/register.cgi\'">OK, Sign Me Up Now &gt;</button>');

       $('#searchMoreEvents').empty();

       $('#searchMoreEvents').append('<button type="button" onclick="window.location=\'/calendar.cgi\'">Search For More Events &gt;</button>');

    }

    if ($('#map')) { $('#map').show(); }

    //$('img[@src$=.png], .PNGfix').pngfix(); // IE6 lacks native PNG support, jQuery plugin uses DirectX filter workaround

    $('.giveMeTalkBubble a').removeAttr('title'); // Native browser 'tooltips' r shite

    $('.giveMeTalkBubble').hover( // IE6 lacks support for :hover on any element, jQuery hover() to rescue

      function() {

        var talkBubbleId = '#' + this.id + 'TalkBubble';
	       var coords = {}

	       $(this).offset( { scroll : false }, coords);

        if ($(talkBubbleId).is(".rp")) {

          var newLeft = (coords.left - $(this).width()) - 280;
          var newTop  = coords.top  + 20;

        } else {

          var newLeft = coords.left + $(this).width();
          var newTop  = coords.top  + 20;

        }

        //$('.giveMeTalkBubble').stop();

        $(talkBubbleId).css( { 'left' : newLeft , 'top' : newTop } );

        $(talkBubbleId).show();

        if (exploder6 == true) { $('.hideFromTalkBubbles').css( { 'visibility' : 'hidden' } ); }

      },

      function() {

        var talkBubbleId = '#' + this.id + 'TalkBubble';

        $(talkBubbleId).css( { 'display' : 'none' } );

        if (exploder6 == true) { $('.hideFromTalkBubbles').css( { 'visibility' : 'visible' } ); }

      }

    );



    $('.giveMeTalkBubbleOnClick').each(

      function() {

        $(this).click(

          function () {

            var talkBubbleId = '#' + this.id + 'TalkBubble';

    	       var coords = {}

    	       $(this).offset( { scroll : false }, coords);

            if ($(talkBubbleId).is(".rp")) {

              var newLeft = (coords.left - $(this).width()) - 600;
              var newTop  = coords.top  + 20;

            } else {

              var newLeft = coords.left + $(this).width();
              var newTop  = coords.top  + 20;

            }

            showHide(this.id + 'TalkBubble');

            $(talkBubbleId).css( { 'left' : newLeft , 'top' : newTop } );

            iexploder6();

          }

        );

      }

    );

    // Admin manageEvents Add/Edit, changes end date to start date when frequency ends is invoked
    $("#SearchRepeatEndsPicker").click(

      function (eventReference) {

        var start_date = $("#sdate").val() ;
        $("#edate").val(start_date) ;
        $("#SearchEndDate").html(start_date) ;

      }

    )

    // Admin managebookings list, changes custom message depending on whether they are booked in or not
    $("input[name='event_bookings']").click(

      function (eventReference) {
    
        var fieldLabel = $('label[@for=' + this.id + ']');
        var labelText = fieldLabel.text(); // get text inside field's label parent

        $("#message").val(labelText) ;
    
      }
    
    );

  }

);


function CustomCalendarCall(cal) {

  if (cal.params.flat) {
   if (typeof cal.params.flatCallback == "function") {
    cal.params.flatCallback(cal);
   } else {
    alert("No flatCallback given -- doing nothing.");
   }
   return false;
  }
  if (cal.params.inputField) {
   cal.params.inputField.value = cal.date.print(cal.params.ifFormat);
  }
  if (cal.params.displayArea) {
   cal.params.displayArea.innerHTML = cal.date.print(cal.params.daFormat);
  }
  if (cal.params.singleClick && cal.dateClicked) {
   cal.callCloseHandler();
  }
  if (typeof cal.params.onUpdate == "function") {
   cal.params.onUpdate(cal);
  }

  var start_date = $("#sdate").val() ;
  $("#edate").val(start_date) ;
  $("#SearchEndDate").html(start_date) ;

};


function iexploder6 () {

  var exploder6 = false;

  if ( $.browser.msie && document.all ) { exploder6 = true; }

  if (exploder6 == true) {

    if ($('.hideFromTalkBubbles').css('visibility') === 'hidden') {

      $('.hideFromTalkBubbles').css('visibility','visible');

    } else {

      $('.hideFromTalkBubbles').css('visibility','hidden');

    }

  }

}
