/******************************************************************************
 * MaRS Site Javascript
 *****************************************************************************/
// Application Delegate
jQuery(document).ready(function() {
  var $ = jQuery.noConflict();

  ///
  // Start code that runs after page loads
  ///

  /******************************************************************************
   * Application Delegate Definition
   *****************************************************************************/
  var appDelegate = {
    searchFormCtl: null,
    textResizerCtl: null,
    newsletterSignupCtl: null,
	homePageSpotlightCtl: null,
	homeEventsPageCtl: null,
	searchDetailPageCtl: null,
	upcomingEventsPageCtl: null,
	marsOnTheWebPageCtl: null,
	videoThumbsPageCtl: null,
    horizontalSliderCtl: null,
    rotatorCtl: null,
    relatedTab1: null,
    relatedTab2: null,
    relatedTab3: null
  };

 /******************************************************************************
  * Application Delegate Implementation
  *****************************************************************************/
  function _initialize() {

    // If this div exists, we're on the homepage so setup the homepage spotlight
    $('#homepage-spotlight').eq(0).each(function() {
      $M.debug("#homepage-spotlight is ready.");

      appDelegate.homePageSpotlightCtl = new HomePageSpotlightController();
    });

    appDelegate.searchFormCtl = new SearchFormController();

    appDelegate.textResizerCtl = new TextResizerController();

    appDelegate.newsletterSignupCtl = new NewsletterSignupController();
    
    appDelegate.rotatorCtl = new RotatorController();
    
	// Get friendfeed search results (on search page)
	jQuery('#friendfeed-results').eq(0).each(function() {
		jQuery('#friendfeed-results').load('/blog/wp-content/themes/MaRS/marsontheweb-search.php?search-details-term=' + escape(jQuery('#friendfeed-search-term').text()), null, function() {
			jQuery('#friendfeed-results div.tabs').tabs();
			jQuery('#friendfeed-results').fadeIn();
		});
	});

	// Get friendfeed search results (on buzz landing page)
	jQuery('#friendfeed-generic').eq(0).each(function() {
		jQuery('#friendfeed-generic').load('/blog/wp-content/themes/MaRS/marsontheweb.php', null, function() {
		    appDelegate.marsOnTheWebPageCtl = new PaginationController('#mars-on-the-web','div.mars-on-the-web-item', 5);
			jQuery('#friendfeed-generic').fadeIn();
		});
	});

    // Setup tabs
    $('div.tabs').tabs();
    
    // Setup accordians
    $('#secondary-content div.accordion').accordion();
    $('#secondary-section div.accordion').accordion();
    $('#main-content div.accordion').accordion({
    		autoHeight: false
    });
    $('#main-section div.accordion').accordion({
    		autoHeight: false
    });
    
    // Setup breadcrumbs
    $('#breadcrumbs').eq(0).each(function() {
      $('#breadcrumbs dl dd').css('display', 'none');
      $('#breadcrumbs dl').mouseover(function() {
        $('#breadcrumbs dl dd').css('display', 'none');
        $('dd', this).css('display', 'block');
      });
      $('#breadcrumbs dl').mouseout(function() {
        $('dd', this).css('display', 'none');
      });
    });
    
    // Setup pagination
    appDelegate.homeEventsPageCtl = new PaginationController('#homepage-events', 'div.home-event-callout', 5);
    appDelegate.searchDetailPageCtl = new PaginationNumberController('#search-results-rel', 'div.search-result-rel', 10);
    appDelegate.searchDetailPageCtl = new PaginationNumberController('#search-results-date', 'div.search-result-date', 10);
    appDelegate.upcomingEventsPageCtl = new PaginationController('#upcoming-events','div.upcoming-event', 10);
    appDelegate.marsOnTheWebPageCtl = new PaginationController('#popular-tags','div.mars-popular-tag-item', 10);
    appDelegate.videoThumbsPageCtl = new PaginationNumberController('#video-thumbs-paginate','div.video', 6);
    appDelegate.relatedTab1 = new PaginationController('#related-tab-1', 'p', 10, { paginationControlSetWidth: '260px' });
    appDelegate.relatedTab2 = new PaginationController('#related-tab-2', 'p', 5, { paginationControlSetWidth: '260px' });
    appDelegate.relatedTab3 = new PaginationController('#related-tab-3', 'p', 10, { paginationControlSetWidth: '260px' }); 

    // Setup calendars
    $('input.calendar').datepicker({ dateFormat: 'yy-mm-dd'});
    
    // Setup horizontal sliders
    appDelegate.horizontalSliderCtl = new SliderController();

    // Setup video thumbnail
    $('#video-thumbnail').click(function() {
  	  $('#video-player').fadeIn().draggable();

    });

    $('#video-player a').click(function() {
  	  $('#video-player').fadeOut();
    });
    
	///
	// Newsletter Validation
	///
	
	// SUCCESS AJAX CALL, replace "success: false," by:     success : function() { callSuccessFunction() }, 
	$("#newsletter-signup-form").validationEngine({
		promptPosition: "topLeft", 
		success :  false,
		failure : function() {}
	});
	 
	// SUCCESS AJAX CALL, replace "success: false," by:     success : function() { callSuccessFunction() }, 
	$("#site-search-form").validationEngine({
		promptPosition: "topLeft", 
		success :  false,
		failure : function() {}
	});	
	
	$("#discovery-document").validationEngine({
		promptPosition: "topRight", 
		success :  false,
		failure : function() {}
	});		
	
	$("#newsletter-main").validationEngine({
		promptPosition: "topRight", 
		success :  false,
		failure : function() {}
	});	
	
	$("#tenant-services").validationEngine({
		promptPosition: "topRight", 
		success :  false,
		failure : function() {}
	});	
	
	$("#submit-job").validationEngine({
		promptPosition: "topRight", 
		success :  false,
		failure : function() {}
	});	
	
	$('form.validation-engine').validationEngine({
		promptPosition: "topRight",
		success: false,
		failure: function() {}
	});
  }
  
  $('div.carousel').carousel({
	  pagination: true,
	  nextBtn: "<span></span>",
	  prevBtn: "<span></span>",
	  autoSlide: true,
	  autoSlideInterval: 3000
  });

  _initialize();
  
  $M.debug(appDelegate.homePageSpotlightCtl);
  
  $M.debug("google.setOnLoadCallback: ended");
});

function HomePageSpotlightController(settings) {
  settings = jQuery.extend({
  }, settings);
  
  var $ = jQuery.noConflict();
  
  /******************************************************************************
   * Home Page Spotlight Definition
   *****************************************************************************/
  var definition = {
    initialize: _initialize
  };

  /******************************************************************************
   * Home Page Spotlight Implementation
   *****************************************************************************/
   function _initialize() {
     var flashvars = { initialURL: $M.docroot + "../../index.xml?path=/index" };
     var params = {wmode: "transparent"};
     var attributes = {};
     swfobject.embedSWF($M.docroot + 'swfs/homePageSpotlight/MaRSHomepage.swf', 'homepage-spotlight', 950, 432, "9.0.124", $M.docroot + "swfs/homePageSpotlight/playerProductInstall.swf", flashvars, params, attributes);

     $('#schema-switcher li a').bind('click', function() {
       var id = 0;
       var schema = $(this).html();
       var swfObj = $('#homepage-spotlight-ctr object').eq(0).get();
       
       $M.debug(swfObj);
       
       swfObj.setSchema(schema);
       id++;        
     });
   }
  
  /* treat as a sort of constructor */
  _initialize();
  
  return null; // Since everything is handled by flash, no javascript code needs to stick around in memory
}

function SearchFormController(settings) {
  settings = jQuery.extend({
    selector: '#site-search-form'
  }, settings);
  
  var $ = jQuery.noConflict();
  
  /******************************************************************************
   * Search Form Definition
   *****************************************************************************/
  var definition = {
    initialize: _initialize
  };
  
  /******************************************************************************
   * Search Form Implementation
   *****************************************************************************/
  function _initialize() {
  }
  
  /* treat as a sort of constructor */
  _initialize();
  
  return definition;
}

function TextResizerController(settings) {
  settings = jQuery.extend({
    selector: '#text-resizer',
    contentSelector: '#template',
    defaultSize: 'medium-text'
  }, settings);
  
  var $ = jQuery.noConflict();
  
  /******************************************************************************
   * Text Resizer Definition
   *****************************************************************************/
  var definition = {
    initialize: _initialize,
    textSizeShouldChange: _textSizeShouldChange
  };
  
  /******************************************************************************
   * Text Resizer Implementation
   *****************************************************************************/
  function _initialize() {
    $(settings.selector + ' a').click(function() {
      definition.textSizeShouldChange($(this).attr('href').replace('#', ''));
    });
    
    definition.textSizeShouldChange(settings.defaultSize);
  }
  
  function _textSizeShouldChange(size) {
    $(settings.contentSelector) .removeClass('small-text')
                                .removeClass('medium-text')
                                .removeClass('large-text');

    $(settings.contentSelector).addClass(size);
    
    $(settings.selector + ' a').removeClass('selected');
    $(settings.selector + ' a.' + size).addClass('selected');
  }
  
  /* treat as a sort of constructor */
  _initialize();
  
  return definition;
}

function SliderController(settings) {
  settings = jQuery.extend({
    orientation: 'horizontal',
    selector: '.horizontal-slider',
    pageSelector: '.page',
    pageWidth: '560'
  }, settings);
  
  var $ = jQuery.noConflict();
  
  /******************************************************************************
   * Slider Definition
   *****************************************************************************/
  var definition = {
    initialize: _initialize
  };
  
  var currentPageIndex = 0;
  var numberOfPages = 0;
  var interval;
  
  /******************************************************************************
   * Slider Implementation
   *****************************************************************************/
  function _initialize() {
    $(settings.selector + ' ' + settings.pageSelector).css('visibility', 'hidden');
    $(settings.selector + ' ' + settings.pageSelector).eq(0).css('visibility', 'visible');
    $(settings.selector + ' .page-set').css('margin-left', '0px');
    numberOfPages = $(settings.selector + ' ' + settings.pageSelector).length;
    
    $(settings.selector + ' a.slide-left').click(function() {
      var shouldSlideRight = false;
      _slide(shouldSlideRight);
    }).css('display', 'none');
    
    $(settings.selector + ' a.slide-right').click(function() {
      var shouldSlideRight = true;
      _slide(shouldSlideRight);
    }).css('display', (numberOfPages > 1) ? 'block' : 'none');
    
    $(settings.selector).css('overflow', 'hidden');
    $(settings.selector + ' ' + settings.pageSelector).css('float', 'left');
  }
  
  function _slide(shouldSlideRight) {
    var nextVisibleIndex = (shouldSlideRight) ? currentPageIndex + 1 : currentPageIndex - 1;
    
    $(settings.selector + ' ' + settings.pageSelector).eq(nextVisibleIndex).css('visibility', 'visible');
    currentPageIndex = (shouldSlideRight) ? currentPageIndex + 1 : currentPageIndex - 1;
    
    $(settings.selector + ' a.slide-left').css('display', (currentPageIndex == 0) ? 'none' : 'block');
    
    $(settings.selector + ' a.slide-right').css('display', ((currentPageIndex + 1) == numberOfPages) ? 'none' : 'block');
                                                                                
    interval = setInterval(function() { _animateSlide(shouldSlideRight) }, 10);
  }
  
  function _animateSlide(shouldSlideRight) {
    var marginLeft = parseInt($(settings.selector + ' .page-set').css('margin-left').replace('px', ''));
    var newMarginLeft = (shouldSlideRight) ? (marginLeft - 10) : (marginLeft + 10);
    $(settings.selector + ' .page-set').css('margin-left', newMarginLeft + 'px');

    if (newMarginLeft % settings.pageWidth == 0) {
      clearInterval(interval);
      
      var nextHiddenIndex = (shouldSlideRight) ? currentPageIndex - 1 : currentPageIndex + 1;
      $(settings.selector + ' ' + settings.pageSelector).eq(nextHiddenIndex).css('visibility', 'hidden');
    }
  }
  
  /* treat as a sort of constructor */
  _initialize();
  
  return definition;
}

function NewsletterSignupController(settings) {
  settings = jQuery.extend({
    selector: '#newsletter-signup-form'
  }, settings);
  
  var $ = jQuery.noConflict();
  
  /******************************************************************************
   * Newsletter Signup Definition
   *****************************************************************************/
  var definition = {
    initialize: _initialize
  };
  
  /******************************************************************************
   * Newsletter Signup Implementation
   *****************************************************************************/
  function _initialize() {
    $(settings.selector).submit(function() {
      return true;
    });
  }
  
  /* treat as a sort of constructor */
  _initialize();
  
  return definition;
}

function RotatorController(settings) {
  settings = jQuery.extend({
    rotatorSelector: 'div.rotator',
    rotatorStateSelector: 'div.rotation-state',
    selectedStateClass: 'rotation-state-selected',
    unselectedStateClass: 'rotation-state-unselected'
  }, settings);
  
  var $ = jQuery.noConflict();
  
  /******************************************************************************
   * RotatorController Definition
   *****************************************************************************/
  var definition = {
    initialize: _initialize
  };
  
  /******************************************************************************
   * RotatorController Implementation
   *****************************************************************************/
  function _initialize() {
    $(settings.rotatorSelector).each(function(i, val) {
      // We'll animate the rotator if there is more than one rotation state
      if ($(settings.rotatorStateSelector, val).length > 1) {
        $(val).css('min-height', $(val).height() + 'px');
        window.setInterval(function() { _rotate($(val)); }, 5000);
      }
    });
  }
  
  function _rotate(theRotator) {
    if ($('.' + settings.selectedStateClass, theRotator).length == 0) {
      var firstState = $(settings.rotatorStateSelector + ':first', theRotator).addClass(settings.selectedStateClass);
      firstState.siblings().addClass(settings.unselectedStateClass);
    }    
    else {
      $(settings.rotatorStateSelector, theRotator).removeClass(settings.unselectedStateClass);
      var currentState = $(settings.rotatorStateSelector + '.' + settings.selectedStateClass, theRotator).eq(0);
      var nextState = currentState.next();
      
      // We've gone past the last state, so loop back to the beginning
      if (nextState.length == 0) {
        nextState = $(settings.rotatorStateSelector, theRotator).eq(0);
      }

      currentState.fadeOut(function() {
        currentState.removeClass(settings.selectedStateClass).addClass(settings.unselectedStateClass);
        nextState.fadeIn(function() {
           nextState.addClass(settings.selectedStateClass);
           nextState.addClass(settings.selectedStateClass);
           nextState.siblings().addClass(settings.unselectedStateClass);
        });
      });
    }
  }
  
  /* treat as a sort of constructor */
  _initialize();
  
  return definition;
}

function PaginationController(contentSelector, itemSelector, itemNumber, settings) {
  settings = jQuery.extend({
		pageClass: 'pagination-page',
		itemClass: 'pagination-item',
		numberOfItemsPerPage: itemNumber,
		previousPageLinkClass: 'pagination-previous-link',
		nextPageLinkClass: 'pagination-next-link',
		pageLinkClass: 'pagination-link',
		paginationControlSetWidth: null
  }, settings);
  
  var $ = jQuery.noConflict();
  
  /******************************************************************************
   * PaginationController Definition
   *****************************************************************************/
  var definition = {
    initialize: _initialize,
    selectedPage: 0,
    numberOfPages: 0,
    numberOfItems: 0
  };
  
  /******************************************************************************
   * PaginationController Implementation
   *****************************************************************************/
  function _initialize() {
	// Determine the number of items in all of the content
	var numberOfItems = $(itemSelector, contentSelector).length;
	definition.numberOfItems = numberOfItems;
	var isFirstPage = true;
	var pageCount = 0;
	
	// Wrap numberOfItemsPerPage in a page
	for (var strider = 0; strider < numberOfItems; strider += settings.numberOfItemsPerPage) {
		if (isFirstPage) {
			pageClass = settings.pageClass + ' selected';
			isFirstPage = false;
		}
		else {
			pageClass = settings.pageClass;
		}
		
		$(itemSelector, contentSelector).slice(strider, strider + settings.numberOfItemsPerPage).wrapAll('<div class="' + pageClass + '"></div>');
		pageCount++;
	}
	
	definition.numberOfPages = pageCount;

	$('.' + settings.pageClass, contentSelector).css('display', 'none');
	$('.' + settings.pageClass + '.selected', contentSelector).css('display', 'block');
	if (numberOfItems > itemNumber) {
	$(contentSelector).append('<div class="pagination-controls"></div>');

	while (pageCount-- > 0) {
		var pageLinkClass = (pageCount != 0) ? settings.pageLinkClass : settings.pageLinkClass + ' selected';
		
		$('div.pagination-controls', contentSelector).prepend('<a href="javascript:void(0)" class="' + pageLinkClass + '">' + (pageCount + 1) + '</a>');		
	}
	$('div.pagination-controls', contentSelector).prepend('<a href="javascript:void(0)" class="' + settings.previousPageLinkClass + '">&lt;</a> ');
	$('div.pagination-controls', contentSelector).append(' <a href="javascript:void(0)" class="' + settings.nextPageLinkClass + '">&gt;</a>');
	$('div.pagination-controls', contentSelector).append('&nbsp;&nbsp;&nbsp;<span class="status-text" style="font-weight:bold;">' + _statusText() + ' </span>');
	
	$('div.pagination-controls a', contentSelector).click(_pageWasSelected);
	
	if (settings.paginationControlSetWidth != null) {
		$('div.pagination-controls').css('width', settings.paginationControlSetWidth);
	}
	}
  }
  
  function _pageWasSelected() {
	  var selectedPage;
	
	  $('.' + settings.pageClass, contentSelector).removeClass('selected');
	
	  if ($(this).hasClass(settings.nextPageLinkClass)) {
		  selectedPage = definition.selectedPage + 1;
		  
		  if (selectedPage >= definition.numberOfPages) {
			  selectedPage--;
		  }
	  }
	  else if ($(this).hasClass(settings.previousPageLinkClass)) {
		  selectedPage = definition.selectedPage - 1;
		  
		  if (selectedPage < 0) {
			  selectedPage++;
		  }
	  }
	  else {
		  selectedPage = parseInt($(this).html()) - 1;
	  }
	  
          $('a.' + settings.pageLinkClass, contentSelector).removeClass('selected').eq(selectedPage).addClass('selected');

	  definition.selectedPage = selectedPage;
	  
	  $('.' + settings.pageClass, contentSelector).eq(selectedPage).addClass('selected');

	  $('.' + settings.pageClass, contentSelector).css('display', 'none');
	  $('.' + settings.pageClass + '.selected', contentSelector).css('display', 'block');
	  
	  $('.status-text', contentSelector).html(_statusText());
  }
  
  function _statusText() {
	  var startItem = definition.selectedPage * settings.numberOfItemsPerPage + 1;
	  var endItem = startItem + $(itemSelector, contentSelector).slice(definition.selectedPage * settings.numberOfItemsPerPage, definition.selectedPage * settings.numberOfItemsPerPage + settings.numberOfItemsPerPage).length - 1;
	  return startItem + ' - ' + endItem + ' of ' + definition.numberOfItems;
  }
  
  /* treat as a sort of constructor */
  $(contentSelector).eq(0).each(_initialize);
  
  return definition;
}

function PaginationNumberController(contentSelector, itemSelector, itemNumber, settings) {
  settings = jQuery.extend({
		pageClass: 'pagination-page',
		itemClass: 'pagination-item',
		numberOfItemsPerPage: itemNumber,
		previousPageLinkClass: 'pagination-previous-link-number',
		nextPageLinkClass: 'pagination-next-link-number',
		pageLinkClass: 'pagination-link-number'
  }, settings);
  
  var $ = jQuery.noConflict();
  
  /******************************************************************************
   * PaginationController Definition
   *****************************************************************************/
  var definition = {
    initialize: _initialize,
    selectedPage: 0,
    numberOfPages: 0,
    numberOfItems: 0
  };
  
  /******************************************************************************
   * PaginationController Implementation
   *****************************************************************************/
  function _initialize() {
	// Determine the number of items in all of the content
	var numberOfItems = $(itemSelector, contentSelector).length;
	definition.numberOfItems = numberOfItems;
	var isFirstPage = true;
	var pageCount = 0;
	
	// Wrap numberOfItemsPerPage in a page
	for (var strider = 0; strider < numberOfItems; strider += settings.numberOfItemsPerPage) {
		if (isFirstPage) {
			pageClass = settings.pageClass + ' selected';
			isFirstPage = false;
		}
		else {
			pageClass = settings.pageClass;
		}
		
		$(itemSelector, contentSelector).slice(strider, strider + settings.numberOfItemsPerPage).wrapAll('<div class="' + pageClass + '"></div>');
		pageCount++;
	}
	
	definition.numberOfPages = pageCount;

	$('.' + settings.pageClass, contentSelector).css('display', 'none');
	$('.' + settings.pageClass + '.selected', contentSelector).css('display', 'block');
	if (numberOfItems > itemNumber) {
	$(contentSelector).append('<div class="pagination-controls"></div>');

	while (pageCount-- > 0) {
		var pageLinkClass = (pageCount != 0) ? settings.pageLinkClass : settings.pageLinkClass + ' selected-number';
		
		$('div.pagination-controls', contentSelector).prepend('<a href="javascript:void(0)" class="' + pageLinkClass + '">' + (pageCount + 1) + '</a>');		
	}
	$('div.pagination-controls', contentSelector).prepend('<a href="javascript:void(0)" class="' + settings.previousPageLinkClass + '">&lt;</a> ');
	$('div.pagination-controls', contentSelector).append(' <a href="javascript:void(0)" class="' + settings.nextPageLinkClass + '">&gt;</a>');
	$('div.pagination-controls', contentSelector).append('<br /><span class="status-text" style="font-weight:bold;">' + _statusText() + ' </span>');
	
	$('div.pagination-controls a', contentSelector).click(_pageWasSelected);
	}
  }
  
  function _pageWasSelected() {
	  var selectedPage;
	
	  $('.' + settings.pageClass, contentSelector).removeClass('selected');
	
	  if ($(this).hasClass(settings.nextPageLinkClass)) {
		  selectedPage = definition.selectedPage + 1;
		  
		  if (selectedPage >= definition.numberOfPages) {
			  selectedPage--;
		  }
	  }
	  else if ($(this).hasClass(settings.previousPageLinkClass)) {
		  selectedPage = definition.selectedPage - 1;
		  
		  if (selectedPage < 0) {
			  selectedPage++;
		  }
	  }
	  else {
		  selectedPage = parseInt($(this).html()) - 1;
	  }
	 
	  $('a.' + settings.pageLinkClass, contentSelector).removeClass('selected-number').eq(selectedPage).addClass('selected-number');
 
	  definition.selectedPage = selectedPage;
	  
	  $('.' + settings.pageClass, contentSelector).eq(selectedPage).addClass('selected');

	  $('.' + settings.pageClass, contentSelector).css('display', 'none');
	  $('.' + settings.pageClass + '.selected', contentSelector).css('display', 'block');
	  
	  $('.status-text', contentSelector).html(_statusText());
  }
  
  function _statusText() {
	  var startItem = definition.selectedPage * settings.numberOfItemsPerPage + 1;
	  var endItem = startItem + $(itemSelector, contentSelector).slice(definition.selectedPage * settings.numberOfItemsPerPage, definition.selectedPage * settings.numberOfItemsPerPage + settings.numberOfItemsPerPage).length - 1;
	  return startItem + ' - ' + endItem + ' of ' + definition.numberOfItems;
  }
  
  /* treat as a sort of constructor */
  $(contentSelector).eq(0).each(_initialize);
  
  return definition;
}


///
// Functions and state variables arranged in a namespace-like way.
// e.g., $M.method, or $M.property, so that variable names don't clash.
///
var $M = {
  inDebugMode: false,
  docroot: '/docroot/MaRS/',
  
  ///
  // Prints the given string to the browser debug console
  // if the variable $M.inDebugMode is set to true.
  ///
  debug: function (str) {
    if (! this.inDebugMode) {
      return;
    }

    if (window.console) {
      window.console.log(str);
    }
    else {
      alert(str); // For lesser browsers, like IE
    }
  }
}




function slideShow() {

	//Set the opacity of all images to 0
	$('#buzzGallery a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#buzzGallery a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$('#buzzGallery .caption').css({opacity: 1.0});

	//Resize the width of the caption according to the image width
	$('#buzzGallery .caption').css({width: $('#buzzGallery a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	$('#buzzGallery .content').html($('#buzzGallery a:first').find('img').attr('rel'))
	.animate({opacity: 1.0}, 400);
	
	//Call the buzzGallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('buzzGallery()',6000);
	
}

function buzzGallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#buzzGallery a.show')?  $('#buzzGallery a.show') : $('#buzzGallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#buzzGallery a:first') :current.next()) : $('#buzzGallery a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#buzzGallery .caption').animate({opacity: 0.0}, { queue:false, duration:50 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#buzzGallery .caption').animate({opacity: 0.5},100 ).animate({height: '100px'},500 );
	
	//Display the content
	$('#buzzGallery .content').html(caption);
	
	
}
