/**
 * rolloverShareLinks
 *
 * Copyright (c) 2009 Studiocom, Inc. (http://studiocom.com)
 *
 * Built on top of the jQuery library
 *   http://jquery.com
 *
 */

(function($) {
	// =====================================================
	// = Plugin function. Initializes each matched element =
	// = @example $("#mycontent").rolloverShareLinks();    =
	// ===================================================== 
	$.fn.rolloverShareLinks = function(options) {		
		return this.each(function() {
			$this = $(this);
			
			$this.data('state','HIDDEN');
			
			//Bind indicator rollover event
			$this.children('.share_indicator').mouseenter(indicator_rollover);
		});
	};

	// =============================================================
	// = Plugin function. Assigns tooltips to each matched element =
	// = @example $("#mycontent").tooltipsShareLinks();            =
	// ============================================================= 
	$.fn.tooltipsShareLinks = function(options) {
		return this.each(function() {
			$this = $(this);
			
			$(this).find('.share_links a').tooltip({
				delay: 200, 
				fade:0,
				showURL: false, 
				top: -35,
				left: -36,
				track: true
			});
			$(this).parent('.share').data('hasTooltips',true);
		});
	};
	function linksOver() {
		$(this).siblings('.share_indicator').unbind('mouseleave',links_rollout);
		$(this).unbind('mouseenter',linksOver);
		if($(this).parent('.share').data('state') == 'CLOSE')
			$(this).parent('.share').data('state','ANIMATING');
	}
		
	$.fn.onShareRollover = function() {
		if($(this).parent('.share').data('state') != 'ANIMATING'){
			$(this).parent('.share').data('state','ANIMATING');

			//hide indicator
			$(this).mouseleave(links_rollout);
			$(this).fadeOut('fast',function(){$(this).unbind('mouseleave',links_rollout);});
		
			//show share_links element
			$(this).siblings('.share_links').css('display','block');
			var sl = $(this).siblings('.share_links');
			sl.mouseleave(links_rollout);
			sl.mouseenter(linksOver);
			
			var anim = {bottom:'2px'};
			$(this).siblings('.share_links').animate(anim,'fast','linear',function(){
				var bClose = $(this).parent('.share').data('state') == 'CLOSE';
				$(this).parent('.share').data('state','VISIBLE');
				$(this).unbind('mouseenter',linksOver);
				$(this).siblings('.share_indicator').unbind('mouseleave',links_rollout);
				//Bind rollout events for share_links
				
				//Tooltip for links
				if(typeof $(this).parent('.share').data('hasTooltips') == 'undefined'){
					$(this).parent('.share').find('.share_links a').tooltip({
						delay: 200, 
						fade:0,
						showURL: false, 
						top: -35,
						left: -36,
						track: true
					});
					$(this).parent('.share').data('hasTooltips',true);
				}
				if(bClose){ $(this).mouseleave(); }
			});
		}
		else
		{
			if($(this).parent('.share').data('state') == 'CLOSE')
				$(this).parent('.share').data('state','ANIMATING');
		}
	}
	
	// ======================================================
	// = Private. Fires when mouse rolls over the indicator =
	// ====================================================== 
	function indicator_rollover(e) {
		$(this).onShareRollover();
	}
	
	// ====================================================
	// = Private. Fires when mouse rolls out of the links =
	// ==================================================== 
	function links_rollout(e){
		if($(this).parent('.share').data('state') != 'ANIMATING'){		
			$(this).parent('.share').data('state','ANIMATING');
		
			//hide indicator
			var anim = {bottom:'-75px'};
			$(this).parent('.share').children('.share_links').animate(anim,'fast','linear');
		
			//show share_links element
			$(this).parent('.share').children('.share_indicator').fadeIn('fast',function(){
				$(this).parent('.share').data('state','HIDDEN');
				$(this).mouseleave(links_rollout);
				
				//Unbind rollout events for share_links
				$(this).siblings('.share_links').unbind('mouseleave',links_rollout);
				
				$(this).siblings('.share_links').css('display','none');
			});
		}
		else
		{
			$(this).parent('.share').data('state','CLOSE');
		}
	}
})(jQuery);
