$("ul.sf-menu",'#navigation').superfish({ 
    autoArrows:    false,
    dropShadows:   false,
    hoverClass:    'sfHover',
    pathClass:     'sfActive'
});

function resetLeftNavigation(){
  //slide left nav up
  $(".flexipages_widget div.menu ul ul","#column_left").slideUp('slow');
  //alert('foo');
  // hide reset button
  $('#reset').hide();
  // show loading...
  $('#wait_for_reset').show();
  // get left nav code and re-initialize left nav
  $.ajax({
    url: window.location.href,
    cache: true,
    success: function(html){
      navHTML = $("#column_left .flexipages_widget div.menu",html);
      $('#column_left .flexipages_widget div.menu').replaceWith(navHTML);
    },
    complete: function() {
      $(".flexipages_widget .menu ul","#column_left ").treeview({
        animated: "slow",
        persist: "location",
        collapsed: true,
        unique: true
      });
      // rerun all left nav set up code
      $('.flexipages_widget .menu ul ul a','#column_left').each(function(i) {
        var htmlStr = $(this).html();
        $(this).html('<span class="pipe">|</span>'+htmlStr);
      });
      $('div.hitarea','#column_left').hover(
        function () {
          $(this).addClass("hover");
          $(this).parent().addClass("highlight");
        }, 
        function () {
          $(this).removeClass("hover");
          $(this).parent().removeClass("highlight");
        }
      );
      $(".flexipages_widget div.menu","#column_left").append('<div id="nav_reset"><span id="reset">Reset Menu</span><span id="wait_for_reset">Loading<span style="text-decoration:blink">&hellip;</span></span></div>');
      $('#wait_for_reset').hide();
      $('#reset').click(function () { 
        resetLeftNavigation();
      });
    }
  });
}

$(document).ready(function(){

  // video clip widget
  //$('#mycarousel').jcarousel({
  jQuery('#mycarousel').jcarousel({
    vertical: true,
    scroll: 2
  });
  
  $("#search-results").tabs();

  // index main navigation tabs
  $('ul.sf-menu > li a','#navigation').each(function(i) {
    iclass = 'tab'+i;
    $(this).addClass(iclass);
  });
  // add spans around main navigation tab text
  $('ul.sf-menu > li a','#navigation').wrapInner(document.createElement("span"));
  // hide main navigation tab text
  $('ul.sf-menu span','#navigation').hide();
  
  // adds pipes to left navigation
  $('.flexipages_widget .menu ul ul a','#column_left').each(function(i) {
    var htmlStr = $(this).html();
    $(this).html('<span class="pipe">|</span>'+htmlStr);
  });
  // adds jQuery treeview to left navigation
  $(".flexipages_widget .menu ul","#column_left ").treeview({
    animated: "slow",
    persist: "location",
    collapsed: true,
    unique: true
  });
  // adds highlight to li's when hitarea is hovered over
  $('div.hitarea','#column_left').hover(
    function () {
      $(this).addClass("hover");
      $(this).parent().addClass("highlight");
    }, 
    function () {
      $(this).removeClass("hover");
      $(this).parent().removeClass("highlight");
    }
  );
  
  // add left nav reset button
  $(".flexipages_widget div.menu","#column_left").append('<div id="nav_reset"><span id="reset">Reset Menu</span><span id="wait_for_reset">Loading<span style="text-decoration:blink">&hellip;</span></span></div>');
  // hide "loading..."
  $('#wait_for_reset').hide();
  // add click event to reset button
  $('#reset').click(function () { 
    resetLeftNavigation();
  });
  
  // hover effect for sermon files lists
  $('.sermon-files li','#content').hover(
    function () {
      $(this).addClass("hover");
    }, 
    function () {
      $(this).removeClass("hover");
    }
  );
  // hover sermon list
  $('div.sermon-list','#content').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  // activate sermon files
  $('div.sermon-files-hitbox','#content').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  $('.sermon-detail .sermon-row .clip-list li','#content').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  $('#events-list li','#column_right').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  $('#events-list h4','#column_right').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  
  $('#events-list li .notes-show').click(function(){
    $('#events-list li').removeClass('hidenotes');
    $('#events-list li').removeClass('shownotes');
    $(this).parent().addClass('shownotes');
  });
  $('#events-list li .notes-hide').click(function(){
    $('#events-list li').removeClass('hidenotes');
    $('#events-list li').removeClass('shownotes');
    $(this).parent().addClass('hidenotes');
  });
  
  $('div.sermon-files-hitbox','#content').click(function(){
    // reinitalize
    $('div.sermon-files-hitbox').removeClass('active');
    $('div.sermon-files').removeClass('active');
    // hitbox clicked
    $(this).addClass('active');
    // check to see if 'this' file panel is already active
    $(this).parent().parent().parent().parent().parent().next().addClass('active');
    $(this).parent().parent().parent().parent().parent().parent().parent().addClass('active');
  });
  
  $('.sermon-files-close','#content').click(function(){
    $('div.sermon-files-hitbox').removeClass('active');
    $('div.sermon-files').removeClass('active');
    $('div.sermon-list').removeClass('active');
  });
  
  $('a.sermon_series_sermons_list_title','#content').toggle(
    function () {
      $(this).addClass('active');
      $(this).next().slideDown('slow');
      return false;
    }, 
    function () {
      $(this).removeClass('active');
      $(this).next().slideUp('slow');
      return false;
    }
  );
  
  //$('.sermon-detail .sermon-row.collapsible h2','#content').append('<span class="hide">HIDE</span><span class="show">SHOW</span>');
  
  $('.sermon-detail .sermon-row.collapsible h2','#content').toggle(
    function () {
      $(this).parent().addClass('active');
      $(this).next().slideDown('slow');
    }, 
    function () {
      $(this).parent().removeClass('active');
      $(this).parent().removeClass('open');
      $(this).next().slideUp('slow');
    }
  );
  
  /*   $('#pod-sermon-sidebar .sermon-files .sermon_download_instructions_title','#content').toggle(
    function () {
      $(this).addClass('active');
      $(this).next().slideDown('slow');
    }, 
    function () {
      $(this).removeClass('active');
      $(this).next().slideUp('slow');
    }
  ); */
  
  $('.widget li','body.archive #column_right,body.single #column_right,body.page-template-page_whc_blog_3_column-php #column_right,body.page-template-page_whc_missions_3_column-php #column_right,body.page-template-page_whc_missions_local_3_column-php #column_right,body.page-template-page_whc_refuge_3_column-php #column_right,body.search #column_right,#pod-sermon-sidebar').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  
  $('.widget .home-hero-event','#event-hero').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  
  $('.notes-hide','#events-list').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  
  $('.notes-show','#events-list').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  
  $('.post_excerpt','#blog-hero').hover(
    function () {
      $(this).addClass('hover');
    }, 
    function () {
      $(this).removeClass('hover');
    }
  );
  
  $('.post_excerpt','#blog-hero').click(
    function () {
      var url = $(this).find('a').attr('href');
      window.location = url;
    }
  );
  
  $('.widget .home-hero-event','#event-hero').click(
    function () {
      var url = $(this).find('a').attr('href');
      window.location = url;
    }
  );
  
  $('li.pod-field','#pod-sermon-sidebar').click(
    function () {
      var url = $(this).find('a').attr('href');
      window.location = url;
    }
  );
  
  $('li','.sermon-files').click(
    function () {
      var url = $(this).find('a').attr('href');
      window.location = url;
    }
  );
  
  $('select.filter','body.page-template-page_whc_events_3_column-php').before('<label for="event_sponsor" class="filter_label">Filter by ministry area:</label>');
  $('input.pod_search','body.page-template-page_whc_events_3_column-php').before('<label for="search" class="search_label">Search by keyword(s):</label>');
  
  $('a','#post_share').click(function(){
       this.target = "_blank";
  });
  
  $(".scrollable").scrollable();

//$(".scrollable .items div img").trigger("click");

  
  $(".items img").click(function() {

    // see if same thumb is being clicked
    if ($(this).hasClass("active")) { return; }

    // calclulate large image's URL based on the thumbnail URL (flickr specific)
    var url = $(this).attr("data-image");
    var ahref = $(this).attr("data-link");
    var description = '<a href="'+ahref+'">'+$(this).attr("data-description")+'</a>';

    // get handle to element that wraps the image and make it semi-transparent
    var wrap = $("#image_wrap").fadeTo("medium", 0.5);

    var img = new Image();

    // call this function after it's loaded
    img.onload = function() {

      // make wrapper fully visible
      wrap.fadeTo("fast", 1);

      // change the image
      wrap.find("img").attr("src", url);

    };

    img.src = url;
    $('a','#image_wrap').attr("href", ahref);
    $('span','#image_wrap').html(description);

    // activate item
    $(".items img").removeClass("active");
    $(this).addClass("active");

  // when page loads simulate a "click" on the first image
  }).filter(":first").click();
/*
//ONE IDEA//
$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };
    function runIt() {
			$(".items img:first").click();
              .wait()
			$(".items img:nth-child(2)").click();	
              .wait()
			$(".items img:nth-child(3)").click();
              .wait()
			$(".items img:nth-child(4)").click();
              .wait()
			$(".items img:nth-child(5)").click();
              .wait()
			$(".items img:nth-child(6)").click(runIt);
			
    }
    runIt();

//ANOTHER IDEA// - http://www.chapterthree.com/blog/josh_koenig/simple_jquery_tricks_flipping_through_list
$(".items img:first-child").click();
var total = $(".items img").size();
var count = 0;
$(".mylink a").click( function() {
  $(".items img:nth-child("+count+")").hide();
  count++;
  if (count == total) {
    count = 0;
  }
  $(".items img:nth-child("+count+")").show();
  return false;
});
*/
  
});
