﻿// Don't run this for IE 5.0 and 5.5
if (!(window.showModelessDialog && !document.implementation)) {

// Do this as soon as possible
$(document).domready(function(){
	// A list of PNGs to correct for IE 6.0
	$('img, .backgroundPNG, #primaryNavigation h2, #primaryNavigation h2.opened, .searchSite, #thirdLevelLinksLine, #thirdLevelLinksBorderTopRight, #tooltip, #tooltip div, #whatIsAnandaImage, #whatIsAnandaImageCorner, #whatIsAnandaMenu').ifixpng();
	// Add a class to the body
	$('body').addClass('jsEnabled');
}); 

/////////////////////////////////////////////////////////////////////

// Do this when the document is (mostly) done loading
$(document).ready(function(){
	
	// TO DO:
	// Error handling in case AJAX call fails
	
	// Get the visitor's current date
	var today = new Date();
	var date = (today.getMonth() + 1) + "-" + today.getDate();
	
	// Get today's Daily Quote HTML
	$("#dailyQuoteRightBottom").load("http://www.ananda.org/includes/applications/daily-quote/daily-quote.php #dailyQuoteContent", {output: "complete", date: date}, function(){
		$("#dailyQuoteContainer").show();
	});

	////////////////////////////////////////
	
	// Replace images in the "featured home page area" on mouseover
	/*$("#whatIsAnandaMenu ul a").hoverIntent({
			over: function(){
				$("#whatIsAnandaLine").animate({
          			opacity: 1 
      			}, 250);
				$("#whatIsAnandaLine").attr("className", $(this).attr("className"));
				$("#whatIsAnandaLine").animate({
          			opacity: 0
      			}, 250);
			},
			out: function(){
			},
			timeout: 500,
			sensitivity: 5
	});	*/
	
	////////////////////////////////////////
	
	// Add classes to special links

	// Files
	$("a[href$='.pdf']").not("a:has(img)").addClass("pdf");
	$("a[href$='.doc']").not("a:has(img)").addClass("document");
	$("a[href$='.mp3']").not("a:has(img)").addClass("audio");
	$("a[href$='.ram']").not("a:has(img)").addClass("audio");
	$("a[href^='javascript:videoWindow']").not("a:has(img)").addClass("video");

	// External links: Anything outside of www.ananda.org
	$("#mainArticle a[href^='http://'], #articleInfoColumn a[href^='http://'], #bookBox a[href^='http://']").not("[href*='www.ananda.org']").not("[href^='http://ananda.org']").not("a:has(img)").addClass("external");
	
	
	////////////////////////////////////////
	
	// Carousels for media listings
		
	////////////////////////////////////////
	
	// Add tooltips to any link with a title
	$('a[title]').tooltip({
		delay: 250,
		showBody: "|",		
		fixPNG: true,
		showURL: false
	});
	
	// Add tooltips to .useTooltipContent links and grab HTML from the 
	// first following .tooltipContent
	$('a.useTooltipContent').tooltip({			
		bodyHandler: function() { 
			var tooltipContent = $(this).next(".tooltipContent").html(); 
			
			// If no tooltip content was found, sometimes we have to check the next item 
			// with the appropriate class, starting from the parent
			if (null == tooltipContent) {
				tooltipContent = $(this).parent().next(".tooltipContent").html();
			}
			return tooltipContent;
    	},				 
		delay: 250,
		fixPNG: true,
		showURL: false
	});
	
	// For footnotes
	/*$('a.footnoteReference').tooltip({		
		bodyHandler: function() { 
			var tooltipContent = $(this).next(".tooltipContent").html(); 
			
			// If no tooltip content was found, sometimes we have to check the next item 
			// with the appropriate class, starting from the parent
			if (null == tooltipContent) {
				tooltipContent = $(this).parent().next(".tooltipContent").html();
			}
			return tooltipContent;
		},				 
		delay: 250,
		fixPNG: true,
		showURL: false
	});*/
	
	////////////////////////////////////////

	// Disable links with href of "placeholder"
	$("a[href=placeholder]").click(function(event){
		return false;
	});
	
	////////////////////////////////////////
	 
	 // Set placeholder text in search field, and remove it when the user clicks
	 $(".searchSite input.text").val("Search").focus(function(){
	 	$(this).val("");
	 });
	 
	 // Same as above, for a section
	 $(".searchSection input.text").val("Search this section").focus(function(){
	  $(this).val("");
	 });
	 
	// While the mouse is over a tab, give it an "active" class
	 $("#tabContainer li").hover(function(){
	  $(this).addClass("active");
	 },function(){
	  $(this).removeClass("active");
	 });
	
	/////////////////////////////////////
	
	//Disable current links from being clickable
	
	/*$("#wrapup h2.current a, #wrapup li.current a, #wrapup a.current").click(function(event){			
		return false;																	  
	}*/
	
	
	////////////////////////////////////
	
	// Configuration
	var speed = 1000;
	
	// Default value
	var clickedOnNavigationLink = false;
	
	// Initialize
	// Add the opened class to the current h2	
	$("#primaryNavigation h2").addClass("closed");
	$("#primaryNavigation h2.current").addClass("opened").removeClass("closed");

	// Click effects to expand navigation
	/*$("#primaryNavigation h2 a").not(".home").click(function(event){								 
		
		// If the list is open, close it
		// Regarding the if: 
		//  - Open h2s have the class of .opened, unless the page has just loaded, in which case they will only have .current
		if ($(this).parent().hasClass("opened")) {
			//alert("test");
			$(this).parent().next("ul").hide(speed);
			$(this).parent().removeClass("opened");
			$(this).parent().addClass("closed");
		}	
		
		// If the list is closed
		else {													 
			
			// Close all other lists
			$("#primaryNavigation ul").not($(this).parent().next("ul")).hide(speed);
			$("#primaryNavigation h2").not($(this).parent()).removeClass("opened");
			$("#primaryNavigation h2").not($(this).parent()).addClass("closed");
			
			// Open this list
			//alert("test2");
			$(this).parent().next("ul").show(speed);
			$(this).parent().removeClass("closed");
			$(this).parent().addClass("opened");	
		}		
		
		// Don't send the user through the link
		return false;
	});*/
	

	// Hover effects to expand navigation
	/*$("#primaryNavigation h2 a").hoverIntent({
		over: function(event){
			$("#primaryNavigation ul").not($(this).parent().next("ul")).hide(speed);
			$("#primaryNavigation h2").not($(this).parent()).removeClass("opened");
			$("#primaryNavigation h2").not($(this).parent()).addClass("closed");
			$(this).parent().next("ul").show(speed);
			$(this).parent().removeClass("closed");
			$(this).parent().addClass("opened");
		},
		out: function(event){
		},
		timeout: 1000,
		sensitivity: 10
	});*/
	
	
	/* Check if the user clicked on the link within the h2 */
	$("#primaryNavigation a").click(function(event){
			clickedOnNavigationLink = true;
			return;
		});
	
	// Toggle navigation when side-arrow on left is clicked
	$("#primaryNavigation h2").click(function(event){
		
		/* If they clicked on the link within the h2, follow the normal behavior */
		if (clickedOnNavigationLink == true) {
			return;	
		}
		
		// Close other lists
		$("#primaryNavigation ul").not($(this).next("ul")).hide(speed);
		$("#primaryNavigation h2").not($(this)).removeClass("opened");
		$("#primaryNavigation h2").not($(this)).addClass("closed");
		
		// Open this list
		//alert("test3");
		$(this).next("ul").toggle(speed);
		$(this).toggleClass("opened");
		$(this).toggleClass("closed");
		
		return false;
		
	});
	
	
	
	// Disable header links
	// Necessary when using hover effects to expand navigation
	/*$("#primaryNavigation h2 a").not(".home").click(function(event){
		return false;
	});*/
			
});

//////////////////////////////////////////////////////////////////////////////



// Not only when the DOM is ready, but when the images have finished loading,
// important, but subtle difference to $(document).ready();
$(window).bind('load', function () {
	
	// Replace images in the "featured home page area" on mouseover
	// Hide the foreground image so that it can fade up
	$("#whatIsAnandaImage").animate({
          opacity: 0 
    }, 1);
	
	// When hovering over a link
	$("#whatIsAnandaMenu ul a").hoverIntent({
			over: function(){
			
				var imageClass = $(this).attr("className");
				
				// Set the class of the hidden foreground, to change the image there
				$("#whatIsAnandaImage").queue(function(){
					$("#whatIsAnandaImage").attr("className", imageClass);
					$(this).dequeue();
				});
				
				// Fade in
				$("#whatIsAnandaImage").animate({
          			opacity: 1 
      			}, 1000);

				// Change the background div to the same
				$("#whatIsAnandaImage").queue(function(){
					$("#whatIsAnandaLine").attr("className", imageClass);
					$(this).dequeue();
				});
				
				// Fade out the foreground (the visitor won't see any change here, but it
				// lets us fade in the next image)
				$("#whatIsAnandaImage").animate({
          			opacity: 0 
      			}, 1);
				
				
			},
			out: function(){	
			},
			timeout: 500,
			sensitivity: 5
	});
});
 

};
