(function($) { 
	
	$.fn.wtdtoptips = function(options) {
		
		var opts = $.extend({}, $.fn.wtdtoptips.defaults, options);
		var bannerPos = 0;

		if(this.length){
			return this.each(function(){
				setTopTips($(this), opts);				  
			});
		}
		
		function setTopTips($obj, opts){
			$obj.find('ul.toptips-content li').hide();
			$obj.find('ul.toptips-content li:first-child').show();
			$obj.find('ul.toptips-nav li:first-child').addClass('active');
			$obj.css('height', 	'245px');
			
			var navWidth = 0;

			$obj.find('ul.toptips-nav li').each(function(index, element){
				navWidth += 46;
			});

			$obj.find('ul.toptips-nav').wrap('<div class=\'toptips-nav-wrapper\'>');
			$obj.find('ul.toptips-nav').css({
				'position':'relative',
				'left':'0px',
				'width': navWidth + 'px'
			}); 

			$obj.find('div.toptips-nav-wrapper').css({
				'width':'229px',
				'overflow':'hidden',
				'position':'relative',
				'margin':'0 0 0 28px'
			});
			$obj.append(
				'<div class=\'tip-carousel-prev\'><a href="#" title=\'Click here to see more tips\'>Click here to see more tips</a></div>' +
				'<div class=\'tip-carousel-next\'><a href="#" title=\'Click here to see more tips\'>Click here to see more tips</a></div>'
			);

			$obj.find('div.tip-carousel-prev a, div.tip-carousel-next a').bind('click', function(event) {
	
				calculateMovement($obj, opts, $(this));
				event.preventDefault();

			});

			$obj.find('ul.toptips-nav li a').mouseenter(function(){
				//
			});

			contentChange($obj, opts);
		}

		function contentChange($obj, opts){	
			$obj.find('ul.toptips-nav li a').bind('click', function(event){
				var index = $obj.find('ul.toptips-nav li').index($(this).parent());
				$obj.find('ul.toptips-nav li').removeClass('active');
				$(this).parent().addClass('active');
				$obj.find('ul.toptips-content li').hide();
				$obj.find('ul.toptips-content li').eq(index).show();
				event.preventDefault();
			});
		}

		function calculateMovement($obj, opts, evtButton){
				if(evtButton.parent().hasClass('tip-carousel-prev') == true){
					if($obj.find('ul.toptips-nav').css('left') == '0px' || $obj.find('ul.toptips-nav').is(':animated')){
						return false;
					}
					bannerPos = bannerPos + 46;
				}
				else{
					var reachedEnd = -($obj.find('ul.toptips-nav li').length * 46) + (5 * 46);
					if($obj.find('ul.toptips-nav').is(':animated')){
						return false;
					}
					else if ($obj.find('ul.toptips-nav').css('left') == reachedEnd +'px'){
						bannerPos = 0;
					}
					else{
						bannerPos = bannerPos - 46;
					}
				}

				moveCarousel($obj, opts);
		}

		function moveCarousel($obj, opts){
			if(opts.slide == true){
				$obj.find('ul.toptips-nav').stop().animate({
					left: bannerPos + 'px'
				}, opts.speed, opts.easing);
			}
			else{
				$obj.find('ul.toptips-nav').css('left', bannerPos + 'px');
			}
		}
	};
	
	// Form Defaults
	$.fn.wtdtoptips.defaults = {
		speed: 300,
		timer: 5000,
		easing: 'easeOutQuint',
		slide: true
	};
	
	
})(jQuery);
