/// <reference path="jquery-vsdoc.js" />
var litium = {};

litium.debug = function() {
	if (window.console && window.console.log) {
		console.log(arguments[0]);
	} else {
		alert(arguments[0]);
	}
};

litium.utilities = {
	// Makes clickable links out of items.
	createLink: function(options) {
		$(options.target).each(function() {
			var $this = $(this);
			var $link = $this.find(options.source);
			if ($link.length) {
				$this.addClass('link');
				$this.click(function() {
					document.location = $link.attr('href');
					return false;
				});
			}
		});
	}
};

litium.framework = (function() {
	var self = {};

	var initWebSiteSelector = function() {
		$('#header #websites ul').dropify();
	};

	var setFooterPosition = function() {
		// keep the footer in place at the bottom of the window.
		var CLASSNAME = 'b';
		var $footer = $('#footer');
		var h = $('#header').outerHeight(),
			p = $('#page').outerHeight(),
			f = $footer.outerHeight(),
			w = $(window).height();
		if ((h + p + f) < w) {
			if (!$footer.hasClass(CLASSNAME)) {
				$footer.addClass(CLASSNAME);
			}
		} else {
			if ($footer.hasClass(CLASSNAME)) {
				$footer.removeClass(CLASSNAME);
			}
		}
	};

	self.initComments = function(options) {
		var showCommentForm = function() {
			$('#comment').show();
		};

		// show comment form when the page is done loaded if the url contains the fragment identifier of the comment form.
		var tryShowCommentForm = function() {
			var endsWith = function(s, suffix) {
				return (s.indexOf(suffix, s.length - suffix.length) !== -1);
			}

			if (endsWith(document.location.href, '#comment')) {
				showCommentForm();
			}
		};

		// options.authorInputSelector: (string) selector for the input text element used by the user for writing his/her name.
		// options.showCommentForm: (boolean) whether to show the comment form on page load or not.
		$(document).ready(function() {
			// add fragment to comment pagination links.
			$('#comments .pagination a').attr('href', function() {
				return $(this).attr('href') + '#comments';
			});

			// show comment form when the 'add comment' link is clicked.
			$('#comments #addcomment').click(function() {
				showCommentForm();
				$(options.authorInputSelector).focus();
				return false;
			});

			// show comment form on page load?
			if (options.showCommentForm) {
				showCommentForm();
			} else {
				tryShowCommentForm();
			}

			(function initCommentPostedConfirmation() {
				var $confirmationElement = $('#confirmation');
				var backgroundColor = $confirmationElement.css('backgroundColor');

				$confirmationElement.delay(100)
					.animate({ 'backgroundColor': '#b3d7e3' }, 1000)
					.delay(3000).animate({ 'backgroundColor': backgroundColor }, 1000, function() {
						$(this).hide();
					});
			})();
		});
	};

	self.init = (function() {
		$(document).ready(function() {
			initWebSiteSelector();
			setFooterPosition();
		});
		$(window).resize(function() {
			setFooterPosition();
		});
	});

	return self;
})();

litium.templates = {
	calendar: (function() {
		var self = {};

		self.toggleEditor = function() {
			$('#calendareditor').toggleClass('active');
		};

		self.init = function() {
			$(document).ready(function() {
				$('#add').click(function() {
					if (document.location.search.indexOf('CalendarItemId') === -1) {
						litium.templates.calendar.toggleEditor();
						return false;
					}
				});
				$('#calendareditor input.cancel').click(function() {
					litium.templates.calendar.toggleEditor();
					return false;
				});
			});
		};

		return self;
	})(),

	collection: (function() {
		var self = {};

		self.init = function() {
			$(document).ready(function() {
				(function() {
					// function for making all boxes on each row the same height. depends on the equalizeCols jQuery plugin.
					var numberOfColumns = 2,
						min = 0,
						max = min + numberOfColumns,
						$boxes = $('#article ul.pages > li'),
						boxes = $boxes.length;

					while (max <= boxes) {
						$boxes.slice(min, max).children('.box').equalizeCols();
						min = max;
						max += numberOfColumns;
					}
				})();
			});
		};

		return self;
	})(),

	include: (function() {
		var self = {};
		var defaults = { iframeHeight: '600px' };

		self.resize = function(iframe, iframeHeight) {
			try {
				iframe.style.height = iframe.contentWindow.document.body.scrollHeight + 'px';
			} catch (e) {
				iframe.style.height = iframeHeight;
			}
		};

		self.init = function(options) {
			///<summary>
			/// Initialises the resizing of iframes in the document.
			/// Parameters: options: { iframeHeight: height declaration, e.g. '600px' }
			///</summary>
			var i;
			var iframe;
			var iframes = document.getElementsByTagName('iframe');
			var settings = jQuery.extend({}, defaults, options);
			var onloadValue = 'litium.templates.include.resize(this, "' + settings.iframeHeight + '");';

			for (i = 0; iframe = iframes[i]; i++) {
				iframe.setAttribute('onload', onloadValue);
			}
		};

		return self;
	})(),

	startpage: (function() {
		var self = {};

		var initComments = function() {
			// adds a class name to the list of comments if the list contains more than one item.
			$('#secondary ul.comments').each(function() {
				if ($(this).children().length > 1) {
					$(this).addClass('multiple');
				}
			});
		};

		var initSlideShow = function() {
			// initialises the image slide show using the jQuery cycle plugin.
			$('#slideshow').cycle({
				timeout: 5000,  // milliseconds between slide transitions (0 to disable auto advance)
				pause: 1     // true to enable "pause on hover"
			});
		};

		self.init = function() {
			$(document).ready(function() {
				initSlideShow();
				initComments();
			});
		};

		self.initFeedList = function(options) {
			///<summary>
			/// Called after successful fetching of external rss.
			/// Ensures that links in the feed list are opened in a new window.
			///</summary>
			var $feedList = $('#' + options.elementId);

			if ($feedList.length) {
				$feedList.find('a').click(function() {
					window.open($(this).attr('href'));
					return false;
				});
			}
		};

		return self;
	})()
};
