/* 
Sitewide JavaScript functions built on jquery 
v3.5.0 - May 19, 2010 - Initial version by James Baughn
v3.5.2 - Sept. 21, 2010 - Added automatic sliding, code refactoring
v3.5.3 - Nov. 5, 2010 - Fixed bug with text resizer not saving cookies
*/

// Carousel widgets */
this.init_carousel = function() {
 $('.carousel').each( function() {
  var wid = this.id;
  var elt = $(this);

  var totalwidth = elt.find('.carouselwrapper').outerWidth();
  var itemwidth = elt.find('li').outerWidth();  
  var itemstoshow = Math.floor(totalwidth / itemwidth);
  if( itemstoshow == 0 ) { itemstoshow = 1; }
  var numitems = elt.find('li').length;
  var curritem = 0;
  var unwrapped = false;
  
  var ctrltoggle = elt.find('.ctrltoggle');
  var ctrlinfo = elt.find('.ctrlinfo');
  var ctrlplayer = elt.find('.ctrlplayer');
   
  var autoenabled = false;
  var autoid;
  var autocounter;  

  function carousel_advance( direction, jumpto ) {
   var olditem = curritem;
   var newitem = ( direction ? olditem + direction : jumpto );
   if( newitem < 0 ) { newitem = 0; }
   if( newitem >= numitems ) { newitem = numitems - 1; }
   var newleft = newitem * itemwidth * -1;
   curritem = newitem;
   elt.find('ul').animate({ 'left':newleft },{ queue:true,duration:(itemwidth > 200 ? 500 : 200) });
   ctrlinfo.find('img').eq(olditem).attr('src', '/images/icons/dot-inactive.png');
   ctrlinfo.find('img').eq(newitem).attr('src', '/images/icons/dot-active.png');
   elt.find('.ctrlleft').css('opacity', (newitem == 0 ? '0.5' : '1.0') );
   var fudge = (direction > 1 ? direction : 1);
   elt.find('.ctrlright').css('opacity', (newitem >= (numitems - fudge) ? '0.5' : '1.0') );
  }

  function carousel_wrap_toggle() {
   if( autoenabled ) { carousel_player_shutoff(); }
   if( !unwrapped ) {
    carousel_advance( 0, 0 );
    curritem = 0; unwrapped = 1;
    elt.find('li').css('float','none').css('clear','both');
    ctrltoggle.text('View one');
   } else {
    unwrapped = 0;
    elt.find('li').css('float','left').css('clear','none');
    ctrltoggle.text('View all');
   }
   ctrlinfo.toggle();
   elt.find('.ctrlleft').toggle();
   elt.find('.ctrlright').toggle();
  }

  function carousel_player_shutoff() {
   ctrlplayer.text('Play');
   window.clearInterval( autoid ); autoenabled = false;
  }
  
  function carousel_player_toggle() {
   if( autoenabled ) {
    carousel_player_shutoff();
   } else {
    ctrlplayer.text('Pause');
    autocounter = 0; autoenabled = true;
    autoid = window.setInterval( function(a,b) { 
     if( ++autocounter == numitems ) {
      carousel_advance( 0, 0 );
      carousel_player_shutoff();
     } else {
      carousel_advance( 0, autocounter );
     }
    }, 8000);
   }
  }
  
  if( numitems > 1 ) {
   // Left and right buttons
   elt.find('.ctrlleft').show().click(function() {
    carousel_advance( itemstoshow * -1, 0 );
    if( autoenabled ) { carousel_player_shutoff(); }
   });
   elt.find('.ctrlright').show().click(function() {
    carousel_advance( itemstoshow, 0 );
    if( autoenabled ) { carousel_player_shutoff(); }
   });
   
   // Individual item jumps
   if( ctrlinfo.length > 0 ) {
    if( numitems < (itemwidth / 10) ) {
     for( i = 1; i <= numitems; i++ ) {
      ctrlinfo.append('<img src="/images/icons/dot-inactive.png" style="cursor:hand;">');
     }
    }
    if( numitems > 20 ) {
     ctrlinfo.append('<br>');
    }
    ctrlinfo.find('img').click( function() {
     carousel_advance( 0, ctrlinfo.find('img').index(this) );
     if( autoenabled ) { carousel_player_shutoff(); }
    });
   }
   
   // Initialize widget
   carousel_advance( 0, 0 );
   
   // View all toggle
   if( ctrltoggle.length > 0 ) {
    ctrltoggle.text('View all').click( carousel_wrap_toggle );
   }
   
   // Auto play toggle
   if( ctrlplayer.length > 0 ) {
    carousel_player_toggle();
    ctrlplayer.click( carousel_player_toggle );
   }
  }
 });
};

// Tooltips
this.init_tooltips = function(){	
 $("img[title],a[title]").hover(function(e){											  
  this.t = this.title;
  this.title = "";									  
  $("body").append("<p id='tooltip'>"+ this.t +"</p>");
  $("#tooltip")
   .css("top",(e.pageY - 10) + "px")
   .css("left",(e.pageX + 10) + "px")
   .fadeIn("fast");		
  },
  function(){
   this.title = this.t;		
   $("#tooltip").remove();
  });	
 $("img[title]").mousemove(function(e){
 $("#tooltip")
  .css("top",(e.pageY - 10) + "px")
  .css("left",(e.pageX + 10) + "px");
 });			
};

// Text resizing tool
this.init_textsizer = function(){	
 $('#textsizer').click(function(event){
  if( $('body').hasClass('sizelarge') ) {
   $('body').animate({fontSize:'80%'}, 400);
   $('body').attr('class','sizenormal');
   $('#textsizer span').text('Enlarge text');
   bakecookie('textsize','',-1);
  } else {
   $('body').animate({fontSize:'100%'}, 400);
   $('body').attr('class','sizelarge');
   $('#textsizer span').text('Return text to normal');
   bakecookie('textsize',1,360);
  }
  event.preventDefault();
 });
};

// Set cookie function
function bakecookie(name,value,days) {
 if(days) {
  var date = new Date();
  date.setTime(date.getTime()+(days*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
 } else {
  var expires = "";
 }
 var d = document.domain.replace(/^www\./, '');
 document.cookie = name+"="+value+expires+";domain=" + d + ";path=/";
}

// Page initialization
$(document).ready(function(){
 init_tooltips();
 init_carousel();
 init_textsizer();
});

