;(function (jQuery) {
    
jQuery.fn.legrandRotator = function (interval, duration, ease) {
	// default paramiters
    interval = interval || 5000;
	duration = duration || 2000;
	ease = ease || {easing:'swing'};
	
    return this.each(function () {
		// setup
		var $rotator = jQuery(this),
			items = [], // uninitialised
			running = true,
			timer = null;
		
		//Bind Stop and Start functions for the auto rotator
		$rotator.bind('stop', function () {
			running = false;
			//clearTimeout(timer);
		}).bind('start', function () {
			running = true;
			//timer = setTimeout(rotate, interval);
		});
		
		// Add button transparency layer
		//$rotator.find('.tabs li a.tab-button img').before('<span class="heading-bg"></span>');
		$rotator.find('.tabs li a.tab-button').prepend('<span class="heading-bg"></span>');
		
		// Set the overflow to hidden in main scroller
		// Add the scroll buttons
		$rotator.find('div.scroll-Container').css('overflow','hidden')
		.before('<img src="/images/left.png" border="0" class="scrollButtons left" /><img src="/images/right.png" border="0" class="scrollButtons right" />');
		// Hide the content title for javascript enabled browsers
		$rotator.find('div.panels div.panel h2 span').css({
			position : 'absolute',
			top : '-999px',
			left : '-999px'
		});
		// Hide all content below the main scroller until we need it
		$rotator.find('div.panels div.panel').css({
			opacity : 0.0
		});		
		// Set padding here to lower content below the main scroller (Using padding so content anchor keeps content at the top of the scroller.)
		$rotator.find('div.panels div.panel h2').css({
			paddingTop : '259px'
		});
		
  	var strLinkTextSelector = ".linkText";
  	var strLinkTextBgSelector = strLinkTextSelector + " .bg";
		var intTabAnimationDuration = 250;
		
		// Bind click and hover events to the scroll buttons as well as call to navigate function
		jQuery('img.scrollButtons').click(navigate).bind('mouseenter', function () {
			$rotator.trigger('stop');
			jQuery(this).animate({
				top : 2,
				opacity : 1
			}, intTabAnimationDuration);
		}).bind('mouseleave', function () {
			jQuery(this).animate({
				top : 0,
				opacity : .80
			}, intTabAnimationDuration);
		}).css({ // Set initial state
			top : 0,
			opacity : .80
		});
		// Bind the navigation hover and clicks to update the selected nav:
		jQuery('.tabs li').hover(function () {
			
			if (!jQuery(this).hasClass("selected")){
				jQuery(strLinkTextSelector, this).stop().animate({
				  marginLeft : 15
				}, intTabAnimationDuration);
				jQuery('span.heading-bg', this).stop().animate({
					opacity : .50
				}, intTabAnimationDuration);
			}
		  }, function () {
		  	if (!jQuery(this).hasClass("selected")){
				jQuery(strLinkTextSelector, this).stop().animate({
				  marginLeft : 10
				}, intTabAnimationDuration);
				jQuery('span.heading-bg', this).stop().animate({
					opacity : .25
				}, intTabAnimationDuration);
			}
		  }).click(function () {
				var intLinkBgBottom, intLinkTextBottom, intLinkTextOffset;
				intLinkBgBottom = 29;
				intLinkTextOffset = 4;
				//intLinkBgBottom = 0;
				intLinkTextBottom = intLinkBgBottom + intLinkTextOffset;
				
		    // Reset all button states
				jQuery(this).parents('ul:first').find('li').each(function () {
					jQuery('span.heading-bg', this).stop().animate({
						bottom : intLinkBgBottom,
						opacity : .25
					}, intTabAnimationDuration);
					
					/*jQuery('img', this).stop().animate({
						bottom : intLinkBgBottom
					}, intTabAnimationDuration);*/
					jQuery(strLinkTextSelector, this).stop().animate({
						bottom : intLinkTextBottom
					}, intTabAnimationDuration);
					jQuery(strLinkTextBgSelector, this).css("background-image", "url(/css/images/img_carrotDown.png)");
					
					jQuery(this).removeClass('selected');
				});
				
				// Show content
				jQuery( jQuery(this).find('a.tab-button').attr('href').slice( jQuery(this).find('a').attr('href').indexOf('#') ) ).stop().animate({
					opacity : 1.0
				}, 4000);
				// Highlight current selected navigation
			
				intLinkBgBottom = 0;
				//intLinkBgBottom = -29;
				intLinkTextBottom = intLinkBgBottom + intLinkTextOffset;
		  	jQuery('span.heading-bg', this).stop().animate({
			  	bottom : intLinkBgBottom,
					opacity : 1.0
				}, intTabAnimationDuration);
				
				jQuery(strLinkTextSelector, this).stop().animate({
					bottom : intLinkTextBottom
				}, intTabAnimationDuration);
				jQuery(strLinkTextBgSelector, this).css("background-image", "url(/css/images/img_carrotUp.png)");
				/*jQuery('img', this).stop().animate({
					bottom : 0
				}, intTabAnimationDuration);*/
					
				// set the current navigation selected state
			  	jQuery(this).addClass('selected');
				// Scroll to selected content
				$rotator.find('div.panels').scrollTo( jQuery(this).find('a').attr('href').slice(jQuery(this).find('a').attr('href').indexOf('#')));
		  });
		// Check the url for a hash and set the inital on state
		if (window.location.hash) {
		  var hash = window.location.hash.substr(1),
		  id = $rotator.find('.tabs a.tab-button[href$="' + hash + '"]').attr('id');
		  if (id == 'tab1' || id == 'tab2' || id == 'tab3' || id == 'tab4') {
				$rotator.find('div.scroll-Container').stop().scrollTo('#tab1', duration, ease);
		  }else{
				$rotator.find('div.scroll-Container').stop().scrollTo('#tab5', duration, ease);
		  }
		  $rotator.find('.tabs a.tab-button[href$="' + hash + '"]').parent('li').click();
		} else {
		  $rotator.find('div.scroll-Container').stop().scrollTo('#tab1', duration, ease);
		  $rotator.find('.tabs li:first').click();
		}
		// called when scroll buttons are clicked
		function navigate(){
			// Check the current selected item to decide where to scroll to
			switch (jQuery('li.selected a').attr('id')) {
				case 'tab1':
				case 'tab2':
				case 'tab3':
				case 'tab4':
					$rotator.find('div.scroll-Container').stop().scrollTo('#tab5', duration, ease);
					$rotator.find('.tabs a.tab-button[id$="tab5"]').parent('li').click();
				break;
				default:
					$rotator.find('div.scroll-Container').stop().scrollTo('#tab1', duration, ease);
					$rotator.find('.tabs li:first').click();
				break;
			}
		}
		// Auto rotator function
		function rotate(change) {
			// check if this is the first run and if the rotator is on or off
			if (change != 'first' && running == true) {
				//check to see if this is not the last element in the list of lis
				if(jQuery('ul.tabs li.selected').next('li').get(0)) {
					//if next item is 5 scroll it into view
					if(jQuery('ul.tabs li.selected').next('li').find('a.tab-button').attr('id') == 'tab5') {
						jQuery('div.scroll-Container').stop().scrollTo('#tab5', duration, ease);
					}
					jQuery('ul.tabs li.selected').next('li').click();
				}else{
					jQuery('div.scroll-Container').stop().scrollTo('#tab1', duration, ease);
					jQuery('.tabs li:first').click();
				}
			}
			// trigger the effect again in (interval) seconds
			//clearTimeout(timer);
			timer = setTimeout(rotate, interval);
		}
		// inital call to the auto rotator (passing in 'first' to set the initial time before the rotation starts)
        rotate('first');
    });
};
    
})(jQuery);
