﻿/* INIT SHARE
----------------------------------------------------*/
(function ($)
{
	$.fn.InitShare = function ()
	{
		$('div.shareable').hide();
		$(this).find('strong').detach().appendTo($(this));

		var sharemorespan = $('#share span.sharemore:eq(0)'),
			container = $('#share');
		if (sharemorespan.length === 0) { sharemorespan = $('#sharemore'); }
		sharemorespan.hide();

		var sharemore = (sharemorespan) ? sharemorespan.html() : 'Fler';

		var visibleUl = $('<ul class="share-visibles"><li class="share-more"><a href="javascript:;">' + sharemore + '</a></li></ul>');
		var visibleLi = $(this).find('div.shareable li:lt(2)').clone();
		visibleLi.find('img').remove();
		visibleLi.eq(0).addClass('share1');
		visibleLi.eq(1).addClass('share2');
		if ($(this).parent().attr('id') == 'article')
		{
			visibleUl.prepend(visibleLi).prependTo(this);
		} else
		{
			visibleUl.prepend(visibleLi).appendTo(this);
		}
		visibleUl.find('li.share-more a').bind('click', function (e)
		{
			e.preventDefault();

			if (sharemorespan.hasClass('relativeposition'))
			{
				var offset = $(this).position();

				var elm = container.find('div.shareable');
				elm.css({
					'margin-top': offset.top,
					'margin-left': offset.left - 10
				});
				if (elm.is(':visible') === false)
				{
					container.data('orgHeight', container.css('height'));
					$('#share').css('height', (elm.height() + 40) + 'px');
				}
				else
				{
					container.css('height', container.data('orgHeight'));
					console.log(container.data('orgHeight'))
				}
				elm.toggle();


			}
			else
			{
				var offset = $(this).offset();
				var elm = $(this).parent().parent().parent().find('div.shareable');
				elm.css({
					'top': offset.top,
					'left': offset.left - 10
				});
				elm.toggle();
			}
			if (elm.is(':visible') === true)
			{
				container.addClass('open');
			}
			else
			{
				container.removeClass('open');
			}

		});
	};
})(jQuery);

$jq(document).ready(function () { $jq('div#share').InitShare() })
