          // simple image gallery
          // inspired by http://css-tricks.com/snippets/jquery/cycle-through-a-list/
          
          // the gallery accounts for the 3 different view types of news
          // each has it's own variation of html which we need to account for          
          
          (function ($) {
            $(document).ready(function() {
              
              var initGallery = null;
              var galleryID = 1;
              var currGalleryID = 0;
              var delay = 200; //millisecond delay between images
              var first = true;
              
              simpleGalleryInit();  // use function so can call via setTimeout()
              
              
              function simpleGalleryInit() {
                var j = new Array();  // stores current image displyed for each gallery
                var jmax = new Array(); // stores total images in gallery
                if (first) {
                  first = false;
                  jmax.length = 0;
                  j.length = 0;
                }
                j[0] = 0;              
                var parentClass = 0;
                var parentClasses ='';
                var galleryName = '';
                var viewType = '';
                var viewClass = '';
                var galleryClass = '';                
                if (initGallery == null) $(".simple-image-gallery .item-list ul li").css("visibility", "hidden");
                
                // what view type are we on: news home, all news, single news item ?
                // set up initial css and variables for this view type
                if ($('.view-display-id-news_home').length > 0) {
                  $(".simple-image-gallery ").css("height", "200px");
                  viewType = 'home';
                  viewClass = '.jcarousel-item';
                  galleryClass = '.gallery-num-';
                } else if ($('.view-display-id-allnews').length > 0 || $('.view-id-projects').length > 0) {
                  $(".simple-image-gallery ").css("height", "200px");
                  viewType = 'allposts';
                  viewClass = '.views-row';
                  galleryClass = '.gallery-num-';
                  
                }
                else $(".simple-image-gallery ").css("height", "286px");
                
                if (viewType == 'home' || viewType == 'allposts') {
                  // get total number of images in this gallery
                  // used as max value for showing either last or first image when reached end of image list
                  //alert('baa');
                  var galleryIDnum = 1;
                  $(viewClass).each(function(e) {
                    var num=($(this).find('ul li a > img').length);
                    if (num > 0) {
                      jmax[galleryIDnum] = num - 1;
                      galleryIDnum++;
                      //alert(galleryIDnum);
                    }
                  });
                } else jmax[0] = $(".simple-image-gallery .item-list ul li").length -1;           
                
                // wait to page loaded to set up gallery
                // allows us to resize image margins before they are hidden
                if (viewType == 'home') setUpGalleries();   
                else {
                  $(window).load(function() {
                    setUpGalleries();                                    
                  });
                }
                
                // for all news page keep checking for new galleries appearing via infinite scroll                
                if ( viewType == 'allposts' ) {
                  if (initGallery != null) {
                    clearTimeout(initGallery);
                    setUpGalleries();  
                  }                                  
                  initGallery = setTimeout(simpleGalleryInit, 1000);                 
                }
                
                
                function setUpGalleries() {
                  // vertically realign old blogger images as not square
                  $('.simple-gallery-image').vAlign();
                     
                  if (jmax[0] >= 0 || viewType == 'home' || viewType == 'allposts') {

                    // set up titles, controls and listeners for each image gallery                  
                    $(".simple-image-gallery .item-list ul li").each(function(i, ele) {
                      
                      if ($(this).closest('.item-list').find('.controls').length==0) {
                        // display only the first image                     
                        $(".simple-image-gallery .item-list ul li").css("display", "none");
                        $(".simple-image-gallery .item-list ul li:eq(0)").css("display", "block");
                        if (viewType == 'home' || viewType == 'allposts') $(".simple-image-gallery .item-list ul li.first ").css("display", "block");
                      }
                      
                      // get the image title from the alt tag or title tag if no alt tag                    
                      var title = $(this).find('img').attr('alt');
                      if (title == '') var title = $(this).find('img').attr('title');
                      // truncate sentence at word boundry and add ellipses
                      // http://stackoverflow.com/questions/1199352/smart-way-to-shorten-long-strings-with-javascript
                      var max = 40;
                      if (viewType == 'home' || viewType == 'allposts') max = 25;
                      var tooLong = title.length>max;
                      if (title.length>max) {
                        title = title.substr(0,max-1);
                        title = title.substr(0,title.lastIndexOf(' '));
                        title = title +'...';
                      }
                      
                      currGalleryID = galleryID;
                      
                      // give the gallery a unique Number
                      if (viewType == 'home') {
                        if (!$(this).closest('.jcarousel-item').hasClass('minigallery')) {
                          $(this).closest('.jcarousel-item').addClass('minigallery gallery-num-' + galleryID);
                          galleryID++;
                        }                        
                      } else {
                        if (!$(this).closest('.views-row').hasClass('minigallery')) {
                          $(this).closest('.views-row').addClass('minigallery gallery-num-' + galleryID);
                          galleryID++;
                        }                    
                      }
                      
                      // now add title block at start of <li></li> block
                      if (title != '' && $(this).find('.title').length==0) $(this).prepend('<div class="title"><p>' + title + '</p></div>');
                      
                      if ($(this).closest('.item-list').find('.controls').length==0) {
                        // if only one image then dont show next and prev buttons
                        //alert('foo ' + jmax[currGalleryID] + ' - ' + currGalleryID);
                        if ( (viewType == '' || jmax[currGalleryID] >= 1) && jmax[0] != 0) {                          
                          $(this).closest('.item-list').append('<div class="controls"><div class="next-image">&#9658;</div><div class="full-screen"></div><div class="prev-image">&#9668;</div></div>');
                          
                          // listen for next and prev buttons
                          $(this).closest('.item-list').find(".prev-image").click(function() { 
                            if (viewType == 'home') parentClass = $(this).closest('.jcarousel-item').attr('class');
                            if (viewType == 'allposts') parentClasses = $(this).closest('.views-row').attr('class');
                            var p = parentClasses.indexOf("gallery-num-");
                            parentClass = parentClasses[(p+12)];
                            prevImage(parentClass);
                          });            
                          $(this).closest('.item-list').find(".next-image").click(function() { 
                            if (viewType == 'home') parentClasses = $(this).closest('.jcarousel-item').attr('class');
                            if (viewType == 'allposts') parentClasses = $(this).closest('.views-row').attr('class');
                            var p = parentClasses.indexOf("gallery-num-");
                            parentClass = parentClasses[(p+12)];
                            nextImage(parentClass);
                          });
                        } else {
                          $(this).closest('.item-list').append('<div class="controls"><div class="full-screen"></div></div>');
                        }
                        
                        $(this).closest('.item-list').find(".full-screen").click(function() { 
                          galleryName = ($(this).parents('.item-list').find('a.colorbox').attr('rel'));
                          $("a[rel='" + galleryName + "']").colorbox({open:true});                  
                        }); 
                        
                        // fade controls in and out with mouse hover                        
                        $(this).closest('.item-list').hover(function(){
                          $(this).find(".title").fadeIn(100);
                          $(this).closest('.item-list').children(".controls").fadeIn(100);
                        }, function(){
                          $(this).find(".title").fadeOut(200);
                          $(this).closest('.item-list').children(".controls").fadeOut(200);
                        });                         
                      
                      }
                    });                      
                 
                  }
                  
                  // add background color and make galleries visible now all loaded
                  $(".simple-image-gallery .item-list ul").css("background", "#151515");
                  $(".simple-image-gallery .item-list ul li").css("visibility", "visible");                  
                }
                
                function prevImage(galleryNum){
                  // do we have multiple galleries on the page
                  // if so we use the jcarousel index to determine the gallery
                  if (galleryNum > 0) {
                    if( j[galleryNum] === undefined ) j[galleryNum] = 0;
                    $(galleryClass + galleryNum + " .simple-image-gallery .item-list ul li:eq(" + j[galleryNum] + ")").fadeOut(delay).css("display", "none");                  
                  } else {
                    galleryNum = 0;
                    $(".simple-image-gallery .item-list ul li:eq(" + j[galleryNum] + ")").fadeOut(delay).css("display", "none");  
                  }
                  (j[galleryNum] == 0) ? j[galleryNum]=jmax[galleryNum] : j[galleryNum]--;
                  if (galleryNum > 0) {
                    $(galleryClass + galleryNum + " .simple-image-gallery .item-list ul li:eq(" + j[galleryNum] + ")").fadeIn(delay).css("display", "block");
                  } else $(".simple-image-gallery .item-list ul li:eq(" + j[galleryNum] + ")").fadeIn(delay).css("display", "block");
                };   
                
                function nextImage(galleryNum){    
                  // do we have multiple galleries on the page
                  // if so we use the jcarousel index to determine the gallery    
                  if (galleryNum > 0) {
                    if( j[galleryNum] === undefined ) j[galleryNum] = 0;
                    $(galleryClass + galleryNum + " .simple-image-gallery .item-list ul li:eq(" + j[galleryNum] + ")").fadeOut(delay).css("display", "none");
                  } else {
                    galleryNum = 0;
                    $(".simple-image-gallery .item-list ul li:eq(" + j[galleryNum] + ")").fadeOut(delay).css("display", "none");
                  }
                  
                  (j[galleryNum] == jmax[galleryNum]) ? j[galleryNum]=0 : j[galleryNum]++;
                  if (galleryNum > 0) $(galleryClass + galleryNum + " .simple-image-gallery .item-list ul li:eq(" + j[galleryNum] + ")").fadeIn(delay).css("display", "block");
                  else $(".simple-image-gallery .item-list ul li:eq(" + j[galleryNum] + ")").fadeIn(delay).css("display", "block");
                };                    
              } // eof simpleGalleryInit                       
   
            });
            
                
            // VERTICALLY ALIGN FUNCTION - http://www.seodenver.com/simple-vertical-align-plugin-for-jquery/
            $.fn.vAlign = function() {
              return this.each(function(i){
              var ah = $(this).height();
              //var ph = $(this).parent().height();
              var ph = 286;
              var mh = Math.ceil((ph-ah) / 2);
              $(this).css('margin-top', mh);
              });
            };            
          }(jQuery));
          




          

