var climateCentral = {
	Config : {
		autoScroll : true
		,gaUA : "UA-4974354-1"
		,iSlidePaging : 6
		,sPathImg : "/-/img/"
		,sIEhoverSel : "#nav ul>li, #menus>ul"
		,sIEhoverClass : "ie-hover"
		,sHTMLtag : "can-has-js"
		,sPNGSel : "img, #page, #nav li a.top, #nav ul ul li, #nav ul ul li:hover, #section, #section-lead li a, #menus li p, #main .col-2 p#subscribe a, #trailer-gradient"
	}

	/*
		STOP EDITING HERE
	*/
	,init : function() {
		var c = climateCentral;
		var p = c.Project;

		$(document).ready(function() {
			// List function(s) to fire onload here
			p.tagIt();
			p.homeSlides();
			p.resetSwap();
			p.shareForm();
			p.sideTabs();
			p.relatedSlides();

			if (typeof DD_belatedPNG != "undefined") {
				DD_belatedPNG.fix(c.Config.sPNGSel);
				p.ieHover();
			}
			$.ga.load(c.Config.gaUA);
		});
	}

	/*
		CLIENT-SPECIFIC FUNCTIONS
	*/
	,Project : {
		tagIt : function() {
			var c = climateCentral;
			$("html").addClass(c.Config.sHTMLtag);
		}

		,homeSlides : function() {
			var c = climateCentral;
			var oSlides = $("#feature li.feature");
			$("#feature li.feature:first").addClass("first");
			$("#feature li.feature:last").addClass("last");

			if (oSlides.length > 1) {
				var tmp = 1;

				// Build the slide navigation
				$("#feature").append('<ul id="slides"></ul>');

				oSlides.each(function() {
					$("#slides").append('<li><a class="top" href="#home-slide-' + tmp + '">Feature ' + tmp + '</a><div class="feature-secondary"></div></li>');

					$(this).attr("id", "home-slide-" + tmp);

					if (tmp != 1) {
						$(this).hide();
					} else {
						$("#slides li:last-child").addClass("active");
					}

					var oDiv = $("#slides li:last-child div");
					var oP = $(this).find("p.sect");
					oDiv.append('<p class="' + oP.attr('class') + '">' + oP.html() + '</p>');
					oDiv.append('<h4>' + $(this).find('h1 cite').html() + '</h4>');
					oDiv.append($(this).find("p.ad img").clone());
					oDiv.append($(this).find("p.summary").clone());

					tmp++;
				});

				// Function to show a slide (stored in fShow for auto-paging/timeout purposes)
				var fShow = function(sDir) {
					var sDir = (c.Config.autoScroll) ? "next" : sDir;
					var oCurrent = $("#feature li.feature:visible");

					if (oCurrent.hasClass("last")) {
						var oShow = $("#feature li.first");
					} else {
						var oShow = oCurrent.next("li.feature");
					}

					var sId = oShow.attr("id");
					$("#slides li").removeClass("active");
					$("#slides li a[href$=#" + sId + "]").parent("li").addClass("active");

					oCurrent.hide();
					oShow.fadeIn(600);

					if (c.Config.autoScroll) {
						c.Config.slideTimer = window.setTimeout(fShow, c.Config.iSlidePaging * 1000);
					}
				};

				$("#slides li a").click(function() {
					c.Config.autoScroll = false;
					window.clearTimeout(c.Config.slideTimer);

					if (!$(this).parent().hasClass("active")) {
						var sSlug = $(this).attr("href").split("#")[1];

						$("#feature li.feature").hide();
						$("#feature li[id=" + sSlug + "]").fadeIn(600);

						// Change the display of the bullets
						$(this).parent().siblings().removeClass("active");
						$(this).parent().addClass("active");
					}

					return false;
				});

				$(window).load(function() {
					window.setTimeout(fShow, c.Config.iSlidePaging * 1000);
				});
			}
		}

		,relatedSlides : function() {
			var c = climateCentral;

			$("div.related").each(function() {
				var oSlides = $(this).children("div.relatedGroup");
				if (oSlides.length > 1) {
					$(this).append('<div class="related-nav"><a href="#" class="related-prev"><img src="' + c.Config.sPathImg + 'icon-arrow-prev.png" alt="Previous" /></a><a href="#" class="related-next"><img src="' + c.Config.sPathImg + 'icon-arrow-next.png" alt="Next" /></a></div>');

					var oPrev = $(this).find("a.related-prev");
					var oNext = $(this).find("a.related-next");

					$(this).find(".related-nav a").click(function() {
						if ($(this).hasClass("none") == false) {
							var oSlides = $(this).parents("div.related");
							var oCurrent = oSlides.children("div.relatedGroup:visible");

							if ($(this).hasClass("related-next")) {
								var otherLink = oSlides.find("a.related-prev");
								var oNextToShow = oCurrent.next("div.relatedGroup");

								oCurrent.hide();
								oNextToShow.show();

								var oUpcoming = oSlides.children("div.relatedGroup:visible").next("div.relatedGroup");
							} else if ($(this).hasClass("related-prev")) {
								var otherLink = oSlides.find("a.related-next");
								var oNextToShow = oCurrent.prev("div.relatedGroup");

								oCurrent.hide();
								oNextToShow.show();

								var oUpcoming = oSlides.children("div.relatedGroup:visible").prev("div.relatedGroup");
							}

							otherLink.removeClass("none");

							if (oUpcoming.length == 0) {
								$(this).addClass("none");
							}
						}
						return false;
					});

					oPrev.addClass("none");
					oSlides.not(":first").hide();
				}
			});
		}

		,resetSwap : function() {
			var c = climateCentral;

			$("input[type=reset]").replaceWith('<a class="reset"><img src="' + c.Config.sPathImg + 'btn-clear-data.png" alt="' + $(this).attr("value") + '" /></a>');
			$("a.reset").click(function() {
				var sConf = confirm("Are you sure you want to reset this form?");
				if (sConf) {
					$(this).parents("form").each(function() {
						this.reset();
					});
				}
			});
		}

		,shareForm : function() {
			$(".shareTools h3").append('<a class="shareClose" href="#">Close</a>');

			$("li.tool-share a, li.tool-email a").click(function() {
				var oParent = $(this).parents("div.articleTools-container");
				oParent.addClass("share");
				return false;
			});

			$(".shareTools h3 a").click(function() {
				var oParent = $(this).parents("div.articleTools-container");
				oParent.removeClass("share");
				return false;
			});
		}

		,sideTabs : function() {
			var oHeadlines = $(".tabs h2");

			if (oHeadlines.length > 1) {
				$("div.tabs").prepend('<ul class="tabs mod"></ul>');

				oHeadlines.each(function() {
					var sTxt = $(this).text();
					var sId = $(this).parents("div.tab").attr("id");
					$("ul.tabs").append('<li><a href="#' + sId + '" class="unselected">' + sTxt + '</a></li>');
					$(this).remove();
				});

				$("ul.tabs li:first-child a").removeClass("unselected");
				$("div.tabs div.tab:first").addClass("active");

				$("ul.tabs a").click(function() {
					var sId = $(this).attr("href").split("#")[1];
					$("div.tab").removeClass("active");
					$("#" + sId).addClass("active");

					$("ul.tabs a").addClass("unselected");
					$(this).removeClass("unselected");
					return false;
				});
			}
		}

		,ieHover : function() {
			var c = climateCentral;

			$(c.Config.sIEhoverSel).hover(
				function() {
					$(this).addClass(c.Config.sIEhoverClass);
				},
				function() {
					$(this).removeClass(c.Config.sIEhoverClass);
				}
			);
		}
	}
};

climateCentral.init();
