﻿/* All rights reserved by Damian Wielgosik - ferrante.pl */
(function() {
	var ferrante = {
		modules: {},
		addModule: function(name, module) {
			ferrante.modules[name] = module;
			(typeof module.init === "function") || (module.init = function() {});
		},
		run: function() {
			for (var i in ferrante.modules) {
				ferrante.modules[i].init();
			}
		}
	};
	
	(function() {
		var codeDict = {
			"0" : "czysty tekst",
			"1" : "lista"
		},
		slideSpeed = 500,
		snippetWidth = 500,
		snippetTabRatio = 25,
		snippetClassName = "code-snippet",
		codeSwitcherClassName = "code-switcher";

		this.init = function() {
			$('code').each(function(i, element) {
				var snippet = $('<figure class="'+snippetClassName+'"></figure>'),
					switcher = $('<a href="#plain-code" class="'+codeSwitcherClassName+'">'+ codeDict[0] + '</a>'),
					codeText = $(element).text().trim(),
					codeLines = codeText.split('\n'),
					codeFormats = {
						listing: $('<ol></ol>'),
						plain: $("<pre></pre>")
					};

				codeFormats.plain.append($("<code></code>").text(codeText));
				snippet.append(switcher);

				for (var i = 0, j = codeLines.length, li, line; i < j; i++) {
					li = $('<li></li>');
					
					line = $('<code></code>');
					line.text(codeLines[i]);
					li.append(line);

					line.css('padding-left', (codeLines[i].split('\t').length-1)*snippetTabRatio + 3);
					line.css('width', snippetWidth-(codeLines[i].split('\t').length-1)*snippetTabRatio);
					codeFormats.listing.append(li);
				}

				snippet.append(codeFormats.listing);

				if ($(element).parent().is('pre')) {
					$(element).parent().replaceWith(snippet);
				} else { 
					$(element).replaceWith(snippet);
				}
				
				(function(snippet, formats, toggle, animating) {
					switcher.click(function(e) {
						if (animating === true) {
							return;
						}
						
						e.preventDefault();

						if (++toggle > 1) {
							toggle = 0;
						}

						$(this).text(codeDict[toggle]);

						var visibleCode = $(snippet.children()[1]);
						animating = true;
						visibleCode.slideUp(slideSpeed, function() {
							if (toggle === 1) {
								formats.plain.hide();
								visibleCode.replaceWith(formats.plain);
								formats.plain.slideDown(slideSpeed,  function() {
									animating = false;
								});
							} else {
								formats.listing.hide();
								visibleCode.replaceWith(formats.listing);
								formats.listing.slideDown(slideSpeed, function() {
									animating = false;
								});
							}	
						});
					});
				})(snippet, codeFormats, 0, false);
			});
		}

		ferrante.addModule("codeSnippets", this);
	}).call(this);

	(function() {
		this.init = function() {
			$('article .hint').each(function(i, element) {
				var parent = $(element).parents('p').eq(0),
					id = $(element).attr('href');
					
				var note = $(id).html();
				var newNote = $('<figure class="i"><sup id="'+id.replace(/#/g, '')+'">'+(i+1)+'</sup>'+note+'</figure>');

				newNote.css('margin-top', (parseInt($(parent).css('margin-bottom'), 10)+($(parent).height()+$(parent).offset().top)-$(element).offset().top)*-1);
				newNote.insertAfter(parent);
			});

			$('#notes').prev('hr').remove();
			$('#notes').remove();
		}
		ferrante.addModule("articleNotes", this);
	}).call({});

	(function() {
		this.init = function() {
			$('#search').focus(function(e) {
				if ($(this).val() === "szukaj") {
					$(this).val("");
				}
			}).blur(function(e) {
				if ($(this).val() === "") {
					$(this).val("szukaj");
				}
			});

		};
		ferrante.addModule("search", this);
	}).call({});

	$().ready(function() {
		ferrante.run();
	});
})();
