﻿// 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.closed, #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 dateWithoutYear = (today.getMonth() + 1) + "-" + today.getDate();
	var date = "2008-" + (today.getMonth() + 1) + "-" + today.getDate();
	
	var moreHTML = '<a class="viewMore" title="Expand to see the full saying" href="/inspiration/daily/" onClick="pageTracker._trackEvent(\'DailyInspiration\', \'More\', \'' + dateWithoutYear + '\');">(More...)</a></span>';
	//var lessHTML = '<p class="viewLess"><a href="/inspiration/daily/" title="Collapse the saying" onClick="pageTracker._trackEvent(\'DailyInspiration\', \'Less\');">(Less...)</a></p>';
	var lessHTML = '<p class="viewLess"><a href="/inspiration/daily/" title="Collapse the saying" onClick="pageTracker._trackEvent(\'DailyInspiration\', \'Less\', \'' + dateWithoutYear + '\');">(Less...)</a></p>';
	
	// Get today's Daily Quote HTML, only on the home page
	if ( $("#home").length > 0 ) {
		
		$("#dailyQuoteRightBottom").load("/includes/applications/daily-quote/daily-quote.php #dailyQuoteContent", {output: "complete", date: date}, function(){
		
		// Hide what needs to be hidden by default
		$("#dailyQuoteContainer span.more, #dailyQuoteContainer div.more").hide();
		
			// Add the "More..." stuff in
			// If span.more exists, add the moreHTML link in front of it
			if ( $("#dailyQuoteContainer span.more").length > 0 ) {
				$("#dailyQuoteContainer span.more").before(moreHTML);
				// Add link to view less
				$("#dailyQuoteContainer .attribution").before(lessHTML);
			}
			// If div.more exists and NOT span.more, add the moreHTML link in front of it
			else if ( $("#dailyQuoteContainer div.more").length > 0 ) {
				$("#dailyQuoteContainer div.more").before(moreHTML);
				// Add link to view less
				$("#dailyQuoteContainer .attribution").before(lessHTML);
			}
			
			// Show the Daily Quote 
			$("#dailyQuoteContainer").show();
			
			// When a user clicks to view more
			$("#dailyQuoteContainer a.viewMore").click(function(event){	
																
				$("#dailyQuoteContainer span.more, #dailyQuoteContainer div.more").show();
				$("#dailyQuoteContainer .viewLess, #dailyQuoteContainer .viewLess a").show();
							
				$(this).hide();
				
				// Don't follow the link
				event.preventDefault();
			});
			
			// When a user clicks to view less
			$("#dailyQuoteContainer .viewLess a").click(function(event){	
																
				$("#dailyQuoteContainer span.more, #dailyQuoteContainer div.more").hide();
				$("#dailyQuoteContainer .viewMore").show();
				
				$(this).hide();
				
				// Don't follow the link
				event.preventDefault();
			});
			
			// Tooltips have to be recreated for this block of content
			$('#dailyQuoteContainer [title]').tooltip({
				delay: 250,
				showBody: "|",		
				fixPNG: true,
				showURL: false
			});
					
		}); // Close main Daily Quote function
		
	} // close if home page
	
	
	
	

	////////////////////////////////////////
	
	// 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)").not("a:empty").addClass("pdf");
	$("a[href$='.doc']").not("a:has(img)").not("a:empty").addClass("document");
	$("a[href$='.mp3']").not("a:has(img)").not("a:empty").addClass("audio");
	$("a[href$='.ram']").not("a:has(img)").not("a:empty").addClass("audio");
	$("a[href$='.m3u']").not("a:has(img)").not("a:empty").addClass("audio");
	$("a[href^='javascript:videoWindow']").not("a:has(img)").not("a:empty").addClass("video");

	// External links: Anything outside of www.ananda.org
	$("a[href^='http://']").not("[href*='www.ananda.org']").not("[href^='http://ananda.org']").not("a:has(img)").not("a:empty").addClass("external");
	
	// Add tracking code for direct downloads from within ananda.org
	$("a[href^='/mp3/'], a[href^='http://www.ananda.org/mp3/'], a[href$='.pdf'], a[href$='.doc'], a[href$='.ram'], a[href$='.mp3'],   a[href$='.swf']").not(".external").bind("click", function (arr) {																																												
			// Get the address
			var href = $(this).attr("href");
			// Standardize it
			href = href.replace("http://www.ananda.org/","/");
			// Add the Google Analytics tracking code, using the actual address
			pageTracker._trackPageview(href);
	});
	
	// Add tracking code for all links to other websites
	$(".external").bind("click", function (arr) {		
			// Get the address
			var href = $(this).attr("href");
			// Standardize it
			href = href.replace("http://","/external/");
			// Add the Google Analytics tracking code, using the actual address
			pageTracker._trackPageview(href);
	});
	
	
	////////////////////////////////////////
	
	// Carousels for media listings
		
	////////////////////////////////////////
	
	// Add tooltips to any link with a title
	$('[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
		$(this).next("ul").toggle(speed);
		$(this).toggleClass("opened");
		$(this).toggleClass("closed");
		
		// Track the click with Google Analytics
		var h2Label = $(this).text();
		
		if ( $(this).hasClass("opened") ) {
			pageTracker._trackEvent('PrimaryNavigation', 'ArrowOpened', h2Label);
		}
		
		if ( $(this).hasClass("closed") ) {
			pageTracker._trackEvent('PrimaryNavigation', 'ArrowClosed', h2Label);
		}
		
		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
	});
});
 

};
