var editMode = false;
var cDir = window.location.pathname.topLevelDir();
var cPage = window.location.pathname.fileName();
var pageReqSetup = 0;
$(document).ready(function(){
	//hasJS
	$("body").addClass("hasJS");
	
	//pURL
	$("a[href='/signin/']").attr("href","/signin/?pURL="+window.location.pathname);
			
	
	//mainMenu
	$("#mainMenu").children("li").each(function(){
		$(this).hover(
		function(){
			$(this).children(".sub").show();
		},
		function(){
			$(this).children(".sub").hide();
		});

		if( $(this).children("a").attr("href").topLevelDir() == cDir ){
			$(this).children("a").css("background-position","0px -26px").addClass("current");
		}
	})
	.end()
	.find("ul.sub").each(function(){
		$(this).children("li:last").children("a").css("borderBottom","none");
	});
	
	//myPhoneBook
	var mpbDisplayTimer;
	$("#myPhoneBookButton, #myPhoneBookButton img").click(function(){
		if ( window.bob_signedIn ){
			var sendData = {
				userID: window.bob_userID
			}
			$.ajax({
				type: "GET",
				url: "/_includes/myphonebook.2.asp",
				cache: false,
				data: sendData,
				dataType: "html",
				success: function(html){
					$("#myPhoneBook .content").html(html);
					$("#myPhoneBook ul").each(function(){

						var currentPage = 0;
						var numPerPage = 5;
						var maxDisplay = 5;
						var padSize = 2;

						var $list = $(this);
						var totalItems = $list.find("li").length;
						var totalPages = Math.ceil( totalItems / numPerPage );

						$list.bind('repaginate', function(){
							var start = currentPage * numPerPage;
							var end = currentPage * numPerPage + numPerPage;
							if ( totalItems < end ){
								end = totalItems
							}
							
							/*
							var startDisplay = currentPage - padSize;
							if ( startDisplay < 0 ) startDisplay = 0;
							var endDisplay = startDisplay + (maxDisplay - 1);
							
							
							if ( endDisplay >= totalPages){
								var dDiff = endDisplay - totalPages;
								endDisplay = totalPages;
								startDisplay = endDisplay - maxDisplay;
								if ( startDisplay < 0 ) startDisplay = 0;
								endDisplay = endDisplay - 1;
							}
	
							var display = "";

							var i = 0;
							for ( i = startDisplay; i <= endDisplay; i++ ){
								display += i+1;
								if( i == currentPage ) display += "c";
								display +=",";
							}
							*/
							
							$("#mpbCount").text(start+1).append(" - ").append(end).append(" of ").append(totalItems).append(" entries");
							//$("#mpbCount").empty().append(display);
							
							$list.find("li").hide().slice(start, end).show();
						});

						$("#mpbPrevious").click(function(){
							currentPage--;
							if ( currentPage < 0 ) { currentPage = totalPages - 1;}
							$list.trigger("repaginate");
						});
						$("#mpbNext").click(function(){
							currentPage++;
							if ( currentPage > totalPages - 1 ) { currentPage = 0;}
							$list.trigger("repaginate");
						});

						$list.trigger("repaginate");

					});
				},
				error: function(){
					$("#myPhoneBook .content").html("<p>Sorry, but this feature is not currently available.</p>");
				}
			});
			$("#myPhoneBookWrapper").show();
		}
		else{
			$("#myPhoneBookWrapper").show();
		}
		
	}).hover(
		function(){
			$("#myPhoneBookButton img").attr("src","/_images/mpb-icon-anime.gif");
		},
		function(){
			$("#myPhoneBookButton img").attr("src","/_images/mpb-icon.gif");
		}
	);

	$("#mpbClose").mouseover(function(){
		mpbDisplayTimer = setTimeout(function(){$("#myPhoneBookWrapper").hide();},400);
	});
	$("#myPhoneBook").mouseover(function(){
		clearTimeout(mpbDisplayTimer);
	});
	
	$("#plus").click(function(){
		var cFontSize = parseInt($("body").css("font-size"));
		if( cFontSize < 15 ){
			cFontSize++;
			$("body").css("font-size",cFontSize);
		}
		setCookie("bob_fontsize",cFontSize,1,"/");
		if(pageReqSetup){ pageSetup(); }
	});
	$("#minus").click(function(){
		var cFontSize = parseInt($("body").css("font-size"));
		if( cFontSize > 9 ){
			cFontSize--;
			$("body").css("font-size",cFontSize);
		}
		setCookie("bob_fontsize",cFontSize,1,"/")
		if(pageReqSetup){ pageSetup(); }
	})
	
	//external link setup
	$("a[href^='http://http']").each(function(){
		var x = $(this).attr("href");
		x = x.replace(/http:\/\/http/,"http");
		$(this).attr("href",x);
	});
	
	$("a[href^='http://'], a[href*='.pdf']").each(function(){
		$(this).attr({
			target: "_blank"
		});
	});
	
	//background rollovers setup
	$(".rollover:not(.current)").hover(
		function () {
			var h = "0px -" + $(this).css("height");
			$(this).css("background-position",h);
		},
		function () {
			$(this).css("background-position","0px 0px");
		}
	);
	
	//mailto setup
	$(".mailto").each(function(){
		var name = $(this).attr("title");
		var html = "<a href='mailto:" + name + "'>" + name + "</a>";
		$(this).html(html);
	});
	
	//search form
	$("#searchTerm").click(function(){
		$(this).select();
	}).blur(function(){
		if ( $(this).val() == "" ){
			$(this).attr("value",'Search Business Directory');
		}
	});
	
	//newsletter form
	$("#newsletter_email_address").click(function(){
		$(this).select();
	}).blur(function(){
		if ( $(this).val() == "" ){
			$(this).attr("value",'Email');
		}
	});
	
	$("#newsletter_submit").click(function(){
		submitNewsletterEmail();
		return false;
	});
	
	$("#newsletter_form").submit(function(){
		submitNewsletterEmail();
		return false;
	});
	
	//font size
	var bob_fontsize = getCookie("bob_fontsize");
	if (bob_fontsize != null && bob_fontsize != ""){
		$("body").css("font-size",bob_fontsize+"px");
	}
	
	$(".tabBrown").append("<span class=\"brownFoot\"></span>");

});
