// JavaScript Document

var currentPanel = 1;
var totalPanels = 0;
var autoPlay = true;
var timePassed = 0;
var timeToChange = 3;

$(document).ready(function(){
						   
   function autoAdvance(){
	 
	 if(window.timePassed == window.timeToChange){
		 window.timePassed = 0;
		 if(window.currentPanel == window.totalPanels){
			 currentPanel = 0;
		  }
		  if(autoPlay == true){
			  $('.marquee_nav a.marquee_nav_item:nth-child('+(window.currentPanel+1)+')').trigger('click');
		  }
     }else{
		 window.timePassed += 1;
	 }
	 /* debug */$('.timePassed').html('timePassed = '+window.timePassed);
	 /* debug */$('.autoPlay').html('autoPlay = '+window.autoPlay);
	 
   }
						   
  /* debug */$('.autoPlay').html('autoPlay = '+window.autoPlay);
  /* debug */$('.timePassed').html('timePassed = '+window.timePassed);
  /* debug */$('.timeToChange').html('timeToChange = '+window.timeToChange);
  /* debug */$('.currentPanel').html('currentPanel = '+window.currentPanel);
  
  //set interval runs function autoAdvance every 1 sec
  setInterval(autoAdvance, 1000);
  
  $('.marquee_container').hover(
	 function(){
		 window.autoPlay = false;
		 $(this).removeClass('autoplay');
	 },
	 function(){
		 window.autoPlay = true;
		 window.timePassed = 0;
		 $(this).addClass('autoplay');
	 }
	);
  
  //Preload
  $('.marquee_panels img').imgpreload(function(){
	  initializeMarquee();										   
  });
  
  //Generate Photo Lineup
  $('img.marquee_panel_photo').each(function(index){
	 var photoWidth = $('.marquee_container').width();
	 var photoPosition = index * photoWidth;
	 
	 //ADD FancyBox Option to all Images in Slideshow
	 $('.marquee_photos').append('<a rel="example_group" href="'+$(this).attr('src')+'" title="'+$(this).attr('title')+'" ><img class="marquee_photo" style="border:none left:'+photoPosition+'" src="'+$(this).attr('src')+'" alt="'+$(this).attr('alt')+'" width="'+photoWidth+'" height="350" /></a>');
	 
	 //original
/*	 $('.marquee_photos').append('<img class="marquee_photo" style="left:'+photoPosition+'" src="../Includes/'+$(this).attr('src')+'" alt="'+$(this).attr('alt')+'" width="'+photoWidth+'" height="350" />');*/
	 
	 //Tells div with class marquee_photos to be as wide as all images inserted inside
	 $('.marquee_photos').css('width', photoPosition+photoWidth);
   });
   // Generate Navigation Links
   $('.marquee_panels .marquee_panel').each(function(index){
	 $('.marquee_nav').append('<a class="marquee_nav_item"></a>');
	 window.totalPanels = index + 1;
	 /* debug */$('.totalPanels').html('totalPanels = '+window.totalPanels);
	 
	});
   
     // Set up Navigation Links
	 $('.marquee_nav a.marquee_nav_item').click(function(){
	    $('.marquee_nav a.marquee_nav_item').removeClass('marquee_nav_item_selected');
		$(this).addClass('marquee_nav_item_selected');
		
		var navClicked = $(this).index();
		var marqueeWidth = $('.marquee_container').width();
		//To Create a sliding to the left effect all pictures start from 0,0 so this will find
		//width and move to the negative amount.
		var distanceToMove = marqueeWidth * (-1);
		var newPhotoPosition = navClicked * distanceToMove + 'px';
		
		//new caption will get the corresponding data from the navigation item clicked
		var newCaption = $('.marquee_panel_caption').get(navClicked);
		window.currentPanel = navClicked + 1;
		/* debug */$('.currentPanel').html('currentPanel = '+window.currentPanel);
		
		$('.marquee_photos').animate({left: newPhotoPosition}, 1000);
		$('.marquee_caption').animate({top: '340px'}, 500, function(){
																	
			//this is Jqueries way to grab all of html inside of an object
		    var newHTML = $(newCaption).html();
			$('.marquee_caption_content').html(newHTML);
			setCaption();
		 });
	  });
   
});


function setCaption(){
	var captionHeight = $('.marquee_caption').height();
	var marqueeHeight = $('.marquee_container').height();
	var newCaptionHeight = marqueeHeight - captionHeight - 15;
	$('.marquee_caption').delay(100).animate({top: newCaptionHeight}, 500);

}

function  initializeMarquee(){
	$('.marquee_caption_content').html(
	  $('.marquee_panels .marquee_panel:first .marquee_panel_caption').html()								
	 );	
	$('.marquee_nav a.marquee_nav_item:first').addClass('marquee_nav_item_selected');
	$('.marquee_photos').fadeIn(1500);
	setCaption();
}




//Basic Information For FancyBox Taken from Group Settings

$(document).ready(function() {

	/* This is basic - uses default settings */
	
		$("a[rel=example_group]").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		//'transitionIn'		: 'none',
		//'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});
	
});




