/* 
 * Developed by AMAI bvba (http://amai.be)
 * author: Bram Joris <bram at amai.be)
 */

var stepTime = 1000;
var birdTimeout;
var handjesTimeOut;
var resetMoverTimeOut;
var animateDruppel1timeout; 
var animateDruppel2timeout;
var animateVullingtimeout;
var resetMoverTimeout;
var animateTopBottlestimeout; 
	
var flesDoorPijpTo = 1314;//= flesMidden4

//header = 156
//spacer = 385
//details = 625
	
// $(window).load(function(){
// 	
//   init();	
// 
// }); 

$( function(){
    
    if (!( $.browser.msie && $.browser.version=="6.0"))
    {
      init();
    }
    else
    {
      $('#filter').remove();
    }
    
    
    $('.scroller').mousemove( function(e){
        var pos = e.pageY - $(this).offset().top;       
        var containerHeight = $(this).height();
        var contentHeight = $(this).children('div.scrollerContent').height() ;
        var hiddenHeight = contentHeight - containerHeight;
        
        var posP = Math.round((pos/containerHeight)*100);
        
        $(this).children('div.scrollerContent').animate( { top: ((hiddenHeight*( posP/100 ))*-1) }, { duration: 400, queue: false, easing: 'easeOutCirc' } );
    } );
    
    $('#textServices a').hover( function(){
      $(this).find('span.readmore').addClass('hover');
    }, function(){
      $(this).find('span.readmore').removeClass('hover');
    } );
    
    $('#panelAnnouncements a').hover( function(){
      $(this).find('h2 span').addClass('hover');
    }, function(){
      $(this).find('h2 span').removeClass('hover');
    } );
    
//     
//     $('#filter').css( {width: $(window).width()} );
//     
//     $(window).resize( function(){
//       $('#filter').css( {width: $(window).width()} );
//     } );
if (mobile)
{
  $('#filter').remove();
  $('#gras').css( 'width', 1700);
  $('#disclaimerBox').css( 'width', 975);
  $('#disclaimerBox > div').css( 'width', 975);
}


  $('a.keepUsPosted').click( function(e){
    e.preventDefault();
    $.scrollTo( $('#textStayTuned'), {
      axis: 'y',
      duration: 600,
      easing: 'easeOutCirc',
      onAfter: function(){
        $('#panelStayTuned').animate( { scale: 1.4 }, { duration: 200, easing: 'easeInExpo', complete: function(){
          $('#panelStayTuned').animate( { scale: 1 }, { duration: 1000, easing: 'easeOutElastic' } );
        } } );
      }
    } );
  } );

  
  pulseAttentionBirds();
  setInterval( function(){
    pulseAttentionBirds();
  }, 2500 );
  
  
} );

function pulseAttentionBirds()
{

  $('#attentionBirdLeft').animate( { left: 0 }, { duration: 400, easing: 'easeInExpo', complete: function(){
    $('#attentionBirdLeft').animate( { left: -82 }, { duration: 1200 } );
  } } );
  
  $('#attentionBirdRight').animate( { right: -27 }, { duration: 400, easing: 'easeInExpo', complete: function(){
    $('#attentionBirdRight').animate( { right: -109 }, { duration: 1200 } );
  } } );

}

function init(){
	ajaxStuff();
	
	if ( $.browser.msie )
  {    
    if ( $('#handjes').is(':visible') )
    {
      seizoenStuff();

    	detailsHeightSet(360);
    	animateTopBottles();
    	animatePackaging();
    	hideBirds();
    	animateBird();
    	
    	randomClouds();
    }
  }
  else
  {
  if ( ! mobile )
  {
    seizoenStuff();
  
  	detailsHeightSet(360);
  	animateTopBottles();
  	animatePackaging();
  	hideBirds();
  	animateBird();
  	
  	randomClouds();
	}
	}
	
	
	addHandler($("#resultPanel1"),resultPanel1Ani );
	addHandler($("#resultPanel2"),resultPanel2Ani );
	addHandler($("#resultPanel3"),resultPanel3Ani );
	
// 	$('body').mousemove( function(e) {
//     $('#ajax_loader').css( { left: e.pageX+10, top: e.pageY+15 } );
// //     $('#ajax_loader').stop().animate( { left: e.pageX+10, top: e.pageY+15 }, { duration: 100 } );
//   } );
  
  $('body').bind('ajaxSend', function() {
    $(this).addClass('ajaxLoading');
  } );
	$('body').bind('ajaxComplete', function() {
    $(this).removeClass('ajaxLoading');
  } );
 }

function detailsHeightSet(detailsHeight){
	$('div#container').css({'display':'block'});

	//adjusting the animation
	flesDoorPijpTo = $('#header').height() + $('#spacer').height() + detailsHeight + 174;
	//rode streep height = details.height - 228
	
	var handjeshouderHeight =  detailsHeight - 350;
  
	$('#handjesHouder').animate({height:handjeshouderHeight},{duration:600, queue:false, easing:'easeInOutCubic'});
	$('#gras').animate({top:flesDoorPijpTo+400},{duration:600, queue:false, easing:'easeInOutCubic'});
}


function animateDetailsHeight(toHeight){
	$('#details').animate({height:toHeight},{duration:600, queue:false, easing:'easeInOutCubic'})
               .css('overflow', 'visible'); //animate fix
}

function randomClouds(){
	for ( var i = 1; i < 6; i++) {
		var sCloud = "#wolk" + i;
		var cloud = $(sCloud);
		var maxLeft = $('#gras').width()-cloud.width();
		var maxTop = 1000;
		
		var newLeft = Math.random()*maxLeft;
		var newTop =  Math.random()*(maxTop/2)+Math.random()*(maxTop/2);
		
		//cloud.css({left:newLeft, top:newTop});
	}
	
	setInterval ( moveClouds, 100 );
}

function moveClouds(){
	for ( var i = 1; i < 6; i++) {
		var sCloud = "#wolk" + i;
		var currrentLeft = parseInt($(sCloud).css("left").substring(0,$(sCloud).css("left").indexOf("px"))); 
		if(currrentLeft>$('#gras').width()){
			resetCloud($(sCloud));
		}else{
			$(sCloud).css({left:currrentLeft+1});	
		}
	}
}

function resetCloud(cloud){	
	var newLeft = -cloud.width();
	var newTop =  150 + Math.random()*750 ;
	cloud.css({left:newLeft, top:newTop});
	
}

function addHandler(panelDiv,animation ){
	panelDiv.mouseenter(function() {
		animation.stopAnimation();
		animation.direction(1);
		animation.startAnimation();
	}).mouseleave(function() {
		animation.stopAnimation();
		animation.direction(-1);
		animation.startAnimation();
	});
}

function hideBirds(){
	$('#vogelZwartRechtsNaarLinks').css({'display':'none'});
	$('#vogelZwartLinksNaarRechts').css({'display':'none'});
	$('#vogelWitLinksNaarRechts').css({'display':'none'});
	$('#vogelWitRechtsNaarLinks').css({'display':'none'});
	
	//$('#vogelZwartRechtsNaarLinks').stop();
	//$('#vogelZwartLinksNaarRechts').stop();
	//$('#vogelWitLinksNaarRechts').stop();
	//$('#vogelWitRechtsNaarLinks').stop();
}

function animateBird(){
	//alert("animateBird");
	var birdNumber = Math.floor(Math.random()*4);
	if(birdNumber <0 || birdNumber >3)birdNumber=1;
	//alert("animateBird birdNumber" + birdNumber);
	var sBird;
	var leftFrom;
	var topFrom;
	var leftTo;
	var topTo;
	
	switch(birdNumber){
		case 0:
			sBird = $('#vogelZwartRechtsNaarLinks'); 
			break;
		case 1:
			sBird = $('#vogelZwartLinksNaarRechts');
			break;
		case 2:
			sBird = $('#vogelWitLinksNaarRechts');
			break;
		case 3:
			sBird = $('#vogelWitRechtsNaarLinks');
			break;
	}
	
	sBird.css({'display':'block'});
	
	var randomAdd = Math.random() * 1000;
	
	if(birdNumber==1 || birdNumber==2){
		leftFrom = 0-sBird.width();
		topFrom = 100+ Math.random()*500 + randomAdd;
	}else{
		leftFrom = $('#gras').width();
		topFrom = 100+Math.random()*500 + randomAdd;
	}
		
	if(birdNumber==1 || birdNumber==2){
		leftTo = $('#gras').width();
		topTo = 100+ Math.random()*500 + randomAdd;
	}else{
		leftTo = -sBird.width();
		topTo = 100 +Math.random()*500 + randomAdd;
	}

	//alert("leftFrom: " + leftFrom + " leftTo" + leftTo);
	var timeVar = stepTime*10 + stepTime*10 *Math.random(); 
	sBird.css({left:leftFrom, top:topFrom});
	sBird.animate({top:topTo,left:leftTo},{duration:timeVar, easing:'linear', queue:false, complete: nextBird});
}

function nextBird(){
	hideBirds();
	birdTimeout = setTimeout(animateBird, 5000*Math.random());
}

function animatePackaging(){
	$('#grijperarmen').css({'display':'none'});
	$('#gevuldePalet').stop();
	$('#gevuldePalet').css({'display':'none'});
	
	$('#grijper').stop();
	$('#dubbeldoosjes1').stop();
	$('#dubbeldoosjes2').stop();
	
	$('#grijper').css({left:740, top:86});
	$('#dubbeldoosjes1').css({left:747, top:270});
	$('#dubbeldoosjes2').css({left:747, top:270});
	
	$('#grijper').animate({top:134, easing:'easeInOutCubic'}, stepTime*2);
	$('#dubbeldoosjes1').animate({top:332, easing:'easeInOutCubic'}, stepTime*2);
	
	$('#grijper').animate({left:412, easing:'easeInOutCubic'}, stepTime*3);
	$('#dubbeldoosjes1').animate({left:419, easing:'easeInOutCubic'}, stepTime*3,grijperVisibleProceed);
}

function grijperVisibleProceed(){
	$('#grijperarmen').css({'display':'block'});
	$('#grijper').animate({top:305, easing:'easeInOutCubic'}, stepTime*2);
	$('#dubbeldoosjes1').animate({top:504, easing:'easeInOutCubic'}, stepTime*2);
	
	//terug
	$('#grijper').animate({top:134, easing:'easeInOutCubic'}, stepTime*2, grijperVisibleProceed2);
	
}

function grijperVisibleProceed2(){
	$('#grijperarmen').css({'display':'none'});
	$('#grijper').animate({left:740, easing:'easeInOutCubic'}, stepTime*3);
	$('#grijper').animate({top:86, easing:'easeInOutCubic'}, stepTime*2, nextStepPackaging);
	
	$('#doosje').css({left:531});
	$('#doosje').animate({left:740, easing:'linear'}, stepTime*3, doosjeOpnieuw);
}

function doosjeOpnieuw(){
	$('#doosje').css({left:531});
	$('#doosje').animate({left:740, easing:'linear'}, stepTime*3);
}

function nextStepPackaging(){
	$('#grijper').stop();
	
	$('#grijper').animate({top:134, easing:'easeInOutCubic'}, {duration:stepTime*2, queue:true});
	$('#dubbeldoosjes2').animate({top:332, easing:'easeInOutCubic'}, stepTime*2);
	
	$('#grijper').animate({left:412, easing:'easeInOutCubic'}, stepTime*3);
	$('#dubbeldoosjes2').animate({left:419, easing:'easeInOutCubic'}, stepTime*3, nextStepPackagingGrijperVisible);
}

function nextStepPackagingGrijperVisible(){
	$('#grijperarmen').css({'display':'block'});
	$('#grijper').animate({top:265, easing:'easeInOutCubic'}, stepTime*2);
	$('#dubbeldoosjes2').animate({top:464, easing:'easeInOutCubic'}, stepTime*2);
	$('#grijper').animate({top:134, easing:'easeInOutCubic'}, stepTime*2, nextStepPackagingGrijperVisible2);
}

function nextStepPackagingGrijperVisible2(){
	$('#grijperarmen').css({'display':'none'});
	$('#grijper').animate({left:747, easing:'easeInOutCubic'}, stepTime*3);
	$('#grijper').animate({top:86, easing:'easeInOutCubic'}, stepTime*2, intoVrachtwagen);
}
	
function intoVrachtwagen(){
	$('#doosje').css({left:531});
	$('#doosje').animate({left:740, easing:'linear'}, stepTime*3, doosjeOpnieuw);
	
	$('#dubbeldoosjes1').css({left:747, top:270});
	$('#dubbeldoosjes2').css({left:747, top:270});
	
	//$('#gevuldePalet').stop();	
	$('#gevuldePalet').css({'display':'block'});
	$('#gevuldePalet').css({left:418, top:462});
	$('#gevuldePalet').animate({left:98, easing:'linear'}, stepTime*5, camionWegrijden);
}

function camionWegrijden(){
	
	$('#gevuldePalet').css({'display':'none'});
	//**berekenen left
	$('#camion').animate({left:-800, easing:'linear'}, stepTime*10, camionTerugrijden);
}

function camionTerugrijden(){
	$('#camion').animate({left:-7, easing:'linear'}, stepTime*10, animatePackaging);
}


function animateTopBottles(){
	//tandwielOnderAni.startAnimation();
	//tandwielMiddenRechtsAni.startAnimation();
	//tandwielMiddenLinksAni.startAnimation();
	//tandwielbovenAni.startAnimation();
	
	handjesTimeOut = setTimeout(handjesAnimatie, stepTime*3);
	
	$('#flesBoven1').stop();
	$('#flesBoven2').stop();
	$('#flesBoven3').stop();
	$('#flesMidden1').stop();
	$('#flesMidden2').stop();
	$('#flesMidden3').stop();
	$('#flesMidden4').stop();
	
	$('#flesOnder1').stop();
	$('#flesOnder2').stop();
	$('#flesOnder3').stop();
	//bottles
	//beginpoint op band L:497 T:325;
	//middenpoint op band L:693 T:325;
	//eindpoint op mover L:889 T:325;	
	$('#flesBoven1').css({left:497});
	$('#flesBoven2').css({left:693});
	$('#flesBoven3').css({left:889, top:326});
	$('#flesMidden1').css({left:909});
	$('#flesMidden2').css({left:653});
	$('#flesMidden3').css({left:397});

	$('#flesMidden4').css({left:140, top:427});	
	$('#vullingF3').css({height:0});
	$('#vullingF3').css({'display':'none'});
	$('#flesOnder1').css({left:17, top:174});
	$('#flesOnder2').css({left:163, top:174});//146
	$('#flesOnder3').css({left:309, top:174});
	
	$('#flesBoven1').animate({left:693, easing:'easeInOutCubic'}, stepTime*4);
	$('#flesBoven2').animate({left:889, easing:'easeInOutCubic'}, stepTime*4, resetTopBottles);
	
	$('#flesBoven3').animate({left:889+20, easing:'easeInOutCubic'}, {duration:stepTime*0.8, queue:false} );
	$('#flesBoven3').animate({top:427, easing:'easeInOutCubic'}, {duration:stepTime*2*0.8, queue:false} );
	
	$('#mover').animate({left:893, easing:'easeOutCubic'}, {duration:stepTime*0.8, queue:false});
	$('#mover').animate({top:324, easing:'easeInOutCubic'}, {duration:stepTime*2*0.8, queue:false});
	resetMoverTimeOut = setTimeout(resetMover, stepTime*2*0.8);

	//startpoint midden: L:909
	//eindpoint midden: L140
	//256 spatie
	
	$('#flesMidden1').animate({left:653, easing:'easeInOutCubic'}, {duration:stepTime*4, queue:false});
	$('#flesMidden2').animate({left:397, easing:'easeInOutCubic'}, {duration:stepTime*4, queue:false});
	$('#flesMidden3').animate({left:140, easing:'easeInOutCubic'}, {duration:stepTime*4, queue:false});	
	animateDruppel1timeout = setTimeout(animateDruppel1, stepTime*(3+0.5));
	animateDruppel2timeout = setTimeout(animateDruppel2, stepTime*4.5*0.9);
	animateVullingtimeout = setTimeout(animateVulling, stepTime*4*0.97);

	var randomRot = -Math.random()*10 - 50;
	
//	$('#flesMidden4').left()
	
	//$('#flesMidden4').stop();
	$('#flesMidden4').animate({left:72, easing:'easeInCubic'}, {duration:stepTime*0.8, queue:true});
	//rotate:randomRot
	$('#flesMidden4').animate({left:10,top:485}, {duration:stepTime*0.5, queue:true});
	//rotate:0
	$('#flesMidden4').animate({left:17,top:510, easing:'easeOutCubic'}, {duration:stepTime*0.3, queue:true});
	//calculate top
//	alert("miljaar");
//leftFrom = $('#gras').width()
	var valueTopFlesMidden4= $('#flesMidden4').css("top");
	
	//alert(parseInt($('#flesMidden4').css("top").substring(0,$('#flesMidden4').css("top").indexOf("px"))) );
//	alert("flesDoorPijpT:" + flesDoorPijpTo + "top:" +valueTopFlesMidden4 );
//rotate:0, 
	$('#flesMidden4').animate({top:flesDoorPijpTo, easing:'easeInCubic'}, {duration:stepTime*2, queue:true});
	
	$('#flesOnder1').animate({left:163, easing:'easeInOutCubic'}, {duration:stepTime*4, queue:false});
	$('#flesOnder2').animate({left:309, easing:'easeInOutCubic'}, {duration:stepTime*4, queue:false});
	$('#flesOnder3').animate({left:455, easing:'easeInOutCubic'}, {duration:stepTime*4, queue:false}, resetTopBottles);	
}

function handjesAnimatie(){
	handjesAni.setRepeat(true);
	handjesAni.startAnimation();
	handjesAni.setRepeat(false);
}

function animateDruppel1(){
	animateDruppel('#druppel1');
}

function animateDruppel2(){
	$('#druppel2').css({'display':'block'});
	$('#druppel2').css({top:177});
	$('#druppel2').animate({top: 405, easing:'easeInCubic'}, {duration:stepTime*0.5, queue:false,  complete: function() {
			$(this).css({top:177});
	      	$(this).css({'display':'none'});
    	}
	});
}

function animateVulling(){
	$('#vullingF3').animate({height:28}, {duration:stepTime, queue:false,  complete: function() {
	      //$(this).css({'display':'none'});
		}
	});
}

function animateDruppel(druppel){
	$(druppel).css({'display':'block'});
	$(druppel).css({top:177});
	$(druppel).animate({top: 405, easing:'easeInCubic'}, {duration:stepTime*0.5, queue:false,  complete: function() {
			$(this).css({top:177});
	      	$(this).css({'display':'none'});
    	}
	});
}

function resetMover(){
	//mover begin L:873 T:223
	//mover rechts :L893
	//mover beneden : T 324
	resetMoverTimeout = setTimeout(resetMover2, stepTime);
	$('#mover').animate({top:224, easing:'easeInOutCubic'}, {duration:stepTime*2*0.8, queue:false});
}

function resetMover2(){
	$('#mover').animate({left:873, easing:'easeOutCubic'}, {duration:stepTime*0.8, queue:false});	
}

function resetTopBottles(){
	//tandwielOnderAni.stopAnimation();
	//tandwielMiddenRechtsAni.stopAnimation();
	//tandwielMiddenLinksAni.stopAnimation();
	//tandwielbovenAni.stopAnimation();
	//handjesAni.stopAnimation();
	animateTopBottlestimeout = setTimeout(animateTopBottles, stepTime);
}
