if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

var currentPage = 1;
var inMotion = false;
// create a 'bucket' for the thumbnails
var theThumbs = [];
var timer;

$(document).ready(function(){
  timer = window.setInterval(slideForwards, 7000);
  $('#back-div').css('background-image', 'url(/portals/0/images/slides/big_'+currentPage+'.jpg)');
  $('.thumb').filter(':not(.current)').css('bottom','66px')
  // grab the slider
  var slider = $('#slider2'),
  // count the slides
  		numOfSlides = $.makeArray($('.panel1')).length; 
  // determine the width
      slideWidth = slider.width();
  // create an object out of the _bg classes
  // for the backgrounds

  // top and cap the slider
  slider.find('.panel1').filter(':first').before(slider.find('.panel1').filter(':last').clone().addClass('_cloned'));
  slider.find('.panel1').filter(':last').after(slider.find('.panel1').filter(':first').next().clone().addClass('_cloned'));
  slider.scrollLeft(slideWidth);
  
  // handle forward and backwards clicks
  var slides = $('.viewport').find('.panel1');
  
  $('.thumb').each(function(i, elem){
    theThumbs.push(elem);
  });
   
  $('.leftArrow').click(function(){
    window.clearInterval(timer);
    slideBackwards(slider);
  });
  
  $('.rightArrow').click(function(){
    window.clearInterval(timer);
    slideForwards(slider);
  });

  $('.thumb').click(function(){
    window.clearInterval(timer);
  	if(inMotion == false){
    

    inMotion = true
    var page = theThumbs.indexOf(this) +1,
        numOfPages = currentPage - page,
        dir = currentPage > page ? -1 : 1;

			var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = slideWidth * dir * n;

    currentPage = page;
      
    // set current thumbnail
    $('.current').removeClass('current');

    $(theThumbs[currentPage-1]).addClass('current');

    $('.thumb').each(function(i,elem){
      if($(elem).hasClass('current') && $(elem).css('bottom') == '66px'){
        $(elem).css('bottom', '0px');
      }
      else if(!$(elem).hasClass('current') && $(elem).css('bottom') =='0px'){
        $(elem).css('bottom','66px');
      }    
    });

	    slider.animate({
	      scrollLeft: '+=' + left
	    },600, function(){
	        inMotion = false;
          changeBg();
	    });

        // .filter(':not(.current)').animate({
        //   bottom:'+=66px'
        // });

  	}else{
     return false; 
    }
  });
});

function slideForwards(slider){
var slider = $('#slider2');
 if(inMotion == false){
    inMotion = true;
    var slides = slider.find('.panel1'),
    slideWidth = slider.filter(':first').width();
    currentPage++;
    // check for a reset
      // if user reached the loop
       if(currentPage == 6){
          currentPage = 1
          slider.scrollLeft(0);
       }
    // set current thumbnail
    $('.current').removeClass('current');

    $(theThumbs[currentPage-1]).addClass('current');

    $('.thumb').each(function(i,elem){
      if($(elem).hasClass('current') && $(elem).css('bottom') == '66px'){
        $(elem).css('bottom', '0px')
      }
      else if(!$(elem).hasClass('current') && $(elem).css('bottom') =='0px'){
        $(elem).css('bottom','66px')
      }    
    });
    // move the slider
    slider.animate({
      scrollLeft:'+=' + slideWidth
    },'slow', function(){
        inMotion = false;
       changeBg();
    });     
 }
}
function slideBackwards(slider){
 if(inMotion == false){
    inMotion = true;
    currentPage--;
    var slider = $('#slider2'),
        slides = slider.find('.panel1'),
        slideWidth = slider.filter(':first').width();

    if(currentPage == 0) currentPage = slides.length - 2;
    if(currentPage < 0) currentPage = slides.length - 2;

      // set current thumbnail
    $('.current').removeClass('current');

    $(theThumbs[currentPage-1]).addClass('current');

    $('.thumb').each(function(i,elem){
      if($(elem).hasClass('current') && $(elem).css('bottom') == '66px'){
        $(elem).css('bottom','0px')
      }
      else if(!$(elem).hasClass('current') && $(elem).css('bottom') =='0px'){
        $(elem).css('bottom','66px')
      }    
    });

    // check for a reset
    // if user reached the loop  
    if(currentPage  == 1){
      slider.scrollLeft(1920);
    }
    if(currentPage  == 5){
      slider.scrollLeft(5760);
    }

    // move the slider
    slider.animate({
      scrollLeft:'-=' + slideWidth
    },'slow', function(){
        inMotion = false;
       changeBg();
    });
 }
}

 function changeBg() {
  
  var backgrounds = {
      '1' : '#b4d5e8',
      '2' : '#010713',
      '3' : '#c4bc7d',
      '4' : '#030b16',
      '5' : '#5692b7'
  };

  $('.surround').css('background-color',backgrounds[currentPage.toString()]);

  
  $('#back-div').css('background-image', 'url(/portals/0/images/slides/big_'+currentPage+'.jpg)');
}
    // var $elem = $(elem);
    // // see if it's already faded, but
    // // has just become current
    // if($elem.hasClass('.current') && elem.css('bottom') == '66px'){
    //   // if it's already faded out
    //   // bring it back
    //   $elem.animate({
    //     bottom:'-=66px'
    //   },'fast');
    // }

  // $('.thumb').filter(':not(.current)').each(function(i,elem){
  //   if($(elem).css('bottom') == '66px'){
  //     return false;
  //   }else{
  //     $(elem).animate({
  //       bottom:'+=66px'
  //     }, 'fast');
  //   }
  // });
  
  // if($('.thumb').filter('.current').css('bottom') == '66px'){
  //     $('.thumb').filter(".current").animate({
  //     bottom:'-=66px'
  //   }, 'fast');
  // }else{
  //     $('.thumb').filter(".current").animate({
  //     bottom:'+=66px'
  //   }, 'fast');    
  // }

  //     if($(this).css('bottom') == '0px'){
  //     $(this).animate({
  //       bottom:'+=0px'
  //     });
  //   }

  //   if($(this).css('bottom') == '66px'){
  //     $(this).animate({
  //       bottom:'-=66px'
  //     });
  //   }
    
  // $('.thumb').filter(':not(.current)').each(function(i,elem){
  //   if($(elem).css('bottom') == '0px'){
  //     $(elem).animate({
  //       bottom:'+=66px'
  //     });
  //   }

  //   else if($(elem).css('bottom') == '66px'){
  //     $(elem).animate({
  //       bottom:'+=0px'
  //     });
  //   }    
  // });  

// /**
//   * Check whether the browser supports RGBA color mode.
//   *
//   * Author Mehdi Kabab <http://pioupioum.fr>
//   * @return {boolean} True if the browser support RGBA. False otherwise.
//   */
//  function isRGBACapable() {
//      var $script = $('script:first'),
//              color = $script.css('color'),
//              result = false;
//      if (/^rgba/.test(color)) {
//          result = true;
//      } else {
//          try {
//              result = ( color != $script.css('color', 'rgba(0, 0, 0, 0.5)').css('color') );
//              $script.css('color', color);
//          } catch (e) {
//          }
//      }
// 
//      return result;
//  }
// 
//  $.extend(true, $, {
//      support: {
//          'rgba': isRGBACapable()
//      }
//  });
// 
//  var properties = ['color', 'backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'outlineColor'];
//  $.each(properties, function(i, property) {
//      $.fx.step[property] = function(fx) {
//          if (!fx.init) {
//              fx.begin = parseColor($(fx.elem).css(property));
//              fx.end = parseColor(fx.end);
//              fx.init = true;
//          }
// 
//          fx.elem.style[property] = calculateColor(fx.begin, fx.end, fx.pos);
//      }
//  });
// 
//  // borderColor doesn't fit in standard fx.step above.
//  $.fx.step.borderColor = function(fx) {
//      if (!fx.init) {
//          fx.end = parseColor(fx.end);
//      }
//      var borders = properties.slice(2, 6); // All four border properties
//      $.each(borders, function(i, property) {
//          if (!fx.init) {
//              fx[property] = {begin: parseColor($(fx.elem).css(property))};
//          }
// 
//          fx.elem.style[property] = calculateColor(fx[property].begin, fx.end, fx.pos);
//      });
//      fx.init = true;
//  }
// 
//  // Calculate an in-between color. Returns "#aabbcc"-like string.
//  function calculateColor(begin, end, pos) {
//      var color = 'rgb' + ($.support['rgba'] ? 'a' : '') + '('
//              + parseInt((begin[0] + pos * (end[0] - begin[0])), 10) + ','
//              + parseInt((begin[1] + pos * (end[1] - begin[1])), 10) + ','
//              + parseInt((begin[2] + pos * (end[2] - begin[2])), 10);
//      if ($.support['rgba']) {
//          color += ',' + (begin && end ? parseFloat(begin[3] + pos * (end[3] - begin[3])) : 1);
//      }
//      color += ')';
//      return color;
//  }
// 
//  // Parse an CSS-syntax color. Outputs an array [r, g, b]
//  function parseColor(color) {
//      var match, triplet;
// 
//      // Match #aabbcc
//      if (match = /#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(color)) {
//          triplet = [parseInt(match[1], 16), parseInt(match[2], 16), parseInt(match[3], 16), 1];
// 
//          // Match #abc
//      } else if (match = /#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(color)) {
//          triplet = [parseInt(match[1], 16) * 17, parseInt(match[2], 16) * 17, parseInt(match[3], 16) * 17, 1];
// 
//          // Match rgb(n, n, n)
//      } else if (match = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) {
//          triplet = [parseInt(match[1]), parseInt(match[2]), parseInt(match[3]), 1];
// 
//      } else if (match = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([01\.]*)\s*\)/.exec(color)) {
//          triplet = [parseInt(match[1], 10), parseInt(match[2], 10), parseInt(match[3], 10),parseFloat(match[4])];
// 
//          // No browser returns rgb(n%, n%, n%), so little reason to support this format.
//      }
//      return triplet;
//  }
// /**
//   * Check whether the browser supports RGBA color mode.
//   *
//   * Author Mehdi Kabab <http://pioupioum.fr>
//   * @return {boolean} True if the browser support RGBA. False otherwise.
//   */
// 
// 
//   
// $(document).ready(function(){    
//   
//  $('#slider2')
//  .anythingSlider({
//      autoPlay            : true,
//   delay               : 8000,
//      width               : 600,
//      height              : 550,
//      resizeContents      : false,
//      navigationFormatter : function(i, panel){
//          return '<img class="testImg" src="/portals/0/images/slides/thumb_' + ['sec', 'bigeast', 'creflo', 'parkin', 'florida_hospital'][i - 1] + '.jpg">';
//      }
//  })
//  $('.testImg').parent('a').fadeTo('fast', .50)
//  // .anythingSliderFx({
//  //  '.quoteSlide'         : [ 'expand', '500px', '1000', 'easeOutElastic' ],
//  //  '.expand'             : [ 'expand', '10%', '400', 'easeOutBounce' ],
//  //  '.textSlide h3'       : [ 'expand', '200px', '500', 'easeOutCirc' ],
//  //  '.textSlide img,.fade': [ 'slide' ],
//  //  '.textSlide li'       : [ 'fade' ]
//  // });
//  
//  // External Controls
//  $("#leftArrow").click(function(){
//      $('#slider2').data('AnythingSlider').goBack();
//  });
//  
//  $("#rightArrow").click(function(){
//      $('#slider2').data('AnythingSlider').goForward();
//  });
//  
//  // Background div
//  $('#slider2').bind('slide_complete', function(e, slider){
//              var hashNumber = window.location.hash.split('-')[1];
//                      // set up colors
//                      var backgrounds = {
//                          'first' : '#010713',
//                          'second' : '#030b16',
//                          'third' : '#c4bc7d',
//                          'fourth' : '#5692b7',
//                          'fifth' : '#b4d5e8'
//                      };
//          switch($('#slider2').data('AnythingSlider').currentPage.toString()){
//              case '1' :
//                  changeBg(backgrounds.first)
//                      $('#status').text( 'Now on page #' + slider.currentPage );
//                      $('#back-div').css('background-image', 'url(/portals/0/images/slides/big_'+slider.currentPage+'.jpg)');
//              break;
//              case '2':
//                  changeBg(backgrounds.second)
//                      $('#status').text( 'Now on page #' + slider.currentPage );
//                      $('#back-div').css('background-image', 'url(/portals/0/images/slides/big_'+slider.currentPage+'.jpg)');
//              break;
//              case '3':
//                  changeBg(backgrounds.third)
//             $('#status').text( 'Now on page #' + slider.currentPage );
//                      $('#back-div').css('background-image', 'url(/portals/0/images/slides/big_'+slider.currentPage+'.jpg)');
//              break;
//              case '4':
//                  changeBg(backgrounds.fourth)
//                      $('#status').text( 'Now on page #' + slider.currentPage );
//                      $('#back-div').css('background-image', 'url(/portals/0/images/slides/big_'+slider.currentPage+'.jpg)');
//              break;
//              case '5':
//                  changeBg(backgrounds.fifth)
//                      $('#status').text( 'Now on page #' + slider.currentPage );
//                      $('#back-div').css('background-image', 'url(/portals/0/images/slides/big_'+slider.currentPage+'.jpg)');
//              break;
//          }
//          // fade out correct thumbnails
// 
//      });
// 
// function anything (){
//          var slider = $('#slider2').data('AnythingSlider')
//      $('#status').text( 'Now on page #' + slider.currentPage );
//          $('#back-div').css('background-image', 'url(/portals/0/images/slides/big_'+slider.currentPage+'.jpg)');
// }
//      
// });
// 
// function changeBg (color) {
//      $('.surround').animate({
//              'background-color': color
//      }, 200);
// }
// 
// 
//  function fadeOthers() {
//          var targets = $('.thumbNav li a');
//       targets.each(function(){
//           var origSrc = $(this).find('img').attr('src');
//           // if this is the current link, do nothing
//           if($(this).hasClass('cur')){
//               $(this).find('img').attr('src').replace('_fade.png', '.png')
//           }
//           else{
//              $(this).find('img').attr('src', origSrc.replace('.png', '_fade.png'))
//           }
//       });
//  }
//  // targets.livequery(function(){
//  //  $(this).click(function(){
//  //              targets.each(function(){
//  //               var $this = $(this);
//  //               var source = $this.attr('src')
//  //               if($this.hasClass('cur')){
//  //                   $this.attr('src', source.replace('.png','_fade.png'));
//  //               }else{
//  //                  $this.attr('src', source.replace('_fade.png','.png'))
//  //               }       
//  //          });
//  //  })
//  //})
// 

