function set_cookie( name, value, exp_y, exp_m, exp_d, path, domain, secure )	{
	  var cookie_string = name + "=" + escape ( value );
	  if ( exp_y ){
		var expires = new Date ( exp_y, exp_m, exp_d );
		cookie_string += "; expires=" + expires.toGMTString();
	  }
    	cookie_string += "; path=" + escape ( path );
	  if ( domain )
			cookie_string += "; domain=" + escape ( domain );
	  if ( secure )
			cookie_string += "; secure";
	  document.cookie = cookie_string;
	}

function readCookie(cookieName) {
	 var theCookie=""+document.cookie;
	 var ind=theCookie.indexOf(cookieName);
	 if (ind==-1 || cookieName=="") return ""; 
	 var ind1=theCookie.indexOf(';',ind);
	 if (ind1==-1) ind1=theCookie.length; 
	 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

var rotating = true;

$(document).ready(function() {
		//$(".side-nav h4.yellow a:contains('Property Search')").find(".side-nav ul").prepend("<li><a href='http://maps.roktech.net/nassauflpa/#'>Search By Map</a></li>");


		// switch hash links to javascript void links
		$("a[href='#']").attr("href","javascript:;");
		// highlight the current navigation item
		$(".navigation li a").each(function(){if(this.href == window.location){$(this).parents("li").addClass("current");}});
		// IE6 IS DUMB
		$(document).pngFix();
				
		// BACK TO TOP
		$('a.top').click(function(){
			 $('html, body').animate({scrollTop: '0px'}, 300);
			 return false;
		});
		
		
		//////////////////////
		// HEADER
		//////////////////////
		$(".map_search_link").click(function(e) {
			var cookieValue = readCookie('map_agreed');
			if(!$(this).hasClass('isAgreed') && cookieValue != 1){
				$("#acceptPop").dialog({modal:true,autoOpen:false,width:500,height:365});
					$("#acceptPop").load(baseURL + "pop.accept.php",function() {
					$("#acceptPop").dialog("open");
					$("#acceptPop a[title='Accept']").attr("href","#").live("click",function() {
						$(".map_search_link").addClass('isAgreed');
						set_cookie('map_agreed','1','2019','1','1','','','');
						window.open($(".map_search_link").attr('href'));
						return false;
					});
				});
				return false;
			}
		});
		
		
		//////////////////////
		// HOME PAGE
		//////////////////////
		
		if(template=="home") {		
			// TABS
			$('.tabs ul li').hover(function() {
				$(this).addClass('active')
				}, function() {
				$(this).removeClass('active');
			});
			$('.tabs  ul  li .tabTrigger').live("click", function() {
				$('.tabs ul li').removeClass("current").find('ul').hide();
				$(this).parent().addClass('current').find('ul').show();
			});
		
			// SEARCH
			$('.search ul li').hover(function() {
				$(this).addClass('active')
				}, function() {
				$(this).removeClass('active');
			});
			
			// terms and conditions
			$(".readterms, #nav #second li a").live("click",function() {
				if(!$(this).hasClass('map_search_link')){
					$("#acceptPop").dialog({modal:true,autoOpen:false,width:500,height:365});
					$("#acceptPop").load(baseURL + "pop.accept.php",function() {
						$("#acceptPop").dialog("open");
					});
				}
			});
		
			$(".readterms, #nav #second li a").live("click",function() {
				var cookieValue = readCookie('map_agreed');
				if(cookieValue != 1){
					var link = $(this);
					$("#acceptPop").dialog({modal:true,autoOpen:false,width:500,height:365});
						$("#acceptPop").load(baseURL + "pop.accept.php",function() {
						$("#acceptPop").dialog("open");
						$("#acceptPop a[title='Accept']").attr("href","#").live("click",function() {
							set_cookie('map_agreed','1','2019','1','1','','','');
							window.location = $(link).attr('href');
							return false;
						});
					});
					
					return false;
				};
			});

			$("#frmQuick").submit(function(e) {
				if(!$(this).find("#terms").is(":checked")) {								  
					e.preventDefault();
					$("#acceptPop").dialog({modal:true,autoOpen:false,width:500,height:365});
						$("#acceptPop").load(baseURL + "pop.accept.php",function() {
						$("#acceptPop").dialog("open");
						$("#acceptPop a[title='Accept']").attr("href","#").live("click",function() {
							$.get("/do.accept.php?ajax=1",function(msg) {
								$("#terms").attr("checked","checked");
								$("#frmQuick").submit();
							});
						});
					});
				}
		  });
		  
			$("#frmLocation").submit(function(e) {
				if(!$(this).find("#termsLoc").is(":checked")) {		
					e.preventDefault();
					$("#acceptPop").dialog({modal:true,autoOpen:false,width:500,height:365});
						$("#acceptPop").load(baseURL + "pop.accept.php",function() {
						$("#acceptPop").dialog("open");
						$("#termsAccept").live("click",function() {
							$("#termsLoc").attr("checked","checked");
							$("#acceptPop").dialog("close");
							//$.get("/do.accept.php?ajax=1",function(msg) {
								
								$("#frmLocation").submit();
							//});
						});
					});
				}
		  });
			
		$("#termsAccept").live("click",function() {
			$("#acceptPop").dialog("close");
		});
		  
		  // SLIDESHOW
			$(".slide").css({position:"absolute",top:0,left:12}).show();
			$(".slide:not(0)").fadeOut(0);
			
			$(".slide:first").fadeIn(0);
			$(".controls a:first").addClass("current")
			$('.controls a').click(function(e,i) {
				currentSlide = $(this).index()+1;
				$(".slide:not('#slide_" + currentSlide + "')").fadeOut();					
				$("#slide_" + currentSlide).fadeIn();
				$(".controls .current").removeClass('current');
				$(".controls a").eq(currentSlide-1).addClass('current');	
				rotating = false;
			});	
			var currentSlide = 2;
			 setInterval(function() {
				if(rotating) {
					$(".slide:not('#slide_" + currentSlide + "')").fadeOut();					
					$("#slide_" + currentSlide).fadeIn();
					$(".controls .current").removeClass('current');
					$(".controls a").eq(currentSlide-1).addClass('current');	
					currentSlide = currentSlide<$(".slideshow .slide").length?currentSlide+1:1;
				 }
			},5000);
		  
		}
			
			
		// highlight the current navigation item
		$(".navigation li a").each(function() {
			if(this.href == window.location) {
				$(this).addClass("current");	
			}
		});
		$("a[title='Where to file and What is Available']").attr("href","javascript:;");
		
		$(".contact, #contactUsLink").live("click",function() {
			$("#popContact").remove();
			$("body").append("<div id='popContact'></div>");
			$("#popContact").load(baseURL + "pop.contact.php",function() {
				$("#popContact").dialog({modal:true, width:440,height:410});
			});
		});
		
		$('#closeLink').live('click',function() {
			$('#popContact').dialog('close');								  
		});

		// NAVIGATION
		$('ul#nav li').hover(function() {
			$(this).addClass('active')
			}, function() {
			$(this).removeClass('active');
		});
		
		$('.navigation li').hover(function() {
			$(this).find('ul:first').show()
			}, function() {
			$(this).find('ul:first').hide();
		});
			
		// sitemap dialog
		$("#sitemapPop").dialog({modal:true,autoOpen:false,width:500});
		$(".sitemapLink").live("click",function() {
			$("#sitemapPop").load(baseURL + "resources/pop.sitemap.php",function() {
				$("#sitemapPop").dialog("open");																 
			});
		});
			
		// events/links tabs
		$(".tabs li a[href='javascript:;']").live("click",function() {
			$(".tabs li").removeClass("current").removeClass("active");
			$(this).parent().addClass("current").addClass("active");
			$(".tabs li ul").hide();
			$(this).parent().find("ul").show();
			
		});
		
		$(".search > ul > li .tabTrigger").live("click",function() {
			$(".search .tabContent").hide().parent().removeClass("current");
			$(this).parent().find(".tabContent").show().parent().addClass("current");	
		});		
		
		
			
	});
