// THIS SCRIPT IS INCLUDED ON ALL PAGES


/**
 * This version of jQuery has an outdated or just plain broken implementation 
 * of $.browser, so, this is a simple function to return the browser type and 
 * version, if possible.
 */
var browserType = (function() {
  var ua = navigator.userAgent.toLowerCase(),
      // The order of these regular expresions is important!
      re = [
        /(chrome)[ \/]([\w.]+)/,
        /(webkit)[ \/]([\w.]+)/,
        /(opera)(?:.*version)?[ \/]([\w.]+)/,
        /(msie) ([\w.]+)/,
        /(mozilla)(?:.*? rv:([\w.]+))?/
      ],
      m, i;
  
  for (i=0; i<re.length; i++) {
    if ( m = ua.match(re[i]) ) {
      return { browser: m[1], version: m[2] };
    }
  }
  
  // Nothing matched, so, we don't really know what this is...
  return { browser: 'unknown', version: 'unknown' }
})();


/**
 * Image Viewer with dot-based navigation jQuery-plugin
 * Copyright (c) 2011, Transparency
 * Author: Martin Koistinen <mkoistinen@transparency.com>
 *
 * The object(s) provided here should have the following structure:
 *   ul.dots>li*n>a.dot>img
 * 
 * The ul.dots will be 'display: block'ed so that it can be hidden away from
 * non-JS-enabled browsers, but displayed for those with.
 *
 * viewer is a jQuery object defining a viewer that uses the interface newImage( src, caption )
 *
 */

;(function($, undefined) {
  
  $.fn.dotNavigator = function( viewer, timeout ) {
    return this.each(function() {
      var $dots = $(this),
          $viewer = $(viewer),
          currentSlide = 0,
          countSlides = $dots.find('a.dot').length,
          slideTimer,
          setupTimer;
      

      // Default timeout is 10 seconds
      if (!timeout) { timeout = 10000; }
      
      setupTimer = function() {
        if (!slideTimer) {
          slideTimer = setInterval(function() {
            $dots.trigger('nextSlide');
          }, timeout);  // 10 seconds
        }
      };
  
      $('a.dot', $dots).click(function() {
        var $me = $(this),
            $img = $me.find('img');
        
        // Which slide number is this?
        currentSlide = $dots.find('a.dot').index($me);
        if ($viewer.length) {
          // Tell the viewer to display this new slide
          //$viewer.html($me.find('img').clone());
          $viewer.trigger('newImage', [ $img.attr('src'), $img.attr('alt') ]);
          
          // Clear the other dots of 'selected'
          $('a.dot', $dots).removeClass('selected');
          
          // Set this one to 'selected'
          $me.addClass('selected');
        }

        if (slideTimer) {
          clearInterval(slideTimer);
          slideTimer = false;
        }
        setupTimer();
        
        return false;
      });
      
      // If the user enters the viewer area, stop the countdown timer
      $viewer.bind('mouseenter', function() {
          if (slideTimer) {
              clearInterval(slideTimer);
              slideTimer = false;
          }
      })
      
      // (Re)-setup a countdown timer
      .bind('mouseleave', setupTimer);
      
      // Public interface for going to next slide
      $dots.bind('nextSlide', function( evt ) {
        var nextSlide = (currentSlide + 1) % countSlides;
        $dots.find('a.dot').eq(nextSlide).trigger('click');
      });
      
      // Since we're here, JS obviously works, un-hide JS-only elements
      $dots.show();
      
      // Start everything by clicking the first dot
      $dots.find('a.dot').eq(0).click();      
    });
  }
  
  
})(jQuery);


;(function($) {

  /**
   * Viewer.  This is an image viewer.
   * Copyright (c) 2010, Transparency
   * Author: Martin Koistinen
   */
  
  // Requires a structure like this in the DOM: 
  // div>img.plane.plane0+img.plane.plane1+div.caption>div.inner
  
  $.fn.slideViewer = function($items) {  
    var speed = 600; // This is set for all imageViewers
  
    return this.each(function () {
      var self = this,
          curPlane = 0, // 0 or 1
          $caption = $('.caption', this),
          captionOpacity = $('body').hasClass('ie6') ? 0.6 : 1.0, // Total hack =(
          $viewerBody = $(this).siblings('.viewer-body');
  
      // Setup the starting configuration
      $('.plane0', self).css('zIndex', 10);  // In front
      $('.plane1', self).css('zIndex', 0);   // Behind
      $('.caption', self).css('opacity', 0); // Invisible
  
      function changeImage(imgSrc, caption) {
        var $oldPlane = $('.plane' + curPlane, self),
            $newPlane = $('.plane' + (1 - curPlane), self),
            captionHTML = caption+'';

        // OK, $newPlane is below the $oldPlane, so load the new image
        $newPlane.attr('src', imgSrc);
    
        // Now fade the $oldPlane and its caption text until the are transparent
        $caption.animate({ opacity: 0 }, speed);
        $oldPlane.animate({ opacity: 0 }, speed, function() {
          // OK, once finished, fade in the new caption after a brief pause
          if (caption) { // Don't bring in the caption if there isn't one to display
            $caption
              .find('.inner').html(captionHTML).end()
              //.delay(speed) // delay() isn't available in v1.2.6 =()
              .animate({ opacity: captionOpacity }, speed, function() {
                // OK, the whole transition is complete, now prepare for the next one
                $newPlane.css({ zIndex: 10, opacity: 1 });
                $oldPlane.css({ zIndex: 0, opacity: 1 });
                curPlane = (1 - curPlane);
              });
            }
            else {
              // OK, the whole transition is complete, now prepare for the next one
              $newPlane.css({ zIndex: 10, opacity: 1 });
              $oldPlane.css({ zIndex: 0, opacity: 1 });
              curPlane = (1 - curPlane);
            }
        });
      } /* end of changeImage() */
  
      // Bind this function to the viewer
      $(self).bind('newImage', function(evt, src, caption) {
          changeImage(src, caption);
      });
      
    });
    
  };



  $(function() { // DOM READY

    // Add some classes to the #container for non-IE browsers
    // We cannot rely on the old verion of jQuery's $.browser, sadly.
    // This needs to go in DOM READY, because Drupal loads scripts in
    // the HEAD.
    $('#outer-wrapper').addClass(browserType.browser);

    // Set-up the image viewer, if present
    $('#imageViewer .viewer').slideViewer();
    $('#imageViewer .dots').dotNavigator( $('#imageViewer .viewer').get(0), 5000 );
    
    // Make the topic select, if any work
    $('#topic-filter').bind('change', function() {
    	var url = $(this).val();
    	
    	if (url) {
    		document.location.href=url;
    	}
    });
    
    // Hide the comment form, if present
    $('#comments form.comment-form').hide();

    // The "Add New Comment" heading will be turned into a toggle
    $('h2.title.comment-form').addClass('closed').bind('click', function() {
      var $self = $(this),
          $form = $self.siblings('form.comment-form'),
          speed = 250;
      
      if ($self.hasClass('open')) {
        $self.addClass('transition').removeClass('open');
        $form.hide(speed, function() {
          $self.addClass('closed').removeClass('transition');
        });
      }
      else {
        $self.addClass('transition').removeClass('closed');
        $form.show(speed, function() {
          $self.addClass('open').removeClass('transition');
        });
      }
    });
    
  });


})(jQuery, undefined);;

