// javascript for leolaleblanc.com

// these functions are called when each page loads.
window.onload = function() {
 if (document.getElementById('s')) { loadsearch(); }
 if (document.getElementById('slideshow')) { startslides(); }
}



// initialize jquery for wordpress.
jQuery(document).ready(function($){

 // initialize drop-down menus.
 $('div.dropdown').hover(
  function() {
   //$(this).find('ul').slideDown(100); // removed animation as it can cause problems
   $(this).find('ul').show();
  },
  function() {
   //$('.dropdown ul').slideUp(100);
   $('.dropdown ul').hide();
   $('div.dropdown ul').css("z-index", "50");
  });


 // initalize galleries
 $('.postwithgallery').each(function() {
  galleryid = this.id;
  $(this).find('.panel').each(function(i){$(this).attr('id', galleryid + '-panel' + ++i);});
  $(this).find('.thumb').each(function(i){$(this).attr('id', galleryid + '-thumb' + ++i);});
  $(this).find('.loading').hide();
  $(this).find('#'+galleryid+'-thumb1').addClass('thumb-lit');
  $(this).find('#'+galleryid+'-panel1').show();
  
  $(this).find('.thumb').mouseenter(function(){
    galleryid = '#gallery-'+this.id.split('-')[1];
    panelid = '#'+this.id.replace('thumb', 'panel');
    $(galleryid).find('.thumb-lit').removeClass('thumb-lit');
    $(this).addClass('thumb-lit');
    $(galleryid).find('.panel:visible').hide();
    $(panelid).show();
    })
 })
 
 // hides final separator from categories list
 $('.trail').each(function() {
  separators = $(this).find('.separator');
  lastseparator = separators.eq(separators.length - 1);
  $(lastseparator).hide();
 })
 $('.postcategories').each(function() {
  separators = $(this).find('.separator');
  lastseparator = separators.eq(separators.length - 1);
  $(lastseparator).hide();
 })

 // add 'pdf-link' class to PDF links
 $("a[href$='.pdf']").addClass('pdf-link');
 
 // add 'button' class to buttons
 $('input[type="submit"], input[type="reset"], input[type="button"], button').addClass('button');

 // add 'rel="stylesheet"' to jpg links (so they open with slimbox 2)
 $("a[href*=.jpg]").attr('rel','lightbox');

});



// initialize search box
function loadsearch() {
 var searchbox = document.getElementById('s');
 searchbox.onclick = function() {
  if (searchbox.value == 'search') {
   searchbox.value = '';
   searchbox.className = 'searchboxactive';
  }
 };

 searchbox.onblur = function() {
  if (searchbox.value == '') {
   searchbox.value = 'search';
   searchbox.className = 'searchboxinactive';
  }
 };
}



// creates a slideshow from <ul id="slideshow">, which should contain <li>s with <img>s.
function startslides() {
 // number of seconds between slides; use 1 for quick testing, 2 - 4 for working website
 slideshow_seconds = 4;

 // controls fading speed (use 50-90) and smoothness; smoothness was .05, but slides did not quite reach 100% opacity.
 slideshow_fadingspeed = 60; // milliseconds
 slideshow_smoothness = .05;

 // build slide array and declare global variables.
 slides = new Array();
	slides = document.getElementById("slideshow").getElementsByTagName("li");
 currentslide = 0;
	slideshow_can_continue = true;

	// set up mouse events and initial slide opacity.
	for(i=1; i<slides.length; i++) {
  slides[i].xOpacity = 0;
  slides[i].title = "Slideshow paused.";
  slides[i].onmouseover = function() { pauseslides(); };
  slides[i].onmouseout = function() { resumeslides(); };
 }
	slides[currentslide].style.display = "block";
	slides[currentslide].xOpacity = .99;

 //schedule fade to start after designated time
 slide_fading = setTimeout(fadeslide,(slideshow_seconds*1000));
}


function fadeslide() {
 nextslide = slides[currentslide+1] ? currentslide+1 : 0;
 slides[nextslide].style.display = "block";

 slides[currentslide].xOpacity -= slideshow_smoothness;
	slides[nextslide].xOpacity += slideshow_smoothness;

 setopacity(slides[currentslide]);
	setopacity(slides[nextslide]);

 // this if / else sequence provides an opportunity to pause the slideshow.
 // if it is okay to continue and the current slide has fully faded, undisplay it and continue fading the next slide.
 if (slideshow_can_continue && (slides[currentslide].xOpacity <= 0)) {
 	slides[currentslide].style.display = "none";
 	currentslide = nextslide;
 	slide_fading = setTimeout(fadeslide,(slideshow_seconds*1000));
 // else if it is not okay to continue and the current slide has fully faded, stop.
 } else if (!slideshow_can_continue && (slides[currentslide].xOpacity <= 0)) {
  return;
 // else the slide has not full faded, so continue to fade.
 } else {
	 slide_fading_out = setTimeout(fadeslide,slideshow_fadingspeed);
 }
}


// crop opacity at .99, and translate "xOpacity" variable into CSS values.
function setopacity(obj) {
	if(obj.xOpacity > .99) {	obj.xOpacity = .99; }
	obj.style.opacity = obj.xOpacity;
	obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	//obj.innerHTML = obj.style.opacity; // useful for testing
}


// pause slideshow
function pauseslides() {
 clearTimeout(slide_fading);
 //clearTimeout(slide_fading_out); // this allows slideshow to be paused mid-fade.
 slideshow_can_continue = false;
}


// resume slideshow
function resumeslides() {
 clearTimeout(slide_fading);
 clearTimeout(slide_fading_out);
 slideshow_can_continue = true;
 fadeslide();
}

