/**
* Info Sessions - browse sessions
*
* @category Department_Sitelet_Utility
* @package  InfoSessions_Browse_Sessions
* @author   Jasmit Chilana <Jasmit_Chilana@bcit.ca>
*/


/**
* Gets the sessions given a sector ID and toggles the display based on the state.
*
* @param int sect sector ID.
* @return none.
*/
function init(sect) {

   $(document).ready(function(){
      var status = $("#results" + sect).css("display");
      if(status == 'none'){

         $.post("/infosessions/get_sessions.php", { sector: sect },
         function(data){
            $("#results" + sect).html('<p>Loading...</p>');
            $("#results" + sect).html(data);
            $("#results" + sect).append('<br style="clear: left;" />');
            // need to add the following line in order for thickbox to be initialized once data is
            // returned from a ajax call
            tb_init('a.thickbox, area.thickbox, input.thickbox');
            // auto resize thickbox on load
            thickboxResize();
         });

         $("#results" + sect).fadeIn("slow");

      } else{
         $("#results" + sect).fadeOut("slow");
      }
   });
}


/**
* Dynamically resize the thickbox window dimensions.  This function simply scans the page and
* looks for those anchor tags that have the class of thickbox, takes in the href attribute value, and
* then changes the height and width variables to a ratio of the users viewport dimensions.
*
* Original post: http://manewc.com/2008/11/21/jquery-thickbox-dynamic-resizing-of-windows/
* Last Accessed: June 24, 2009
*
* Modification: changed aspect ratios to be divided by 2. Original post was multiplying it by 0.8

* @return none.
*/
function thickboxResize() {

   var boundHeight = 500; // minimum height
   var boundWidth = 600; // minimum width

   var viewportWidth = (self.innerWidth || (document.documentElement.clientWidth || (document.documentElement.offsetWidth || 0)))
   var viewportHeight =(self.innerHeight || (document.documentElement.clientHeight || (document.documentElement.offsetHeight || 0)))

   $('a.thickbox').each(function(){
      var text = $(this).attr("href");
      if ( viewportHeight < boundHeight  || viewportWidth < boundWidth)
      {
         // adjust the height
         text = text.replace(/height=[0-9]*/,'height=' + Math.round(viewportHeight / 2));
         // adjust the width
         text = text.replace(/width=[0-9]*/,'width=' + Math.round(viewportWidth / 2));
      }
      else
      {
         // constrain the height by defined bounds
         text = text.replace(/height=[0-9]*/,'height=' + boundHeight);
         // constrain the width by defined bounds
         text = text.replace(/width=[0-9]*/,'width=' + boundWidth);
      }

      $(this).attr("href", text);
   });
}

// auto resize when window is resized
$(window).bind('resize', thickboxResize );
