// JavaScript Document

var DLN = DLN || {};

$(function() {
    // Initialize!
    DLN.Rotator.init();
});

DLN.Rotator = {

	init: function (){
		// check to see if the carousel container exists, then bind actions to it
		if ($('#js-rotator-container').length !==0){
			var self = this;
			var pause = false;
			self.createControls(); 								// CREATE THE CONTROLS BEFORE BINDING BEHAVIOR
			self.menuItems = $('.rotator-anchor');				// collection of DOM elements
			self.bind();
			this.rotate(); // Current code starts on last slide, so we want to force a rotation right away
		}
	},
	
    bind: function () {
		var self = this;
		setInterval(this.rotate, 5000);
		self.menuItems.mouseenter(function() {
			pause=true;											// stop the rotation
		}).mouseleave(function() {
			pause=false;										// continue the rotation
		}).click(function(){
			pause=true; 										// stop the rotation
			$(this).addClass('current-anchor'); 				// makes the current anchor active
			$(this).siblings().removeClass('current-anchor'); 	// strips the current-anchor class from siblings

            var topItem = $('#js-rotator li.current');  		// get the current active DIV
			topItem.removeClass('current').addClass('previous').animate({ "opacity": 0.0 }); // hide it

			var myTarget = '#js-rotator ' + $(this).find('a').attr('href');
			var myTargetSelector = $(myTarget);					// grab the target from the anchor and use it to select the div to bring to the front
			myTargetSelector.css({'opacity':0.0}).addClass('current').siblings().removeClass('current next previous'); // bring the target div to the front, strip any classes used in animation from the sibling divs
            myTargetSelector.animate({'opacity':1.0}, 1000, function() {});	// this animation must take place in less time then the rotation on the setInterval comand
			topItem.removeClass('previous');
			return false;
		});
	},
	
	createControls: function () { 								// this function creates an anchor link for each of the divs in the rotation
		var divCount = 0;
		var self = this;
		$('#js-rotator-controls').html('');
		$('#js-rotator li').each(function (){
			divCount++;
			$(this).attr('id', 'div'+divCount); //dynamically generate the ID
			$(this).css({ opacity: 0 }); 				
			$('#js-rotator-controls').append('<li class="rotator-anchor"><a href="#div'+divCount+'">'+divCount+'</a></li>');
		});
		$('#js-rotator-controls').find('li:first').addClass('current-anchor');
	},
	
	rotate: function () {										// this function controls the automatic rotation

	    var self = this;
	    if (this.pause) { 										// if the rotator is paused do nothing
        	return false;
        } else {
        	var topItem = $('#js-rotator li.current');  		// get the current active DIV
	    	var currentAnchor = $('#js-rotator-controls li.current-anchor'); // get the current active anchor
        	var nextItem = topItem.next('li'); 
	    	var nextAnchor = currentAnchor.next('li');			//we traverse the li anchors in reverse order due to the float right on the elements
        	if (nextItem.length == 0) { 						// if there is no nest list item...
        	    nextItem = $('#js-rotator li:first'); 			// set nest list item to the first item in the UL
	    		nextAnchor =  $('#js-rotator-controls li:first'); // due to the float in the CSS, we grab the last LI
	    	}
	    	currentAnchor.removeClass('current-anchor');
	    	nextAnchor.addClass('current-anchor');
	    	topItem.removeClass('current').addClass('previous').animate({ "opacity": 0.0 });
        	nextItem.css({ "opacity": 0.0 }).addClass('current').animate({ "opacity": 1.0 }, 1000, // this animation must take place in less time then the rotation on the setInterval comand
            function() {
                topItem.removeClass('previous');
            });
    	}
    },
    
    setPause: function(bool){
    	pause = bool;
    }
        
};

