$(document).ready(function(){

	var viewportHeight = $(window).height();
	var viewportWidth = $(window).width();
	
	var transitionEase = 'easeInOutExpo';
	var totalmediacount = $('#smallSliderHolderInner .media').length;
	var slidecunt = 0;

	function adjustMenues() {

		$('.adjustMenu').each(function(){
			var menuHeight = $(this).height()-17;
			$(this).css({'margin-top':'-'+(menuHeight/2)+'px'});
		});
	
	}
	
	function showMenues() {
		$('#hideMenuLeft, #hideMenuRight').fadeTo(300, 1, function(){
			$('#hideMenuLeft, #hideMenuRight').fadeTo(300, 0, function(){
				$('#hideMenuLeft, #hideMenuRight').hide();
			});
		});
	}
	
	adjustMenues();
	showMenues();
	
	function closeMenuLeft() {
		$('#stayFade').css({'color':'#333'});
		
		$('#menuAnimLeft').animate({
		  marginTop:'0px',
  		  height:'17px'
  		}, 150, function() {
  		  // Animation complete.
  		  
  		});
	}
	
	closeMenuLeft();
	
	function openMenuLeft() {
		$('#stayFade').css({'color':'#aaa'});
		
		$('#menuAnimLeft').animate({
		  marginTop:'-17px',
  		  height:'51px'
  		}, 150, function() {
  		  // Animation complete.
  		  
  		});
	}
	
	$('#menuAnimLeft').hover(function(){
		openMenuLeft();
	}, function(){
		closeMenuLeft();
	});
	
	
	
	function closeMenuRight() {
		$('#eatFade').css({'color':'#333'});
		
		$('#menuAnimRight').animate({
		  marginTop:'0px',
  		  height:'17px'
  		}, 150, function() {
  		  // Animation complete.
  		  
  		});
	}
	
	closeMenuRight();
	
	function openMenuRight() {
		$('#eatFade').css({'color':'#aaa'});
	
		$('#menuAnimRight').animate({
		  marginTop:'-17px',
  		  height:'51px'
  		}, 150, function() {
  		  // Animation complete.
  		  
  		});
	}
	
	$('#menuAnimRight').hover(function(){
		openMenuRight();
	}, function(){
		closeMenuRight();
	});
	
	
	
	
	function setCounter() {
		$('#slideCounter').html((slidecunt+1) + '/' + totalmediacount);
	}
	
	setCounter();
	
	function moveRight() {
	
		if (slidecunt > -1 && slidecunt < (totalmediacount-1)) {
			slidecunt++;
			$('#smallSliderHolder').scrollTo('.media:eq('+slidecunt+')', 600, {easing: transitionEase, offset: 0, axis: 'x', onAfter:function(){
			
				setCounter();
				
				if (slidecunt == (totalmediacount-1)) {
					$('.slide').show();
					$('#slideRight').hide();
				} else {
					$('#slideRight').show();
					$('#slideLeft').show();
				}
    	
    	    	}           
    		});
		} else {
			moveFirst();
		}
	}
	
	
	function moveLeft() {
	
		if (slidecunt > 0 && slidecunt < totalmediacount) {
			slidecunt--;
			$('#smallSliderHolder').scrollTo('.media:eq('+slidecunt+')', 600, {easing: transitionEase, offset: 0, axis: 'x', onAfter:function(){
			
				setCounter();
				
				if (slidecunt == 0) {
					$('.slide').show();
					$('#slideLeft').hide();
				} else {
					$('#slideRight').show();
					$('#slideLeft').show();
				}
    	
    	    	}           
    		});
		} else {

		}
	
	}
	
	function moveFirst() {

		slidecunt = 0;
		$('#smallSliderHolder').scrollTo('.media:eq('+slidecunt+')', 600, {easing: transitionEase, offset: 0, axis: 'x', onAfter:function(){
		
			setCounter();
			$('.slide').show();
			$('#slideLeft').hide();
    	
    	    }           
    	});

	}
	
	function move() {

		$('#smallSliderHolder').scrollTo('.media:eq('+slidecunt+')', 0, {easing: transitionEase, offset: 0, axis: 'x', onAfter:function(){
		
			setCounter();
				if (slidecunt == 0) {
					$('.slide').show();
					$('#slideLeft').hide();
				} else if (slidecunt == (totalmediacount-1)) {
					$('.slide').show();
					$('#slideRight').hide();
				}
    	
    	    }           
    	});

	}
	
	moveFirst();
	
	$('#smallSliderHolder .media').click(function(){
		moveRight();
	});
	
	$('#slideLeft').click(function(){
		moveLeft();
	});
	
	$('#slideRight').click(function(){
		moveRight();
	});
	
	var small = true;
	
	$('#toggleSize').click(function(){
		if (small == true) {
			small = false;
			switchFull();
		} else {
			small = true;
			switchSmall();
		}
	});
	
	shortcut.add("Esc",function() {
		if (small == false) {
			switchSmall();
		}
	});
	
	$('#toggleSize, #slideCounter').fadeTo(0,0.8);
	
	function resizeFull() {
		
		viewportHeight = $(window).height();
		viewportWidth = $(window).width();
       	var navRatio = viewportWidth / viewportHeight;
       	
       	// Loop Media Positions
       	$(".media").each(
       	 
       	    function(intIndex){
       	    	
       	    	var selectorHolder = '.media:eq('+intIndex+')'; 
       	    
       	    	$(selectorHolder).css({height: viewportHeight, width: viewportWidth, marginTop: viewportHeight*intIndex});   	    
       	    	
       	    		$('#toggleSize').css({'background':'#000 url('+styleSheetDir+'/images/goSmall.gif) no-repeat'});
  	        		var selector = '.image:eq('+intIndex+') img';
		    		var selector2 = '.image:eq('+intIndex+')';
    	     			
       	    		imageWidth = $(selector).width();
       	    		imageHeight = $(selector).height();
       	    		
       	    		var imageRatio = imageWidth / imageHeight;
       	    		
       	    		if (navRatio > imageRatio) {
       	    			var newHeight = (viewportWidth / imageWidth) * imageHeight;
       	    			var newWidth = viewportWidth;
       	    		} else {
       	    			var newHeight = viewportHeight;
       	    			var newWidth = (viewportHeight / imageHeight) * imageWidth;
       	    		}
       	    			
       	    		newTop = 0 - ((newHeight - viewportHeight) / 2);
       	    		newLeft =  0 - ((newWidth - viewportWidth) / 2); 
       	 			
       	    		$(selector).css({height: newHeight, width: newWidth, marginTop: 0, marginLeft: 0});
       	    		$(selector2).css({height: viewportHeight, width: viewportWidth, marginTop: 0, marginLeft: 0});
       	    		$('#smallSlider').css({'position':'fixed','left':'0px','top':'0px'});
       	    		$('#smallSliderHolder').css({height: newHeight, width: newWidth});
       	    		
       	    		$(selector2).scrollTop((newHeight - viewportHeight)/2).scrollLeft((newWidth - viewportWidth)/2).width(viewportWidth);

       	    }
       	 
       	);
	
	}
	
	function switchFull() {
		$('html, body').css({'overflow-y':'hidden'});
		$('html, body').css({'overflow-x':'hidden'});
		stopVideo();
		viewportHeight = $(window).height();
		viewportWidth = $(window).width();
		var fullWidth = viewportHeight * (4/3);
		$('#sliderBg').fadeTo(0, 1);
		resizeFull();
		
		$('#toggleSize').css({'left':''+viewportWidth-62+'px'});
		$('#slideRight').css({'left':''+viewportWidth-62+'px','margin-top':''+(viewportHeight-30)/2+'px'});
		$('#slideLeft').css({'margin-top':''+(viewportHeight-30)/2+'px'});
		
		move();
	}
	
	function switchSmall() {
		$('html, body').css({'overflow-y':'auto'});
		$('html, body').css({'overflow-x':'hidden'});
		viewportHeight = $(window).height();
		viewportWidth = $(window).width();
		
		$('#toggleSize').css({'background':'#000 url('+styleSheetDir+'/images/goBig.gif) no-repeat'});
		
		$('#sliderBg').fadeTo(0, 0, function(){
			$(this).hide();
		});
		$('#smallSlider').css({'position':'absolute','top':'auto','left':'auto'});
		$('#smallSliderHolder').height(330).width(440);
		$('#smallSliderHolder .media img, #smallSliderHolder .media').height(330).width('auto');
		
		$('#toggleSize').css({'left':'378px'});
		$('#slideRight').css({'left':'378px','margin-top':'150px'});
		$('#slideLeft').css({'margin-top':'150px'});
		
		move();
		setVideo();
	}
	
	$('.hoverlink').hide();
	
	$('.hoverlink').hover(function(){
	
		//$(this).fadeTo(100, 1);
	
	}, function(){
	
		//$(this).fadeTo(100, 0);
	
	});
	
	$('.hoverlink_trigger').hover(function(){
	
		$(this).parent().find('.hoverlink').show();
		centerHoverText();
	
	}, function(){
	
		$(this).parent().find('.hoverlink').hide();
	
	});
	
	var boxHeight = 0;
	var innerTextHeight = 0;
	
	function centerHoverText() {
		$('.hoverlink').each(function(){
			boxHeight = $(this).height();
			innerTextHeight = $(this).find('.hoverlinktext').height();
			
			$(this).find('.hoverlinktext').css({'padding-top':''+(boxHeight/2)-(innerTextHeight/2)+'px'});
		});
	}
	
	centerHoverText();
	
	var homeOverlay = false;
	
	function closeHomeOverlay() {
		if (homeOverlay == true) {
			homeOverlay = false;
			$('#homeOverlay').fadeTo(150, 0, function(){
				$(this).hide();
			});
		}
	}
	
	closeMenuRight();
	
	function openHomeOverlay() {
		if (homeOverlay == false) {
			homeOverlay = true;
			$('#homeOverlay').fadeTo(300, 1);
		}
	}
	
	$('.homeOverlayTrigger').mouseenter(function(){
		openHomeOverlay();
	}).mouseleave(function(){
		closeHomeOverlay();
	});
	
	
	var vidLength = 0;
	var volume = 0;
	
	if (wpView == 'single') {
		volume = 100;
	} else {
		volume = 0;
	}
	
	function setVideo() {
	
	    if ($('#video').length) {
	    
	    	$('#video').show();
	
	        var videoUrl = $('#video').attr('name');
	        var videoPosterImage = $('#video').find('.videoInner').attr('name');
	        videoPosterImage = jQuery.trim(videoPosterImage);
	        //alert(videoPosterImage);
	        var videoSpanWidth = 880;
	        var videoSpanHeight = 440;
	
			//alert(videoPosterImage);
			var players;
			if(navigator.appName == "Microsoft Internet Explorer") {
				var players = [{
						type: 'flash',
						src: ''+styleSheetDir+'/jwplayer/player.swf'
				}];
			}
			else {
				var players = [{
						type: 'html5'
					}, {
						type: 'flash',
						src: ''+styleSheetDir+'/jwplayer/player.swf'
				}];
			}
	        jwplayer('mediaplayer').setup({
	            'id': 'player1',
	            'width': videoSpanWidth,
	            'height': videoSpanHeight,
	            'file': videoUrl,
	            'skin': ''+styleSheetDir+'/jwplayer/skins/beelden/beelden.zip',
	            'autostart': true,
	            'volume': volume,
	            //'mute': true,
	            'repeat': 'none',
	            'controlbar': 'none',
	            'image': videoPosterImage,
	            'events': {				
	                onError: function(event) {
						
					},
					onComplete: function(event) {
						
					},
	                onTime: function (event) {
	                	vidLength = jwplayer('mediaplayer').getDuration()-0.9;
	                	
	                    if (event.position > vidLength) {
	                    	jwplayer('mediaplayer').seek(0);
	                    }
	                }
	            },
				'players': players					
				            

	        });			
	
	    }
	
	}
	
	setVideo();
	
	function stopVideo() {
		/*
   		var allPlayers = jwplayer.getPlayers();
    	for (i = 0; i < allPlayers.length; i++) {
        	allPlayers[i].stop();
    	}
    	*/
    	
    	$('#video').hide();
	}
	
	$('.hide').fadeTo(0,0);
	
	if ($('.box').length) {
		
		$('#midSection').masonry({
  			columnWidth: 110, 
  			itemSelector: '.box' 
		}, function(){
			$('.box').fadeTo(200,1);
		});
		
	}
	
	function clearAlt() {
   		$('img').attr('title', '');
	}

	clearAlt();
	
	var link = '';
	
	$('.grablink a').each(function(){
		link = $(this).attr('href');
		$(this).parents().nextAll('.hoverlink_trigger').find('img').wrap('<a href="'+link+'"></a>');
		//$(this).parents().nextAll('.hoverlink_trigger').find('img').append('</a>');
	});

	
	$(window).resize(function(){
		viewportHeight = $(window).height();
		viewportWidth = $(window).width();
		
		if (small == true) {
			switchSmall();
		} else {
			switchFull();
		}
	});
	
});
