(function($) {

	$.fn.kiwi = function() {  

		//globals
		var _isHover = false;
		var _isClick = false;
		var _parentObj;
		
		//initial Load Ani
		
		$('.object').delay(350);
		$('#sketchPad').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
				$('#sketchPad').animate( { top:'+=40px'}, 180, 'easeOutBounce', function() {
					$('#shippingBox').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
						$('#shippingBox').animate( { top:'+=40px'}, 180, 'easeOutBounce', function() {
							$('#cube').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
								$('#cube').animate( { top:'+=40px'}, 180, 'easeOutBounce', function() {
									$('#magGlass').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
										$('#magGlass').animate( { top:'+=40px'}, 180, 'easeOutBounce', function() {
											$('#printMaterials').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
												$('#printMaterials').animate( { top:'+=40px'}, 180, 'easeOutBounce');
											});
										});
									});
								});
							});
						});
					});
				});
			});

		
		//begin event handlers for objects
		$('.objParent').hover(inHandle, outHandle);	
		$('.objParent .tooltip .closeTip').click(function() { _isClick = true; _isHover = false; outObject(_parentObj); });
		
		//set mouseenter function calls for objects
		function inHandle() {
			$(this).unbind('mouseenter'); //unbind mouseover envents to eliminate multiple mouseover triggers.
			$('.objParent').unbind('mouseenter');//Unbind mouseover envents on all objects to eliminate accidental subsequent mouseout triggers.
			$('.objParent').unbind('mouseleave');//Unbind mouseout envents to eliminate mouseout of non active object.
			$(this).bind('mouseleave', outHandle);//bind mouseout envents to current object.
			_isHover = true;
			overObject(this);
		};
		//set mouseleave function calls for objects
		function outHandle() {
			outObject(this);
			$('.objParent').bind('mouseenter', inHandle); //rebind mouseover envents to eliminate multiple mouseover triggers.
		};
		
		//mouseenter for objects
		function overObject(parentObj) {
			$(".object", parentObj).addClass('current').stop(true, true).animate( { top: '-=20px' }, 400, 'easeOutBack');
			$(parentObj).addClass("hover");
			$(this).unbind('mouseenter');
			$(parentObj).find(".tooltip").stop(true, true).delay(250).fadeIn(200);
			if ($(".object").not(".current").is(":animated")) {
			$(".object").not(".current").parent().stop(true, true).css('opacity', 0.5);
			} else {
			$(".object:not(.current)").parent().stop(true, true).animate( { opacity: .5 }, 200, 'linear' );
			}
			_parentObj = parentObj;
		};
		
		//mouseleave for objects
		function outObject(parentObj) {
			if (_isClick == true || _isHover == true) {
			$(".object", parentObj).removeClass('current').stop(true, true).animate( { top:'+=20px' }, 100, 'easeOutBounce' );
			$(parentObj).find(".tooltip").stop(true, true).fadeOut(100);
			$(parentObj).removeClass("hover");
			if ($(".object").not(".current")) {
			$(".object:not(.current)").parent().stop(true, true).animate( { opacity: 1 }, 100, 'linear' );
			}
			_isClick = false;
			_isHover = false;
			}
		};
		
		//begin event handlers for headline
		$('#shelfHeadline').hover(headLineIn, headLineOut);
		$('#shelfHeadline .tooltip .closeTip').click(headLineOut);
		
		//set mouseenter function calls for headline
		function headLineIn() {
			headlineTip();
			bounceObjects();
		};
		//set mouseleave function calls for headline
		function headLineOut() {
			headlineClose();
			allFadeIn();
		};
		
		//open headline tooltip
		function headlineTip() {
			$('#shelfHeadline .tooltip').stop(true, true).fadeIn(200);
		};
		
		//bounce ani
		function bounceObjects() {
			$('#shelfHeadline').unbind('mouseenter');
			$('#shelfHeadline').bind('mouseenter', headlineTip);
			$('#sketchPad').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
				$('#sketchPad').animate( { top:'+=40px'}, 180, 'easeOutBounce', function() {
					$('#shippingBox').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
						$('#shippingBox').animate( { top:'+=40px'}, 180, 'easeOutBounce', function() {
							$('#cube').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
								$('#cube').animate( { top:'+=40px'}, 180, 'easeOutBounce', function() {
									$('#magGlass').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
										$('#magGlass').animate( { top:'+=40px'}, 180, 'easeOutBounce', function() {
											$('#printMaterials').stop(true,true).animate({top:'-=40px', opacity: 1},150, 'easeOutBack', function(){
												$('#printMaterials').animate( { top:'+=40px'}, 180, 'easeOutBounce', function(){
													$('#shelfHeadline').bind('mouseenter', bounceObjects);
												});
											});
										});
									});
								});
							});
						});
					});
				});
			});
		};
		
		//close headline tooltip
		function headlineClose() {
			$('#shelfHeadline .tooltip').stop(true, true).fadeOut(100);
		};
		
		//fade objects in
		function allFadeIn() {
			$('.object').stop(true,true).animate( { opacity: 1}, 100);
		};
		
	};
	
})(jQuery);
