/* Core Javascript for Freshwater Land Trust */

/* GOOGLE MAPS API KEY */
// ABQIAAAATchDDFTh4zb3l6cGmSFWSRQlEp3yYK0RDA-RTjyedCEaTkZQSRTGvEYYd5vdSLXoxK7cYLTSvO_iCA


// assign jQuery shortcut to address WP no conflict mode setup
var $$ = jQuery;

/* Main Onload Call */
$$(function() {
  pageSetup();
  if ($$('#feature-frame').length) setFeaturesCycle();
  if ($$('#council').length) councilSetup();
  if ($$('#photonav').length) featureSetup();
  if ($$('#volunteer-events').length) moveEventList();
  if ($$('#news-events-list').length) setNewsEvents();
  if ($$('#archive-list').length) formatArchives();
});

// function to setup home feature slides using JFlow Plugin
function setFeaturesJFlow(){
	$$("#feature-nav").jFlow({
		slides: "#feature-slides",
		controller: ".fnav", // must be class, use . sign
		slideWrapper : "#jflow-slide", // must be id, use # sign
		selectedWrapper: "jflow-wrap",  // just pure text, no sign
		auto: false,		//auto change slide, default true
		width: "738px",
		height: "700px",
		duration: 1000,
		prev: ".prev", // must be class, use . sign
		next: ".next" // must be class, use . sign
	}); 
}

// function to setup home feature slides using Cycle Plugin
function setFeaturesCycle(){
  var features = $$('#feature-slides').cycle({
    fx: 'fade',
    speed: 2000,
    timeout: 7000,
    pager:  '#feature-nav', 
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#feature-nav li:eq(' + idx + ') a'; 
    } 
  });
  
  // set nav actions
  //$$('#feature-nav a:first').addClass('sel');
  $$('#feature-nav a').each(function(i){
    $$(this).click(function(){
      // goto slide
      features.cycle(i);
      // stop animation
      features.cycle('pause');
      return false;
    });
  });

}

// after function for home features
function changeSelected(curr, next, opts) {
  var ndx = opts.currSlide;
  // change selected states
  $$('#feature-nav a').removeClass('sel');
  $$('#feature-nav a').eq(ndx).addClass('sel');
}


// function to setup left nav links
function pageSetup(){
  // add nav gradient
  if ($$('#nav').length > 0) {
    var hr = $$('div.hr-gradient').clone();
    $$('#nav li.page-item-402').prepend(hr);
  }
  
  // style highlites
  //$('span[style*=text-decoration]').addClass('hilight');
  $$('#post span').css('text-decoration', 'none').addClass('hilight');
  // add external link targets
  $$("a[rel='ext']").attr('target', '_blank');
  
  // add carets to bookmark links
  $$("ul.bookmarks a").append(' &gt;');
  $$("ul.bookmarks li:last").addClass('lastrow');

}

// function to add Council hr
function councilSetup(){
  $$('#council-list li:last').addClass('listBdrTop');
}

// called on project pages to 
function featureSetup() {
	// config hover sensitivity
	var hoverConfig = {    
    sensitivity: 7,
    interval: 200,
    over: showThumb,
    out: hideThumb
  };
  // set event actions
  $$('#photonav a:first').addClass('sel');
  $$('#photonav a').hoverIntent(hoverConfig).click(function(){
    $$('#photonav a').removeClass('sel');
    $$(this).addClass('sel');
    swapMain($$(this));
    return false;
  });
  
  //show thumbnail
  function showThumb() {
    var thumsrc = $$(this).attr('rel');
    $$('#thumbnail').attr('src', thumsrc);
    $$('#thumbnail').fadeIn();
  }
  // hide thumbnail
  function hideThumb() {
    $$('#thumbnail').fadeOut();
  }
  // swap main image
  function swapMain(el) {
    var fullsrc = $$(el).attr('href');
    var desc = $$(el).attr('title');
    var credit = $$(el).attr('lang');
    $$('#mainimage').attr('src', fullsrc);
    $$('#description').html(desc);
    $$('#credit').html(credit);
    return false;
  }
}

// move volunteer list into post
function moveEventList(){
  $$('#volunteer-events').insertBefore('#skills-list');
}

// format archive entries
function formatArchives(){
  // remove underline from last blog archive
  $$("div.archive-entry:last").addClass('lastrow');
}

// setup news & events accordians
function setNewsEvents(){
  /* TABS
  $$('#news-events-tabs a').click(function(){
    //$$('#news-events-tabs a').removeClass('sel');
    $$(this).toggleClass('sel').siblings().removeClass('sel');
    $$('#' + this.id + '-content').show().siblings('[id$=-content]').hide();
    return false;
  });
  */
  
  
  // flip headers
  $$('h2.blockhead').click(function(){
    var list = $$(this).next('div.lister');
    if ($$(list).is(':hidden')){
      $$(list).slideDown('slow');
      $$(this).addClass("open");
    } else {
      $$(list).slideUp('slow');
      $$(this).removeClass("open");
    }
  });
}
