//jQuery.noConflict();
jQuery(document).ready(function($) {

  // initialize scrollable
  $(".scrollable").scrollable({circular: true}).autoscroll({ autoplay: true, interval: 7000 }).navigator();
  
  if (!Modernizr.input.placeholder){
        var s = $('input#sitesearch');
        s.val('Search this site');
        s.focus(function(){if(this.value !== '')this.value = '';});
        s.blur(function(){if(this.value === '')this.value = 'Search this site';});
    }
  
  $('a.anchor').anchorAnimate();
  
 // IMAGE GALLERY
  var imgGallery = (function(){
    var items = $('.thumb-bar ul li'),
       width = $('.thumb-bar ul li:first').outerWidth(),
       scroll = $('.thumb-bar ul').width(''+items.length*width+'px'),
       position = 0,
       thumbs = $('.thumb-bar ul li a').removeClass('current').filter(':first').addClass('current').end(),
       imgs = $('.img-full .img').css({'zIndex':'1','opacity':0}).filter(':first').css({'zIndex':'2','opacity':1}).end(),
       counter = 0,
       prev = $('.controls a.prev'),
       next = $('.controls a.next');
                      
     var totalWidthN = scroll.width() * -1;
      
  
      imgs.each(function(index){
          $(this).click(function(){
            if(index<imgs.length-1){
              thumbs.eq(index+1).trigger('click');
            } else {
              thumbs.eq(0).trigger('click');
            }
          });
       });
       
       prev.click(function(event){
         if (position >= 0) {
           position = totalWidthN + width;
           scroll.animate({left:''+position+'px'},'normal');
         } else if (position >= width*-1) {
           position = 0;
           $('.thumb-bar ul').animate({left:'0px'},'normal');
         } else {
           position += width;
           scroll.animate({left:''+position+'px'},'normal');
         }
         event.preventDefault();
       });
       next.click(function(event){
         if (position <= totalWidthN+width) {
           position = 0;
           scroll.animate({left:'0px'},'normal');
         } else if (position == 0) {
           position -= width;
           scroll.animate({left:''+position+'px'},'normal');
         } else {
           position -= width;
           scroll.animate({left:''+position+'px'},'normal');
         }
         event.preventDefault();
       });
    
       thumbs.each(function(index){    
         $(this).click(function(event){
           event.preventDefault();    
           var target = $(this).attr('href'),
           index = $(this).parent().index();
           showImg(index);
           this.className = 'current';
           position = width*index*-1;
           scroll.animate({left:''+position+'px'},'normal');
       });
  
     });
     function showImg(target) {
       //var cTarget = target.substr(1);
       imgs.animate({opacity:0},'fast').css('zIndex','1').eq(target).css('zIndex','2').animate({opacity:1},'fast');     
       thumbs.removeClass('current');
     }
  })();

});
jQuery.fn.anchorAnimate = function(settings) {

   settings = jQuery.extend({
    speed : 450
  }, settings);  
  
  return this.each(function(){
    var caller = this
    $(caller).click(function (event) {  
      event.preventDefault();
      var locationHref = window.location.href;
      var elementClick = $(caller).attr("href");
      
      var destination = $(elementClick).offset().top;
      $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
        window.location.hash = elementClick;
      });
        return false;
    })
  })
}

