/********* CUSTOM SCRIPTS FOR JVLPHOTO.COM *************************/
/********* Written by Greg Pettit (monkey-house.ca) ****************/
/********* Contains: Accordion, Quotator **************************/

/***************** Accordion ******************/
/* built on an implementation originally found at stemkoski.com */
jQuery(document).ready(function() { 

  //ACCORDION BUTTON ACTION
  $('h3.accordionButton').click(function() {

    if($(this).next().css('display') == 'none') {

      /*if the object AFTER the one being clicked (always a div.accordionContent)
       * has the CSS property 'display' with a value of none, show it and hide
       * the rest. Then change the background image to be the minus sign */

      $('ul.accordionContent').slideUp();
      $(this).next().slideDown();
      $('h3.accordionButton').removeClass('expanded'); //if there has been an expandAll
      $(this).addClass('expanded');

    } else {

      /* Otherwise, it must be showing, so hide it and change the background
       * image to plus sign */

      $(this).removeClass('expanded');
      $(this).next().slideUp();
    }
  });

  //EXPAND AND COLLAPSE BUTTON ACTIONS

  /*self-evident, but when the element with the id "expandAll" is clicked,
   * show all instances of div.accordionContent with a slideDown and make
   * sure that all backgrounds are minus sign*/

  $('#expandAll').click(function() {
    $('h3.accordionButton').addClass('expanded');
    $('ul.accordionContent').slideDown();
        
  });

  /*self-evident, but when the element with the id "collapseAll" is clicked,
   * hide all instances of div.accordionContent with a slideUp and make
   * sure all background images are the plus sign */
  $('#collapseAll').click(function(){
    $('ul.accordionContent').slideUp()
    $('h3.accordionButton').removeClass('expanded');
  });
});


//HIDE THE DIVS ON PAGE LOAD
// Moved to the document.ready function on the page itself
// 
// $("ul.accordionContent").hide();
//

/***************** Quotator ******************/
/* I did indeed look at another script called Quotator.js by http://merrickchristensen.com/ */
/* This is not it. But I liked the name. */
  
function quotator(target) {
  // set some global variables: the target element, an initalized object for the
  // JSON data object, and an index counter we'll use to dig into our object
  var $target = target;
  var quoteobj = {};
  var maxindex = 0;
  var index = 0;
    
  // get the quotes from a JSON file and make them into a JavaScript object
  $.getJSON("quotes.json",setQuotes);   
    
  function setQuotes(data) {
    var quotes;
    quoteobj = eval(data.quotes);
    maxindex = quoteobj.length-1;
    index = Math.floor(Math.random()*maxindex+1);
    $target.html(quoteobj[index].quote);
  }
    
  setInterval(rotate,15000);
    
  function rotate() {
    if (index == maxindex) {
      index=0;
    } else {
      index++;
    }
    $target.fadeOut(1000, function(){
      $target.html(quoteobj[index].quote).fadeIn(1000);
    });       
  }
}

