$(document).ready(function(){
	if ( !isTablet() ) {
		$("ul.sf-menu").superfish({ 
			animation: {height:'show'},   // slide-down effect without fade-in 
			delay:     1200               // 1.2 second delay on mouseout 
		});
	} else {
		$.get("/jquery/superfish/superfish.onclick.js", function(){
			$("ul.sf-menu").superfishOnClick({ 
				animation: {height:'show'},   // slide-down effect without fade-in 
				delay:     1200               // 1.2 second delay on mouseout 
			});
		});
	}
	/* highlight link of current page */
	var loc = window.location.toString().split('/');
    loc = loc[loc.length - 1];
	//$("body").append("<br><br><br><br><br><br><br><br>location = " + loc );
	var linkSelector = "#logobar li a[href$='"+loc+"']";
	if ( loc ) {
		/* if homepage, loc will be blank. for some reason IE8 will match on this, and 
		* all links get underlined. hence the test */
    	$("#logobar li a[href$='"+loc+"']").parents("li").removeClass("hiddentab").addClass("currenttab");
	}
	//$("#volumeDiscounts").hide();
						   
	/* 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");
		}
	}
	
	if ( $(".datepicker").length ) {
		$(".datepicker").datepicker();
	}
});
