
// Transition functions
$(function() {
		
		$.makeImageTransition = function (tObectId,selection) {

			// this function is only used for the auto-slideshow. Therefore, we can check a flag to see
			// whether the auto feature has been turned off dynamically.
			if(jtransition.isSlideShowOn)
			{
			
				// reference current selection and current element before applying new
				var old_selection = jtransition.arr[tObectId]['gCurrentTransitionSection'];
				var oldElement = $("[id='imgTransition']" && "[transitionselection='"+old_selection+"']");
										
				// //get the next image
				if(selection)
				jtransition.arr[tObectId]['gCurrentTransitionSection'] = selection;
				else
				jtransition.arr[tObectId]['gCurrentTransitionSection']++; 
				
				// confirm the selection;
				$.evaluateCurrentSelection(tObectId);			
	
				// all other statements from this point depend on the state of the transitionsEnabled variable
				if(jtransition.transitionsEnabled)
				{
	
					// if the current selection has an interval setting, apply it now;		
					$.evaluateTransitionInterval(tObectId);
					
					// to ensure our new element is properly revealed, set its z-index in relation to the old element
					var zindex = (oldElement)?$(oldElement).css('zIndex')-1 :1;
	
					// reference the new selection 
					var newElement = $("[id='imgTransition']" && "[transitionselection='"+jtransition.arr[tObectId]['gCurrentTransitionSection']+"']");
					
					// give the new selection the next z-order below the visible image
					$(newElement).css('zIndex', zindex);
			
					// make the new image visible, but keep it below the stack order so the fade will be smooth
					/*
					* Keep in mind, it's important to bring the image back into display mode. The initial load
					* configures the image to be displayed by simply adjusting the Z order. After that, it is
					* turned off (that is, when another image is clicked and takes its place on the stage. So on
					* the second time around, the image must be turned back on. Just make sure the statement that
					* follows actually turns on the image. SHOW was giving trouble.
					*/
					// using an interval, begin to fade-in the next image
					if(jtransition.arr[tObectId]['fadeindelay'] && jtransition.arr[tObectId]['fadeindelay'][jtransition.arr[tObectId]['gCurrentTransitionSection']])
					{
						var delayAmount = jtransition.arr[tObectId]['fadeindelay'][jtransition.arr[tObectId]['gCurrentTransitionSection']];
						
						// controls duration of fade
						
						$(newElement).css({'opacity':'0','display':'block'}).stop().animate({opacity:'1'},{duration: delayAmount})
						
					
					} else {
						$(newElement).css({'opacity':'0','display':'block'}).stop().animate({opacity:'1'},{duration: 3050})
					}
					
					
					// make transition								
					if(jtransition.arr[tObectId]['fadeoutdelay'] && jtransition.arr[tObectId]['fadeoutdelay'][old_selection])
					{
						var delayAmount = jtransition.arr[tObectId]['fadeoutdelay'][old_selection];
						
						// controls duration of fade
						
						$(oldElement).stop().animate({opacity:'0'},{duration: delayAmount,complete: function()
						{	 
							$.finalizeOldToNewImageSwap(this,newElement);
						}});
						
					
					} else {
						$(oldElement).stop().animate({opacity:'0'},{duration: 2775,complete: function()
						{	 
							$.finalizeOldToNewImageSwap(oldElement,newElement);
						}});
					}
					
					// if the phase limit is set and we are on the last image, stop transitions
					$.evaluateIntervalLimitation(tObectId);
				
					
				}
			} else {
					
					// because we are here and yet evaluated that the slideshow was turned off, our interval timer might
				// still be running; terminate the last known interval id
				$.terminateTransition(jtransition.arr[tObectId]['transitionIntervalId']);	
			}
		};
		$.makeImmediateImageTransition = function (tObectId,selection) {
			
			// reference current selection and current element before applying new
			var old_selection = jtransition.arr[tObectId]['gCurrentTransitionSection'];
			var oldElement = $("[id='imgTransition']" && "[transitionselection='"+old_selection+"']");
			
			// skip if old selection is the same as the new
			if(old_selection!=selection)
			{		
				// apply new
				jtransition.arr[tObectId]['gCurrentTransitionSection'] = selection;
				
				// to ensure our new element is properly revealed, set its z-index in relation to the old element
				var zindex = (oldElement)?$(oldElement).css('zIndex')-1 :1;
				
				
				//reference the new selection
				var newElement = $("[id='imgTransition']" && "[transitionselection='"+jtransition.arr[tObectId]['gCurrentTransitionSection']+"']");
				
				// give the new selection the next z-order below the visible image
				$(newElement).css('zIndex', zindex);
				
				// using an interval, begin to fade-in the new image
				$(newElement).css({'opacity':'0','display':'block'}).stop().animate({opacity:'1'},{duration: 250})
	
				// fade out old image
				
				$(oldElement).stop().animate({opacity:'0'},{duration: 250,complete: function()
				{
					$.finalizeOldToNewImageSwap(this,newElement);
				}});
			}
		};
		/*
		* This is always called by a transition function; either from makeImageTransition() or makeImmediateImageTransition().
		* This is because these transition functions share the same code when finalizing image swaps. This function allows me
		* to maintain one code reference for all other transition functions.
		*/
		$.finalizeOldToNewImageSwap = function (oldElement,newElement) {
			
			// to ensure our new element is properly revealed, set its z-index to the value of the old element
			var zindex = (oldElement)?$(oldElement).css('zIndex') :2;
			
			// prevent old image from hijacking clicks
			$(oldElement).css({'display':'none'});
			// give it a low stacking order
			$(oldElement).css('zIndex', 1);
			// finalize the new selection by setting it highest in the stack order
			$(newElement).css('zIndex', zindex);
		};
		$.transitionToImageById = function (tObectId,selection) {
			//alert(identifier)
			// end current transitions
		    $.terminateTransition(jtransition.arr[tObectId]['transitionIntervalId']);
			
			// perform 1-time transition
			$.makeImmediateImageTransition(tObectId,selection)
			
		};
		$.startTransitionsAfterMaxIdleTime = function(selectionId,transitionNumber) {
			
			jtransition.IdleCountTime++;
			
			if(jtransition.IdleCountTime>=jtransition.MaxIdleTime) {
				// reset the count
				jtransition.IdleCountTime = 0;
				// just in case, make sure the old interval terminated
				$.terminateTransition(jtransition.arr[selectionId]['transitionIntervalId']);
				// begin our transitions again	
				jtransition.arr[selectionId]['transitionIntervalId'] = setInterval('jQuery.makeImageTransition('+selectionId+')', transitionNumber);
			} else {
				// wait
				window.setTimeout(function() { $.startTransitionsAfterMaxIdleTime(selectionId,transitionNumber); }, 1000);
			}
		};
		$.hideCurrentTransition = function (tObectId) {
			
			// if the transitions are disabled, all of our images are stacked and possibly with full opacity. Removing the
			// foremost element might leave a ghosted (or full viewed) image behind it. To prevent this, hide all stacks if
			// transitions are OFF
			if(!jtransition.transitionsEnabled)
			{
				$("[id='imgTransition']").each(function(){
					$(this).css({'display':'none'});
				});
			}
			
			// go ahead and proceed with the standard routine
			var element = $("[id='imgTransition']" && "[transitionselection='"+jtransition.arr[tObectId]['gCurrentTransitionSection']+"']");
			$(element).animate({opacity:'0'},{duration: 250,complete: function()
			{	 		  	
			  	$(element).css({'display':'none'}); // keeps it from hijacking clicks
				// give it a low stacking order
				$(element).css('zIndex', 1);

			}});
		};
		$.resetTransitionVars = function (tObectId) {
			jtransition.arr[tObectId]['gCurrentTransitionSection'] = 0;
		};
		$.terminateTransition = function (transitionId) {
			if(typeof transitionId !== 'undefined')
				clearInterval(transitionId);
		};
		$.evaluateIntervalLimitation = function (selectionId) {
			if(jtransition.isPhaseLimit && jtransition.isPhaseLimit[selectionId] && jtransition.arr[selectionId]['gCurrentTransitionSection']==jtransition.arr[selectionId]['numOfTransitionSections'])
			{
				 // end transitions
				 $.terminateTransition(jtransition.arr[selectionId]['transitionIntervalId']);			 
			}
		};
		$.evaluateCurrentSelection = function (selectionId) {				
			if(jtransition.arr[selectionId]['gCurrentTransitionSection']<1) // evaluate less than 0
				jtransition.arr[selectionId]['gCurrentTransitionSection'] = jtransition.arr[selectionId]['numOfTransitionSections']; // give hightest count	
			else if(jtransition.arr[selectionId]['gCurrentTransitionSection']>jtransition.arr[selectionId]['numOfTransitionSections']) 
				jtransition.arr[selectionId]['gCurrentTransitionSection'] = 1; // give lowest number
		};
		$.evaluateTransitionInterval = function (selectionId) {				
			if(jtransition.arr[selectionId]['interval'])
			{
				if( jtransition.arr[selectionId]['interval'][jtransition.arr[selectionId]['gCurrentTransitionSection']] )
				{
					var transitionNumber = jtransition.arr[selectionId]['interval'][jtransition.arr[selectionId]['gCurrentTransitionSection']];
					
					// end the current transition
					$.terminateTransition(jtransition.arr[selectionId]['transitionIntervalId']);
					// begin new transition with new interval number
					jtransition.arr[selectionId]['transitionIntervalId'] = setInterval('jQuery.makeImageTransition('+selectionId+')', transitionNumber);
				}
			}
		};
		
		
});

