(function($) {
	$.fn.moreList = function(opts) {
		var o = {
			charsInFeed : 150,
			replaceMoreText : true,
			moreText : 'czytaj więcej...',
			backText : 'zwiń',
			moreTextSign : '[...]',
			separatedParts : true/*TODO is short and full part*/
		};
		if (opts) {
			$.extend(o, opts);
		}
		cms2.eventDispatcher.dispatchEvent({
			type : 'MORE_LIST_INIT',
			data : {}
		})
		return this.each(function(i) {
			$("#" + $(this).attr('id') + " > *").each(
					function(j) {
						if ($('.hideableContent', this).length == 1) {
							$('.hideableContent', this).html(
									$('.hideableContent', this).html().replace(
											/<\p>/g, "<\p> "));
							var t = $('.hideableContent', this).text();
							var hiddenableParent=$('.hideableContent',this)[0].parentNode;
							$(hiddenableParent).append(
									'<div class="tempContent" style="display:none">'
											+ $('.hideableContent', this)
													.html() + '</div>');
							if (t.length > o.charsInFeed) {
								var splited = t.split(' ');
								var idx = 0;
								t = '';
								while (t.length < o.charsInFeed) {
									t += splited[idx++] + ' ';
								}
								t += o.moreTextSign;
							}
							$('.hideableContent', this).html(t);
							$(hiddenableParent).append(
									'<div class="tempShortContent" style="display:none">'
											+ t + '</div>')
							if (o.replaceMoreText) {
								$('.moreControllHook', this).html(o.moreText)
							}
							$('.moreControllHook', this).click(function() {
								switchBlock(this.parentNode);return false;
							})
							$(hiddenableParent).attr('data-counter',j);
							$(hiddenableParent).attr('expanded', 'false')
							if (o.replaceMoreText) {
								$(hiddenableParent).attr('moreText', o.moreText)
								$(hiddenableParent).attr('backText', o.backText)
							}
							if (location.hash
									&& location.hash.substr(1) == ('ml' + j)) {
								switchBlock(hiddenableParent)
							}
						}
					})
		})
	};
	var lastBlockRef
	function switchBlock(blockRef) {
		if (lastBlockRef && $(lastBlockRef).attr('expanded') == 'true'
				&& lastBlockRef != blockRef) {// wyłacz rozwiniety
			switchExecute(lastBlockRef)
		}
		lastBlockRef = null
		lastBlockRef = blockRef
		switchExecute(blockRef);
		cms2.eventDispatcher.dispatchEvent({
			type : 'MORE_LIST_SWITCH',
			data : {}
		})
	}
	function switchExecute(blockRef) {
		var expanding;
		if ($(blockRef).attr('expanded') == 'false') {// show full
			expanding = true
			$('.hideableContent', blockRef).html(
					$('.tempContent', blockRef).html()).css('display', 'none')
					.addClass('fullText').fadeIn('slow')
		} else {// show small
			expanding = false
			$('.hideableContent', blockRef).removeClass('fullText').html(
					$('.tempShortContent', blockRef).text())
		}
		$(blockRef).attr('expanded', expanding ? 'true' : 'false')
		if ($(blockRef).attr('moreText')) {
			$('.moreControllHook', blockRef).html(
					$(blockRef).attr('expanded') == 'true' ? $(blockRef).attr(
							'backText') : $(blockRef).attr('moreText'))
		}
		if (expanding) {
			var lo='ml'+$( blockRef).attr('data-counter');
			location.hash = lo;
		}
	}
})(jQuery);
