$(document).ready(function(){	
	
	/* the nav show / hide states */	
	$("#nav li:eq(0)").css({"margin-left":"0px"});
	$("#nav li:not(.subnav li)").hover(function(){
		$(".open").find(".subnav").hide();
		$(this).addClass("hoverli").find(".subnav").show();
	}, function(){
		$(this).removeClass("hoverli").find(".subnav").hide();
		$(".open").find(".subnav").show();
	});
	
	/* the popup */
	createPopUp = function(popupheading, popupbody, $caller){
		
		$(".popup, #popupbg").remove();
		var homedir = $("#homedir").val();
		
		$.ajax({
			type: "POST",
			data: {"popupbody":popupbody, "popupheading":popupheading, "homedir":homedir},
			url: homedir+"_lib/php/popup.php",
			success: function(data) {
				$("body").prepend(data);
				
				var h = $(".popup").height();
				var topsubtract = "-"+(h+50);
				var wh = $(window).height();			
				var scrolledY = document.body.scrollTop || document.documentElement.scrollTop || self.pageYOffset;
				var screenHeight = document.body.clientHeight || document.documentElement.clientHeight || self.innerHeight;
				var top = (wh > h)?scrolledY + ((wh - h)/2):$caller.offset()['top'] - (wh/2);
				
				$("#popupbg").click(function(){$(".popupclose").trigger("click");}).css({"height":screenHeight}).fadeTo(300,0.3);
				$(".popup").show().css({"top":top});
				
				$(".popupclose").click(function(){
					$(".popup").animate({"top":topsubtract},300, function(){
						$(this).remove();
					});
					$("#popupbg").hide().remove();
					return false;
				});
				
				// close on esc
				$(document).keyup(function(e) { 
						if (e.which == 27) { $(".popupclose").trigger("click"); }
				});				
				
				
			}
		});
	}
	
	/* the waypoints */
	initWayPoints = function(){
		
		$start = $('.followwrapper').parent().parent();
		$start.waypoint(function(event, direction) {
		}, {
			offset: '-100%'
		}).find('.followwrapper').waypoint(function(event, direction) {
			if(direction === "down"){
				$(this).parent().parent().parent().addClass('sticky');
			}else{
				$(this).parent().parent().parent().removeClass('sticky');
			}
			event.stopPropagation();
		});

		
		$('.bodyblock').waypoint(function(event, direction) {
	
			var curr = $(this).attr('id');
			if(direction === "up"){
				if($(this).prev().index() != 1){curr = $(this).prev().attr('id');}
			}
			
			if(curr){ curr = curr.substring(0, curr.indexOf("block"));}
			
			$('.followwrapper ul li').find('.selectedsub').removeClass('selectedsub');
			$('.followwrapper ul li').find('.nolinkcolor').removeClass('nolinkcolor');
	
			$.each($('.followwrapper ul li'), function(){
				t = $(this).text();
				t = t.replace(/\s/g, "");
				t = t.toLowerCase();
				if(curr){curr = curr.toLowerCase();}
				
				if(t == curr){
					
					$(this).find('a').addClass('nolinkcolor');
					if(t == "press"){	
						
						//$(this).find('a').addClass('selectedsub');
						
						// add subsub nav
						$('.subsubfloatblock').remove();
						pos = $(this).offset();
						$cont = $('<div></div>').addClass('subsubfloatblock')
																		.css({'top':0,
																		'left':pos.left}).appendTo($('body'));												
						$l = $('<a></a>').attr({'href':'../downloads/PayvmentMediaKit.6.14.11.zip'})
														 .addClass('subsubnavbutton')
														 .text('Download our media kit')
														 .appendTo($cont);
						w = $('.subsubfloatblock').width();
						$('.subsubfloatblock').css({"margin-left":((w-(w*2))/2)}).fadeIn('fast');
						// / ad subsubnav
						
					}else{
						$('.subsubfloatblock').fadeOut("fast", function(){$(this).remove()});
					}
				}	
			})
			
	
			event.stopPropagation();
		}, {
			triggerOnce: false,
			offset: '0%'
		});

	}

	/* clone the nav into the follow nav */
	cloneNav = function(){
		$cpn = $(".open").find(".subnav").clone().removeClass("subnav");
		$cpn.appendTo(".followwrapper");
	}	
	
});
