﻿/// <reference path="jquery-1.6.2-vsdoc.js" />
/// <reference path="modernizr-2.0.6-development-only.js" />

// -----------------------------------
// GOOGLE ANALYTICS
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-81174-8']);
_gaq.push(['_trackPageview']);
(function () {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

// -----------------------------------
// MAIN
$(function () {

	$(document).on("click", ".reload", function () {
		window.location.reload();
		return false;
	});

	$("nav a").click(function () {

		_gaq.push(['_trackEvent', "Navigation", $(this).text(), $(this).attr("href")]);

		if ($(this).parent().hasClass("active")) { $(this).blur(); return false; }

		var $this = $(this),
			href = $this.attr("href"),
			section = $this.data("section"),
			isUrl = href.search("#") != 0,
			$content = $(section);

		// SET ACTIVE NAV
		$this.parent().addClass("active").siblings().removeClass();

		// LOAD ANY DYNAMIC CONTENT
		if (isUrl) {
			$content.find("div.inner")
				.append("<img src=\"/content/images/loading.gif\" class=\"content-loading\" alt=\"Content Loading\"/>")
				.load(href)
				.ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {
					// DISPLAY ANY ERROR
					$(this).html(jqXHR.responseText);
				});
		}

		// GOOGLE AND FACEBOOK PLUG-INS
		// containers are off screen and are moved back if selected
		if (section == "#social" && $("#social-likeus").css("position") != "static") {
			$("#social-likeus").insertBefore("#social-icons").css({ position: "static", left: "0" });
		}

		// LOAD IMAGES WITHIN CONTENT
		$content.find("img").each(function () {
			var src = $(this).data("imgsrc");
			if (src != $(this).attr("src")) {
				$(this).attr("src", src);
			}
		});

		// FADE IN SELECTED
		// FADE OUT PREVIOUS
		$content.fadeIn("slow").siblings("div.content").fadeOut("fast");

		$this.blur();
		return false;
	});

	// BAND MEMBER HIGHLIGHT
	$("a.highlight").hover(function () {
		var highlight = $(this).data("highlight");
		$("#bandmembers").toggleClass(highlight);
	});

	// BAND MEMBER OVERLAY
	$("#bandmembers a").overlay({
		mask: {
			color: null,
			maskId: "mask-bandmember"
		},
		onBeforeLoad: function () {
			var overlay = this.getOverlay(),
				img = overlay.find("img:eq(0)"),
				src = img.data("photo");
			if (img.attr("src") != src) {
				img.attr("src", src);
			}
			$("div.scrollable-photos", overlay).data("scrollable").begin();
			overlay.appendTo("body");
			_gaq.push(['_trackEvent', "BandMember", this.getTrigger().text(), this.getTrigger().attr("href")]);
		}
	});

	//BAND MEMBER SCROLLABLE PHOTOS
	$("div.scrollable-photos").scrollable({
		onSeek: function () {
			var img = this.getItems().eq(this.getIndex()),
				src = img.data("photo");
			if (img.attr("src") != src) {
				img.attr("src", src);
			}
		}
	}).navigator().click(function () {
		$(this).data("scrollable").next();
	});

	$("a[rel=external]").prop({ target: "_blank", title: function (i, val) { return (val.length) ? val + " | Opens in a new window" : "Opens in a new window"; } });

	$(window).resize(function () { rePositionOverlay(); });

	// IE < 9 HACK
	if (!Modernizr.opacity) {
		$("<div class=\"clearer\"></div>").insertBefore("div.content:eq(0)");
		$("div.content:gt(0)").each(function () {
			$(this).append("<div class=\"content-top\"></div><div class=\"content-bottom\"></div>");
		});
	}

	DivideList("#setlist ul", "#setlist div.inner", 15);

	// BACKGROUND IMAGE FADE-IN
	fadeInBackground();

	insertGooglePlus();

});

function insertGooglePlus() {
	var po = document.createElement("script");
	po.type = "text/javascript"; po.async = true; po.src = "https://apis.google.com/js/plusone.js";
	var s = document.getElementsByTagName("script")[0];
	s.parentNode.insertBefore(po, s);
}

// -------------------------------------------------------
// DIVIDE LIST
function DivideList(list, returnTo, size) {
	var theList = $(list).remove(), group;
	while ((group = theList.find("li:lt(" + size + ")")).length) {
		$("<ul/>").append(group).appendTo($(returnTo));
	}
}
// -------------------------------------------------------
// FADE IN BACKGROUND
function fadeInBackground() {
	if (readCookie("faded") == null) {
		$("#bkgfadein").delay(1000).animate({ "opacity": "show" }, 2000, function () {
			document.cookie = "faded=true";
			$("body").addClass("bkg-band");
			$(this).remove();
		});
	}
	else {
		$("body").addClass("bkg-band");
	}
}

// Copyright (c) 2011 Russ Cam
// -------------------------------------------------------
// Dual licensed under the MIT and GPL licenses.
//   - http://www.opensource.org/licenses/mit-license
//   - http://www.opensource.org/licenses/gpl-3.0
(function ($) {
	$.fn.wrapChildren = function (options) {

		options = $.extend({
			childElem: undefined,
			groupSize: 1,
			wrapper: '<div>'
		}, options || {});

		if (options.childElem === undefined) return this;

		return this.each(function () {
			var elems = $(this).children(),
				len = pos = 0;

			elems.each(function (i, value) {
				len += $(value).is(options.childElem) ? 1 : 0;
				if (len > 0 && (len % options.groupSize === 0) || (i === elems.length - 1)) {
					elems.slice(pos, i + 1).wrapAll(options.wrapper);
					len = 0;
					pos = i + 1;
				}
			});
		});

	}
})(jQuery);
// -------------------------------------------------------
// CREATE COOKIE
function createCookie(name, value, days) {
	var date = "", expires = "";
	if (days) {
		date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = "; expires=" + date.toGMTString();
	}
	else {
		expires = "";
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}
// -------------------------------------------------------
// READ COOKIE
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}
// -------------------------------------------------------
// REPOSITION OVERLY
function rePositionOverlay() {
	var $overlay = $("div.overlay-bandmember"),
		w = $overlay.width(),
		h = $overlay.height(),
		left = ($(window).width() - w) / 2,
		top = ($(window).height() - h) / 2;
	$overlay.css({ left: left, top: top });
};

// -------------------------------------------------------
// IMAGE CACHING
function cacheImg(imgSrc) {
	var img = new Image();
	img.src = imgSrc;
	return img;
}
