$(document).ready(function(){

	$("#Menu li:last-child").addClass("last");

	$("#slideshow").cycle({
		fx: "fade",
		timeout: 4500
	});

	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"));	

//	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: 1,
		speed: 200
	});
	$("#HomeNewProducts .ProductListContainer").jCarouselLite({
		btnNext: "#featured-next2",
		btnPrev: "#featured-prev2",
	    visible: 4,
	 	scroll: 1,
		speed: 200
	});
	
//	$("a.fancybox").fancybox();

	//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
	/*console.log(url);
	if (url.protocol!="https") {
		window.location='https://'+url.domain+url.path;
	};*/
	$("#Menu > ul > li > a").each( function (){ // for each menu item in the UL start processing...
	
		// this is the URI of each menu item
		var href = $(this).attr('href') + "";
		// this is URI of the current url
		var currLocation = url.path;
		
		// Strip last slash from menu item urls (if it exists)
		if (href.charAt(href.length-1)=="/") {
			href = href.slice(0, -1)
		}
		//Strip last slash from current URL item (if it exists)
		if (currLocation.charAt(currLocation.length-1)=="/") {
			currLocation = currLocation.slice(0, -1)
		}
		
		// Replace the ampersant representation found in the URL with an actual ampersant.
		// Needed for accurate comparison.
		currLocation.replace(/-%26-/,' & '); 
		
		// this is an array of the items in the menu item in the loop
		var href = href.split('/'); 
		
		// this is an array of the items in the URI
		var menuHref = currLocation.split('/'); 
			
		if (menuHref[1]=='categories') { var indexToTest = 2 };
		if (menuHref[1]=='brands') { var indexToTest = 1 };
		//if (menuHref[1]=='products') { var indexToTest = 2 };
		
		
		if ((indexToTest!=undefined)&&( menuHref[indexToTest] == href[indexToTest])) {
			// 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"
		}
	});

	
	
	

	$('.SideCategoryListClassic > ul > li > ul > li').each(function() {
		innerMenu = $(this).children('ul');
		//alert(innerMenu.length);
		if (innerMenu.length == 0) {
			$(this).addClass('noBackground');
		}
	});
	
	$("#UpcomingEvents").PaRSS(
		"http://sacredspaces.org/rss.php?action=newblogs&type=rss",	// rss feed url (required)
		2,		// number of items (optional)
		"M jS g:i a",	// date format (optional)
		"newsitem"         // display format (optional)
	);
	$("#UpcomingEventsFull").PaRSS(
		"http://sacredspaces.org/rss.php?action=newblogs&type=rss",	// rss feed url (required)
		10,		// number of items (optional)
		"M jS g:i a",	// date format (optional)
		"fullnewsitem"         // display format (optional)
	);
	
	
	$('.SubCategoryListGrid li:last-child').remove();
	
	/*var playlist = new Array();
	var target;
	
	$('#ProductDescription .ProductDescriptionContainer a').each(function() {
		url = $(this).attr('href');
		extension = url.substring(url.length-3,url.length);
		if (extension=="mp3") {
			target = $(this).parent();
			playlist[playlist.length] = [url,$(this).html()];
		}
	});
	playlistFormated = '{ file: "'+playlist[0][0]+'", title:"'+playlist[0][1]+'" }';
	
	for (a=1; a<playlist.length; a++) {
		playlistFormated = playlistFormated + ', { file:" '+playlist[a][0]+'", title:"'+playlist[a][1]+'" }';
	}
	*/
	//alert(playlistFormated);
	
	//var target = $('#ProductDescription .ProductDescriptionContainer a').parent();
	
	/*$(target).html('\
			<div id="player-container"></div><script type="text/javascript">\
			heightOfItem = 30;\
			heightOfControlBar = 23;\
			totalHeight = heightOfControlBar+(heightOfItem*4);\
			jwplayer("player-container").setup({\
				flashplayer: "/templates/__custom/js/player.swf",\
				height: totalHeight,\
				width: 470,\
				autostart: true,\
				volume: 80,\
				playlist: ['+playlistFormated+'],\
				"playlist.position": "bottom",\
				"controlbar": "top",\
				"playlist.size": totalHeight,\
				skin:"/templates/__custom/js/sourcebooks.zip"\
			});\
			</script>\
	');*/

	//alert($('#BrandContent form > span').text());
	if (($('#BrandContent form > span').text()=="There are no products listed under this brand.")) {
		$('#BrandContent').css({
			'background':"#fff",
			'padding':'15px'
		});
		$('#BrandContent form > span').html('There are no books under this author.');
	}
	
	
	
	
});
