$(function(){

  // homepage code slider
  $('#coda-slider-1').codaSlider();

  // scrollers
  $('#back-to-top').localScroll();
  $('#subnav').localScroll();
  $('nav').localScroll();

  // agenda
  $('.details').hide();
  $('.event').click(function(){
    $('.details', this).toggle('slow');
  });

  $('.event').hover(
    function(){
        $(this).addClass('hovered');
        $(this).css('cursor','pointer');
    },
    function(){
        $(this).removeClass('hovered');
        $(this).css('cursor','default');
  });


  // validations
  $("#new_humin_company").validate();
  $("#new_humin_designer").validate();

    setupAjaxForms();

  // scroll
    $(window).scroll(function(){
        if($(window).scrollTop()>=250){
            $("#back-to-top").fadeIn();
        } else {
            $("#back-to-top").fadeOut();
        }
    });


  // tooltip, tnx Coda and http://jqueryfordesigners.com/coda-popup-bubbles/
  Tooltip ();

	disableForms();
});

var setupAjaxForms = function(){
    $('.feedback, .error, div#humin_designer, a#bedrijf_link').hide();
    
    $('a#bedrijf_link').click(function(e){
        $('a#bedrijf_link, div#humin_designer').hide();
        $('a#designer_link, div#bedrijf').show();
        
        e.preventDefault();
    });
    
    $('a#designer_link').click(function(e){
        $('a#bedrijf_link, div#humin_designer').show();
        $('a#designer_link, div#bedrijf').hide();
        
        e.preventDefault();     
    });

    $('form#new_humin_company,form#new_humin_designer').ajaxForm({
      beforeSubmit: function(){
            $('input#humin_company_submit, input#humin_designer_submit').hide();
            $('div#humin_company_spinner, div#humin_designer_spinner').show();
        },
        success: function(){
            $('div.feedback').show();
        },
        error: function(){
            $('div.error').show();          
        },
        complete: function(){
            $('div#bedrijf, div#humin_designer, div.formnav').hide();       
        }
    });

}

function Tooltip() {
     $('.bubbleInfo').each(function () {
         var distance = 10;
         var time = 250;
         var hideDelay = 500;

         var hideDelayTimer = null;

         var beingShown = false;
         var shown = false;
         var trigger = $('.trigger', this);
         var info = $('.popup', this).css('opacity', 0);


         $([trigger.get(0), info.get(0)]).mouseover(function () {
             if (hideDelayTimer) clearTimeout(hideDelayTimer);
             if (beingShown || shown) {
                 // don't trigger the animation again
                 return;
             } else {
                 // reset position of info box
                 beingShown = true;

                 info.css({
                     top: -60,
                     left: 0,
                     display: 'block'
                 }).animate({
                     top: '-=' + distance + 'px',
                     opacity: 1
                 }, time, 'swing', function() {
                     beingShown = false;
                     shown = true;
                 });
             }

             return false;
         }).mouseout(function () {
             if (hideDelayTimer) clearTimeout(hideDelayTimer);
             hideDelayTimer = setTimeout(function () {
                 hideDelayTimer = null;
                 info.animate({
                     top: '-=' + distance + 'px',
                     opacity: 0
                 }, time, 'swing', function () {
                     shown = false;
                     info.css('display', 'none');
                 });

             }, hideDelay);

             return false;
         });
     });
 }

var disableForms = function(){
	$('body#humin').find('a#designer_link,div#humin_designer').hide();
  $('body#my-precious').find('a#designer_link,div#humin_designer').hide();
}
