var activeElm;
var superSized;
(function($) {
  $.fn.fullBg = function(){
    var bgImg = $(this);		
    
    settings = {
      min_width : 1000
    }
    
    function resizeImg() {
    
      
      var imgwidth = bgImg.width();
      var imgheight = bgImg.height();
      
//       console.log(imgwidth, imgheight);
      
      
      
      var winwidth = $(window).width();
      var winheight = $(window).height();
 
      var widthratio = winwidth / imgwidth;
      var heightratio = winheight / imgheight;
 
      var widthdiff = heightratio * imgwidth;
      var heightdiff = widthratio * imgheight;
 
      if(heightdiff>winheight) {
        bgImg.css({
          width: winwidth+'px',
          height: heightdiff+'px'
        });
      } else {
        bgImg.css({
          width: widthdiff+'px',
          height: winheight+'px'
        });		
      }
    } 
    resizeImg();
    $(window).resize(function() {
      resizeImg();
    }); 
    jQuery(document).ready(function(){
      resizeImg();
    });

  };
})(jQuery)

jQuery(document).ready(function(){
  $('input, textarea').placeholderFunction('input-focused');
	jQuery("div.right .full-size").fullBg();
	jQuery("div.left .full-size").fullBg();
	jQuery('.left .block, .right .block').hover(
	  function(){
	    jQuery(this).parent().css({
	      'z-index' : 1
	    });
	    animateIn('.'+ jQuery(this).parent().attr('class'));
	  },
	  function(){
	    animateOut('.'+ jQuery(this).parent().attr('class'));
	  }
	);
	
	jQuery('a.slide-up').live('click', function(){
    jQuery('#gallery-wrapper').animate({
      top : '495px'
    });
    jQuery(this).removeClass('slide-up').addClass('slide-down');	  
	  return false;
	});
  
	jQuery('a.slide-down').live('click', function(){
    jQuery('#gallery-wrapper').animate({
      top : '0'
    });      
    jQuery(this).removeClass('slide-down').addClass('slide-up');	  
	  return false;
  });
  
  if(!no_supersized) {
      superSized = jQuery.supersized({
      slideshow : 1,
      autoplay : 1,
      start_slide : 1,
      slide_interval : 3000,
      transition : 1,
      transition_speed : 1000,
      new_window : 1,
      pause_hover : 0,
      keyboard_nav : 0,
      performance : 1,
      min_width : 0,
      min_height : 0,
      vertical_center : 0,
      horizontal_center : 0,
      fit_portrait : 1,
      fit_landscape : 0,
      navigation : 1,
      thumbnail_navigation : 0,
      slide_counter : 1,
      slide_captions : 1,
      slides : images
    });

    $("#scrollable").scrollable({
      keyboard : false
    }).navigator({
      navi : 'nav.paginator ul',
      naviItem : 'li'
    });
  }
  
  jQuery('nav.paginator ul li').bind('click', function(){
    jQuery('nav.paginator ul').find('li').each(function(){
      jQuery(this).removeClass('active-li');
    });
    jQuery(this).addClass('active-li');
  });
  
  jQuery('nav.paginator ul').find('li').each(function(i){
    jQuery(this).html(i+1);
  });

	jQuery('nav ul li').hover(
	  function(){
	    jQuery(this).addClass('active');
	  },
	  function(){
	    jQuery(this).removeClass('active');	  
	  }
	);
	
	jQuery(window).resize(function(){
	  jQuery(activeElm).css({
	    width : (jQuery(window).width() - 50) +'px'
	  });
	});
	
	jQuery('#pauseplay').bind('click', function(){
	  if(jQuery(this).hasClass('play')) {
	    jQuery(this).addClass('pause');
	    jQuery(this).removeClass('play');
	  }
	  else {
	    jQuery(this).removeClass('pause');
	    jQuery(this).addClass('play');	    
	  }
	});
});

function animateIn(elm) {
  activeElm = elm;
  var otherElm;
  var half_screen = Math.ceil(jQuery(window).width() / 2);
  
  if(elm == '.left') { otherElm = '.right'; } else { otherElm = '.left'; }
  jQuery(otherElm).css({'z-index' : 0});

  
  jQuery(elm).stop().animate({
    width : (jQuery(window).width() - 50) +'px'
  },200);
}

function animateOut(elm) {
  var otherElm;
  var half_screen = Math.ceil(jQuery(window).width() / 2);
  
  if(elm == '.left') { otherElm = '.right'; } else { otherElm = '.left'; }
  
  jQuery(elm).stop().animate({
    width : half_screen +'px'
  });
}

jQuery('form#direct-contact').live('submit', function(){
  jQuery(this).validate({
    rules : {
      naam : 'required',
      email : {
        required : true,
        email : true
      },
      opmerkingen : 'required'
    },
    messages : {
      naam : '',
      email : '',
      opmerkingen : ''
    },
    submitHandler: function(form) {
      jQuery.ajax({
        type : 'POST',
        url : '../includes/submit-mail.php',
        data : {
          naam                          : jQuery('input#naam').val(),
          email                         : jQuery('input#email').val(),
          opmerkingen                   : jQuery('textarea#opmerkingen').val()
        },
        success : function(i) {
          if(i == 'success') {
            var inner_height = jQuery('div.inner').height();
            jQuery('div.inner').height(inner_height);
            jQuery('#contact-wrapper').fadeOut(500, function(){
              jQuery(this).remove();
              jQuery('div#success').fadeIn(200);
            });
          }
        }
      });
    }    
  });
  return false;
});

