/*
	Perfection or Vanity Scripting
	-------------------------------------
	Encoding: UTF-8
	Version: 1.0.1
*/

var Vanity = {

	_browser: {
		ie: /*@cc_on!@*/false
	},

	start: function() {
		this.ajaxifyEntries();
		this.insertMiniblog();
		this.makeCitations();
		this.branchCode();
		this.makeConfig();
		if (!$.browser.safari) { this.enhanceSearch(); }
		this.randomPosts();
		this.splitColumns();
		this.smallFixes();
		if (document.body.id) {
			this.recentPosts();
			this.sharePost();
			this.moderateComms();
			this.textileTags();
			this.previewComm();
		}
		if (this._browser.ie) {
			this.noExplorer();
		}
		this.addGoogleStats();
	},
	
	ajaxifyEntries: function() {
		$('#content span.extended a').bind('click', this, function(e) {

			var that = e.data;
			var href = this.href.split('#')[0], hash = this.href.split('#')[1];
			that._ajaxBinding = $(this);

			$(this).removeAttr('title')
				.parent().addClass('loading').end()
				.unbind('click').bind('click', function() { return false; });

			$.ajax({
			  type: "GET", 
				url: href, 
				dataType: "text/plain",
				success: function(response) {
					// yep, lame. unfortunately Jogger doesn't have API/XML for entry
					var start = response.indexOf('<div id="more" class="more">') + 28;
					var end = response.indexOf('</div><!-- more -->');
					if (start && end) {
				    var more = response.substring(start, end)
							.replace(/<h2>/g, '<h4>').replace(/<\/h2>/g, '</h4>');
						var vessel = document.createElement('div');
						vessel.innerHTML = more;
						vessel.style.display = 'none';
						vessel.className = 'more';
						var entry = Vanity._ajaxBinding.parent().parent().prev().get(0);
						entry.appendChild(vessel);
						Vanity.branchCode();
						var speed = 1500;
						if (more.length > 3000) { speed = more.length / 2; }
						$(vessel).slideDown(speed);
					} else {
						document.location = this.href;
					}
			  },
				complete: function() {
					Vanity._ajaxBinding.parent().css('visibility', 'hidden');
				},
				error: function() {
					document.location = this.href;
				}
			});
			
			e.preventDefault();

		})
	},
	
	_ajaxBinding: {},
	
	insertMiniblog: function() {
		if (typeof Miniblog != 'undefined') {

			var cookie = $.cookie('miniblog');
			var mBlog = $(document.createElement('div'));
			var mWrap = $(document.createElement('div'));
			var mInner = $(document.createElement('div'));
			var mMainTitle = $(document.createElement('h3'));
			
			mBlog.attr('id', 'miniblog');
			mWrap.attr('id', 'miniblog-wrap').append(mInner).appendTo(mBlog);
			mInner.attr('id', 'miniblog-inner');
			mMainTitle.text('Miniblog').appendTo(mInner).bind('click', function() {
				var p = $(this.parentNode.parentNode);
				var self = this;
				if (this.className == 'disabled') {
					mBlog.removeClass('disabled');
					if (cookie) { h = tHeight; }
					p.animate({ height: h }, function() {
						self.className = 'enabled';
						mWrap.removeAttr('style');
						$.cookie('miniblog', '');
					});
				} else {
					h = p.get(0).offsetHeight;
					p.animate({ height: 33 }, function() {
						self.className = 'disabled';
						mBlog.addClass('disabled');
						$.cookie('miniblog', 'off');
					})
				}
			});
			
			var miniblogJSON = Miniblog.posts;
			delete Miniblog.posts;
			
			for (var i = 0; i < miniblogJSON.length; i++) {
				var mPost = $(document.createElement('div'));
				var mHeader = $(document.createElement('div'));
				var mTitle = $(document.createElement('h4'));
				var mTitleLink = $(document.createElement('a'));
				var mDate = $(document.createElement('span'));
				var mDateDay = $(document.createElement('span'));
				var mDateMonth = $(document.createElement('span'));
				var mContent = $(document.createElement('div'));
				
				var day = miniblogJSON[i].date.split('.')[0];
				var month = miniblogJSON[i].date.split('.')[1];
				var body = miniblogJSON[i].body;
				var title = miniblogJSON[i].title;
				var link = miniblogJSON[i].link;

				mTitleLink.text(title).attr('href', link).appendTo(mTitle);
				mDateDay.text(day).addClass('d' + day).appendTo(mDate);
				mDateMonth.text('.' + month).addClass('m' + month).appendTo(mDate);
				
				mDate.addClass('date');
				mHeader.addClass('header').append(mTitle, mDate);
				mContent.addClass('content').append(body);
				
				mPost.addClass('post').append(mHeader, mContent).appendTo(mInner);
			}
			
			$('#content-2nd ul.entries').after(mBlog);
			
			if (cookie) {
				var tHeight = mBlog.get(0).offsetHeight;
				mBlog.addClass('disabled');
				mWrap.css('height', '33px');
				mBlog.find('h3').addClass('disabled');
			}			
			
		}
	},
	
	makeCitations: function() {
		$('#content .body blockquote[@cite]').each(function() {
			var cite = this.cite;
			if (/^https?:\/\//.test(cite)) {
				var citation = document.createElement('p');
				$(citation).addClass('cite').text('Źródło: ');
				var link = document.createElement('a');
				var desc = (/^https?:\/\/[\w-\.]+\//).exec(cite)[0]; //todo: other chars?
				$(link).text(desc).attr('href', cite).appendTo(citation);
				$(this).append(citation)
			}
		})
	},

	branchCode: function() {
		$('ol.code').not('.parsed').each(function(i) {
			// wrap listing in extra div
			var wrapper = document.createElement('div');
			$(wrapper).addClass('code-wrapper');
			$(this).addClass('parsed').wrap(wrapper);

			var lines = $(this).children('li');

			var switchCont = $(document.createElement('div'));
			var switchButton = $(document.createElement('strong'));
			var switchButtonLine = $(document.createElement('span'));
			var switchBlock = $(document.createElement('span'));
			var switchList = $(document.createElement('span'));

			switchList.addClass('active');
			switchButton.append(switchButtonLine);
			switchCont.addClass('code-switcher').append(switchButton, switchBlock, switchList);

			//hide listings with more than 3 lines
			if (lines.length > 3) {
				$(this).parent().hide();
				$(switchBlock).add(switchList).hide();
				switchButtonLine.text('Pokaż kod').toggle(showCode, hideCode);
			} else {
				switchButtonLine.text('Schowaj kod').toggle(hideCode, showCode);
			}

			$(this).parent().before(switchCont);

			//generate block element without formatting for easier access
			var pre = document.createElement('pre');
			var code = document.createElement('code');

			lines.each(function() {
				var parsed = '';
				var space = /\bspace\b/;
				var indent = /\b(i)(\d+)\b/;
				if (space.test(this.className)) { parsed += '\n'; }
				if (indent.test(this.className)) {
					var times = RegExp.$2;
					var j = 0;
					while (j < times) { parsed += '  '; j++; }
				}

				parsed += $(this).text();
				parsed += '\n';

				var span = document.createElement('span');
				$(span).text(parsed).appendTo(code);
			})

			$(pre).addClass('code').append(code);
			$(this).parent().append(pre);

			//toggle list or block (pre) view
			function toggleClass() { $(switchBlock).add(switchList).toggleClass('active'); }
			var pass = { ol: $(this), pre: $(pre) }

			switchBlock.addClass('pre').text('blok').bind('click', pass, function(e) {
				toggleClass();
				e.data.ol.hide();
				e.data.pre.show();
			});

			switchList.addClass('list').text('lista').bind('click', pass, function(e) {
				toggleClass();
				e.data.pre.hide();
				e.data.ol.show();
			});
			
			// toggle listings
			function showCode() {
				var self = $(this);
				self.parent().parent().next().slideDown('normal', function() {
					self.text('Schowaj kod').parent().siblings().show();
				});
			}
			function hideCode() {
				var self = $(this);
				self.parent().parent().next().slideUp('normal', function() {
					self.text('Pokaż kod').parent().siblings().hide()
				});
			}

			$(pre).css('display', 'none'); //hide() doesn't work in Safari
		});
	},

	makeConfig: function() {
		var switcher = document.createElement('div');
		var inside = document.createElement('span');
			$(inside).text('Pokaż panel konfiguracyjny').appendTo(switcher)
				.bind('click', function() {
					var t = $(this);
					if (t.is('.active')) {
						t.removeAttr('class');
						$('#config').removeAttr('class');
					} else {
						t.addClass('active');
						$('#config').addClass('visible');
					}
				});
			$(switcher).attr('id', 'config-switcher');

		var vessel = document.createElement('div');
		var colText = document.createElement('div');
		var listText = document.createElement('ul');
		var descText = document.createElement('p');
			$(colText).addClass('text').append(listText).appendTo(vessel);
			$(descText).text('Rozmiar tekstu').appendTo(colText);
			$(vessel).attr('id', 'config');

		function makeButton(clName, text, callback) {
			var button = document.createElement('li');
			var inner = document.createElement('span');
			$(inner).addClass(clName).attr('title', text).text(text).bind('click', function(e) {
				var self = $(e.target);
				var ul = self.parent().parent();
				ul.find('li').not(self).removeClass('active');
				self.parent().addClass('active');
				if (/small|normal|large/.test(clName)) {
					$.cookie('configText', clName, {expires: 365})
				} else {
					$.cookie('configLayout', clName, {expires: 365})
				}
				callback() 
			}).appendTo(button);
			return button
		}

		function changeClass(id, clName) {
			var container = $(document.body);
			container.get(0).className = clName;
		}

		var self = this;
		var c = 'content';
		var m = 'main';

		var sizeOne = makeButton('small', 'Mały', function() { changeClass(c, 'small') });
		var sizeTwo = makeButton('normal', 'Normalny', function() { changeClass(c, 'normal') });
		var sizeTri = makeButton('large', 'Duży', function() { changeClass(c, 'large') });

		var layRight = makeButton('right', 'Po prawej stronie', function() { changeClass(m, 'right') });
		var layBottom = makeButton('bottom', 'Na dole', function() { changeClass(m, 'bottom') });

		$(listText).append(sizeOne).append(sizeTwo).append(sizeTri);

		var settingText = $.cookie('configText');
		var settingLayout = $.cookie('configLayout');

		if (settingText) { 
			$(vessel).find('.' + settingText).parent().addClass('active'); 
			changeClass('content', settingText);
		} else { 
			$(sizeTwo).addClass('active'); 
		}

		if (settingLayout) { 
			$(vessel).find('.' + settingLayout).parent().addClass('active'); 
			changeClass('main', settingText);
		}	else { 
			$(layRight).addClass('active');
		}

		$('#about').after(vessel).after(switcher);

	},

	enhanceSearch: function() {

		var liveForm = $('#search');
		var liveQuery = liveForm.find('input.edit');
		var liveLegend = liveForm.find('legend');
		
		liveLegend.text('Wpisz co chcesz wyszukać i poczekaj…');
		liveQuery.attr({ autocomplete: 'off' });
		
		var title = liveLegend.text();		
		$.each(liveQuery, function() {
			var inp = $(this);
			inp.val(title);
			inp.bind('focus', function() {
				if (inp.val() == title) inp.val('');
			});
			inp.bind('blur', function() {
				if (inp.val() == '') inp.val(title);
			});
		});
		
		var liveContainer = $(document.createElement('div'));
		var liveOuter = $(document.createElement('div'));
		var liveInner = $(document.createElement('div'));
		var liveShadow = $(document.createElement('div'));
		var liveList = $(document.createElement('ul'));
		var liveBrand = $(document.createElement('p'));
		var liveBrandImg = $(document.createElement('img'));
		
		// search.getBranding() doesn't work :(
		liveBrandImg.attr('src', 'http://www.google.com/uds/css/small-logo.png');
		liveBrand.text('Powered by ').append(liveBrandImg);

		liveContainer.attr('id', 'livesearch').append(liveOuter, liveShadow).hide();
		liveList.attr('id', 'results').appendTo(liveInner);
		liveOuter.addClass('outer').append(liveInner);
		liveInner.addClass('inner').append(liveBrand);
		liveShadow.addClass('shadow');
		
		var search = new GwebSearch();
		var domain = 'perfectionorvanity.com';
		var name = / \u2013 Perfection or Vanity$/;
		var ellipsis = /<b>\.{3}<\/b>$/;
		var article = /\/\d{4}\/\d{2}\/\d{2}\/[\w-]+\/$/;

		search.setResultSetSize(GSearch.LARGE_RESULTSET);
		search.setSiteRestriction(domain);
		search.setNoHtmlGeneration();

		search.setSearchCompleteCallback(this, function() {
		
			function makeResult(title, url) {
				var liveResult = $(document.createElement('li'));
				var liveResultLink = $(document.createElement('a'));
				var liveResultDesc = $(document.createElement('p'));
				liveResultLink.text(title).attr('href', url).appendTo(liveResult);
				liveResult.appendTo(liveList);
			}
		
			var results = search.results;
			if (results.length) {
				for (var i = 0; i < results.length; i++) {
					var r = results[i];
					if (r.unescapedUrl.match(article)) {
						var title = r.titleNoFormatting.replace(name, '');
						makeResult(title, r.unescapedUrl);
					}
				}
				showList();
			} else {
				liveQuery.removeClass('loading');
				hideList();
			}
			liveContainer.show();
			liveQuery.removeClass('loading');

		});
		
		var lastSearch, wasSearch, keyPressed, itemIndex = 0;
		
		function makeSearch() {
			var query = liveQuery.val();
			if (query != lastSearch) {
				liveList.empty();
				search.clearResults();
				search.execute(query);
				lastSearch = query;
			} else {
				liveQuery.removeClass('loading');
			}
		}
		
		function showList() {
			liveForm.addClass('live').next().show();
			wasSearch = true;
		}
		
		function hideList() {
			liveForm.removeClass('live').next().hide();
			wasSearch = false;
		}
		
		function resetSearch() {
			lastSearch = '';
			itemIndex = 0;
			liveQuery.removeClass('loading').val('');
			hideList();
		}
		
		function setCursor(index) {
			index = index - 1;
			liveList.find('li').removeClass('selected').eq(index).addClass('selected');
		}
		
		liveForm.after(liveContainer).bind('submit', function(e) {
			e.preventDefault();
		});
		
		liveForm.find('input.submit').bind('click', function(e) {
			liveForm.submit();
		})
		
		liveQuery.bind('keyup', function(e) {
			var key = e.charCode || e.keyCode || 0;
			if (this.value == '') {
				resetSearch();
			}
			if (/27/.test(key)) { // esc
				if (wasSearch && itemIndex) {
					itemIndex = 0;
					hideList();
				} else {
					resetSearch();
				}
			} else if (/38/.test(key)) { // up
				if (wasSearch) {
					var next = itemIndex - 1;
					if (next < 1) {
						next = liveList.find('li').length;
					}
					itemIndex = next;
					setCursor(next);
					e.preventDefault(e);
				}
			} else if (/40/.test(key)) { // down
				if (wasSearch) {
					var next = itemIndex + 1;
					if (next > liveList.find('li').length) {
						next = 1;
					}
					itemIndex = next;
					setCursor(next);
					e.preventDefault(e);
				}
			} else if (/13/.test(key)) {
				if (wasSearch && itemIndex) {
					var url = liveList.find('li').eq(itemIndex - 1).find('a').attr('href');
					hideList();
					document.location = url;
				} else {
					liveForm.submit();
				}
			} else {
				if (keyPressed) { clearTimeout(keyPressed); }
				keyPressed = setTimeout(function() {
					liveQuery.addClass('loading');
					makeSearch();
				}, 400);
			}
		});
	},

	randomPosts: function() {
		function inArray(arr, elem) {
			var i;
			for (i=0; i < arr.length; i++) { if (arr[i] == elem) { return true; }}
			return false;
		}

		var ul = $('#content-2nd ul.delimited'), li = ul.find('li');

		if (li.length > 5) {
			var rd, k = 0, arr = [];
			while (k < 5) {
				rd = Math.floor(Math.random() * li.length);
				if (!(inArray(arr, rd))) {
					arr[k] = rd;
					k++;
				}
			}
			li.each(function(i) {
				if (!(inArray(arr, i))) { $(this).remove(); }
			});
		}

		ul.removeClass('delimited');
	},

	recentPosts: function() {
		var href = $('#top a:first').get(0).href + 'rss/5';
		$.ajax({
			method: 'GET',
			url: href,
			dataType: 'xml',
			success: function(response){				
			
				var items = response.getElementsByTagName('item');
				var ul = document.createElement('ul');				
				ul.className = 'entries higher';
				
				for (var i = 0; i < items.length; i++) {
					var strong = document.createElement('strong');
					var a = document.createElement('a');
					var li = document.createElement('li');
					var title = items[i].getElementsByTagName('title')[0].firstChild.nodeValue;
					var link = items[i].getElementsByTagName('link')[0].firstChild.nodeValue;
					strong.appendChild(document.createTextNode(title));
					a.appendChild(strong);
					a.href = link;
					li.appendChild(a);
					ul.appendChild(li);
				}
				
				var h3 = document.createElement('h3');
				var span = document.createElement('span');
				
				span.appendChild(document.createTextNode('Najnowsze na Perfection or Vanity'));
				h3.appendChild(span);
				
				$('#livesearch').after(ul).after(h3);
				
		  },
			error: function() {

			}
		});
	},
	
	previewComm: function() {
		var preview = $('#preview-comment');
		if (preview) {
			preview.ScrollTo(800)
		}
	},
	
	sharePost: function() {
		$('#meta dl').addClass('dropdown')
			.find('dd').hide().end()
			.find('dt').bind('click', function() {
				var self = $(this);
				self.addClass('clicked').next().show();
				$(document).bind('click', function(e) {
					if (e.target !== self.get(0)) {
						self.removeClass('clicked').next().hide();
						$(this).unbind('click');
					}
				});
			})
	},

	splitColumns: function() {
		$('#content-2nd ul.columns').each(function() {
			var vessel = document.createElement('div');
			var colSt = document.createElement('ul');
			var colNd = document.createElement('ul');
				colSt.className = 'first';
				colNd.className = 'second';

			$(vessel).addClass('columns').append(colSt).append(colNd);

			$(this).find('li')
				.not(':odd').appendTo(colSt).end()
				.not(':even').appendTo(colNd).end().end()
			.before(vessel).remove();
		});
	},
	
	textileTags: function() {
		var textileCont = $(document.createElement('div'));
		var textileLabel = $(document.createElement('p'));
		var textileList = $(document.createElement('ul'));
		
		function makeButton(className, desc) {
			var li = $(document.createElement('li'));
			var a = $(document.createElement('a'));
			a.text(desc).attr('href', '').appendTo(li);
			li.addClass('textile-' + className).appendTo(textileList);
		}
		
		makeButton('link', 'Link');
		makeButton('quote', 'Cytat');
		makeButton('code', 'Kod');
		makeButton('pre', 'Blok kodu');
		
		textileLabel.text('Textile:');
		textileCont.append(textileLabel, textileList).attr('id', 'textile');
		
		var self = this;
		$(textileCont).find('li').each(function() {
			var s = this.className.replace('textile-', '');
			var a = $(this).children('a').get(0);
			switch (s) {
				case 'link': {  a.isLink = true; break; }
				case 'quote': { a.tagStart = 'bq. '; break; }
				case 'code': {  a.tagStart = '@'; a.tagEnd = '@'; break; }
				case 'pre': {  a.tagStart = 'bc. '; break; }				
			}
			$(a).bind('click', function(e) {
				self._insertTag(this);
				e.preventDefault();
			})
		});
		
		$('#disclaimer').before(textileCont);
		
	},
	
	_insertTag: function(el) {
	
		var canvas = $('#post-comment textarea').get(0);
		var link = false;
		if (typeof el.isLink != 'undefined') { link = true; }

		var tagStart = '', tagEnd = '';
		if (link) {
			var url = prompt('Podaj link', 'http://');
			if (url) {
				var desc = prompt('Podaj opis', '');
				if (desc) {
					tagStart = '"' + desc + '":' + url;
				}
			}
		} else {
			if (typeof el.tagStart == 'undefined') { tagStart = ''; } else { tagStart = el.tagStart; }
			if (typeof el.tagEnd == 'undefined') { tagEnd = ''; } else { tagEnd = el.tagEnd; }
		}

		if (document.selection) {
			canvas.focus();
			sel = document.selection.createRange();
			if (sel.text.length > 0) { sel.text = tagStart + sel.text + tagEnd; }
			else {
				if (tagEnd == '') {
					sel.text = tagStart;
				} else {
					sel.text = tagEnd;
				}
			}
			canvas.focus();
		} else if (canvas.selectionStart || canvas.selectionStart == '0') {
			var startPos = canvas.selectionStart;
			var endPos = canvas.selectionEnd;
			var cursorPos = endPos;
			var scrollTop = canvas.scrollTop;
			if (startPos != endPos) {
				canvas.value = canvas.value.substring(0, startPos)
				+ tagStart + canvas.value.substring(startPos, endPos)
				+ tagEnd + canvas.value.substring(endPos, canvas.value.length);
				cursorPos += tagStart.length + tagEnd.length;
			} else {
				
				if (tagEnd == '') {
					canvas.value = canvas.value.substring(0, startPos) + tagStart
					+ canvas.value.substring(endPos, canvas.value.length);
					cursorPos = startPos + tagStart.length;
				} else {
					canvas.value = canvas.value.substring(0, startPos) + tagEnd
					+ canvas.value.substring(endPos, canvas.value.length);
					cursorPos = startPos + tagEnd.length;
				}
			}
			canvas.focus();
			canvas.selectionStart = cursorPos;
			canvas.selectionEnd = cursorPos;
			canvas.scrollTop = scrollTop;
		} else {
			if (tagEnd == '') {
				canvas.value += tagStart;
			} else {
				canvas.value += tagEnd;
			}
			canvas.focus();
		}
		
	},
	
	moderateComms: function() {
		var comments = $('#comments');
		if (comments) {
			comments.find('blockquote.body > del').each(function() {
				var self = $(this).parent();
				var modBlock = $(document.createElement('p'));
				var modLink = $(document.createElement('a'));
				modLink.attr('href', '').text('Zmoderowano').bind('click', self, function(e) {
					self.removeClass('moderated').addClass('moderated-visible');
					$(this).parent().remove();
					e.preventDefault();
				});
				modBlock.addClass('moderated-show').append(modLink);
				self.addClass('moderated');
				$(this).after(modBlock);
				
			})
		}
	},
	
	noExplorer: function() {
		var cancel = $.cookie('explorer-warning');
		if (!cancel) {
			var html = '<div class="top-shadow"></div><div class="outer"><div class="inner"><div class="ok"><a href="" title="Jak wolisz, ale jeśli się coś rozsypie, to nie patrz później na mnie. ;-)">Zamknij komunikat i nie pokazuj go ponownie</a></div><p>Hej, wykryłem że używasz Internet Explorera. Ta strona wygląda u Ciebie poprawnie, ponieważ <span title="trzy dni poprawek i haków w CSS i JS">specjalnie postarałem się</span>, aby tak było - Explorer ma bardzo słabe wsparcie dla CSS oraz innych standardowych technik projektowania. Jeśli jesteś tutaj aby się czegoś nauczyć, proszę - <a href="http://browsehappy.pl/" title="BrowseHappy.pl – Surfuj bez obaw">zmień przeglądarkę</a>. IE jest najgorszym wyborem dla kodera / projektanta, serio.</p></div></div><div class="bottom-shadow"></div>';
			var container = $(document.createElement('div'));
			container.attr('id', 'explorer').html(html).find('.ok a').bind('click', function(e) {
				$.cookie('explorer-warning', 'true', {expires: 365});
				container.remove();
				e.preventDefault();
			});
			$('body').prepend(container);
		}
	},
	
	smallFixes: function() {
		//$('#content').add('#content-2nd').hide().show();
		if (document.body.id && this._browser.ie) {
			$('#post-comment input[@type="submit"]')
				.eq(0).val(' Wyślij ').end()
				.eq(2).val(' Stop ')
		}
		if ($.browser.safari) {
			$('#livesearch').css('margin-top', '-30px')
		}
	},
	
	addGoogleStats: function() {
		var GAID = 'UA-325299-4';
		var head = document.getElementsByTagName('head')[0];
		var scr = document.createElement('script');
		scr.type = 'text/javascript';
		scr.src = (location.protocol == 'http:' ? 'http://www.' : 'https://ssl.') + 'google-analytics.com/'+'urchin.js';
		var once = 0;
			function runMe() { 
				if (scr.readyState == 'loading' || urchinTracker == undefined || once) { return; }
				clearInterval(timer);
				once++;
				_uacct = GAID;
				urchinTracker();
			}
		var timer = setInterval(runMe, 2000);
		scr['onload'] = runMe;
		scr['onreadystatechange'] = runMe;
		head.appendChild(scr); 
	}
}

$().ready(function () { Vanity.start() });