

var counter = 1;
var slide = 0;
var zoomed = false;
var lastIndex =1;
var paused =false;
var timer ="";
$(document).ready(function() {
     $(window).load(
      function() {
            slideShow(3000);

       }
     );
	
});
function slideShow(speed) {
       

        
	//append a LI item to the UL list for displaying caption
	$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');

	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});
	$(".counter-dot-empty:first").addClass('counter-dot-full');
        //$(".counter-dot-empty:first").removeClass('counter-dot-empty');
	//Get the first image and display it (set it to full opacity)
	$("ul.slideshow li").eq(0).css({opacity: 1.0});

        $("ul.slideshow li").eq(0).addClass('show');


	//Get the caption of the first image from REL attribute and display it
	$('#slideshow-caption h3').html($('ul.slideshow a:first').find('img').attr('title'));
	$('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt'));

	//Display the caption
	$('#slideshow-caption').css({opacity: 1.0, bottom:0});


	//Call the gallery function to run the slideshow
       // $(document).ready(function() {
	timer = setInterval('gallery()',speed);
        //}
	//pause the slideshow on mouse over


	 $('.counter-pause').click(function () {
                 
		   var this_img = $('.counter-pause').css("background-image")//, "url(images/play.png)");
		  // alert(this_img.indexOf("pause"));

		   if(this_img.indexOf("pause")>-1){
		       //alert("hello");
		       setRotator('pause');
		   }else{
		       setRotator('play');
		   }
//		   pause = false;
//	         }else{
//
//		 }
        });

        function setRotator(mode){
	    		
		        if (mode=='pause'){
			  clearInterval(timer);
			  $('.counter-pause').css("background-image", "url(http://www.citycentrearchives.ca/cca-jquery/images/play.png)");

			}
		        if (mode=='play'){
			  clearInterval(timer);
			  timer = setInterval('gallery()',3000);
                          $('.counter-pause').css("background-image", "url(http://www.citycentrearchives.ca/cca-jquery/images/pause.png)");
			}
	}


	$('.slideshow-container').hover(
		function () {
		       // paused =false;
			//pauseSwitch();
			 $('.arrow').fadeOut('fast');
			//alert ('going');
		},
		function () {
		        if ($('.main-exit').css('display') =='none'){
			  setRotator('play');
			}
		}
	);
	    
	    $('.pu-mid').hover(
		function () {
			setRotator('pause');

		}
	);
	$('.banners').hover(
		function () {
			clearInterval(timer);

		}
	);
	$('.trigger').hover(
		function () {
			setRotator('pause');
		}
	);
	$('.counter-dots').hover(
		function () {
			//paused =false;
			//pauseSwitch();
		}
	);
	
       $('.trigger').click (
	    function () {
		setRotator('pause');
	    }
	    );
        
	$('.counter-dot-empty').click(
		function () {
		       
			clearInterval(timer);
			 // $('.trigger-bg').effect("pulsate", { times:3 }, 2000);
                        var id = this.id;
                        var index = parseInt(id.substring(4))-1;
			var c = index + 1+ "";
			//var cNext = index +1 +"";
                        //alert(index);
                        //$('ul.slideshow li').css({opacity: 0.0});
                        //$('ul.slideshow li').eq(index).css({opacity: 1.0});
                         var group = ".button-group"+c;
			  $('ul.slideshow li').removeClass('show');
                        $('[class*="button-group"]').fadeOut('fast');

                           $('.arrow').fadeOut('fast');
			   $('.pu-mid').fadeOut('fast');
                        $('ul.slideshow li').eq(index).css({opacity: 1.0});
                        $('ul.slideshow li').eq(index).css('display','none');
                        $('ul.slideshow li').eq(index).addClass('show');
                        $('ul.slideshow li').eq(index).fadeIn('slow',function(){
                          $('ul.slideshow li').css({opacity: 0.0});
                          $('ul.slideshow li').eq(index).css({opacity: 1.0});
			     $('[class*="button-group"]').fadeOut('fast');
			  $(group).fadeIn('fast');
                        }); 
                        $(".counter-dot-full").addClass('counter-dot-empty');
                        $(".counter-dot-full").removeClass('counter-dot-full');
                        document.getElementById(this.id).className = 'counter-dot-full';
                       
			 
			 // alert(group);

                          
			lastIndex = c;
                        counter=index+1;
                       // timer = setInterval('gallery()',speed);
                         paused =true;
			 setRotator('play');

		}

	);
}

function gallery() {



        //if no IMGs have the show class, grab the first image
$('[class*="button-group"]').fadeOut('fast');
 $('.pu-mid').fadeOut('fast');
  $('.arrow').fadeOut('fast'); 
	var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:eq(0)'));
	//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().attr('id') == 'slideshow-caption')? $('ul.slideshow li:eq(0)') : current.next()) : $('ul.slideshow li:eq(0)'));
        
         var c = counter +"";
            
	 var cNext = counter +1 +"";
        var index = $('ul.slideshow li:last').index();

        var d_empty = 'dot-'+ counter;

        counter++;
        var d_full = 'dot-' + counter;

        if(counter == 10){
           counter =1;
           d_full= 'dot-1';
           d_empty= 'dot-9';
           cNext = counter +"";
           c= 1
        }
          
          document.getElementById(d_empty).className = 'counter-dot-empty';
          
          
  
          document.getElementById(d_full).className =  'counter-dot-full';
          //var triggers = '.button-group' + counter;
	  //alert(triggers);
	  if (c==0)
	      c = 1;
	  var group = ".button-group"+c;
	  var nextGroup = ".button-group"+cNext;
	  $(group).fadeOut('fast');
	  $(nextGroup).fadeIn('slow');
          var link = $('.ri-link').eq(counter-1).attr('href');
          var target = $('.ri-link').eq(counter-1).attr('target');
          $('.readmore').attr('href',link);
          $('.readmore').attr('target',target);
        //Get next image caption
	var title = next.find('img').attr('title');
	var desc = next.find('img').attr('alt');

	//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 caption first, and then set and display the caption
	$('#slideshow-caption').animate({opacity: 0.0}, 300, function () {
			//Display the content
			$('#slideshow-caption h3').html(title);
			$('#slideshow-caption p').html(desc);
			$('#slideshow-caption').animate({opacity: 1.0}, 1000);
	});
        
	//Hide the current image
	current.animate({opacity: 0.0}, 1000).removeClass('show');

}


