$(document).ready(function(){

	$("#PrimaryMenu > ul > li:last-child").addClass("last");
	//$(".SubCategoryListGrid > ul > li:last-child").addClass("last");

	$("#cycle").cycle({
		fx: "fade",
		pager:  '#cycle-nav',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#cycle-nav li:eq(' + (idx) + ') a';
        }
	});
	
	$("#espressocycle").cycle({
		fx: "fade"
	});

	function textReplacement(input) {
		var originalvalue = input.val();
		input.focus(function() {
			if($.trim(input.val()) == originalvalue){ input.val(""); }
		});
		input.blur(function() {
			if($.trim(input.val()) == ""){ input.val(originalvalue); }
		});
	}

	textReplacement($("#search_query"));
	textReplacement($("#nl_first_name"));
	textReplacement($("#nl_email"));
	
// REWRITE THE OUTPUT OF THE "(X ITEMS)" FIELD TO MATCH THEME
	var cartValue = $("#crt").html();
	var cartDigit;
	
	cartValue = jQuery.trim(cartValue); /Trim whitespace for IE/
	switch (cartValue)
	{
	case "":
	$("#crt").html("0");
	number = 0;
	break;
	case "(1 item)":
	$("#crt").html("1");
	number = 1 ;
	break;
	default:
	//cartDigit = 3;
	
	
	if(cartValue != ""){
	splitup=cartValue.split(" ");
	number = splitup[0].replace("(", "");
	$("#crt").html(number);
	}
	break;
	}

//	jCarouselLite example -- you will need to add a <div class="ProductListContainer"> around the <ul>
//	in the HomeFeaturedProducts Panel

	$("#HomeFeaturedProducts .ProductListContainer").jCarouselLite({
        btnNext: "#featured-next",
        btnPrev: "#featured-prev",
	      visible: 4,
	 	  scroll: 4,
		  speed: 800
   });
	
//Sticky Sidebar
//	$('#Wrapper').waypoint(function(event, direction) {
//		
//	}).find('#product-side-box').waypoint(function(event, direction) {
//		$(this).toggleClass('sticky', direction === "down");
//		event.stopPropagation();
//	});
	
//Truncate Grid View Product Titles
	
	$(".ProductList .ProductDetails a").truncate( 45 );
	
//bcNav

	$('#SideCategoryList').bcNav({
		classify: true,
		replaceheader: false,
		inserttree: false
	});

	
	$("a.fancybox").fancybox();
	
	$("a.fancyvideo").click(function() {
	$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 640,
			'height'		: 355,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			'wmode'			: 'transparent',
			'allowfullscreen'	: 'true'
			}
		});
		return false;
	});
	
	$("#fancyvideoprod").fancybox({
		'width' : 500,
		'height' : 400,
		'autoScale' : false,
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'type' : 'iframe'
	 }); 
	
	$("#InstantCouponPopupA").fancybox({
	}); 
	
		
	jQuery("#formsubmit").click(function() {
			
		var pageurl = window.location.pathname;
		var coupon_email = jQuery("input#coupon_email").val(); 
		var thankyou_url = jQuery("input#thankyou_url").val(); 
			
		jQuery.getJSON("http://forms.madwirewebdesign.com/hostedforms/beansociety/mailout.php?coupon_email=" + coupon_email + "&thankyou_url=" + thankyou_url + "&callback=?", function(data) { 
			if ((pageurl == '/cart.php') || (pageurl == '/checkout.php'))
			{
				$("#InstantCouponPopupA").trigger('click');
				//activateLB();
			}
			else
			{
				window.location = "/thank-you/"
			}
		});  
	});

	//used for adding an active class to links to categories in pages menu when on that category or sub-category
	//if you do not want the active class remove this and .ActivePage style in the css
	// function parseUri(sourceUri){
	 
	 // var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
	 // uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d* <smb://d*>))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\ <smb://?(%5B%5E#%5D*)>\?([^#]*) <smb://?(%5B%5E#%5D*)>)?(?:#(.*))?").exec(sourceUri),
	 // uri = {};
	
	 // for(var i = 0; i < 10; i++){
	 // uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	 // }
	
	 // /* Always end directoryPath with a trailing backslash if a path was present in the source URI
	 // Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	 // if(uri.directoryPath.length > 0){
	 // uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	 // }
	 
	 // return uri;
	 
	// }
	
	// var url = parseUri(window.location); // this gets the current url
	
	// $("#Menu ul li a").each( function (){ // for each menu item in the UL start processing...
	
	// var href = $(this).attr('href').split('/'); // this is an array of the items in the menu item in the loop
	// var currLocation = url.path; // this is URI of the current url
	// var menuHref = currLocation.split('/'); // this is an array of the items in the URI
	// if ( menuHref[2] == href[2] ) {
		// // if the current category = the category of the the menu item in the loop...
		// // in this case if the url category is NCAA and the menu "a" is NCAA, then proceed below to add the class
	// $(this).parent().addClass('ActivePage'); // add class "ActivePage" to the LI of the "a"
	// }
	
	// });
	//end active class to pages menu
	
	// For Side Category List -- Uncomment to use
	// I made some small changes that might be helpful.  It puts a class only on the parent ul - 'ActivePageContainer' and the child li - 'ActivePage'.  There might be some issues to sort out if the menu is more than 1 level deep.
	
	// function parseUri(sourceUri){

	// var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
	// uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d* <smb://d*>))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\ <smb://?(%5B%5E#%5D*)>\?([^#]*) <smb://?(%5B%5E#%5D*)>)?(?:#(.*))?").exec(sourceUri),
	// uri = {};

	// for(var i = 0; i < 10; i++){
	// uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	// }

	// /* Always end directoryPath with a trailing backslash if a path was present in the source URI
	// Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	// if(uri.directoryPath.length > 0){
	// uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	// }

	// return uri;

	// }

	// var url = parseUri(window.location); // this gets the current url

	// $(".SideCategoryListClassic ul li a").each( function (){ // for each menu item in the UL start processing...

	// var href = $(this).attr('href').split('/'); // this is an array of the items in the menu item in the loop
	// var currLocation = url.path; // this is URI of the current url
	// var menuHref = currLocation.split('/'); // this is an array of the items in the URI
	// if ( menuHref[2] == href[4] ) {
	// // if the current category = the category of the the menu item in the loop...
	// // in this case if the url category is NCAA and the menu "a" is NCAA, then proceed below to add the class
	// $(this).parent().addClass('ActivePage'); // add class "ActivePage" to the LI of the "a"
	// $(this).parent().parent().addClass('ActivePageContainer');
	// }

	// });
	//end side category list active class

});
