﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
/// <reference path="jquery-cookie.js" />

;(function($) {
	$.fn.styleSheetSwitcher = function(options) {
		/// <summary>
		///		Add stylesheet switcher.
		/// </summary>
		///	<param name="options" type="Object" optional="true">
		///		Extra options that can override the defaultItem settings (optional).
		///		 1. defaultItem;
		///		 2. classInclude;
		///		 3. classExclude;
		///		 4. classCurrent;
		///		 5. changeText;
		///		 6. disableCookie;
		///		 7. onSwitch;
		///	</param>
		/// <field name="defaultItem" type="String" Type="Number">
		///     The begin stylesheet. Value must match title or ordered zero-based number.
		/// </field>
		/// <field name="classInclude" type="String">
		///     Only include stylesheets that contains this class.
		/// </field>
		/// <field name="classExclude" type="String">
		///     Exclude stylesheets that contains this class.
		/// </field>
		/// <field name="classCurrent" type="String">
		///     Class to indicate current selected stylesheet.
		/// </field>
		/// <field name="changeText" type="String">
		///     Title of the links in menu.
		/// </field>
		/// <field name="disableCookie" type="Boolean">
		///     Don't want to use cookies for saving state.
		/// </field>
		/// <field name="onSwitch" type="Function" variable="title">
		///     Function executed when switched from stylesheet.
		/// </field>
		///	<returns type="jQuery" />
		/// <todo>
		///		* multiple stylesheets support
		///		* url support
		/// </todo>

		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.disableCookie && !!$.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.changeText).replace(/{name}/, title) })
							.text(title).addClass(current ? opties.classCurrent : '').click(function() {
								$(this).fadeTo('fast', 0.25, function() {
									$('a', ul).each(function() {
										$(this).removeClass(opties.classCurrent);
									});
									opties.defaultItem = $(this).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(this.title);
											if (!opties.disableCookie && !!$.cookie)
												$.cookie('ssswitcher', this.getAttribute('title'));
										}
									});
									return false;
								}).fadeTo('fast', 1);
							})
						)
					);
				});
				return ul;
			})()
		);
	};

	// public defaults;
	$.fn.styleSheetSwitcher.defaults = {
		defaultItem: 0,
		classInclude: false,  // e.g. 'ssswitcher'
		classExclude: false,  // e.g. 'NOssswitcher'
		classCurrent: 'current',
		changeText: "Change page style to {name}",
		disableCookie: false,
		onSwitch: function() { }
	};
})(jQuery);   // plugin code ends;