function SBCLibrary() {
	this.schedule = null;
	this.featureCount = 0;
	this.firstFeature = null;
	this.init = function() {
		this.loadFeatureSchedule();
	}
	this.loadFeatureSchedule = function() {
		var context = this;
		$.ajax({
			url: "xml/feature-schedule.xml",
			dataType: ($.browser.msie) ? "text" : "xml",
			success: function(data) {
				if (typeof data == "string") {
					xml = new ActiveXObject("Microsoft.XMLDOM");
					xml.async = false;
					xml.loadXML(data);
				} else xml = data;
				context.schedule = xml;
				context.loadFeatures();
			}
		 });
	}
	this.loadFeatures = function() {
		var context = this;
		var today = new Date();
		var foundSpecial = false;
		var dayData = $(this.schedule).find("day[date='" + (today.getMonth() + 1) + "/" + today.getDate() + "/" + today.getFullYear() + "']").find("block").each(function() {
			foundSpecial = true;
			context.parseFeatureData(this);
		});
		if (!foundSpecial) {
			var dayData = $(this.schedule).find("day[id='" + today.getDay() + "']").find("block").each(function() {
				context.parseFeatureData(this);
			});
		}
	}
	this.parseFeatureData = function(data) {
		var today = new Date();
		var time = today.getTime();
		var compareStart = this.getDatedTime(today, $(data).attr("start"));
		var compareEnd = this.getDatedTime(today, $(data).attr("end"));
		if (time >= compareStart && time < compareEnd) {
			var count = 0;
			var context = this;
			$(data).find("feature").each(function() {
				if (++count <= 3) {
					var feature = $(this).attr("id")
					// create nav bar link
					var featureLink = "<a onmouseover=\"SBC.overFeature('" + feature + "');\" onmouseout=\"SBC.outFeature('" + feature + "');\" " + 
							"onclick=\"SBC.clickFeature('" + feature + "');\">&nbsp;</a>";
					var featureNav = "<li class='feature-" + feature + "'>" +featureLink + "</li>";
					$(".bar-features ul").append(featureNav);
					// create footer lnk
					var footerNav = "<li><a href='default.aspx?feature=" + feature + "'>" + $(this).text() + "</a></li>";
					$(".nav-btm-featured ul").append(footerNav);
					// create features text overlay
					if (count == 2) $(".feature-overlay").append(featureLink);
					// load feature on home page
					var sPath = window.location.pathname;
					sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
					if (window.location.pathname.toLowerCase().indexOf("default.aspx") >= 0 ||
					(window.location.search.substring(1) == "" && window.location.pathname.toLowerCase().indexOf("aspx") < 0)) {
						if (context.firstFeature == null) context.firstFeature = feature;
						context.loadFeatureMarquee(feature);
					}
				}
			});
		}
	}
	this.loadFeatureMarquee = function(feature) {
		var context = this;
		$.ajax({
			url: "features/" + feature + ".htm",
			dataType: "text",
			success: function(data) {
				var featureHTML = "<li class='fcon-" + feature + "'><div id='" + feature + "'>" + data + "</div></li>";
				$(".section-home ul").append(featureHTML);
				if (++context.featureCount == 3) context.displayFeature();
			}
		 });
	}
	this.displayFeature = function() {
		currentFeature = SBC.getQSValue("feature");
		if (currentFeature == null) currentFeature = this.firstFeature;
		SBC.clickFeature(currentFeature, true);
	}
	this.getDatedTime = function(today, val) {
		var timeParts = val.split(":");
		var compare = new Date(today.getFullYear(), today.getMonth(), today.getDate(), this.parseIntLeadingZero(timeParts[0]), parseInt(timeParts[1]));
		return compare;
	}
	this.parseIntLeadingZero = function(val){
		while (val.charAt(0) == "0") val = val.substring(1, val.length);
		if (val.length == 0) val = 0;
		return val;
	}
	/*
	*/
	/* MAIN NAVIGATION */
	this.hardNavigation = false;
	this.isDownLevel = function() {
		if ($.browser.msie) {
			if (parseInt($.browser.version, 10) < 8) return true;
		}
		return false;
	}
	this.submitLocator = function() {
		document.forms["store-locator-form"].submit();
	}
	this.navigationTimeout = null;
	this.currentMain = null;
	this.currentSub = null;
	this.navHardSet = false;
	this.mainNavAreas = ["about", "products", "locator", "contact"];
	this.setMainNav = function(mainNav, hardSet) {
		clearTimeout(this.navigationTimeout);
		if (hardSet) this.navHardSet = true;
		this.expandMainNav(mainNav);
		this.currentMain = mainNav;
	}
	this.resetMainNav = function(mainNav) {
		var context = this;
		clearTimeout(this.navigationTimeout);
		this.navigationTimeout = setTimeout(function() { context.collapseMainNav(); }, 500);
	}
	this.expandMainNav = function(mainNav) {
		$(".nav-main").css("overflow", "visible");
		this.setMainNavOutStates();
		this.setMainOverState(mainNav);
	}
	this.collapseMainNav = function() {
		$(".nav-main").css("overflow", "hidden");
		this.setMainNavOutStates();
		this.currentMain = null;
	}
	this.setMainNavOutStates = function() {
		var offsets = ["0", "-151px", "-317px", "-486px"];
		for (var i = 0; i < this.mainNavAreas.length; i++) {
			var mainName = this.mainNavAreas[i];
			$(".nav-main-" + mainName + "-details").css("background-image", "url(images/nav-" + mainName + "-out." + (this.isDownLevel() ? "gif" : "png") + ")");
			$(".nav-main-" + mainName + " .nav-main-item").css("background-position", offsets[i] + " 0");
		}
		this.setSubNavItemVisible(this.currentMain, false);
	}
	this.setMainOverState = function(mainNav) {
		var mainName = this.mainNavAreas[mainNav - 1];
		var offsets = ["0", "-151px", "-317px", "-486px"];
		$(".nav-main-" + mainName + "-details").css("background-image", "url(images/nav-" + mainName + "." + (this.isDownLevel() ? "gif" : "png") + ")");
		$(".nav-main-" + mainName + " .nav-main-item").css("background-position", offsets[mainNav - 1] + " -23px");
	}
	this.setSubNav = function(mainNav, subNav) {
		clearTimeout(this.navigationTimeout);
		switch (mainNav) {
		case 1:
		case 2:
			if (mainNav != this.currentMain) {
				this.setMainNavOutStates();
				this.setSubNavItemVisible(this.currentMain, false);
				this.setMainOverState(mainNav);	
				this.currentMain = mainNav;			
			}
			this.currentSub = subNav;
			this.setSubNavItemVisible(mainNav, true);
			break;
		case 3:
			$(".nav-main-locator .nav-main-locator-details").css("background-position", "-170px 0");
			break;
		}
	}
	this.setSubNavItemVisible = function(mainNav, isVisible) {
		if (mainNav < 3) {
			var navSection = ".nav-main-" + this.mainNavAreas[mainNav - 1];
			$(navSection + " li a").each(function() {
				$(this).css("opacity", isVisible ? "1" : "0");
			});
		}
	}
	this.resetSubNav = function(mainNav) {
		clearTimeout(this.navigationTimeout);
		this.setSubNavItemVisible(this.currentSub, false);
		this.currentSub = null;
		if (mainNav == 3) $(".nav-main-locator .nav-main-locator-details").css("background-position", "0 0");
		var context = this;
		this.navigationTimeout = setTimeout(function() { context.collapseMainNav(); }, 300);
	}
	/* FEATURE NAVIGATION */
	this.revealFeatures = function(featureID, override) {
		var context = this;
		$(".bar-features li a").each(function() {
			$(this).css("background-position", "0 -241px");
		});
		$(".bar-features ul").stop().animate({ top:0 }, { duration:500, easing:"easeInOutQuad" });
		$(".bar-features").stop().animate({ top:-47 }, { duration:500, easing:"easeInOutQuad", complete:function() {
			context.featureState = "open";
			if (override) {
				context.selectFeature(featureID);
			} else {
				context.overFeature(featureID);
			}
		}});
	}
	this.hideFeatures = function() {
		var context = this;
		$(".bar-features").stop().animate({ top:14 }, { duration:500, easing:"easeInOutQuad", complete:function() {
			context.featureState = "closed";
			$(".bar-features li a").each(function() {
				$(this).css("background-position", "0 -324px");
			});
			$(".feature-overlay").css("top", 0);
			$(".feature-overlay").css("display", "block");
		}});
	}
	this.featureState = "closed";
	this.selectedFeature = null;
	this.featureTimeout = null;
	this.overFeature = function(featureID) {
		clearTimeout(this.featureTimeout);
		switch(this.featureState) {
		case "open":
		case "selected":
			if ((featureID != this.selectedFeature) && !this.isIOS()) {
				$(".feature-" + featureID + " a").css("background-position", "0 -152px");
				$(".feature-" + featureID).stop().animate({ top:-4 }, { duration:100, easing:"easeInOutQuad" });
			}
			break;
		}
	}
	this.outFeature = function(featureID) {
		switch(this.featureState) {
		case "open":
			var context = this;
			this.transitionSelectedOut(featureID);
			this.featureTimeout = setTimeout(function() { context.hideFeatures(); }, 1000);
			break;
		case "selected":
			if (featureID != this.selectedFeature) {
				$(".feature-" + featureID ).css("top", -4);
				$(".feature-" + featureID + " a").css("background-position", "0 -152px");
				this.transitionSelectedOut(featureID);
			}
			break;
		}
	}
	this.transitionSelectedOut = function(featureID) {
		$(".feature-" + featureID).stop().animate({ top:0 }, { duration:100, easing:"easeInOutQuad", complete:function() {
			$(".feature-" + featureID + " a").css("background-position", "0 -241px");
		}});
	}
	this.clickFeature = function(featureID, override) {
		if ($(".section-home").length) {
			if ($(".fcon-" + featureID).length == 0)
				featureID = $(".section-home li:nth-child(1)").attr("class").substr(5);
		}
		clearTimeout(this.featureTimeout);
		switch (this.featureState) {
		case "closed":
			$(".feature-overlay").css("display", "none");
			this.revealFeatures(featureID, override);
			break;
		case "open":
			this.selectFeature(featureID);
			break;
		case "selected":
			if (featureID != this.selectedFeature) {
				this.deselectFeature(this.selectedFeature);
				this.selectFeature(featureID)
			}
			break;
		}
		if ($(".section-home").length) this.insertFeature(featureID);
	}
	this.currentFeature = null;
	this.insertFeature = function(featureID) {
		if (this.currentFeature != null) {
			$(".fcon-" + this.currentFeature).css("display", "none");
		}
		$(".fcon-" + featureID).css("display", "block");
		this.currentFeature = featureID;
	}
	this.deselectFeature = function(featureID) {
		this.selectedFeature = null;
		this.outFeature(featureID);
	}
	this.selectFeature = function(featureID) {
		if ($(".section-home").length) {
			this.featureState = "selected";
			$(".feature-" + featureID + " a").css("background-position", "0 0");
			$(".feature-" + featureID ).css("top", -67);
			this.selectedFeature = featureID;
		} else {
			document.location.href = "Default.aspx?feature=" + featureID;
		}
	}
	this.productInfo = null;
	this.products = [
		["drip-coffee"], ["classic-mocha", "white-chocolate-mocha", "caramel-mocha", "classic-latte"], ["chai-tea-latte", "iced-tea"], ["cocoa-trio"],
		["vanilla-coffee-javakula", "chocolate-javakula", "caramel-javakula", "chocolate-coffee-crunch-javakula", "vanilla-cremekula","strawberry-fruitkula", "coldbrewed-iced-coffee", "coldbrewed-vanilla-latte", "coldbrewed-marble-mocha"],
		["iced-vanilla-latte", "iced-mocha-latte", "iced-latte"]
	];
	this.loadProductData = function(category, drink) {
		this.currentCategory = category;
		this.currentDrink = drink;
		var context = this;
		$.ajax({
			url: "xml/nutrition.xml",
			dataType: ($.browser.msie) ? "text" : "xml",
			success: function(data) {
				if (typeof data == "string") {
					xml = new ActiveXObject("Microsoft.XMLDOM");
					xml.async = false;
					xml.loadXML(data);
				} else xml = data;
				context.productInfo = xml;
				context.setProductData();
			}
		 });
	}
	this.setProductSizes = function(productID) {
		var sizes = new Array();
		var sizeDropDown = $(".nutrition li > div select#nutrition-size");
		sizeDropDown.empty();
		var count = 0;
		$(this.productInfo).find("product[id='" + productID + "']").find("variation").each(function() {
			var size = $(this).attr("size");
			sizeDropDown.append("<option value='" + size + "'" + (++count == 1 ? " selected='selected'" : "") + ">" + size.toLowerCase() + "</option>");
		});
		$(".nutrition li > div select#nutrition-size").css("display", (count > 1) ? "block" : "none");
	}
	this.getProductNutrition = function(productID, size) {
		var info;
		$(this.productInfo).find("product[id='" + productID + "']").find("variation[size='" + size + "']").each(function() {
			info = {
				milk: $(this).attr("milk"),
				whip: $(this).attr("whip"),
				extras: $(this).attr("extras"),
				calories: $(this ).attr("calories"),
				fatCalories: $(this).attr("fatcalories"),
				totalFat: $(this).attr("totalfat"),
				saturatedFat: $(this).attr("saturated"),
				transFat: $(this).attr("trans"),
				cholesterol: $(this).attr("cholesterol"),
				sodium: $(this).attr("sodium"),
				carbohydrates: $(this).attr("carbohydrates"),
				fiber: $(this).attr("fiber"),
				sugar: $(this).attr("sugar"),
				protein: $(this).attr("protein"),
				vitaminA: $(this).attr("vitamina"),
				vitaminC: $(this).attr("vitaminc"),
				calcium: $(this).attr("calcium"),
				iron: $(this).attr("iron"),
				caffeine: $(this).attr("caffeine")
			}
		});
		return info;
	}
	this.currentCategory = null;
	this.currentDrink = null;
	this.setProductData = function() {
		if (this.currentDrink) {
			$(".coffee-panel").css("display", "none");
			$(".product-" + this.currentDrink).css("display", "block");
			$(".nutrition-panel").css("display", "block");
			this.setProductSizes(this.currentDrink);
			this.setNutrition();
		}
	}
	this.setNutrition = function() {
		var size = $("#nutrition-size").val();
		var info = this.getProductNutrition(this.currentDrink, size);
		this.setNutritionField("calories", info.calories);
		this.setNutritionField("fat-calories", info.fatCalories);
		this.setNutritionField("total-fat", info.totalFat);
		this.setNutritionField("saturated-fat", info.saturatedFat);
		this.setNutritionField("trans-fat", info.transFat);
		this.setNutritionField("cholesterol", info.cholesterol);
		this.setNutritionField("sodium", info.sodium);
		this.setNutritionField("total-carbohydrate", info.carbohydrates);
		this.setNutritionField("dietary-fiber", info.fiber);
		this.setNutritionField("sugars", info.sugar);
		this.setNutritionField("protein", info.protein);
		this.setNutritionField("vitamin-a", info.vitaminA);
		this.setNutritionField("vitamin-c", info.vitaminC);
		this.setNutritionField("calcium", info.calcium);
		this.setNutritionField("iron", info.iron);
		this.setNutritionField("caffeine", info.caffeine);
		var notes = new Array();
		if (info.milk.toUpperCase() != "N/A") notes.push(info.milk);
		if (info.whip.toUpperCase() != "N/A") notes.push(info.whip);
		if (info.extras.toUpperCase() != "N/A") notes.push(info.extras);
		var noteData;
		if (notes.length > 0) {
			noteData = "* " + notes[0];
			for (var i = 1; i < notes.length; i++) {
				noteData += ", " + notes[i];
			}
		} else {
			noteData = "N/A";
		}
		this.setNutritionField("notes", noteData);
	}
	this.setNutritionField = function(fieldName, fieldValue) {
		var field = $(".nutrition ." + fieldName);
		if (fieldValue.toUpperCase() == "N/A") {
			field.parent().parent().remove();
		} else {
			field.text(fieldValue);
		}
	}
	this.urlQSVariables = null;
	this.setUrlInfo = function() {
		var url = window.location.href;
		var querystring = url.split("?");
		if (querystring.length > 1) {
			var qsVariables = querystring[1].split("&");
			this.urlQSVariables = new Array();
			for (var i = 0; i < qsVariables.length; i++) {
				var nvPair = qsVariables[i].split("=");
				this.urlQSVariables.push({name:nvPair[0], value:nvPair[1]});
			}
		} 
		
	}
	this.getQSValue = function(variableName) {
		this.setUrlInfo();
		if (this.urlQSVariables != null) {
			for (var i = 0; i < this.urlQSVariables.length; i++) {
				if (this.urlQSVariables[i].name == variableName) return this.urlQSVariables[i].value;
			}
		}
		return null;
	}
	this.focusLocator = function() {
		$("#nav-address").val("");
	}
	this.blurLocator = function() {
		if ($("#nav-address").val().length <= 0) {
			$("#nav-address").val("Enter Zip Code");
		}
	}
	this.overLocator = function() {
		$(".section-locator .input-field").css("background-position", "-177px 0");
	}
	this.outLocator = function() {
		$(".section-locator .input-field").css("background-position", "0 0");
	}
	this.currentProductPanel = 1;
	this.productPrevious = function(panelCount) {
		if (this.currentProductPanel > 1) {
			this.currentProductPanel--;
			this.setProductPosition();
		}
		this.checkNavigation(panelCount);
	}
	this.productNext = function(panelCount) {
		if (this.currentProductPanel < panelCount) {
			this.currentProductPanel++;
			this.setProductPosition();
		}
		this.checkNavigation(panelCount);
	}
	this.checkNavigation = function(panelCount) {
		$(".product-previous").css("display", (this.currentProductPanel > 1 ? "block" : "none"));
		$(".product-next").css("display", (this.currentProductPanel < panelCount ? "block" : "none"));
	}
	this.setProductPosition = function() {
		var newPos = -((this.currentProductPanel - 1) * 980);
		$(".products").stop().animate({ left:newPos }, { duration:200, easing:"easeInOutQuad" });
	}
	this.currentDiorama = 1;
	this.dioramaPrevious = function() {
		if (this.currentDiorama > 1) {
			this.currentDiorama--;
			this.setDioramaPosition();
		}
		this.checkDiorama();
	}
	this.dioramaNext = function() {
		if (this.currentDiorama < 5) {
			this.currentDiorama++;
			this.setDioramaPosition();
		}
		this.checkDiorama();
	}
	this.checkDiorama = function() {
		$(".diorama-previous").css("display", (this.currentDiorama > 1 ? "block" : "none"));
		$(".diorama-next").css("display", (this.currentDiorama < 5 ? "block" : "none"));
	}
	this.setDioramaPosition = function() {
		var newPos = -((this.currentDiorama - 1) * 992);
		/*/console.log("next: " + newPos);/*/
		$(".diorama-detail").stop().animate({ left:newPos }, { duration:200, easing:"easeInOutQuad" });
	}
	this.selectCareerStore = function() {
		window.open($("#gourl").val(), "Careers");
	}
	this.isIOS = function() {
		return (
			(navigator.platform.indexOf("iPhone") != -1) ||
			(navigator.platform.indexOf("iPod") != -1) ||
			(navigator.platform.indexOf("iPad") != -1)
		);
    }
}
