$(document).ready(function() {
  // hover
  $('.splash_menu a').hover(function(){
    $(this).find('img').stop().animate({
      opacity:0
    })							 
  }, function(){
    $(this).find('img').stop().animate({
      opacity:1
    })							 
  });
	
  $('.prev, .next').hover(function(){
    $(this).stop().animate({
      opacity:0.5
    })							 
  }, function(){
    $(this).stop().animate({
      opacity:1
    })							 
  });
  
	
  $('#ContactForm').forms({
    ownerEmail:'#'
  });
	
  $('ul#menu').superfish({
    delay:       600,
    animation:   {
      height:'show'
    },
    speed:       600,
    autoArrows:  false,
    dropShadows: false
  });
	
  // give all big images opacity 0
  $(".gallery_big_img img").css({
    opacity:0
  });
  // find the active big images and give them opacity 1
  // TODO: find the first one, add class, make visible
  $(".gallery_big_img img.active").css({
    opacity:1
  });
  
  // hide all caption divs
  $(".caption > div").css({
    opacity:0, 
    display:'none'
  });
  // show just the active caption
  // TODO: find the first one, add class, make visible
  $(".caption > .active").css({
    opacity:1, 
    display:'block'
  });
  
  $(".gallery_carousel").on('click', 'a', function(e){
    e.preventDefault(); // prevent normal anchor click behaviour
 
    // use href to find this thumbnail's virtual zero-origin index
    var ImgIndex = $(this).attr("href").slice(4);
    ImgIndex = ImgIndex - 1;
    
    // make a jQuery object from this gallery's big images
    var bigContainer = $(this).closest('.project').find('.gallery_big_img');
    var bigImgs = bigContainer.find('.bigimage');
    var Img = $(bigContainer).find('.active');
    
    // find the zero-origin index of the active big image
    var bigIndex = bigImgs.index(Img);


    // if an inactive thumb
    if (ImgIndex != bigIndex) {
      var newImg = $('.bigimage:eq(' + ImgIndex + ')', bigContainer);
      $(Img).stop().animate({
        opacity:0
      }, 600, function(){
        $(this).css({
          display:'none'
        })
      })
      $(Img).removeClass('active');
      newImg.css({
        display:'block'
      }).stop().animate({
        opacity:1
      }, 600, function(){});
      newImg.addClass('active');
    }
    Img=newImg;
  });
	
  // dynamic sizing?
    
  var h_cont=780;
  var h, new_h;
  setHeight();
  h=new_h;
  setSize();
  
  function setHeight(){
    new_h=$(window).height();
  }
  
  function setSize(){
    if (h>h_cont) {
      $('.main').stop().animate({
        paddingTop:~~((h-h_cont)/2)
      });	
    } else {
      $('.main').stop().animate({
        paddingTop:0
      });	
    }
  }
  
  setInterval(setNew,1);
  
  function setNew(){
    setHeight();
    if (h!=new_h) {
      h=new_h;
      setSize();
    }
  }
});
 

$(window).load(function() {	
	
  // scroll
  $('.scroll').cScroll({
    duration:700,
    step:54,
    trackCl:'track',
    shuttleCl:'shuttle'
  });
  
  $('.track').css({
    height:0
  });
  
  //content switch
  var content=$('#content'),
  nav=$('.menu');
  nav.navs({
    useHash:true,
    hoverIn:function(li){
      $('> a strong',li).css({
        display:'block'
      }).stop().animate({
        opacity:'1'
      },600);
    },
    hoverOut:function(li){
      if (!$(li).hasClass('sfHover')) {
        $('> a strong',li).stop().animate({
          opacity:'0'
        },600, function(){
          $(this).css({
            display:'none'
          })
        });
      }
    },
    hover:true
  });
  
  nav.navs(function(n, _){
    content.cont_sw(n);
  });
  
  content.cont_sw({
    showFu:function(){
      var _=this					
      $.when(_.li).then(function(){	
        _.next.css({
          display:'block'
        }).stop().animate({
          height:433
        },600);	
      });
      $('#splash').stop().animate({
        height:0
      },600)
      $('.menu').stop().animate({
        opacity:1
      },600)
      $('#back').css({
        display:'block'
      }).stop().animate({
        opacity:1
      },600, function(){
        $(this).css({
          opacity:'none'
        })
      })
    },
    hideFu:function(){
      var _=this
      _.li.stop().animate({
        height:0
      },600, function(){
        $(this).css({
          display:'none'
        })
      })
      $('#splash').stop().animate({
        height:514
      },600)
      $('.menu').stop().animate({
        opacity:0
      },600)
      $('#back').stop().animate({
        opacity:0
      },600, function(){
        $(this).css({
          display:'none'
        })
      })
    },
    preFu:function(){
      var _=this
      _.li.css({
        position:'absolute', 
        display:'none'
      });
      $('.menu').css({
        opacity:0
      })
      $('#back').css({
        opacity:0, 
        display:'none'
      })
    }
  })
});

$(document).ready(function() {
  Cufon.replace('#menu a, h2, h3, footer, .splash_menu a, #back', {
    fontFamily: 'Bebas', 
    hover:true
  });
  
  Cufon.now();
  
  var baseGallery = {};
  baseGallery = {
    btnNext: ".next",
    btnPrev: ".prev",
    mouseWheel: true,
    visible: 4,
    speed: 600,
    easing: 'easeOutCirc'
  };
  
  $('.gallery_carousel').each(function() {
    $(this).jCarouselLite(baseGallery);
  });
  
});

$(window).load(function() {	
  $('.spinner').fadeOut();
  $('body').css({
    overflow:'inherit'
  })
})
