var activeTimeout;
var activeObject;
var menuTimeout;
var menuOpen;

var homeNews;
var newsWidth;
var newsActiveWidth;

var galleryIndex = 1;
var imageFolder;
var imageName;
var imageCount;

var newsTimeouts = new Array();

$(document).ready(function() {
	
	$('div#HomeNews').each(function()
	{
		var newsItems = $(this).find('div.newsItem');
		var newsSize = Math.floor($(this).width() / (newsItems.length + 2));
				
		newsItems.eq(0).addClass('firstNews');
		newsItems.eq(newsItems.length - 1).addClass('lastNews');

		newsItems.each(function()
		{
			$(this).width(newsSize);
			$(this).attr('sizer', newsSize);
			
			$(this).mouseenter(function()
			{
				var oldOne = newsItems.filter('.newsActive');
				oldOne.addClass('newsVert');
				oldOne.removeClass('newsActive');
				oldOne.width(parseInt(oldOne.attr('sizer')));
				
				$(this).removeClass('newsVert');
				$(this).width(parseInt($(this).attr('sizer')) * 3);
				
				$(this).addClass('newsActive');
			});
			
		});
	
		newsItems.eq(0).mouseenter();
	});
	/*
	if ($('div#HomeNews').length > 0)
	{
		homeNews = $('div#HomeNews');
		// Make first active
		homeNews.find('div.newsItem:first').addClass('newsFirst newsActive'); 
		// Remove vertical styling
		homeNews.find('div.newsItem:first').removeClass('newsVert'); 
		
		var newsItems = homeNews.find('div.newsItem').length;
		newsWidth = Math.floor((homeNews.width() * 0.8)) / (newsItems - 1);
		var newsHeight = homeNews.height();
		newsActiveWidth = homeNews.width() / 5;
		
		homeNews.find('div.newsItem').css('width', newsWidth);
		
		homeNews.find('div.newsItem div.newsContainer').height((homeNews.width() * 0.8) / (newsItems - 1));
		homeNews.find('div.newsItem div.newsContainer').css('left', -(newsHeight / 2) + (newsWidth / 2));
		homeNews.find('div.newsItem div.newsContainer').css('top', (newsHeight / 2) - (newsWidth / 2));
		
		homeNews.find('div.newsItem:first').css('width', newsActiveWidth);
		
		if ($.browser.msie == true)
		{
			homeNews.find('div.newsItem div.newsContainer').css({left: 0, top: 0, backgroundColor: '#ffffff'});
		}
	}
	
	$('div#HomeNews div.newsItem').mouseenter(function()
	{
		animateNews($(this));
	});
	*/
	
	if ($('div.controlBorder').length > 0)
		var backoffice = true;
						   
	if ($('div#SiteContainer').attr('class') == 'home')
	{
		$('body').addClass('home');
	}
	
	if (backoffice)
	{
		$('div#KlantenHeader').css('zIndex','0');
	}
	
	if ($("#dialog").length > 0)
	{
		openDialog();
	}

	var menu = $('#Menu #Items li:not(#Menu #Items li li)');
    
    $('#Tab').click(function() {
	if ($('#InfoPanel').is(":hidden"))
	{
		$('#InfoPanel').slideDown("slow");
	}
	else
	{
		$('#InfoPanel').slideUp("slow");
	}
    });
	
	if ($('div.button div.fold').length)
	{
		var folds = $('div.button div.fold');
		
		folds.css('display', 'none');
		
		$(folds).parent().find('h2').click(function ()
		{
			//$(this).parent().find('span.arrow').toggleClass('active');
			
			$(this).parent().find('div.fold').toggle('slow');
		});
	}
	
	if ($('ul.reference').length)
	{
		var items = $('ul.reference li');
		
		if (!$('div.controlBorder').length)
		{

			$(items).click(function()
			{
				window.location = $(this).find('a.link').attr('href');
			});

		}
		
		$(items).mouseover(function()
		{
			$(this).addClass('active');
		});
		$(items).mouseout(function()
		{
			$(this).removeClass('active');
		});
	}
	
	$('div#Content ul#Klanten li:not(.inactive)').mouseover(function()
	{
		$(this).addClass('hover');
	});
	$('div#Content ul#Klanten li').mouseleave(function()
	{
		$(this).removeClass('hover');
	});
	
	/* Menu ----------------------------------------------------------------------------------------------------------------------- */

	/*$('div#Header div#Menu > ul > li').mouseenter(function()
	{
		if (menuOpen != null)
		{
			clearTimeout(menuTimeout);
			$(menuOpen).css('display','none');
		}
		
		$(this).find('ul:first').css('display','block');
		menuOpen = $(this).find('ul:first');
		
		var menuWidth = $(this).find('div.dropoutMenu').width();
		menuWidth -= 12;
		$(this).find('div.dropoutMenu div.top, div.dropoutMenu div.bottom').width(menuWidth);
		menuWidth -= 14;
		$(this).find('div.dropoutMenu div.quickLink').width(menuWidth);
		
		var menuHeight = $(this).find('div.dropoutMenu').height();
		menuHeight -= 12;
		$(this).find('div.dropoutMenu div.leftBackground, div.dropoutMenu div.rightBackground').height(menuHeight);*/
		
		/* -------------------------------------------------------------------------- */
		/*if (menuOpen.attr('moved') != 'yes')
		{
			var base = $('div#SiteContainer');
			var basePos = base.offset();
			
			var pos = menuOpen.offset();
			
			var totalSize = pos.left - basePos.left + menuOpen.width();
			if (totalSize > base.width())
			{
				menuOpen.css('left', base.width() - totalSize);
				menuOpen.attr('moved', 'yes');
			}
		}*/
		/* -------------------------------------------------------------------------- */
		
	/*});
	
	$('div#Header div#Menu > ul > li').mouseleave(function()
	{
		closeMenu();
	});
	
	function closeMenu()
	{
		menuTimeout = setTimeout(function() { $(menuOpen).css('display','none') }, 400);
	}*/
	
	$('div#Header div#Menu > ul > li').mouseenter(function()
	{
		clearTimeout(menuTimeout);	
					
		if(menuOpen != null)
		{
			menuOpen.removeClass('hover');
		}
		
		$(this).addClass('hover');					
		
		menuOpen = $(this);	
		
		$(this).find('> ul:visible').each(function()
		{
			
			var container = $('div#SiteContainer').width();			
			var pos = $(this).parent().position();			
			var fullSize = pos.left + $(this).width();
			
			if (fullSize > container)
			{
				$(this).css('left', (container - fullSize - 3) + 'px');
			}
			
			var menuHeight = $(this).find('div.dropoutMenu').height();
			menuHeight -= 12;
			$(this).find('div.dropoutMenu div.leftBackground, div.dropoutMenu div.rightBackground').height(menuHeight);
			
		});		
		
	});
	
	$('div#Header div#Menu > ul > li').mouseleave(function()
	{			
		menuTimeout = setTimeout(closeMenu, 500);
	});
	
	function closeMenu()
	{
		menuOpen.removeClass('hover');
	}
		
	// Language bar ------------------------------------------------------------------------------------------
	var languageTimeout;
	var languageActive;
	
	$('div#LanguageBar div.selectLanguage').mouseenter(function()
	{
		if (languageTimeout != null)
			clearTimeout(languageTimeout);
			
		languageActive = $(this).find('div.dropoutLanguage');
		languageActive.fadeIn(200);
	});
	$('div#LanguageBar div.selectLanguage').mouseleave(function()
	{
		languageTimeout = setTimeout(function(){languageActive.fadeOut(200)}, 300);
	});
	
	//Cufon.replace($('h2'));

	var klant = $('h1.icon.flashtitle span').text();

	var flashvars = {};
	flashvars.SlideTime = 6;
	flashvars.TransitionTime = 1;
	flashvars.xmlUrl = '/content/flash/xml/' + klant + '_Product_Slider_NL.xml';
	
	var params = {};
	params.menu = "false";
	params.wmode = "transparent";
	params.allowscriptaccess = "always";
	
	var attributes = {};
	attributes.id = "myDynamicContent";
	attributes.name = "myDynamicContent";
	
	swfobject.embedSWF("/content/flash/Product_Slider.swf", "FlashContainer", "260", "208", "9.0.0","/content/flash/expressInstall.swf", flashvars, params, attributes);
	
	var windowwidth = $(window).width();
	
	if (windowwidth > $('div#Content').width())
	{
		$('html').css('overflow-x','hidden');
	}
	
	//Social Networks
	loadSocial();
	
	$('div#SocialMediaVideo').html('<object width="540" height="327"><param name="movie" value="http://www.youtube.com/v/FNHeCLo72H0?fs=1&amp;hl=en_US&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/FNHeCLo72H0?fs=1&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="540" height="327"></embed></object>');
	
	$('div#SocialMediaVideo').css('padding-top', '15px');
	
	// Klant slideshow ----------------------------------------------------------------------------------
	if ($('div#Slideshow').length > 0)
	{
		if (backoffice)
			return false;
			
		var slideshow = $('div#Slideshow');
		
		slideshow.find('div.thumbs img').each(function()
		{
			var location = $(this).attr('src');
			var newloc = location.replace(/_thumb/gi, '');
			$(this).wrap('<a href="' + newloc + '" rel="Shadowbox[Slideshow]"></a>');
			Shadowbox.init();
		});
		
		var slidecount = slideshow.find('div.thumbs a').length;
		var direction = 1;
		var slidespeed = 5000;
		var slideshowActive;
		var noslide = false;
		
		if (slidecount == 0)
		{
			slideshow.find('div.slide').remove();
			return false;
		}
		else if (slidecount == 1)
		{
			slideshow.find('div.thumbs a:first').addClass('active');
			slideshow.find('div.slide').remove();
			return false;
		}
		else if (slidecount > 1)
		{
			slideshow.find('div.thumbs a:first').addClass('active');
			slideshowActive = setTimeout(function(){startSlide(direction);}, slidespeed);
		}
	}
	$('div#Slideshow div.slide').click(function()
	{
		if (noslide == true)
			return;
			
		clearTimeout(slideshowActive);
		
		if ($(this).hasClass('slideLeft') == true)		
			startSlide(-1);
		else
			startSlide(direction);
	});
	function startSlide(side)
	{
		var activeslide = slideshow.find('div.thumbs a.active');
		var index = activeslide.index();
		var active = index + side;
		noslide = true;
		
		if (active >= slidecount)
		{
			active = 0;
		}
		else if (active < 0)
			active = (slidecount - 1)
		
		var newslide = slideshow.find('div.thumbs a:eq(' + active + ')');
		
		if (side == 1)
		{
			newslide.addClass('right');
			activeslide.animate({left: '-242px'}, 500, function(){activeslide.removeClass(); activeslide.removeAttr('style')});
		}
		else if (side == -1)
		{
			newslide.addClass('left');
			activeslide.animate({left: '242px'}, 500, function(){activeslide.removeClass(); activeslide.removeAttr('style')});
		}
		else
			return false;
			
		newslide.animate({left: '0'}, 500, function(){newslide.attr('class','active'); newslide.removeAttr('style'); noslide = false;});
		slideshowActive = setTimeout(function(){startSlide(direction);}, slidespeed);
	}
	
	if ($('a#RTL').length > 0)
	{
		Shadowbox.init();
	}
	
	
	// Load images -------------------------------------------------------------------------------------------------------
	if ($('div#Gallery div.galleryInfo').length > 0)
	{
		var gallery = $('div#Gallery');
		imageFolder = gallery.find('div.imageFolder span').text();
		imageName = gallery.find('div.imageName span').text();
		imageCount = gallery.find('div.imageCount span').text();
		
		loadImageRange();
	}

});
	
$(window).resize(function()
{
	var windowwidth = $(window).width();
	
	if (windowwidth > $('div#Content').width())
	{
		$('html').css('overflow-x','hidden');
	}
	else
	{
		$('html').css('overflow-x','visible');
	}
	
});
	
$(window).scroll(function()
{
	if (galleryIndex == imageCount - 1) return;
	
	var ref = $(document).height() - $(window).height() - $(window).scrollTop();
	
	if (ref <= 200)
	{
		loadImageRange();
	}
});


function makeInActive()
{
	$(activeObject).removeClass('active');
}

function flashLightbox(activeSlide, xmlUrl){
	var flashvars = {};
	flashvars.SlideTime = 6;
	flashvars.TransitionTime = 1;
	flashvars.startSlide = activeSlide
	flashvars.xmlUrl = xmlUrl;

	var params = {};
	params.menu = "false";
	params.wmode = "transparent";
	params.allowscriptaccess = "always";
	
	var attributes = {};
	attributes.id = "myDynamicContent2";
	attributes.name = "myDynamicContent2";
	
	swfobject.embedSWF("/content/flash/Product_Slider_Large.swf", "FlashContainer2", "618", "494", "9.0.0","/content/flash/expressInstall.swf", flashvars, params, attributes);

	$("#dialog").dialog('open');
}
//open dialogbox
//$(document).ready(function(){

function openDialog(){
	$("#dialog").dialog({
		autoOpen: false,
		modal: true,
		width: 622,
		height:510,
		draggable: false,
		resizable:false,
		close: function() { 
			$('#myDynamicContent2').remove();
			$('div#dialog').append('<div id="FlashContainer2">&nbsp;</div>');
		}
	});
}
//close dialogbox
function closeDialog(){
	$('#dialog').dialog("close");
	
}

function animateNews(newsItem)
{
	var activeNews = homeNews.find('div.newsActive');
	
	if (newsItem.hasClass('newsVert') == false)
	{
		return false;
	}
	
	homeNews.find('div.newsItem').addClass('newsVert');
	homeNews.find('div.newsItem').removeClass('newsActive');
	newsItem.removeClass('newsVert');
	newsItem.addClass('newsActive');
	
	homeNews.find('div.newsItem').css('width', newsWidth);
	newsItem.css('width', newsActiveWidth);
}

function loadImage(index)
{
	if (galleryIndex - 1 == imageCount) return;
	
	if (galleryIndex <= 9)
	{
		galleryIndex = '0' + galleryIndex;
	}

	$('div#GalleryContent').append('<div class="thumb"><a href="/content/images/photo/' + imageFolder + '/large/' + imageName + galleryIndex + '.jpg"><img src="/content/images/photo/' + imageFolder + '/thumb/' + imageName + galleryIndex + '.jpg" /></a></div>');

	$('div#GalleryContent div.thumb a').lightBox();
	$('div#GalleryContent a:last').fadeIn();

	galleryIndex++;
	
	if (galleryIndex == 20)
	{
		loadImageRange();
	}
}

function loadImageRange()
{
	var range = 20;
	var galleryStart = galleryIndex;
	
	for (iLoad = galleryIndex; iLoad <= (galleryStart + range); iLoad++)
	{
		loadImage(iLoad);
	}
}
