﻿$(document).ready(function () {
	shortenSiteMapLists();

	$('img.mouse-over').hover(function () {
		$(this).attr('src', $(this).attr('src').replace('_norm.', '_hover.'));
	}, function () {
		$(this).attr('src', $(this).attr('src').replace('_hover.', '_norm.'));
	});
});

function shortenSiteMapLists() {
	$("ul.siteMapList").each(function (index) {
		var defaultNumberOfVisibleLinks = 5;



		if ($("li", this).length > defaultNumberOfVisibleLinks + 1) { // +1 for the header that is shown as an li
			var i = 1;
			$("li", this).each(function (index) {
				if (i > defaultNumberOfVisibleLinks + 1) {
					$(this).toggle();
				}
				i++;
			});
			//add more link to bottom of list and attach click event
			$(document.createElement("li"))
				.text("More ...")
				.addClass("mor")
				.appendTo(this)
				.click(function () {
					var totalNumberofLinks = $("li", this.parentNode).length;
					var show = $("li", this.parentNode).last().text() == "More ...";

					i = 1;
					$("li", this.parentNode).each(function (index) {
						if (i > defaultNumberOfVisibleLinks + 1 && i < totalNumberofLinks) {
							if (show) {
								$(this).slideDown();
							} else {
								$(this).slideUp();
							}
						}
						i++;
					});
					if (show) {
						$("li", this.parentNode).last().text("Less ...");
					} else {
						$("li", this.parentNode).last().text("More ...");
					}
				});
		}
	});
}

function preload(arrayOfImages) {
	$(arrayOfImages).each(function () {
		$('<img/>')[0].src = this;
	});
}
