﻿;(function($) {
	$.styleSheetSwitcher = $.fn.styleSheetSwitcher = function(options) {
		var opties = $.extend($.fn.styleSheetSwitcher.defaults, options),
			opties = $.metadata ? $.extend({}, opties, this.metadata()) : opties;

		return this.append(
			(function() {
				var ul = $("<ul/>"),
					index = 0, current = false, title = "",
					classIn = (opties.classInclude ? "." + opties.classInclude : ""),
					classEx = (opties.classExclude ? ":not(." + opties.classExclude + ")" : "");
				if (opties.cookie && !!$.cookie) {
					if ($.cookie("ssswitcher") != null) {
						opties.defaultItem = $.cookie("ssswitcher");
					} else {
						$.cookie("ssswitcher", opties.defaultItem);
					}
				}
				$("link[type=text/css][title]" + classIn + classEx).each(function() {
					title = $.trim(this.title);
					current = !!(index.toString() == opties.defaultItem.toString() || title.toLowerCase() == opties.defaultItem.toString().toLowerCase());
					this.disabled = true;  // needed for IE;
					this.disabled = !current;
					index++;
					ul.append(
						$("<li/>").append(
							$("<a/>").attr({ "href": "#", "title": $.trim(opties.title).replace(/{name}/, title) })
							.text(title).addClass(current ? opties.classCurrent : "").click(function() {
								var item = $(this);
								$("a", ul).each(function() {
									$(this).removeClass(opties.classCurrent);
								});
								opties.defaultItem = item.addClass(opties.classCurrent).text();
								$("link[type=text/css][title]" + classIn + classEx).each(function() {
									this.disabled = true;
									if (this.title == opties.defaultItem) {
										this.disabled = false;
										opties.onSwitch.call(item, this.title);
										if (opties.cookie && !!$.cookie) {
											$.cookie("ssswitcher", this.getAttribute("title"));
										}
									}
								});
								return false;
							})
						)
					);
				});
				return ul;
			})()
		);
	};

	// public defaults;
	$.fn.styleSheetSwitcher.defaults = {
		defaultItem: 0,
		classInclude: false,  // e.g. "ssswitcher"
		classExclude: false,  // e.g. "NOssswitcher"
		classCurrent: "current",
		title: "Change page style to {name}",
		cookie: true,
		onSwitch: function() { }
	};
})(jQuery);  // plugin code ends;
