﻿$(document).ready(function(){

	$('div#main-content img').each( function() {
		if ($(this).css('float') == 'left') $(this).addClass('img-left');
		if ($(this).css('float') == 'right') $(this).addClass('img-right');
	});
		  

	(function( $ ){
	
	  $.fn.rotate = function( options ) {
	  
	  var settings = $.extend( {
		  'rotateTimerInterval' : 10000,
		  'adTimerInterval'     : 8000
		}, options);
		
		// Rotate Tiles
		var tileArray = $('ul#featuredplaces li');
		$(tileArray[0]).css('display','block');
		$(tileArray[1]).css('display','block');
		if ( $.cookie('union-featuredplaces') == null )
		{
			$.cookie('union-featuredplaces', '2');
		}
		else if ( $.cookie('union-featuredplaces') < tileArray.length-1)
		{
			nextTile = parseInt($.cookie('union-featuredplaces'))+1;
			$.cookie('union-featuredplaces', nextTile );
		}
		else $.cookie('union-featuredplaces', '2');
		
		$(tileArray[$.cookie('union-featuredplaces')]).css('display','block');
		
		// Rotating Ads
		var adArray = $('ul.footer-ads li');
		
		if ( adArray.length == 1 ) return;
		
		var currentAd = 0;
		var prevAd;
		var nextAd;
		var adTimer = setTimeout( showNextAd, settings.adTimerInterval );
		
		$(adArray).each( function() { $(this).css('display', 'none')} );
		$(adArray[currentAd]).css('z-index', 2 ).css('display', 'block');
		
		function startAdTimer() {
			adTimer = setTimeout( showNextAd, settings.adTimerInterval );
			}
		
		function stopAdTimer() { 
			clearTimeout ( adTimer ); 
			}
		
		function showNextAd() {
			stopAdTimer();
			if ( currentAd < adArray.length-1 ) nextAd = currentAd + 1;
			else nextAd = 0;
			rotateAd();
			startAdTimer();
		}
		
		function showPreviousAd() {
			stopAdTimer();
			if ( currentAd != 0 ) nextAd = currentAd - 1; 
			else nextAd = adArray.length - 1;
			rotateAd();
			startAdTimer();
		}
		
		// ROTATE AND FADE THE AD
		function rotateAd() {
			$(adArray[nextAd]).css('z-index', 2 ).css('display','block');
			$(adArray[currentAd]).css('z-index', 1);
			$(adArray[currentAd]).css('display','none');
			currentAd = nextAd;
		}
	
		// Rotating Banner 
		var rotateArray = $('ul.rotate li');
		
		if ( rotateArray.length == 1 ) return;
		
		var currentItem = 0;
		var prevItem;
		var nextItem;
		var rotateTimer = setTimeout( showNext, settings.rotateTimerInterval );
		var remainingMargin;
		
		$(rotateArray).each( function() { $(this).css('display', 'none')} );
		$(rotateArray[currentItem]).css('z-index', 2 ).css('display', 'block');
		
		function startTimer() {
			rotateTimer = setTimeout( showNext, settings.rotateTimerInterval );
			}
		
		function stopTimer() { 
			clearTimeout ( rotateTimer ); 
			}
		
		function showNext() {
			stopTimer();
			if ( currentItem < rotateArray.length-1 ) nextItem = currentItem + 1;
			else nextItem = 0;
			rotate();
			startTimer();
		}
		
		function showPrevious() {
			stopTimer();
			if ( currentItem != 0 ) nextItem = currentItem - 1; 
			else nextItem = rotateArray.length - 1;
			rotate();
			startTimer();
		}
		
		// ROTATE AND FADE THE BANNER
		function rotate() {
			$(rotateArray[nextItem]).css('z-index', 2 );
			$(rotateArray[currentItem]).css('z-index', 1);
			$(rotateArray[nextItem]).fadeIn(1000);
			$(rotateArray[currentItem]).fadeOut(1500);
			currentItem = nextItem;
		}
	
	  };
	})( jQuery );

});































