var fullPageSlide = (function () {
  var picArray = [],
    numPics = -1,
    pageIndicator,
    timer,
    interval = 20,
    currentSlide = -1,
    inactiveZ,
    activeZ,
    transitionDuration = 1000,
	firstRun = true;
  
  
  
  
  
  function showNextSlide() {
    var newIndex = currentSlide + 1;
    if (newIndex === numPics) {
      newIndex = 0;
    }
    showSlide({}, newIndex);
  }
  
  
  
 
  
  function setTimer() {
    clearTimeout(timer);
    timer = setTimeout(showNextSlide, interval);
  }
  
  
  
  
  function showSlide(e, newSlide, id) {
    var oldSlide = currentSlide;

    clearTimeout(timer);
	
	$('#pageInd').unbind('updated.circPI', showSlide);

    // prep next slide for display (invisible and on top)
    $(picArray[newSlide]).css({'display': 'block', 'z-index': '0', 'opacity': '0'}); 
    
    // fade out text, replace with new text, fade in text
	if (firstRun === true) {
		// don't fade out text if this is the first slide on page load
		firstRun = false;
	} else {
		$('#strapline h1').fadeOut(transitionDuration, function () {
			  var strap = $(picArray[newSlide]).attr('rel');
			  $('#strapline h1 span').empty();
			  $('#strapline h1 span').append(strap);
			  $('#strapline h1').fadeIn(transitionDuration);
		});
    }
	
    // fade in new slide, update inidicator, reset old slide
    $(picArray[newSlide]).animate({'opacity': '1'}, 
      {  
        duration: transitionDuration, 
        complete: function () { 
          $(picArray[oldSlide]).css({'z-index': '-10', 'opacity': '0', 'display': 'none'});
          $(picArray[newSlide]).css({'z-index': '-1'});
          if (pageIndicator !== undefined) {
            pageIndicator.setCurrentSector(newSlide, true); // set sector and suppress event
          }
          currentSlide = newSlide;
          setTimer();
		  $('#pageInd').bind('updated.circPI', showSlide);
        }
      }); 

  }
  
  
  
  
  
  
  
  
  function init(initObj) {

    // get all images
    $(initObj.imageDivID + ' img').each(function (index) {
      picArray[index] = this;
    });
    numPics = picArray.length;

    // set up page indicator for canvas-enabled browsers
    pageIndicator = undefined;
    if (Modernizr.canvas) {
      pageIndicator = new CircularPageIndicator({  
        canvasID:    initObj.pageIndicatorID,
        totPages:    numPics,
        startPage:    0,
        lineWidth:    10,
        // lineCapStyle:  'round',
        gap:      10,
        defaultColour:  '#CCC',
        hoverColour:  '#FFF',
        activeColour:  '#29ABE2'
        //callback:    sectorClicked
      });
    }

    // set interval used by timer
    interval = initObj.intervalSecs * 1000;

    // start listening for page indicator events
    $('#pageInd').bind('updated.circPI', showSlide);

    // begin slideshow
    showSlide({}, 0);

  }

  
  
  
  
  
  
  return {
    init: init
  };

}());










$(document).ready(function () { 
	setTimeout(function () {
		fullPageSlide.init({
			imageDivID: '.background-wrap',
			pageIndicatorID: 'pageInd',
			intervalSecs: 3,
			inactiveZ: '-10',
			activeZ: '-1'
		});
	}, 500);
}); 

