$(document).ready(function(){

  $("a.anchorlink").anchorAnimate();
  if ($('a.colorgal').length) {
    /*if ($.browser.webkit) {
        $('a.colorgal').colorbox({
          slideshow: true,
          slideshowSpeed: 5000,
          width: "60%",
          height: "80%"
        });
      } else {*/
      $('a.colorgal').colorbox({
        slideshow: true,
        slideshowSpeed: 5000,
        slideshowStart: 'Start slideshow',
        slideshowStop: 'Pause slideshow'
      });/*
    }*/
  }
  
  //$('ul.gallery li').masonry({columnWidth: 70});
  
  // HOMEPAGE CAROUSEL
  var $banners = $('ul#carousel>li').css('opacity',0).filter('.current').css('opacity',1).end(),
    fadeSpeed = 450,
    delayTimer = 5000,
    delayed = null,
    counts = 1;


  startRotation();
/*
  $banners.each(function(){
    $(this).hover(function(){
      stopRotation();
    },function(){
      startRotation();
    });
  });
*/
  function startRotation() {  
    if (delayed) { clearInterval(delayed); }
    delayed = setInterval(function(){ goNext(); },delayTimer);
  }
  
  function stopRotation() {
    if (delayed) { clearInterval(delayed); }
  }
  

  function goNext() {
    if (counts<$banners.length) {
      $banners.animate({opacity: 0},fadeSpeed).removeClass('current');
      $('ul#carousel li').eq(counts).animate({opacity: 1},fadeSpeed).addClass('current');
      counts++;
    } else {
      $banners.animate({opacity: 0},fadeSpeed).removeClass('current');
      $('ul#carousel li:first').animate({opacity: 1},fadeSpeed).addClass('current');
      counts = 1;
    }
  }

  
  $('ul#project-list a.thumb, ul#media-list a.thumb, ul#member-list a.thumb, ul#news-list a.thumb').each(function(){
    $('<span class="hover" />').appendTo(this).hide();
    $(this).hover(function(){
      if ($.browser.msie) {
        $('span.hover',this).show();
      } else {
        $('span.hover',this).stop().fadeTo(450,.6);
      }
    },function(){
      if ($.browser.msie) {
        $('span.hover',this).hide();
      } else {
        $('span.hover',this).stop().fadeTo(450,0);
      }
    });
  });
  
  $('ul#vid-list a.thumb, div.vid-wrap a.thumb').each(function(){
    $('<span class="hover" />').appendTo(this).hide();
    $(this).hover(function(){
      if ($.browser.msie) {
        $('span.hover',this).hide();
      } else {
        $('span.hover',this).stop().fadeTo(450,.25);
      }
    },function(){
      if ($.browser.msie) {
        $('span.hover',this).show();
      } else {
        $('span.hover',this).stop().fadeTo(450,1);
      }
    });
  });

  $('ul#project-slide').cycle({
    timeout: 5000,
    speed: 1500
  });
  
  /*
  
  var container = $('ul#project-slide'),
    $slides = $('ul#project-slide li'),
    width = 640,
    counter = 0,
    speed = 450,
    prev = $('#project-slide-nav a#prev'),
    next = $('#project-slide-nav a#next');
    if ($slides.length) {
      width = $slides.get(0).offsetWidth;
    }
  
  container.css('width',width * $slides.length + 'px');
  $slides.css('float','left');
  
  var totalWidth = parseInt(container.css('width'))*-1;

  prev.click(function(event){
    event.preventDefault();    
    if (counter == 0) {      
      container.animate({'marginLeft': totalWidth + width + 'px'},speed);
      counter = $slides.length - 1;
    } else {
      container.animate({'marginLeft': '-' + width*(counter-1) + 'px'},speed);
      counter--;
    }    
  });
  
  next.click(function(event){
    event.preventDefault();    
    if (counter < $slides.length - 1) {
      container.animate({'marginLeft': '-' + width*(counter+1) + 'px'},speed);
      counter++;
    } else {
      container.animate({'marginLeft': '0px'},speed);
      counter = 0;
    }
  });
  */
});

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;
    })
  })
}
