var fixed = false;
var timeOuts = new Array();



$(function(){
	// hover functionality projects for /projecten
	// paginator for projects
	$('.slider').each(function(){
		var id = '#paginator-' + $(this).attr('id');
		$(this).cycle({
			fx: 'fade',
			pager: id,
			pagerEvent: 'mouseover'
		});
	});
	$('.slider').cycle('pause');
	
	// show the paginator only if there are more than 1 images
	$('.project').each(function(){
		
		var numberOfImages = 0;
		$('.slider img', this).each(function(){
			numberOfImages++;
		});
		
		if (numberOfImages) {
			$(this).bind("mouseenter", function(){
				$(this).children('.image').stop(false, true).fadeOut();
				$(this).children('.slider').stop(false, true).fadeIn();
				if (numberOfImages > 1) {
					$(this).children('.slider_paginator_wrapper').children('.slider_paginator').stop(false, true).slideDown('fast');
				}
			}).bind("mouseleave", function(){
				$(this).children('.image').stop(false, true).fadeIn('normal');
				$(this).children('.slider').stop(false, true).fadeOut('normal');
				if (numberOfImages > 1) {
					$(this).children('.slider_paginator_wrapper').children('.slider_paginator').stop(false, true).slideUp('fast');
				}
			});
		}
		
	});
	
	// EMPTY fancybox on hover project. All the event handlers are removed.
	$('.project').hover(function(){
		$('#fancy_ajax').empty();
	});
	
  // popup functionality.
  // use fancybox jQuery plugin
	enableFancybox();
	
	function hookEvent(element, eventName, callback)
	{
  	if(typeof(element) == "string")
    	element = document.getElementById(element);
  	if(element == null)
    	return;
  	if(element.addEventListener)
  	{
    	if(eventName == 'mousewheel')
      	element.addEventListener('DOMMouseScroll', callback, false);  
    	element.addEventListener(eventName, callback, false);
  	}
  	else if(element.attachEvent)
    	element.attachEvent("on" + eventName, callback);
	}

	function unhookEvent(element, eventName, callback)
	{
  	if(typeof(element) == "string")
    	element = document.getElementById(element);
  	if (element == null)
    	return;
  	if(element.removeEventListener)
  	{
    	if(eventName == 'mousewheel')
     		element.removeEventListener('DOMMouseScroll', callback, false);  
			element.removeEventListener(eventName, callback, false);
  	}
  	else if(element.detachEvent)
    	element.detachEvent("on" + eventName, callback);
	}
	
	function  cancelEvent(e)
	{
  	e = e ? e : window.event;
  	if (e.stopPropagation)
    	e.stopPropagation();
  	if (e.preventDefault)
    	e.preventDefault();
  	e.cancelBubble = true;
  	e.cancel = true;
  	e.returnValue = false;
  	return false;
	}
	
	function startFancybox() {
		hookEvent(window, 'mousewheel', cancelEvent);
		$(document).bind("keydown", function(e) {
			if (e.keyCode == 40 || e.keyCode == 38) { // arrow up and arrow down
				cancelEvent(e);
			}
			
			if (e.keyCode == 38) {
				// go to next image in popoup
				$('.popup_image_list').cycle('next');
			}
			
			if (e.keyCode == 40) {
				$('.popup_image_list').cycle('prev');
			}
		});
	}
	
	function closeFancybox() {
		unhookEvent(window, 'mousewheel', cancelEvent);
		$(window).unbind('keypress');
	}
	
	function enableFancybox(){
  	$(".project .title a, .homepage_project, .show_project a").fancybox({
    	'zooomSpeedIn':       300,
    	'zoomSpeedOut':       300,
    	'overlayOpacity':     0.9,
    	'overlayColor':       '#212121',
    	'enableEscapeButton': true,
    	'easingIn':           'easeOutBack',
    	'easingOut':          'easeInBack',
    	'frameWidth':         880,
    	'frameHeight':        591,
    	'hideOnContentClick': false,
    	'hideOnOverlayClick': true,
    	'padding':            0,
			'centerOnScroll': 		true,
			'callbackOnStart': 		startFancybox,
			'callbackOnClose': 		closeFancybox
  	});

		$('.project').click(function(){
			triggerFancybox($(this));
		});
	}
	
	function disableFancybox(){
		$(".project .title a").unbind('click.fb');
		$('.project').unbind('click');
	}
	
	function triggerFancybox($project){
		$project.children('.text').children('.title').children('a:first').trigger('click');
	}

	// open fancybox on click paginator of project on /projects
	$('.slider_paginator a').click(function(){
		var startingslide;
		var $anchor = $(this);
		$anchor.parent('.slider_paginator').children('a').each(function(i){
			if ($(this).html() == $anchor.html()) {
				startingslide = i;
			}
		});
		$anchor = $anchor.parent('.slider_paginator').parent('.slider_paginator_wrapper').next('.image').next('.text').children('.title:first').children('a:first');
		var href = $anchor.attr('href');
		$anchor.attr('href', href+'/'+startingslide).trigger('click').attr('href', href);
		return false;
	});  

});
