/*_________________________________________________________________________

    created       : 4/01/11
    last updated  : 4/28/11
_________________________________________________________________________*/
	//jQuery.noConflict();     

//  PAGE VARIABLES
	var homePage;
	var admin;
	
	$(document).ready(function(){
		homePage = $('body.home').length;
		admin = $('#adminbar').length;
	});


/*
_________________________________________________________________________*/
//  UNIVERSAL JS (EVERY PAGE)

	//  JAVASCRIPT WARNING
		$(document).ready(function(){
			$("#javascriptWarning").hide();
		});
		
	//  PRIMARY NAV	
		/*  pause before animation  */
			$.fn.pause = function(duration) {
			    $(this).stop().animate({ dummy: 1 }, duration);
			    return this;
			};
	
		/*  slide open nav  */
			$(document).ready(function(){
				/*  variables  */
					var navBarHeight = 0; /* "nuff said */
					var listHeight = 0; /* height of longest subNav (computed below) */
					var linkHeight = 0; /* creates padding at bottom based on current (elastic) size of a given subnav link */
					var navHeight = 0; /* total height (computed below) */
				/*  mouse enter  */
					$("#primaryNavContent").mouseenter(function() {
						if ($(window).width() >= 800) {
							/*  determine navHeight (nav bar + longest <li> + one link height for padding)  */
								navBarHeight = $("#primaryNav ul li a").height();
									
								$("#primaryNav ul li").children().each(function() {
									if($(this).height() > listHeight) {
										listHeight = $(this).height();
									};
								});
								
								linkHeight = $("#primaryNav ul li li").children().height();
							
								navHeight = navBarHeight + listHeight + linkHeight;
	
							/*  animate	 */ 
								$("#primaryNav").stop().pause(150).animate({ height: navHeight}, 300, "easeOutQuad");
								$("#primaryNavContent").stop().pause(150).animate({ height: navHeight}, 300, "easeOutQuad");
							
						}
					}).mouseleave(function() {
						if ($(window).width() >= 800) {
							/*  animate	 */
								$("#primaryNav").stop().pause(100).animate({ height: $("#primaryNavBkgrd").height()}, 300, "easeOutQuad");
								$("#primaryNavContent").stop().pause(100).animate({ height: $("#primaryNavBkgrd").height()}, 300, "easeOutQuad");
						}
					});
				
				/*  resize nav if window is resized after sliding effect  */
					if (! $.browser.msie) {
						$(window).resize(function(){
						    var h = $("#primaryNav ul li").children().height();
						    var w = $(window).width();
						    
						    if (w >= 600) {
						    	$("#primaryNav").animate({ height: h}, 50);
						    } else {
						    	$("#primaryNav").animate({ height: h*2}, 50);
						    }
						});
					}
			});
			
		/*  limit nav to 6  */
			$(document).ready(function(){
				$("#primaryNav .mainNav").children().each(function() {
					if ($(this).index() > 5) {
						$(this).hide();
					}
				});
			});
			
	//  MEMBER LINKS
		$(document).ready(function() {
			//  pull out member menu
				$('#memberLinks .links').load('/members #secondaryNav .sectionMenu');
			//  click
				$("#utilityLinks .memberLinks, #memberLinks .closeBtn").click(function(e) {
					e.preventDefault();
					$("#memberLinks").slideToggle(200, "easeOutQuad");
				});
		});


/*
_________________________________________________________________________*/
//  HOME PAGE ONLY JS
	$(document).ready(function() {
	if (homePage) {

		//  LOGO (HOME PAGE)
			$(document).ready(function(){
				$('.home #logo a img').replaceWith('<img src="/Websites/nyapc/templates/nyapc/images/logoHome.png" alt="The New York Avenue Presbyterian Church, an Inclusive, Justice-Seeking Community"/>');
			});
			
		//  WORSHIP SLIDESHOW
			function worshipSlideshow(currentPhoto) {
				var numberOfPhotos = $("#worshipSlideshow .element").length;
					currentPhoto = currentPhoto % numberOfPhotos;
				var previousPhoto = 0;
					if (currentPhoto == 0) {
						previousPhoto = numberOfPhotos - 1;
					} else {
						previousPhoto = currentPhoto - 1;
					}
				
				$("#worshipSlideshow .element").eq(currentPhoto)
					.fadeIn(1800, "easeOutQuad", function() {
						
						/*  hide previousPhoto  */
							$("#worshipSlideshow .element").eq(previousPhoto).hide();
						
						/*  re-order z-index  */
							$("#worshipSlideshow .element").each(function(i) {
								$(this).css("zIndex", ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos);
							});
			
						setTimeout(function() {worshipSlideshow(++currentPhoto);}, 6000);
					});
			}
			
			$(document).ready(function() {	
				/*  All .element(s) must have position:relative in admin mode  */
					if (admin) {
						$("#worshipSlideshow .element").css({"position" : "relative", "display" : "block"});
					} else if ($("#worshipSlideshow .element").length > 1) {
						/*  create invisible photo to fill frame fluidly  */
							$("#worshipSlideshow .element:first")
								.clone()
								.removeClass("element")
								.addClass("invisiblePhoto")
								.css({"position":"relative", "display":"block", "opacity":0})
								.prependTo("#worshipSlideshow .frame .frame");
							
						/*  animate  */
							worshipSlideshow(0);
					}
			});
		
		//  WORSHIP INFO	
			/*  worship tabs  */
				$(document).ready(function() {
					$("#worshipTabs").tabs({ fx: { height: "toggle", opacity: "toggle" } });
				});
			
			/*  limit content adders  */
				$(document).ready(function() {
					/*  worshipLinks  */
						if ($("#worshipLinks .actions").length) {
							$("#worshipLinks .inlineContentAdder").hide();
						
						}
				});
		
		//  FEATURES
			/*  limit content adders  */
				$(document).ready(function() {
					/*  featureThumb  */
						$("#features .regionBoundary").each(function() {
							if ($(this).find(".actions").length) {
								$(this).find(".inlineContentAdder").hide();
							}
						});
				});
			
			/*  rollover state  */
				$(document).ready(function() {
					$("<span class='rollover'>&nbsp;</span>")
						.insertBefore("#features .featureThumb .textElement img");
					
					if (!admin) {
						$("#features .feature").hover(
							/*  mouseenter  */
								function() {
									$(this).find(".rollover")
										.stop(true, true)
										.fadeIn(200, "easeOutQuad");
								},
							/*  mouseleave  */
								function() {
									$(this).find(".rollover")
										.stop(true, true)
										.fadeOut(200, "easeOutQuad");
								}
						)
						
						$("#features .feature").click(function(){
							var url = $(this).find("h3 a").attr('href');
							    window.location = url;
						});
					}
				});
				
		//  AVEnews (HOME)
			$(document).ready(function() {
				if (admin) {
					/*  show warning  */
						$('#aveNewsWarning').show();
					
					/*  limit content adders  */
						if ($('#aveNewsContent .actions').length) {
							$('#aveNewsContent .inlineContentAdder').hide();
						}
					/*  hide edit buttons  */
						$('#aveNewsContent .blogList h2').each(function() {
							$(this).find('a:first').hide();
						});
					/*  hide "Write New Post" option  */
						$('#aveNewsContent .actions li ul li:first').hide();
				}
				
				/*  move body below date  */
					$('#aveNewsContent .blogList .postBody').each(function() {
						$(this).appendTo($(this).parent());
					});
				
				/*  pull author name into blogList  */
					$('#aveNewsContent .blogList li').each(function() {
						var url = $(this).find('.postTitle a').attr('href');
						$(this).find('.postDate').replaceWith($('<span class="newPostViewInfo"></span>'));
						$(this).find('.newPostViewInfo').load(url + ' .postViewInfo');
					});
				
				/*  add bullets in .postInfo  */
					$('<span class="bullet">&bull;</span>')
						.insertAfter($('#aveNewsContent .blogsElement .blogList .postInfo .postDate'));
				
				/*  add .first to first <li>  */
					$('#aveNewsContent .blogList li:first').addClass('first');
			});
		
		//  COMING EVENTS (HOME)
			$(document).ready(function() {
				/*  get line height  (will function em based at least on page load)  */
				var lineHeight = $('.home #comingEventsContent .eventsList li:first .eventDate').height();
				var heightLimit = lineHeight * 2;
								
				/*  month array  */
				var month=new Array(12);
				    month[0]="Jan.";
				    month[1]="Feb.";
				    month[2]="Mar.";
				    month[3]="Apr.";
				    month[4]="May";
				    month[5]="Jun.";
				    month[6]="Jul.";
				    month[7]="Aug.";
				    month[8]="Sep.";
				    month[9]="Oct.";
				    month[10]="Nov.";
				    month[11]="Dec.";
				
				/*  pull content in from .event  */
					if (!admin) {
						$('#comingEventsContent .eventsList li').each(function() {
							var url = $(this).find('a').attr('href');
							$(this).load((url + ' .event'), function() {
								
								/*  Date Badge  */
									var dateTxt = $(this).find('.eventDate').text();
									var date = new Date(dateTxt);
									
									$(this).prepend('<div class="dateBadge"><span class="date"><span class="day">' + date.getDate() + '</span><span class="month">' + month[ date.getMonth()] + '</span></span></div>');
								
								/*  Hide Back Btn  */
									$(this).find('.eventBack, .eventDate').hide();
									
								/*  expand / contract  */
									var parent = $(this).find('.eventBody');
									var target = $(parent).find('p:last');
									var totalHeight = $(target).height();
									
									if (totalHeight > heightLimit) {
										$(target).css({
											'max-height':heightLimit, 
											'overflow':'hidden'});
										$(this).find('.eventBody').append('<div class="toggle">more...</div>');
									}
									
									$(parent).find('.toggle').click(function() {
										$(target).stop();
										if ($(target).height() < totalHeight) {
											$(target)
												.css({'max-height':totalHeight})
												.css({'height':heightLimit})
												.animate({height:totalHeight}, 200, "easeOutQuad");
											$(this).text('less...');
										} else {
											$(target).animate({height:heightLimit}, 200, "easeOutQuad");
											$(this).text('more...');
										}
									});
							});
						});
					}
					
				/*  misc.  */
					$('#comingEventsContent .eventsList li:last').addClass('last');
					$('#comingEventsContent .eventsList .eventBody p:first').addClass('first');

			});
	
/*
_________________________________________________________________________*/
//  INTERIOR PAGE JS (EVERYTHING BUT HOME)

	} else {

	//  BREADCRUMBS
		$(document).ready(function() {
			var numberOfLinks = $("#breadcrumbs a").length;
			
			if ($("#secondaryNav .sectionMenu .selectedPage").length) {
				$("#breadcrumbs a").eq(numberOfLinks-2).prependTo($("#breadcrumbs"));		
			} else {	
				$("#breadcrumbs a").eq(numberOfLinks-1).prependTo($("#breadcrumbs"));
			}
			$("#breadcrumbs a").prepend('<span class="chev">&#0171;</span>');
			$("#breadcrumbs .bc").hide();
		});
	
	//  SECTION TITLE
		$(document).ready(function() {
			if ($("#secondaryNav .sectionMenu .selectedPage").length) {
				$("#sectionName h3").html($("#sectionName .selectedPageParent:last").text());
			} else {	
				$("#sectionName .sectionTitle a").hide();
			}
		});
		
	//  SIDE IMAGE
		$(document).ready(function() {
			if (!$("#sideImage .element").length && !$("#adminbar").length) {
				$("#sideImage").hide();
			}
		});
			
	//  FOOTER
		/*  pull footer welcome text from home page  */
			$(document).ready(function() {
				$("#footer").load('/home #footerContent', function() {
					$("#footer").find('.actions, .inlineContentAdder').hide();
				});
				
			});
			
	//  BLOGS
		/*  General  */
			$(document).ready(function() {
				/*  move body below date  */
					$('#mainContent .blogList .postBody, #aveNewsContent .blogList .postBody').each(function() {
						$(this).appendTo($(this).parent());
					});
				
				/*  pull author name into blogList  */
					$('.blogList li').each(function() {
						var url = $(this).find('.postTitle a').attr('href');
						$(this).find('.postDate').replaceWith($('<span class="newPostViewInfo"></span>'));
						$(this).find('.newPostViewInfo').load(url + ' .postViewInfo');
					});
				
				/*  add bullets in .postInfo  */
					$('<span class="bullet">&bull;</span>').insertAfter($('#mainContent .blogsElement .blogList .postInfo .postDate'));
			});
	
	//  AVEnews (INTERIOR)
		$(document).ready(function() {
			$('.avenews #sectionName h3').text('AVEnews');
			$('.avenews #pageName').html('<span class="title">AVEnews</span><span class="divider">|</span><span class="subtitle">Blogs, Articles, and Meditations from NYAPC</span>');
			
			$('#blogMenu').find('.inlineContentAdder').hide();
		});
		
	//  CALENDAR (INTERIOR)
		/*  General  */
			$(document).ready(function() {
				/*  month array  */
				var month=new Array(12);
				    month[0]="Jan.";
				    month[1]="Feb.";
				    month[2]="Mar.";
				    month[3]="Apr.";
				    month[4]="May";
				    month[5]="Jun.";
				    month[6]="Jul.";
				    month[7]="Aug.";
				    month[8]="Sep.";
				    month[9]="Oct.";
				    month[10]="Nov.";
				    month[11]="Dec.";
				
				/*  pull content in from .event  */
					if (!admin) {
						$('.eventsList li').each(function() {
							var url = $(this).find('a').attr('href');
							$(this).load((url + ' .event'), function() {
								
								var dateTxt = $(this).find('.eventDate').text();
								var date = new Date(dateTxt);
								console.log($(this).html());
								$(this).prepend('<div class="dateBadge"><span class="date"><span class="day">' + date.getDate() + '</span><span class="month">' + month[ date.getMonth()] + '</span></span></div>');
								
								$(this).find('.eventBack, .eventDate').hide();
							});
						});
					}
			});		
	
	} /* END INTERIOR ONLY JS  */
	});	
	
