$(document).ready(function(){
	
		// Add print header to top of page
		$("body").prepend("<h5 class=\"print\">CanadaWest Foundation</h5>");
		
		// Add print footer to bottom of page
		$("body").after("<h6 class=\"print\"><strong>CanadaWest Foundation</strong><br />900-105 12th Avenue SE, Calgary, AB, T2G 1A1</h5>");
	

		// Active state in main nav
		$("body#about-page #nav li:nth-child(1)").addClass('selected');
		$("body#projects-page #nav li:nth-child(2)").addClass('selected');
		$("body#publications-page #nav li:nth-child(3)").addClass('selected');
		$("body#publications-main #nav li:nth-child(3)").addClass('selected');
		$("body#news-page #nav li:nth-child(4)").addClass('selected');
		$("body#support-page #nav li:nth-child(5)").addClass('selected');
		$("body#blog-page #nav li:nth-child(6)").addClass('selected');
		$("body#contact-page #nav li:nth-child(7)").addClass('selected');

		//Add class to masthead buttons on hover
		$('#masthead .button').hover(function() {
			$(this).addClass('hover');
				}, function() {
			$(this).removeClass('hover');
		});

		//Make masthead buttons clickable
		$("#masthead .button").click(function(){
			window.location=$(this).find("a").attr("href"); return false;
		});

		//Clear value from search form
		$("#CAT_Search")
		.val("Enter search terms")
		.css("color", "#333")
		.focus(function(){
			$(this).css("color", "#007CC3");
			if ($(this).val() == "Enter search terms") {
				$(this).val("");
				}
		})
		.blur(function(){
			$(this).css("color", "#333");
			if ($(this).val() == "") {
				$(this).val("Enter search terms");
				}
		});
		
		//Truncate home page features
		if($('#feature').length) {
			$('.pane h5').jTruncate({  
				length: 25,  
				moreText: "",  
				ellipsisText: "..."
			
			});
			$('.pane p').jTruncate({  
				length: 140,  
				moreText: "",  
				ellipsisText: "..."
				});
			}
			
		//email spam protection - Example Markup: <span class="email">name[at]domain[dot]com</span>
		$('.email').each(function() {
			var $email = $(this);
			var address = $email.text()
			.replace(/\s*\[at\]\s*/, '@')
			.replace(/\s*\[dot\]\s*/g, '.');
			$email.html('<a href="mailto:' + address + '">'+ address +'</a>');
		});
		
		//stripes for publications			
		if($(".publication").length > 0) {										
  		$(".publication:even").addClass("even");
	  }
		
		//stripes for commentaries
		if($(".commentary").length > 0) {															
  		$(".commentary:even").addClass("even");
	  }

    //stripes for news releases
		if($(".newsrelease").length > 0) {
  		$(".newsrelease:even").addClass("even");
    }

    //stripes for presentations    
		if($(".presentation").length > 0) {
  		$(".presentation:even").addClass("even");
    }
		
		//stripes for what's being said	
		if($(".what").length > 0) {														
  		$(".what:even").addClass("even");
    }
		
  //stripes for past events    
		if($(".pastevents").length > 0) {															
		  $(".pastevents:even").addClass("even");
    }

    //stripes for surveys    
		if($(".surveys").length > 0) {															
  		$(".surveys:even").addClass("even");
    }

    //stripes for our latest    
		if($(".ourlatest").length > 0) {															
  		$(".ourlatest:even").addClass("even");
    }
		
	
		if ($('#commentaries').length > 0) {
			var authors = $('#commentaries').children('.commentary');
      if($('.webappsearchresults').length > 0) {
      			authors = $('#commentaries').children('.webappsearchresults').children('.commentary');
      }
      var author_str = '';
			for (i=0;i<authors.length;i++) {
				var author_orig = $(authors[i]).children('.author').html();
				var author_name = author_orig;
				var author_names = author_name.split(',');
				var author_str = '';
				for(j=0;j<author_names.length;j++) {
				  if(j == 0) { 
				    author_str = author_names[j];
					} else {
			      author_str += ', ' + author_names[j];
					}
				}
				//var author_name = author_orig;
				//var author_name = author_name.replace('.', '');
				//var author_name = author_name.toLowerCase();
				//var author_names = author_name.split(' ');
				//author_name = '';
				//for(j=0;j<author_names.length;j++) {
				//	if(j > 0) { 
				//	author_name = author_name + '-' + author_names[j];
				//	} else {
				//		author_name = author_names[j];
				//	}
				//}
				//anchor_str = '<a href="/about-us/staff/' + author_name + '"><strong>' + author_orig + '</strong></a>';
				$(authors[i]).children('.author').html(author_str);
			}
		}
			

		
		if ($('#publication-detail').length > 0) {
			//var authors = $('#commentaries').children('.commentary');
			//for (i=0;i<authors.length;i++) {
				//var author_orig = $(authors[i]).children('.author').html();
				var author_orig = $('.author').html()
				var author_name = author_orig;
				var author_name = author_name.replace('.', '');
				var author_name = author_name.toLowerCase();
				var author_names = author_name.split(' ');
				author_name = '';
				for(j=0;j<author_names.length;j++) {
					if(j > 0) { 
					author_name = author_name + '-' + author_names[j];
					} else {
						author_name = author_names[j];
					}
				}
				$('.author').html('<a href="/about-us/staff/' + author_name + '"><strong>' + author_orig + '</strong></a>');
		}

/*//Publication Search
		if($('#blog-page').length) {
			if( $('#blog-page .trackbacks').filter(":visible")){
			   $('img').show();
			
			}
		}*/
		
		
				
		$(".event-details").hide();
	
		$(".event-trigger").click(function(){
			$(this).toggleClass("active").prev().slideToggle("slow");
			$(this).text($(this).text() == 'Show event details' ? 'Hide event details' : 'Show event details');
		});
			
	

});
