$(document).ready(function(){
	
	/* determine which version of the leftnav script should be used
	* desktop = onmouseover
	* tablet = onclick
	*/
	var lnScript = !isTablet() ? "/leftnav/fsmenu_commented.js" : "/leftnav/fsmenu_commented.onClick.js";
	$.get( lnScript, function(){
		$.get( "/leftnav/leftnav.js", function(){
			listMenu.activateMenu("listMenuRoot", arrow);
		});
	});
	
	
	/* attempt to define a session ID */
	if ( $.getQueryString({id:"Session_ID"}) ) {
		//$("body").append("<br>session id found on query string");
		window.sessionID = $.getQueryString({id:"Session_ID"});
	} else {
		/* if they're on a miva page, we have the session ID on the cart link
		* lets use that.
		* if it's null, use ajax to get the session ID
		* this may be someone visiting the homepage who has no session ID yet
		* this will generate one for them
		*/
		if ( $("#cartLinkSID").length && $("#cartLinkSID").attr("href").indexOf("Session_ID") > -1 ) {
			//$("body").append("<br>session id found on cart link");
			window.sessionID = $("#cartLinkSID").attr("href").split("Session_ID=")[1];
		} else {
			/* this will most likely only happen on the homepage for a new visitor
			* and they don't need a session ID anyway, so lets not do it
			*/
			//$("body").append("<br>not bothering to generate session id");
			/*try {
				$.get( "/mm5/merchant.mvc?Screen=SID", function(data,status,XMLHttpRequest){
					window.sessionID = data;
				});
			} catch (err) {}*/
		}
	} // if ( $.getQueryString({id:"Session_ID"}) ) {
	//$("body").append("<br><br><br><br>session id = " + window.sessionID);
	if ( !document.cookie && !cookieTest() ) {
		/* append session ID to links IF cookies are disabled AND a session ID exists */
		//$("body").append("<br><br><br><br><br><br><br><br><br>cookies are disabled");
		/* cookies are disabled. hopefully the code above to define a sessionID worked
		*/
		if ( sessionID ) {
			/* hopefully we have one now. if not, hopefully it's just a new
			* visitor on the homepage
			*/
			//$("body").append("<br>There are " + $("a").length + " total links on the page, of which " + $("#content a").length + " are in the #content div");
			//$("body").append("<br>adding session id to all links");
			if ( $("#content").length ) {
				//$("body").append("<br>found the content area ... ");
				if ( $("#content a").length ) {
					//$("body").append("and some links (" + $("#content a").length + ")");
					$("#content a").each(function(){
						//$("body").append("<br>--" + $(this).attr("href"));
					});
				}
			}
			var $internalLinks = $("a[href^='http://"+top.location.host.toString()+"'], a[href^='https://"+top.location.host.toString()+"'],a[href^='//"+top.location.host.toString()+"'], a[href^='/'], a[href^='./'], a[href^='../'], a[href^='#']");
			$internalLinks.each(function(){
				if ( $(this).attr("href") ) {
					if( $(this).attr("href").indexOf( "Session_ID" ) == -1 && ( $(this).attr("href").indexOf( "nicepricefavors.com" ) > -1 || $(this).attr("href").charAt(0) == "/" ) ) {
						var newURL = $(this).attr("href").indexOf( "?" ) > -1 ? ($(this).attr("href") + "&Session_ID=" + sessionID) : ($(this).attr("href") + "?Session_ID=" + sessionID );
						$(this).attr("href", newURL );
					}
				}
			});
		} else {
			//$("body").append("<br>there is no session ID");
		}
	}
	
	/* autocomplete */
	if ( 0 ) {
		// jquery 1.8's autocomplete
		// we can't use it until they provide a way to limit the # of items in the list
		var autocompleteTerms = SearchSpring.Autocomplete.Queries;
		$('.searchspring-query').autocomplete({ source: autocompleteTerms });
	} else {
		// autocomplete plugin
		//$("body").append("<br>setting up autocomplete");
		if ('http:' == document.location.protocol) {
			$().ready(function() {
				try {
					if (SearchSpring.Autocomplete && SearchSpring.Autocomplete.Queries) {
						$('.search-query-input').each(function() {
							$(this).autocomplete(SearchSpring.Autocomplete.Queries, {max:10, width:220, scroll:false, matchContains:'word', autoFill:false,selectFirst:false});
							$(this).result(function(event, data, formatted) {
								event.target.form.submit();
							});
						});
					}
				} catch (e) {}
			});
		}
	}
	/* end autocomplete */
	
	
	if ( $(".datepicker").length ) {
		$(".datepicker").datepicker();
	}
});
