// Set/Get Cookies for hard-coded category navigations
jQuery.cookie = function (key, value, options) {
    // key and at least, value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);
        if (value === null || value === undefined) {
            options.expires = -1;
        }
        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        value = String(value);
        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }
    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

// Current Navigation Selector, Header Image Selection and Sidebar Display
// preload nav images
if (document.images) {
    img1 = new Image();
    img1.src = "assets/templates/CoffeeBean/images/menuLink_current.png";
    img2 = new Image();
    img2.src = "assets/templates/CoffeeBean/images/menuLink_active.png";
	img3 = new Image();
	img3.src = "assets/templates/CoffeeBean/images/cc_left_over.png";
	img4 = new Image();
	img4.src = "assets/templates/CoffeeBean/images/cc_repeat_over.png";
	img5 = new Image();
	img5.src = "assets/templates/CoffeeBean/images/cc_right_over.png";
}	
jQuery(document).ready(function() {
	// Load url variable with current url
	var url = jQuery(location).attr('href');
	// Check for https in the url and replace since none of our generated links show the https
	/*
	 * Be SURE that all images/scripts/stylesheets called from the frame are using relative
	 * paths to avoid the non-secure content alert in IE as that will kill this script!
	 */
	if(url.match('^https')) {
		url = url.substr(url.lastIndexOf("/") + 1);
		url = url.replace(/^https/, 'http');
	} else {
		// If no https, just parse the url
		url = url.substr(url.lastIndexOf("/") + 1);
	}
	
	// If there is nothing in the path, it must be the home page.
	if (url == "" || url == "home.asp" || url == "/") {
		url = "/";
		// Choose a header
		jQuery("#headerContent").hide();		
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").hide();
		jQuery("#headerCommon").hide();
		jQuery("#headerHome").show();
		// There are no sidebars on the home view. Adjust the class to apply a new style.
		jQuery("#center").addClass("home");
		// Hide the sidebar
		jQuery("#left").hide();
	}
	//list all leftAccount pages
	else if ((url.indexOf("About-Us") != -1)
				|| url == "Caring-Cup.html"
				|| (url.indexOf("Senior-Management") != -1)
				
				|| (url.indexOf("Our-Stores") != -1)
				|| (url.indexOf("Our-Drink-Menu") != -1)
				|| (url.indexOf("Nutrition") != -1)
				|| (url.indexOf("Kosher") != -1)
				|| (url.indexOf("Store-Locator") != -1)
				
				|| (url.indexOf("Company-Culture") != -1)
				|| (url.indexOf("Guiding-Principles") != -1)
				|| (url.indexOf("Benefits") != -1)
				|| (url.indexOf("Recognition") != -1)
				|| (url.indexOf("FAQs") != -1)
				|| (url.indexOf("Careers") != -1)
				|| (url.indexOf("Spirit-Award") != -1)
				
				|| (url.indexOf("myaccount") != -1)
				|| (url.indexOf("error") != -1)
				|| (url.indexOf("contactus") != -1)
				|| (url.indexOf("Returns-Exchanges") != -1)
				|| (url.indexOf("Shipping-Tax") != -1)
				|| (url.indexOf("ordertracking") != -1)
				|| (url.indexOf("view_wishlist") != -1)
				|| (url.indexOf("Gift-Cards_ep_48-1") != -1)
				|| url == "Gift-Cards_ep_48-1.html"
				|| (url.indexOf("FAQ") != -1)
				|| (url.indexOf("Privacy-Policy") != -1)
				|| (url.indexOf("TERMS-of-USE") != -1)
				
				) {
		// Isolate the customer care pages
		// This is the primary landing page for my account
		jQuery("#headerContent").hide();		
		jQuery("#headerHome").hide();
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").hide();
		jQuery("#headerCommon").show();
		// Apply the home class to expand content div to full site width.
		jQuery("#center").addClass("defaultAccount");
		// Hide the sidebar.
		jQuery("#left").hide();
		jQuery("#leftAccount").show();
		jQuery("#leftAccount2").hide();
		// Highlight proper section of leftAccount menu
		if(url.indexOf("About-Us") != -1){
			// highlight sidemenu item
			jQuery("#menu-about-us").addClass("current");
		}
		else if(url.indexOf("Caring-Cup") != -1){
			// highlight sidemenu item
			jQuery("#menu-caring-cup").addClass("current");
		}
		else if(url.indexOf("Senior-Management") != -1){
			// highlight sidemenu item
			jQuery("#menu-senior-management").addClass("current");
		}
		
///////////below here can be deleted
		//menu-about
		if((url.indexOf("About-Us") != -1)
				|| (url.indexOf("Caring-Cup") != -1)
				|| (url.indexOf("Senior-Management") != -1)
			){
				jQuery("#menu-about").show();
			}
			
		//menu-visit
		else if((url.indexOf("Our-Drink-Menu") != -1)
				|| (url.indexOf("Our-Stores") != -1)
				|| (url.indexOf("Nutrition") != -1)
				|| (url.indexOf("Kosher") != -1)
				|| (url.indexOf("Store-Locator") != -1)
			){
				jQuery("#menu-visit").show();
				if(url.indexOf("Our-Drink-Menu") != -1){
					// highlight sidemenu item
					jQuery("#menu-drink").addClass("current");
				}
				else if(url.indexOf("Nutrition") != -1){
					// highlight sidemenu item
					jQuery("#menu-nutrition").addClass("current");
				}
				else if(url.indexOf("Kosher") != -1){
					// highlight sidemenu item
					jQuery("#menu-kosher").addClass("current");
				}
				else if(url.indexOf("Store-Locator") != -1){
					// highlight sidemenu item
					jQuery("#menu-locator").addClass("current");
				}
			}
		//menu-careers
		else if((url.indexOf("Company-Culture") != -1)
				|| (url.indexOf("Guiding-Principles") != -1)
				|| (url.indexOf("Benefits") != -1)
				|| (url.indexOf("Recognition") != -1)
				|| (url.indexOf("Careers") != -1)
				|| (url.indexOf("FAQs") != -1)
				|| (url.indexOf("Spirit-Award") != -1)
			){
				jQuery("#menu-careers").show();
				if(url.indexOf("Company-Culture") != -1){
					// highlight sidemenu item
					jQuery("#menu-culture").addClass("current");
				}
				else if(url.indexOf("Guiding-Principles") != -1){
					// highlight sidemenu item
					jQuery("#menu-principles").addClass("current");
				}
				else if(url.indexOf("Benefits") != -1){
					// highlight sidemenu item
					jQuery("#menu-benefits").addClass("current");
				}
				else if(url.indexOf("Recognition") != -1){
					// highlight sidemenu item
					jQuery("#menu-recognition").addClass("current");
				}
				else if(url.indexOf("Careers") != -1){
					// highlight sidemenu item
					jQuery("#menu-careers").addClass("current");
				}
				else if(url.indexOf("FAQs") != -1){
					// highlight sidemenu item
					jQuery("#menu-faqs").addClass("current");
				}
				else if(url.indexOf("Spirit-Award") != -1){
					// highlight sidemenu item
					jQuery("#menu-spirit").addClass("current");
				}
			}
		//menu-customer
		else if((url.indexOf("myaccount") != -1)
				|| (url.indexOf("error") != -1)
				|| (url.indexOf("contactus") != -1)
				|| (url.indexOf("Returns-Exchanges") != -1)
				|| (url.indexOf("Shipping-Tax") != -1)
				|| (url.indexOf("ordertracking") != -1)
				|| (url.indexOf("view_wishlist") != -1)
				|| (url.indexOf("FAQ") != -1)
				|| (url.indexOf("Our-Gift-Cards") != -1)
				|| (url.indexOf("Privacy-Policy") != -1)
				|| (url.indexOf("TERMS-of-USE") != -1)
				|| (url.indexOf("Gift-Cards_ep_48-1") != -1)
			){
				jQuery("#menu-customer").show();
				if(url.indexOf("myaccount") != -1){
					// highlight sidemenu item
					jQuery("#menu-account").addClass("current");
				}
				else if(url.indexOf("error") != -1){
					// highlight sidemenu item
					jQuery("#menu-error").addClass("current");
				}
				else if(url.indexOf("contactus") != -1){
					// highlight sidemenu item
					jQuery("#menu-contact").addClass("current");
				}
				else if(url.indexOf("Returns-Exchanges") != -1){
					// highlight sidemenu item
					jQuery("#menu-returns").addClass("current");
				}
				else if(url.indexOf("Shipping-Tax") != -1){
					// highlight sidemenu item
					jQuery("#menu-shipping").addClass("current");
				}
				else if(url.indexOf("ordertracking") != -1){
					// highlight sidemenu item
					jQuery("#menu-order").addClass("current");
				}
				else if(url.indexOf("view_wishlist") != -1){
					// highlight sidemenu item
					jQuery("#menu-wishlist").addClass("current");
				}
				else if(url.indexOf("FAQ") != -1){
					// highlight sidemenu item
					jQuery("#menu-faq").addClass("current");
				}
				else if(url.indexOf("Our-Gift-Cards") != -1){
					// highlight sidemenu item
					jQuery("#menu-gift-cards").addClass("current");
				}
				else if(url.indexOf("Privacy-Policy") != -1){
					// highlight sidemenu item
					jQuery("#menu-privacy").addClass("current");
				}
				else if(url.indexOf("TERMS-OF-USE") != -1){
					// highlight sidemenu item
					jQuery("#menu-terms").addClass("current");
				}
				else if(url.indexOf("Gift-Cards_ep_48-1") != -1){
					// highlight sidemenu item
					jQuery("#menu-cards").addClass("current");
				}
			}
		else{
			jQuery("#menu-about").show();
		}
///////// above here can be deleted
				
				
				
	}	
	//list all leftAccount2 pages
	else if ((url.indexOf("Specialty-Sales") != -1)
				|| (url.indexOf("Company-Growth") != -1)
				|| (url.indexOf("Distribution-Channels") != -1)
				|| (url.indexOf("Territory-Availability") != -1)
				|| (url.indexOf("Developer-Criteria") != -1)
				|| (url.indexOf("Franchise-Process") != -1)
				|| (url.indexOf("Franchises") != -1)
				|| (url.indexOf("Franchise-Inquiry-Form") != -1)
				|| (url.indexOf("Real-Estate-Inquiry-Form") != -1)
				|| (url.indexOf("Frequently-Asked-Questions") != -1)
				) {
		// Isolate the customer care pages
		// This is the primary landing page for my account
		jQuery("#headerContent").hide();		
		jQuery("#headerHome").hide();
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").hide();
		jQuery("#headerEquipment").hide();
		jQuery("#headerCBTL").hide();
		jQuery("#headerCommon").show();
		// Apply the home class to expand content div to full site width.
		jQuery("#center").addClass("defaultAccount");
		// Hide the sidebar.
		jQuery("#left").hide();
		jQuery("#leftAccount").hide();
		jQuery("#leftAccount2").show();
		
		// Show proper section of leftAccount menu
		//menu-business
		if((url.indexOf("Specialty-Sales") != -1)
				|| (url.indexOf("Specialty-Sales-Form") != -1)
			){
				jQuery("#menu-business").show();
			}
		//menu-franchises
		else if((url.indexOf("Company-Growth") != -1)
				|| (url.indexOf("Distribution-Channels") != -1)
				|| (url.indexOf("Territory-Availability") != -1)
				|| (url.indexOf("Developer-Criteria") != -1)
				|| (url.indexOf("Franchise-Process") != -1)
				|| (url.indexOf("Franchises") != -1)
				|| (url.indexOf("Franchise-Inquiry-Form") != -1)
				|| (url.indexOf("Real-Estate-Inquiry-Form") != -1)
				|| (url.indexOf("Frequently-Asked-Questions") != -1)
			){
				jQuery("#menu-franchises").show();
			}
		else{
			jQuery("#menu-business").show();
		}		
	}
	
	//////////
	//products
	//////////
	else if(((url.indexOf("Coffee") != -1 && (url.indexOf("Timer") == -1)) && (url.indexOf("Duet") == -1) && (url.indexOf("Extract") == -1))
				|| (url.indexOf("Light-Subtle") != -1)
				|| (url.indexOf("Rich-Smooth") != -1)
				|| (url.indexOf("Dark-Distinctive") != -1)
				|| (url.indexOf("Decaffeinated_c_7") != -1)
				|| (url.indexOf("Flavored-Coffees") != -1)
				|| (url.indexOf("Caring-Cup-Coffees") != -1)
				
				//Coffee Light & Subtle
				|| (url.indexOf("100-Kona") != -1)
				|| (url.indexOf("100-Jamaican-Blue-Mountain") != -1)
				|| (url.indexOf("Brazil-Cerrado") != -1)
				|| (url.indexOf("House-Blend") != -1)
				
				//Coffee Rich & Smooth
				|| (url.indexOf("Colombia-Nario") != -1)
				|| (url.indexOf("Costa-Rica-La-Cascada") != -1)
				|| (url.indexOf("Costa-Rica-La-Minita-Tarrazu") != -1)
				|| (url.indexOf("Kenya-AA") != -1)
				|| (url.indexOf("Mocha-Java") != -1)
				|| (url.indexOf("Guatemala-Antigua") != -1)
				|| (url.indexOf("Sumatra-Mandheling") != -1)
				|| (url.indexOf("Ethiopia-Yirgacheffe") != -1)
				|| (url.indexOf("Sumatra-Mandheling") != -1)
				|| (url.indexOf("Papua-New-Guinea-SigriA") != -1)
				
				
				//Coffee Dark & Distinctive
				|| (url.indexOf("Tanzania-Peaberry") != -1)
				|| (url.indexOf("Colombia-Nario-Dark") != -1)
				|| (url.indexOf("Espresso-Roast") != -1)
				|| (url.indexOf("Viennese-Blend") != -1)
				|| (url.indexOf("French-Roast-") != -1)
				|| (url.indexOf("Sumatra-Mandheling-Dark") != -1)
				|| (url.indexOf("Bali-Blue-Moon-") != -1)
				|| (url.indexOf("Support-from-Home-Blend") != -1)				
				
				
				//Coffee Decaffeinated
				|| (url.indexOf("Decaf-Espresso-") != -1)
				|| (url.indexOf("Decaf-French-Roast") != -1)
				|| (url.indexOf("Decaf-Viennese-") != -1)
				|| (url.indexOf("Decaf-House-Blend") != -1)
				|| (url.indexOf("Decaf-Colombia-Nario") != -1)
				|| (url.indexOf("Decaf-French-Vanilla") != -1)
				
				
				//Coffee Flavored Coffees
				|| (url.indexOf("Butterscotch-Toffee") != -1)
				|| (url.indexOf("Limited-Edition-Pumpkin-Spice") != -1)
				|| (url.indexOf("Limited-Edition-Cinnamon-French-Toast-Coffee") != -1)
				|| (url.indexOf("Vanilla-Caramel") != -1)
				|| (url.indexOf("Crme-Brule") != -1)
				|| (url.indexOf("Hazelnut-Coffee") != -1)
				|| (url.indexOf("French-Vanilla_p_45") != -1)
				|| (url.indexOf("Decaf-French-Vanilla") != -1)	
				|| (url.indexOf("Raspberry-Cream-Coffee") != -1)	
				|| (url.indexOf("Caramel-Flavored-Ground-Coffee") != -1)	
				
				
				|| (url.indexOf("Papua-New-Guinea-Sigri") != -1)	
				
				){
		
		jQuery("#headerContent").hide();		
		jQuery("#headerCoffee").show();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").hide();
		jQuery("#headerEquipment").hide();
		jQuery("#headerCBTL").hide();
		jQuery("#headerCommon").hide();
		jQuery("#headerHome").hide();
		
		// Apply the default style to the category and product pages.
		jQuery("#center").addClass("default");
		jQuery("#left").show();
		}
		
		
	else if(((url.indexOf("Tea") != -1) && (url.indexOf("Capsules") == -1) && (url.indexOf("Infuser") == -1) && (url.indexOf("Tea-Pot") == -1) && (url.indexOf("Tea-Pitcher") == -1) && (url.indexOf("Timer") == -1) && (url.indexOf("Winter-Dream-Tea") == -1))
				|| (url.indexOf("Tea-Masters") != -1)
				|| (url.indexOf("All-Natural-Teas") != -1)
				|| (url.indexOf("Caring-Cup_c_19") != -1)
				|| (url.indexOf("Green-Teas") != -1)
				|| (url.indexOf("Black-Teas") != -1)
				|| (url.indexOf("Flavored-Teas") != -1)
				|| (url.indexOf("Herbal-Infusions") != -1)
				|| (url.indexOf("Decaffeinated-Teas") != -1)
				|| (url.indexOf("Loose-Leaf-Teas") != -1)
				|| (url.indexOf("Swedish-Berries") != -1)
				|| (url.indexOf("Decaffeinated-Teas_c_24") != -1)
				){
		
		jQuery("#headerContent").hide();		
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").show();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").hide();
		jQuery("#headerEquipment").hide();
		jQuery("#headerCBTL").hide();
		jQuery("#headerCommon").hide();
		jQuery("#headerHome").hide();
		
		// Apply the default style to the category and product pages.
		jQuery("#center").addClass("default");
		jQuery("#left").show();
		
		}
	else if((url.indexOf("Gifts-Sweets") != -1)
				|| (url.indexOf("Powders-Extracts-Sauces") != -1)
				|| (url.indexOf("Gift-Cards") != -1)
				|| (url.indexOf("Gifts") != -1)
				|| (url.indexOf("Clearance-") != -1) 
				|| (url.indexOf("Duet") != -1)
				
				
				//Powders Extracts and Sauces 
				|| (url.indexOf("French-Deluxe-Vanilla-Powder") != -1)
				|| (url.indexOf("French-Deluxe-Vanilla-No-Sugar-Added") != -1)
				|| (url.indexOf("Special-Dutch-Chocolate-No-Sugar-Added") != -1)
				|| (url.indexOf("White-Chocolate") != -1)
				|| (url.indexOf("100-Pure-Coffee-Extract") != -1)
				|| (url.indexOf("100-Pure-Coffee-Extract--Decaf") != -1)
				|| (url.indexOf("Caramel-Sauce") != -1)
				|| (url.indexOf("Special-Dutch-Chocolate") != -1)
				
				// Gift cards
				|| (url.indexOf("Gift-Card") != -1)
				
				// Gifts
				|| (url.indexOf("Pomegranate-Vanilla-Tea-Tin") != -1)
				|| (url.indexOf("Winter-Dream-Tea") != -1)
				|| (url.indexOf("Winter-Dream-Mini-Tea-Tin") != -1)
				|| (url.indexOf("Orange-Cranberry-Mini-Tea-Tin") != -1)
				|| (url.indexOf("Sugar-Plum-Tea") != -1)
				
				// Clearance
				|| (url.indexOf("Holiday-Tea-Trio") != -1)
				|| (url.indexOf("Holiday-Tea-Quartet") != -1)
				|| (url.indexOf("Stir-Brown-Green-Spiral-Travel-Mug") != -1)
				|| (url.indexOf("Stir-Black-White-Flower-Travel-Mug") != -1)
				
				
				){
		
		jQuery("#headerContent").hide();		
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").show();
		jQuery("#headerEquipment").hide();
		jQuery("#headerCBTL").hide();
		jQuery("#headerCommon").hide();
		jQuery("#headerHome").hide();
		
		// Apply the default style to the category and product pages.
		jQuery("#center").addClass("default");
		jQuery("#left").show();
		
		}
		
	else if((url.indexOf("Drinkware") != -1)
				|| (url.indexOf("DRINKWARE") != -1)
				
				
				// Drinkware
				|| (url.indexOf("Tumbler") != -1)
				|| (url.indexOf("Breast-Cancer-Awareness-Acrylic-Cup") != -1)
				|| (url.indexOf("Ceramic-Hot-Cup") != -1)
				|| (url.indexOf("Breast-Cancer-Awareness-Ceramic-Cup") != -1)
				|| (url.indexOf("Jenni") != -1)
	
	){
		
		jQuery("#headerContent").hide();		
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").show();
		jQuery("#headerGifts").hide();
		jQuery("#headerEquipment").hide();
		jQuery("#headerCBTL").hide();
		jQuery("#headerCommon").hide();
		jQuery("#headerHome").hide();
		
		// Apply the default style to the category and product pages.
		jQuery("#center").addClass("default");
		jQuery("#left").show();
		
		}
	
	else if((url.indexOf("EQUIPMENT") != -1)
				|| (url.indexOf("Equipment") != -1)
				|| (url.indexOf("Essentials") != -1)
				|| (url.indexOf("ESSENTIALS") != -1)
				|| (url.indexOf("Bodum") != -1)
				|| (url.indexOf("Mesh-Tea-Infuser-w-Handle") != -1)
				|| (url.indexOf("Ceylon-Ice-Tea-Pitcher") != -1)
				|| (url.indexOf("The-Coffee-Bean-Tea-Leaf-Tea-Timer") != -1)
				){
		
		jQuery("#headerContent").hide();		
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").hide();
		jQuery("#headerEquipment").show();
		jQuery("#headerCBTL").hide();
		jQuery("#headerCommon").hide();
		jQuery("#headerHome").hide();
		
		// Apply the default style to the category and product pages.
		jQuery("#center").addClass("default");
		jQuery("#left").show();
		
		}
	else if((url.indexOf("CBTL-System") != -1)
				|| (url.indexOf("CBTLreg-system") != -1)
				|| (url.indexOf("Contata-S03-Grey-Black") != -1)
				|| (url.indexOf("Capsules") != -1)
				|| (url.indexOf("Kaldi") != -1)
				|| (url.indexOf("Milk-Frother-LM-145P-White") != -1)
				){
		
		jQuery("#headerContent").hide();		
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").hide();
		jQuery("#headerEquipment").hide();
		jQuery("#headerCBTL").show();
		jQuery("#headerCommon").hide();
		jQuery("#headerHome").hide();
		
		// Apply the default style to the category and product pages.
		jQuery("#center").addClass("default");
		jQuery("#left").show();
		
		}
	
	
	
	 else if (url.indexOf("_p_") != -1 || url.indexOf("_c_") != -1) {
		// This is a Category or Product page
		// Show the Content header
		jQuery("#headerHome").hide();
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").hide();
		jQuery("#headerEquipment").hide();
		jQuery("#headerCBTL").hide();
		jQuery("#headerCommon").hide();
		jQuery("#headerContent").show();
		// Apply the default style to the category and product pages.
		jQuery("#center").addClass("default");
		// The sidebar will show without any action here by defalt from the frame	
		// If we are on a product page, display the related products across the bottom of the frame.
		if (url.indexOf("_p_") != -1) {
			jQuery("#listingRelated").show();
			// Since the headers show custom text for categories and we are viewing a product, the header needs to show some default text.
			jQuery("#catHeaderText").hide();
			jQuery("#catHeaderTextProduct").show();
		} else {
			// Hide the related products div on all other pages.
			jQuery("#listingRelated").hide();
		}
	} 
	 else {
		// This is probably a common page.
		// Show the Common header
		jQuery("#headerContent").hide();		
		jQuery("#headerHome").hide();
		jQuery("#headerCoffee").hide();
		jQuery("#headerTea").hide();
		jQuery("#headerDrinkware").hide();
		jQuery("#headerGifts").hide();
		jQuery("#headerEquipment").hide();
		jQuery("#headerCBTL").hide();
		jQuery("#headerCommon").show();
		// Apply the home class to expand content div to full site width.
		jQuery("#center").addClass("home");
		// Hide the sidebar.
		jQuery("#left").hide();
	}
	// Do some formatting for the extra pages that are generated by the system
	 if (url.indexOf("_ep_") != -1 || url.indexOf("ordertracking") != -1) {
		if (!jQuery("#extraContainer").get(0)) {
			// This should toggle a class and be styled in the stylesheet but I was in a hurry to finish
			jQuery("#midContent").css('padding', '20px 0 20px 20px');
		} 
	}
	
	// Check for trailing query string and remove it if nothing follows it.
	if (url.substr(url.lastIndexOf("?") + 1) == '') {
		url = url.substr(0, url.length - 1);
	} else if (url.substr(url.lastIndexOf("?") + 1) != '') {
		// If there is an action going on, it needs to be formatted and this is the easiest way to get the basics we need. Again, being in a hurry.
		jQuery("#midContent table:first").css('width', '733');
		jQuery("#midContent table:first").css('margin', '0 auto');
		// Grab the table with a matching class "alternative"
		var updateLayout = jQuery("table.alternative");
		// Apply the class to the parent td
		updateLayout.parent().addClass('alternative');
		// Remove the class from the table
		updateLayout.removeClass('alternative');
		// Now that the class is on the parent td, it can be styled for all browsers
		jQuery("td.alternative").css('padding', '20px');
	}
	// Check which nav the link is in and apply the appropriate style/class.
	// If this is coming from the top header menu navigation:
	if (!jQuery("ul#headerLinkUL li").find("a[href='" + url + "']").addClass("active")) {
		return;
	} else {
		// This is coming from the top or sidebar category navigation
		// TODO: This is pretty ugly. Clean it up... It also does not have a way to mark categories when viewing products of category...
		// If this is a sub-category request:
		if (jQuery("ul#menuLinks li").find("a[href='" + url + "']").hasClass("submenu")) {
			jQuery("ul#menuLinks li").find("a[href='" + url + "']").parent().parent().parent().addClass("current");
			if (jQuery("div.catSub").find("a[href='" + url + "']")) {
				// This is for the ad insertion above
				var childLevel = true;
				jQuery("div.catSub").find("a[href='" + url + "']").parent().addClass("current");
			}
		} else if (jQuery("div.cat").find("a[href='" + url + "']").hasClass("submenu") || jQuery("div.catSub").find("a[href='" + url + "']").hasClass("submenu")) {
			// This is a top level category or a category that is not in the top (ul#menuLinks) navigation menu
			if (jQuery("div.cat").find("a[href='" + url + "']").hasClass("submenu")) {
				// Top level category. Check for existence in the top nav and update class if it is there.
				jQuery("ul#menuLinks li").find("a[href='" + url + "']").parent().addClass("current");
				// Top level in the sidebar navigation. Update the class.
				jQuery("div.cat").find("a[href='" + url + "']").parent().addClass("current");
			} else {
				// This is a sub-category in the sidebar, probably dynamically generated. Grab it and update the class.
				// This is for the ad insertion above
				var childLevel = true;
				jQuery("div.catSub").find("a[href='" + url + "']").parent().addClass("current");
			}
		} else {
			// This is a top level category request
			jQuery("ul#menuLinks li").find("a[href='" + url + "']").parent().addClass("current");
		}
	} 
	
	// Check and see if this is a category page. If it is, set it up for ad placement 
	if (childLevel == true) {
		// Check the count on the items being displayed
		var numItems = jQuery('.catListingBG').length;
		// If it is less than 4
		if(numItems <= 3) {
			// Place the insert after the first element
			var target = jQuery("div.catListingBG:eq(0)").parent();
		} else {
			// There are 2 rows of items. Insert into the middle of the bottom row.
			var target = jQuery("div.catListingBG:eq(3)").parent();
		}
		// Fetch the content to be inserted
		jQuery.get('assets/templates/CoffeeBean/ads.html', function(data) {
			// Finally, insert the content
			jQuery(data).insertAfter(target);
		});		
	}
});




// Set columns to equal heights

//<![CDATA[
/*
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".height").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".height").equalHeights(4000); Sets all cols to at least 4000px tall.
 * Example 3: $(".height").equalHeights(1000,3000); Cols are at least 1000 but no more
 * than 3000 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */
(function($) {
	jQuery.fn.equalHeights = function(lowHeight, maxHeight) {
		tallest = (lowHeight) ? minHeight: 0;
		this.each(function() {
			if(jQuery(this).height() > tallest) {
				tallest = jQuery(this).height();
			}
		});
		
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			jQuery(this).height(tallest).css({'height': tallest});
		});		
	}
})(jQuery);

// Handle breadcrumb rewrites 
// Insert the following div on desired pages to create editable breadcrumbs
// <div id="breadcrumbs">[CATEGORY_FULLLINE]</div>
jQuery(document).ready(function() {
	if(jQuery("#breadcrumbs").get(0)) {
		var strText = jQuery("#breadcrumbs").html();
		newText = strText.replace("&gt;", "&frasl;");
		jQuery("#breadcrumbs").html(newText);
		jQuery("#breadcrumbs").find("a").addClass("breadcrumb");
		jQuery("#breadcrumbs").find("a:last").removeClass("breadcrumb");
		jQuery("#breadcrumbs").find("a:last").addClass("breadcrumbLast");
	} else if (jQuery("#listingCrumbs").get(0)) {
		// If this is a listing page, grab the last increment of the breadcrumbs and turn it into a heading
		var crumbs = jQuery("#listingCrumbs").find("a:last").html();
		jQuery("#listingCrumbs").html("<h2>" + crumbs + "</h2>");
	} else if (jQuery(location).attr('href').indexOf("_ep_") != -1) {
		// Check for stray breadcrumbs in system generated pages and format if found
		if (jQuery("td.data").get(0)) {
			var crumbdata = jQuery("td.data").html();
			if (crumbdata.substring(crumbdata.indexOf("&nbsp;&gt;&nbsp;"))) {				
				jQuery("td.data").each(function (i) {
					var breadcrumb = jQuery(this);
					var crumb = jQuery(this).html();
					if (crumb.substring(crumb.indexOf("Home</a>&nbsp;&gt;&nbsp;"))) {
						newText = crumb.replace("&gt;", "&frasl;");
						jQuery(this).html(newText);
						breadcrumb.children("a").addClass("breadcrumb");
						breadcrumb.find("a:last").removeClass("breadcrumb");
						breadcrumb.find("a:last").addClass("breadcrumbLast");
						// Check for existence of breadcrumb class and add id to parent
						if (breadcrumb.children("a").hasClass("breadcrumb"))
							jQuery(this).attr("id","breadcrumbs");
					} else {
					   return;
					}
				});
			}
		}
	}
});

// Home page content slider

jQuery.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var wrapper = jQuery('> div', this).css('overflow', 'hidden'),
            slider = wrapper.find('> div#innerWrap'),
            items = slider.find('> div.carouselContent'),
            single = items.filter(':first'),
            
            singleWidth = single.outerWidth(), 
			// note: doesn't include padding or border
            visible = Math.ceil(wrapper.innerWidth() / singleWidth),
            currentPage = 1,
            pages = Math.ceil(items.length / visible);            


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if ((items.length % visible) != 0) {
            slider.append(repeat('<div class="empty" />', visible - (items.length % visible)));
            items = slider.find('> div.carouselContent');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        items.filter(':first').before(items.slice(- visible).clone().addClass('cloned'));
        items.filter(':last').after(items.slice(0, visible).clone().addClass('cloned'));
        items = slider.find('> div.carouselContent'); // reselect
        
        // 3. Set the left position to the first 'real' item
        wrapper.scrollLeft(singleWidth * visible);
        
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        wrapper.after('<a class="arrow back"></a><a class="arrow forward"></a>');
        
        // 5. Bind to the forward and back buttons
        jQuery('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        jQuery('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        jQuery(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};
// Start the carousel
jQuery(document).ready(function () {
  jQuery('.infiniteCarousel').infiniteCarousel();
});

// Equalize the heights of the carousel content divs as soon as they are available to the DOM
jQuery(document).ready(function() {
		jQuery(".listingBG").equalHeights();
	});
	
// Adjust height to fit content of slider so we don't have to guess at a set height.
// This is set after the carousel content is loaded so we can read the height of the equalized variable text nodes.
jQuery(document).ready(function() {
	var wrapperHeight = parseInt(jQuery(".listingBG").height());
	jQuery(".infiniteCarousel").height(wrapperHeight).css({'height': wrapperHeight});
	jQuery(".wrapper").height(wrapperHeight).css({'height': wrapperHeight});
});

/* End Slider */

// Shopping Cart Image Hover
jQuery(document).ready(function() {
	if(jQuery("#cartCount").get() != null) {
		jQuery("#cartCount").hover(function() {
			if (jQuery(this).hasClass("ccStatic")) {
				jQuery(this).removeClass("ccStatic");
				 jQuery(this).addClass("ccOver");
			} else {
				 jQuery(this).addClass("ccOver");
			}
	   },function() {
		   if (jQuery(this).hasClass("ccOver")) {
				jQuery(this).removeClass("ccOver");
				 jQuery(this).addClass("ccStatic");
			} else {
				 jQuery(this).addClass("ccStatic");
			}
	   });	
	} else {
		return;
	}
});

// Email address encoding provided by: http://bronze-age.com/nospam/encode.html
// version 5
var tld_ = new Array()
tld_[0] = "com";
tld_[1] = "org";
tld_[2] = "net";
tld_[3] = "ws";
tld_[4] = "info";
tld_[10] = "co.uk";
tld_[11] = "org.uk";
tld_[12] = "gov.uk";
tld_[13] = "ac.uk";
var topDom_ = 13;
var m_ = "mailto:";
var a_ = "@";
var d_ = ".";

function mail(name, dom, tl, params)
{
	var s = e(name,dom,tl);
	document.write('<a href="'+m_+s+params+'" rel="nofollow" title="Send Us An Email">'+s+'</a>');
}
function mail2(name, dom, tl, params, display)
{
	document.write('<a href="'+m_+e(name,dom,tl)+params+'" rel="nofollow" title="Send Us An Email">'+display+'</a>');
}
function e(name, dom, tl)
{
	var s = name+a_;
	if (tl!=-2)
	{
		s+= dom;
		if (tl>=0)
			s+= d_+tld_[tl];
	}
	else
		s+= swapper(dom);
	return s;
}
function swapper(d)
{
	var s = "";
	for (var i=0; i<d.length; i+=2)
		if (i+1==d.length)
			s+= d.charAt(i)
		else
			s+= d.charAt(i+1)+d.charAt(i);
	return s.replace(/\?/g,'.');
}
