var godkin = {

	config : {
	//	cookieCalendarFilterName : "nation-calendar-filter",
		cookieTabHome : "nation-home-tabs",
		cookieTextSize : "nation-text-size",
		imgs : "/images/",
		swfs : "/swfs/",
		sHTMLtag : "can-has-js",
		oAltStyles : null,
		oTabsHome : null
	},

	/*
		Onload functionality
	*/
	init : function() {
		var c = godkin;

/*		c.loadSifr(); */

		$(document).ready(function() {
			c.tagIt();
			c.accordionLoad();
			c.calendar();
			c.homeFeatures();
			c.login();
			c.slideshow();
			c.tabLoad();
			c.textSizer();
			c.tools();
/*			c.relify(); */
		});
	},

	/*
		Tag the HTML element if JS is active
	*/
	tagIt : function() {
		var c = godkin;

		$("html").addClass(c.config.sHTMLtag);
	},
	
	/*
		Accordions
	*/
	accordionLoad : function() {
		var oAccordion = $("dl.tn-accordion");

		if (oAccordion) {
			$("dl.tn-accordion dt").hover(
				function () {
					$(this).addClass("on");
				},
				function () {
					$(this).removeClass("on");
				}
			);
			$("dl.tn-accordion").accordion({
				alwaysOpen: false,
				autoheight: true,
				header: "dt",
				selectedClass: "active"
			});
		}
	},

	/*
		Event calendar
	*/
	calendar : function() {
		var oCal = $("#calendar-nation");

		if (oCal.length == 1) {
			$("#calendar-nation select").change(function() {
				var c = godkin;

				var oForm = $(this).parents("form");
				/*var calCookie = $.cookie(c.config.cookieCalendarFilterName);*/

				var oCal = $("#calendar-nation");
				var oContent = $(oCal).children("div.content");

				/*
					NB - Commented out until deployed to test server.
						NEEDS MODIFICATION.
				*/
				var sUrl = $(this).parents("form").attr("action"); 
				$.ajax({
				 	cache : false,
					type: "post",
				 	url: sUrl,
					data: "loc=" + $(this).attr("value"),
				 	success : function(src) {
				 		oContent.replaceWith(src);
				 		godkin.calendar();
				 	}
				})
			});

			$("#calendar-nation ul.nav a").click(function() {
				var c = godkin;

				var oCal = $("#calendar-nation");
				var oContent = $(oCal).children("div.content");
				var sUrl = $(this).attr("href");

				if (sUrl) {
					$.ajax({
						cache : false,
						url: sUrl,
						success : function(src) {
							oContent.replaceWith(src);
							c.calendar();
						}
					})
				}

				return false;
			});
		}
	},

	/*
		Homepage features/tabs
	*/
	homeFeatures : function() {
		var c = godkin;
		var oHomeTabs = $("#home-features ul.section-nav");
		var oSections = $("#home-features .section");

		if (oHomeTabs) {
			var tabCookie = $.cookie(c.config.cookieTabHome);

			c.config.oTabsHome = oHomeTabs.tabs();

			if (tabCookie) {
				c.config.oTabsHome.tabs("select", parseInt(tabCookie));

				$(oSections[parseInt(tabCookie)]).addClass("default");
			}

			$("#home-features .foot div").prepend(
				'<p class="mem"><a href="#">Default tab</a></p>'
			);

			$("#home-features p.mem").click(function() {
				var oActive = c.config.oTabsHome.data('selected.ui-tabs');
				$.cookie(c.config.cookieTabHome, oActive, { expires: 14 });

				$(oSections).removeClass("default");
				$(oSections[oActive]).addClass("default");
				return false;
			});
		}
	},

	/*
		Load sIFR
	*/
/*	loadSifr : function() {
		var c = godkin;
*/
		/*
			Register the fonts
		*/
/*		var knockoutFeather = {
			src : c.config.swfs + 'knockout-feather.swf'
		};
*/
		/*
			Activate the fonts
		*/
/*		sIFR.activate(knockoutFeather);
*/
		/*
			Hi, we're the replacements
		*/
/*		sIFR.replace(knockoutFeather, {
			selector: '.mag-subscribe h2',
			wmode: 'transparent',
			css: [
				'.sIFR-root { color: #999999; font-size: 24px; leading: -4px; letter-spacing: 1; text-transform: uppercase; }'
			],
			tuneHeight: '-2px'
		});

		sIFR.replace(knockoutFeather, {
			selector: '.content-w-ad-sm .cols-3 .column h2',
			wmode: 'transparent',
			css: [
				'.sIFR-root { color: #333333; font-size: 17px; leading: -4px; letter-spacing: 1; text-transform: uppercase; }'
			],
			tuneHeight: '-2px'
		});

		sIFR.replace(knockoutFeather, {
			selector: '.content-w-ad-sm .additional h2',
			wmode: 'transparent',
			css: [
				'.sIFR-root { color: #333333; font-size: 15px; leading: -4px; letter-spacing: 1; text-transform: uppercase; }'
			],
			tuneHeight: '-2px'
		});

		sIFR.replace(knockoutFeather, {
			selector: 'form.subscribe .title',
			wmode: 'transparent',
			css: [
				'.sIFR-root { color: #336699; font-size: 30px; leading: -4px; text-align: center; text-transform: uppercase; }',
				'a { text-decoration: none; }',
				'a:link { color: #336699;  }',
				'a:hover { color: #6699CC; }'
			],
			tuneHeight: '-2px'
		});

		sIFR.replace(knockoutFeather, {
			selector: 'form.poll .title',
			wmode: 'transparent',
			css: [
				'.sIFR-root { color: #4E636E; font-size: 17px; leading: -2px; letter-spacing: 1; text-transform: uppercase; }'
			],
			tuneHeight: '-2px'
		});

		sIFR.replace(knockoutFeather, {
			selector: '.content-w-ad-w .cols-2 .col-2 .feature h2',
			wmode: 'transparent',
			css: [
				'.sIFR-root { color: #333333; font-size: 17px; leading: -2px; letter-spacing: 1; text-transform: uppercase; }'
			],
			tuneHeight: '-2px'
		});

		sIFR.replace(knockoutFeather, {
			selector: '.featured-news .title',
			wmode: 'transparent',
			css: [
				'.sIFR-root { color: #333333; font-size: 15px; leading: 0; letter-spacing: 1; text-transform: uppercase; }'
			],
			tuneHeight: '-4px'
		});

		sIFR.replace(knockoutFeather, {
			selector: '.sub-cats .title',
			wmode: 'transparent',
			css: [
			'.sIFR-root { color: #333333; font-size: 20px; leading: 0; letter-spacing: 1; text-transform: uppercase; }'
			],
			tuneHeight: '-4px'
		});

		sIFR.replace(knockoutFeather, {
			selector: '.year-archive h2',
			wmode: 'transparent',
			css: [
				'.sIFR-root { color: #333333; font-size: 16px; leading: -1px; letter-spacing: 1; text-transform: uppercase; }'
			],
			tuneHeight: '-4px'
		});
	},
*/
	/*
		Login/search toggle
	*/
	login : function() {
		var oLogin = $("#tn-login");
		if (oLogin) {
			oLogin.hide();

			$("#global li.login").toggle(function() {
				$("#tn-search").hide();
				$("#tn-login").fadeIn(500);
			}, function() {
				$("#tn-login").hide();
				$("#tn-search").fadeIn(500);
			});
		}
	},

	/*
		Slideshow
	*/
	slideshow : function() {
		var oSlides = $("#slides-main");

		if (oSlides) {
			var c = godkin;

			c.slideHeight();

			oSlides.cycle({ 
				fx:  'scrollHorz', 
				prev:   '#slide-prev', 
				next:   '#slide-next', 
				timeout: 0 
			});
		}
	},

	slideHeight : function() {
		var oSlideshow = $("#slides-main");

		if (oSlideshow.length) {
			var c = godkin;

			var oSlides = $("#slides-main ul.slides li");
			var limit = 0;

			oSlides.each(function() {
				var o = $(this);
				var iHeight = o.height() + 20;
				if (iHeight > limit) limit = iHeight;
			})

			oSlideshow.height(limit / 10 + "em");
		}
	},

	/*
		Tabs
	*/
	tabLoad : function() {
		var oAllTabs = $(".tn-sections > ul.section-nav");
		if (oAllTabs) {
			oAllTabs.tabs();
		}
	},

	/*
		Text sizer
	*/
	textSizer : function() {
		var c = godkin;

		var oTools = $("div.tools");
		c.config.oAltStyles = $("link.text-size");

		if (oTools && c.config.oAltStyles) {
			oTools.append(
				'<dl class="text-size"><dt>Text Size</dt><dd><ul></ul></dd></dl>'
			);

			var oUl = $("dl.text-size ul");
			var styleCookie = $.cookie(c.config.cookieTextSize);

			for (var i = 0; i < c.config.oAltStyles.length; i++) {
				var one = $(c.config.oAltStyles[i]);
				var lastClass = (i == (c.config.oAltStyles.length - 1)) ? "last " : "";

				if (styleCookie == one.attr("id")) {
					c.config.oAltStyles[i].disabled = false;
				} else {
					c.config.oAltStyles[i].disabled = true;
				}
				oUl.append(
					'<li class="' + lastClass + one.attr("id") + '"><a title="' + one.attr("title") + '" href="#' + one.attr("id") + '">A</a></li>'
				);
			}

			$("dl.text-size a").click(function() {
				var slug = $(this).attr("href").split("#")[1];
				for (var i = 0; i < c.config.oAltStyles.length; i++) {
					var one = $(c.config.oAltStyles[i]);
					if (slug == one.attr("id")) {
						$.cookie(c.config.cookieTextSize, one.attr("id"), {expires: 14});
						c.config.oAltStyles[i].disabled = false;
					} else {
						c.config.oAltStyles[i].disabled = true;
					}
				}
				return false;
			});
		}
	},

	/*
		Article tools
	*/
	tools : function() {
		$("ul.article-tools li:has(ul)").addClass("top").children("a").toggle(
			function () {
				$(this).parent().addClass("on");
			},
			function () {
				$(this).parent().removeClass("on");
			}
		);
	},
	relify : function() {
		$("a[rel]").each(
			function () {
				this.href =  this.href + '?rel=' + this.rel;
			}
		);
	
	}
};
godkin.init();
