// JavaScript Document

var slidingTimeline;
var fadingTimeline = false;
var slideSpeedTimeline = 100; // Minimum of 100, higher equals slower

$(document).ready(function()
{
	// Backoffice -------------------------------------------------------------------------
	var backoffice = false;
	if ($('div.controlBorder').length > 0)
		backoffice = true;
	
	
	// Cufon -------------------------------------------------------------------------
	Cufon.now();
	
	Cufon.set('fontFamily', 'Interstate-Regular').replace('h1')('h2')('h3')('.customFont')('div.department a', {hover: {color: '#000000'}});
	Cufon.set('fontFamily', 'Interstate-Light').replace('div.smallBlock h2')('.customFont2')('div#Menu > div > ul > li > a', {hover: {color: '#000000'}});
	
	twitterFeed();
	
	// Menu ------------------------------------------------------------------
	var hoverout;
	var activeHover;
	var dropoutWidth = $('div#Menu ul li ul').width();
	var dropoutLeft;
	
	var menuItems = $('div#Menu > div > ul > li').length;
	var menuItemWidth = $('div#Menu > div > ul > li').outerWidth();
	var menuWidth = menuItems * menuItemWidth;
	$('div#Menu').width(menuWidth);
	
	$('div#Menu > div > ul > li > ul').each(function()
	{
		$(this).append('<div class="dropoutBottom">&nbsp;</div>');
	});
	
	$('div#Menu > div > ul > li').mouseenter(function()
	{
		dropoutLeft = $(this).position().left;
		
		if (hoverout != null)
			clearTimeout(hoverout);
			
		if (activeHover != null)
			closeHover();
			
		if ((dropoutLeft + dropoutWidth) > menuWidth)
		{
			$(this).find('ul').addClass('alignRight')
		}
		
		$(this).addClass('hover');
		
		activeHover = $(this).find('> ul');
	});
	$('div#Menu > div > ul > li').mouseleave(function()
	{
		hoverout = setTimeout(closeHover, 1000);
	});
	
	function closeHover()
	{
		$('div#Menu ul li.hover').removeClass('hover');
		hoverout = null;
		activeHover = null;
	}
	
	// Timeline ------------------------------------------------------------------
	var newClass;
	
	if (backoffice == true)
	{
		$('div.startingPoint').css('display', 'block');
	}
	
	if ($('div.startingPoint span').text() != '')
	{
		var startVal = $('div.startingPoint span').text();
	}
	else
	{
		var startVal = 1;
	}
	
	if($('div#Timeline').length > 0)
	{
		var timelineWidth = $('div#Slider').width();
		var starting = (timelineWidth / 4) * (startVal - 1);
		slider = $('div#Timeline div.position');
	
		slider.slider(
		{
			range: "min",
			value: starting,
			min: 0,
			max: timelineWidth,
			animate: slideSpeedTimeline,
			create: function( event, ui )
			{
				$('div#Timeline div.positionBackground').append('<div class="slider-background">&nbsp</div>');
				
				var leftPos = $('.ui-slider-handle').position().left;
				$('div#Timeline div.positionBackground div.slider-background').css('left', leftPos);
				
				$('div#Timeline div.infoBlock:eq(' + (startVal - 1) + ')').addClass('activeInfo');
				$('div#Timeline div.activeInfo div.block:first').addClass('activeBlock');
				
				startTimeline();
				slideFadeIn();
			},
			slide: function( event, ui )
			{
				var leftPos = $('.ui-slider-handle').position().left;
				$('div#Timeline div.positionBackground div.slider-background').css('left', leftPos);
			},
			change: function( event, ui )
			{
				if (fadingTimeline == true)
					return;
					
				openInfo();
				changeInfo();
			}
		});
		
		$('div#Timeline').live('mouseenter', function()
		{
			clearTimeout(slidingTimeline);
		});
		$('div#Timeline').live('mouseleave', function()
		{
			startTimeline();
		});
	}
	
	function changeInfo()
	{
		if (fadingTimeline == true)
			return;
		
		var thisPos = slider.slider('value');
		var activeInfo = $('div#Timeline div.activeBlock');
		var blockWidth = $('div#Slider div.subject').outerWidth();
		var activeBlock = activeInfo.index() + 1;
		
		var activeInfoIndex = $('div#Timeline div.' + newClass).index() - 1;
		if ($('div#Timeline div.subject:eq(' + activeInfoIndex + ')').position() != null)
		{
			var activeSubject = $('div#Timeline div.subject:eq(' + activeInfoIndex + ')').position().left;
		}
		
		if (thisPos < (blockWidth * 0.2) + activeSubject || thisPos == timelineWidth)
		{
			var newBlock = 1;
		}
		else if (thisPos < (blockWidth * 0.4) + activeSubject)
		{
			var newBlock = 2;
		}
		else if (thisPos < (blockWidth * 0.6) + activeSubject)
		{
			var newBlock = 3;
		}
		else if (thisPos < (blockWidth * 0.8) + activeSubject)
		{
			var newBlock = 4;
		}
		else
		{
			var newBlock = 5;
		}
		
		if ($('div#Timeline div.activeInfo div.block:nth-child(' + newBlock + ')').hasClass('activeBlock') == false)
		{
			fadingTimeline = true;
			
			activeInfo.fadeOut(700, function()
			{
				$('div#Timeline div.infoBlock div.block').removeClass('activeBlock');
				$('div#Timeline div.infoBlock div.block').hide();
				
				if (newClass == 'oplevering')
				{
					$('div#Timeline div.' + newClass + ' div.block:first').addClass('activeBlock');
				}
				else
				{
					$('div#Timeline div.' + newClass + ' div.block:nth-child(' + newBlock + ')').addClass('activeBlock');
				}
				
				$('div#Timeline div.activeInfo div.activeBlock').fadeIn(700, function(){fadingTimeline = false;});
			});
		}
	}
	
	function startTimeline()
	{
		var currentVal = slider.slider('value');
		
		if (currentVal >= $('div#Slider').width())
			return;
		
		slider.slider('value', currentVal + (slideSpeedTimeline / 50));
		$('div#Timeline div.positionBackground div.slider-background').css('left', currentVal + 1);
		
		slidingTimeline = setTimeout(startTimeline, slideSpeedTimeline);
	}
	
	function openInfo()
	{
		var thisPos = slider.slider('value');
		var oldClass = $('div#Timeline div.activeInfo').attr('class');
		
		if (thisPos < timelineWidth * 0.25)
		{
			newClass = ('inventarisatie');
		}
		else if (thisPos < timelineWidth * 0.5)
		{
			newClass = ('schetsfase');
		}
		else if (thisPos < timelineWidth * 0.75)
		{
			newClass = ('uitwerking');
		}
		else if (thisPos < timelineWidth)
		{
			newClass = ('realisatie');
		}
		else
		{
			newClass = ('oplevering');
		}
		
		if (oldClass.search(newClass) == -1)
		{
			//$('div#Timeline div.activeInfo div.activeBlock').fadeOut(700);
			$('div#Timeline div.activeInfo').fadeOut(700, function()
			{
				$('div#Timeline div.activeInfo').removeClass('activeInfo');
				$('div#Timeline div.' + newClass).addClass('activeInfo');
				
				slideFadeIn();
			});
		}
	}
	
	function slideFadeIn()
	{
		$('div#Timeline div.activeInfo').fadeIn(700);
		
		//changeInfo(1);
		
		$('div#Timeline div.activeInfo div.activeBlock').fadeIn(700);
	}
	
	// Teampage -------------------------------------------------------------------------
	$('div.teamBlock div.member').hover(function()
	{
		$(this).addClass('showPopup');
	},
	function()
	{
		$(this).removeClass('showPopup');
	});
	
	$('div.member img').bind('contextmenu', function(e)
	{
		e.preventDefault();
	});
	
	// Partner slider ------------------------------------------------------------------
	var slideSpeed = 400;
	var sliding = false;
	var productSlider;
	var productSliderWidth;
	var products;
	var productWidth;
	var productCount;
	var productsWidth;
		
	if ($('div#PartnerSlider').length > 0)
	{
		$('div#PartnerSlider img').each(function()
		{
			var target = $(this);
			
			if ($(this).parents('a').html() != null)
			{
				target = $(this).parents('a');
			}
			
			target.wrap('<div class="slide"><table border="0" cellspacing="0" cellpadding="0"><tr><td></td></tr></table></div>');
		});

		makeVars($('div#PartnerSlider'));
		
		if (productsWidth >= productSliderWidth)
			products.width(productsWidth);
		
		if (productsWidth > productSliderWidth)
			productSlider.find('div.slideButton.slideRight').addClass('slideActive');
			
		if (backoffice == true)
		{
			$('div#PartnerSlider div.slides').css('left','auto');
			$('div#PartnerSlider div.slides').css('right','0');
		}
	}
	
	function makeVars(target)
	{		
		if (target.attr('id') == 'PartnerSlider')
		{
			var sliderContainer = 'slides';
			var slider = 'slide';
		}
		
		productSlider = target;
		productSliderWidth = productSlider.width();
		products = productSlider.find('div.' + sliderContainer);
		productWidth = products.find('div.' + slider).outerWidth();
		productCount = products.find('div.' + slider).length;
		productsWidth = productWidth * productCount;
	}
	
	$('div.slideButton').click(function()
	{
		if (sliding == true)
			return;
			
		makeVars($(this).parent());
			
		var direction = -1;
		if ($(this).hasClass('slideLeft'))
			direction = 1;
		
		if ($(this).hasClass('slideActive'))
			slideProduct(direction);
		else
			return;
	});
	
	function slideProduct(side)
	{
		var posLeft = products.position().left;
		var maxPos = productsWidth - productSliderWidth;
		var slide = side * productWidth;
		sliding = true;
		
		productSlider.find('div.slideButton').addClass('slideActive');
		
		if (Math.abs(posLeft) == productWidth && side == 1)
		{
			productSlider.find('div.slideLeft').removeClass('slideActive');
		}
		else if (Math.abs(posLeft) >= (maxPos - productWidth) && side == -1)
		{
			productSlider.find('div.slideRight').removeClass('slideActive');
		}
			
		products.animate({left: '+=' + slide}, slideSpeed, function(){sliding = false;})
	}
	
	// Watermark ------------------------------------------------------------------
	$('div#enquete div.enqueteForm_Row input').each(function()
	{
		var label = $(this).parents('div.enqueteForm_Row').find('div.label').text();
		label = label.trim();
		$(this).watermark(label);
	});
	$('div#enquete div.enqueteForm_Row textarea').each(function()
	{
		var label = $(this).parents('div.enqueteForm_Row').find('div.label').text();
		label = label.trim();
		$(this).watermark(label);
	});
	
	// Referenten pagina --------------------------------------------------------------------
	$('div.contentBlock div.leftBlock div.referentInfo table tr td:nth-child(1)').addClass('left');
	
	if ($('div#ImageViewer div.smallImages img').length > 0)
	{
		fadeImage($(this).find('div.smallImages:first img'));
		
		$('div#ImageViewer div.smallImages img').each(function()
		{
			$(this).wrap('<div class="image"></div>');
		});
		
		$('div#ImageViewer div.smallImages img').mouseenter(function()
		{
			fadeImage($(this))
		});
	}
	
	function fadeImage(img)
	{
		if (backoffice == true)
			return;
			
		var image = img.attr('src').replace(/\-small/gi, '');
		var replaceBlock = img.parents('div#ImageViewer').find('div.largeImage');
		var replaceImage = replaceBlock.find('img').attr('src');
		
		if (image != replaceImage)
		{
			replaceBlock.find('img').fadeOut(300, function(){ $(this).remove(); });
			replaceBlock.append('<img src="' + image + '" alt="" />');
			replaceBlock.find('img').fadeIn(300);
		}
	}
	
});

function twitterFeed()
{
	var tweet = $('div#Twitter');
	
	if (tweet.length == 0)
		return false;
	
	$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=everspartners&count=3&include_rts=true&callback=?', function(data)
	{
		var html = '';
		
		$.each(data, function(index, twit)
		{
			var url = 'http://twitter.com/everspartners/status/' + twit.id;
			var date = twit.created_at;
			date = new Date(date);
			
			html += '<div class="tweet">';
			html += '<div class="icon">&nbsp;</div>';
			html += '<div class="text">' + '<a href="' + url + '" target="_blank">' + twit.text + '</a>'
			html += '<div class="created">' + date.toDateString() + '</div></div>';
			html += '</div>';
		});
		
		tweet.html(html);
		tweet.fadeIn();
	});
}
