// JavaScript Document

var debug = false;
var heights = new Array();
var timer;
var gaatOpen;

$(document).ready(function($)
{
	if(debug)
	{
		$('#wrapper').prepend('<div id="debug" style="position: absolute; top: 10px; color: #000;" />');
	}
						  						   
	$('h3').mouseover(function()
	{
		if(!$(this).hasClass('active'))
		{
			$(this).addClass('styleactive');
		}
	}).mouseout(function()
	{
		if(!$(this).hasClass('active'))
		{
			$(this).removeClass('styleactive');
		}
	});
	
	$('h3').click(function()
	{
		var waar = $(this).parent().parent().attr('id');
		
		$(this).addClass('active').removeClass('styleactive');
		
		if($(this).attr('rev') == 'no_preview')
		{
			$(this).next().children('div.first').empty();
			
			var id = 'temp_' + $(this).attr('id');
			$(this).next().append ('<div style="display: none;"><p><a href="#" class="ajax_a" id="' + id + '">&nbsp;</a></p></div>');
			doorKlik($("#" + id));
			
		}
		else
		{
			$(this).next().slideDown('normal', 'swing');
		}
		
		sIFR.replace(font, {
			selector: 'h5'
			,css: [
				'.sIFR-root { color: #512470; font-size: 21px; font-weight: bold }'
			]
		});
		
		$(this).parent().siblings().children('h3').each(function()
		{	   
			if($(this).hasClass('active'))
			{
				if($(this).children('.a_popup_close').length > 0)
				{
					$(this).children('.a_popup_close').remove();
					
					var id = $(this).attr('id');
						
					$this = $(this);
					
					$this.next().animate({ 'height': '0px'}, 'normal', 'swing', function()
					{
						$this.animate({ 'width' : '184px'}, 'normal', 'swing').next().animate({ 'width' : '198px' }, 'normal', 'swing', function()
						{
							$("#" + id).removeClass('active open styleactive').next().css('height', 'auto');
							$("#" + id).next().hide().children().css('display','block');
							$("#" + id).next().children('.more').empty();
							/*$this.next().css('background', '#abaa9b url(images/gradient.jpg) top left no-repeat');*/
							
							$this.next().children("div:not(.more, .first)").remove();
						});
					});
				}
				else
				{
					$(this).removeClass('active open styleactive').next().slideUp('normal', 'swing').blur();
				}
			}
			else
			{
				$(this).removeClass('active open styleactive').next().slideUp('normal', 'swing').blur();
			}
		});
		
		$(this).parent().parent().siblings().children('li').children('h3').each(function()
		{	   
			if($(this).hasClass('active') && $(this).hasClass('open'))
			{
				if($(this).children('.a_popup_close').length > 0)
				{
					$(this).children('.a_popup_close').remove();
					
					var id = $(this).attr('id');
						
					$this = $(this);
					
					$this.next().animate({ 'height': '0px'}, 'normal', 'swing', function()
					{
						$this.animate({ 'width' : '184px'}, 'normal', 'swing').next().animate({ 'width' : '198px' }, 'normal', 'swing', function()
						{
							$("#" + id).removeClass('active open styleactive').next().css('height', 'auto');
							$("#" + id).next().hide().children().css('display','block');
							$("#" + id).next().children('.more').empty();
							/*$this.next().css('background', '#abaa9b url(images/gradient.jpg) top left no-repeat');*/
							
							$this.next().children("div:not(.more, .first)").remove();
						});
					});
				}
				else
				{
					//$(this).removeClass('active open styleactive').next().slideUp('normal', 'swing').blur();
				}
			}
			else
			{
				//$(this).removeClass('active open styleactive').next().slideUp('normal', 'swing').blur();
			}
		});
	});
	
	$(".ajax_a, .project, .alinea, .nieuwsbericht").click(function() { doorKlik ($(this)); });
	
	$("h3.active").next().show();
	
	$(".colorboxiframe").colorbox({ maxWidth:"75%", maxHeight:"75%" });
	
	setInterval (zIndexFix, 400);
		
});

$().click(function() { 
	timer = setInterval (function() { document.title = 'Stadkwadraat'; }, 75);
	setTimeout (function() { clearInterval (timer); }, 1500);
});

function zIndexFix () {
	
	if ($("h3.open").length > 0)
		resetZindex($("h3.open").parent().parent().attr('id'));
	else
		resetZindex();
	
}

function doorKlik (elm) {
	
	clearTimeout (gaatOpen);
	
	//console.log($(elm).parent().parent().parent().prev().parent().parent().attr('id'));
	
	//var element_id = $(elm).parent().parent().parent().parent().parent().attr('id');
	
	
	$("h3.open").each (function() { 
		closeElement($(elm));
	});
	
	id = $(elm).parent().parent().parent().prev().attr('id');
	heights[id] = $(elm).parent().parent().height();
								 
	var waar = $(elm).parent().parent().parent().parent().parent().attr('id');
	
	var id = $(elm).parent().parent().parent().prev().attr('id');
	resetZindex($(elm).parent().parent().parent().parent().parent().attr('id'));
	$("#" + id).addClass('open').next().css({'background': '#FFFFFF', 'border-left': '2px solid #f0f0f1', 'border-bottom': '2px solid #f0f0f1'});
							
	if ($(elm).parent().parent().parent().prev().attr('id').match(/nieuws/)) {
		cmd = 'nieuwsbericht';
		if ($(this).hasClass('nieuwsbericht'))
			vars = $(elm).attr('rel');
		else
			vars = $(elm).parent().parent().parent().prev().attr('id').replace('nieuws_', '');
	} else if ($(elm).parent().parent().parent().prev().attr('id').match(/vacature/)) {
		cmd = 'vacature';
		vars = $(elm).parent().parent().parent().prev().attr('id').replace('vacature_', '');
	} else if ($(elm).parent().parent().parent().prev().attr('id').match(/stadinzicht/)) {
		cmd = 'alinea';
		if ($(elm).hasClass('alinea'))
			vars = $(elm).attr('rel');
		else
			vars = $(elm).parent().parent().parent().prev().attr('id').replace('stadinzicht_', '');
	} else if ($(elm).parent().parent().parent().prev().attr('id').match(/discipline_/)) {
		cmd = 'project';
		if ($(elm).hasClass('project'))
			vars = $(elm).attr('rel');
		else
			vars = $(elm).parent().parent().parent().prev().attr('id').replace('discipline_', '');
	}
	
	$.get('ajax.php', { cmd: cmd, vars: vars, param: Math.floor(Math.random()*999999) }, function(data)
	{
		document.title = 'Stadinzicht';
		
		if($('#' + id).attr('rel') == '3kolom')
		{
			var elm = 593;
			var sec = 606;
		}
		else if($('#' + id).attr('rel') == '4kolom')
		{
			var elm = 798;
			var sec = 812;
		}
		else
		{
			var elm = 388;
			var sec = 402;
		}
		
		$('#' + id).animate({ 'width' : elm + 'px' }, 'normal', 'swing', function()
		{
			if(sec == 402)
			{
				$(this).next().css({ 'background-image': 'url(images/content_bg_402px.gif)' });
			}
			else if(sec == 606)
			{
				$(this).next().css({ 'background-image': 'url(images/content_bg_607px.gif)' });
			}
			else if(sec == 812)
			{
				$(this).next().css({ 'background-image': 'url(images/content_bg_812px.gif)' });
			}
			
			/*var margin = 810-sec;
			if(margin < 0){
				margin=0;
			}
			
			if((element_id == 'element2' || element_id == 'element3') && sec == 402)
			{
				$('#'+element_id).css({'padding-left': margin/2+'px'});
				$('#'+element_id).css({'padding-right': margin/2+'px'});
			}
			else
			{
				if( $(this).next().css('float') == 'right')
				{
					$('#'+element_id).css({'padding-left': margin+'px'});
				}
				else
				{
					$('#'+element_id).css({'padding-right': margin+'px'});
				}
			}*/
			
			if($('#' + id + ' .a_popup_close').length == 0)
			{
				$('#' + id).append('<a href="#" class="a_popup_close"><img src="images/popup_close.png" alt="sluiten" class="close_popup" /></a>');
			}
			
			$('#' + id).next().children('.more').css({ 'display': 'block', 'padding': '8px' }).html(data).slideDown('normal', 'swing');
			
			$('#' + id).next().children('div:first').hide();
			sIFR.replace(font,
			{
				selector: 'h5, .do_sifr span',
				css:
				[
					'.sIFR-root { color: #512470; font-size: 21px; font-weight: bold }'
				]
			});
			
			$('.colorbox').colorbox();
			$('a.flash').prettyPhoto();
			$('.colorboxiframe').colorbox({ maxWidth: '75%', maxHeight: '75%' });
			$('.colorboxblog').colorbox({ maxWidth: '840px', maxHeight: '75%' });
			
			$('.project_popup').click(function()
			{
				getProject($('#' + id).next().children('.more'), $(this).attr('rel'));
			});
			
			$('.alinea_popup').click(function()
			{
				getAlinea($('#' + id).next().children('.more'), $(this).attr('rel'));
			});
			
			$('.nieuwsbericht_popup').click(function()
			{
				getNieuwsbericht($('#' + id).next().children('.more'), $(this).attr('rel'));
			});
			
			$('.vacature_popup').click(function()
			{
				getVacature($('#' + id).next().children('.more'), $(this).attr('rel'));
			});
			
			$('.a_popup_close').click(function()
			{
				var waar = $(this).parent().parent().parent().parent().attr('id');

				$this = $(this);
				var id = $(this).parent().attr('id');
				
				if(heights[id] > 0)
				{
					height = heights[id] + 36;
				}
				else
				{
					height = 0;
				}
				
				$this.parent().next().animate({'height': height + 'px'}, 'normal', 'swing', function()
				{
					$this.parent().animate({'width': '184px'}, 'normal', 'swing').next().css({ 'background-image': 'url(images/content_bg.gif)' }).animate({'width': '198px' }, 'normal', 'swing', function()
					{
						$this.parent().next().css('height', 'auto');
						
						$this.parent().next().children('div:not(.more, .first)').remove();
						
						if(height > 0)
						{
							$this.parent().next().children('div:first').show();
						}
							
						$this.parent().removeClass('open');
						
						if(height == 0)
						{
							$this.parent().removeClass('active');
						}
							
						$this.parent().next().children('.more').empty().hide();
						$this.parent().next().css({ 'display' : 'none' });
						$this.parent().children('.a_popup_close').remove();
						
						document.title = 'Stadinzicht';
					 });												  
				});
			});
		}).next().animate({'width' : sec + 'px' });
	});
}

function getProject(elm, project_id)
{
	$.get('ajax.php', { cmd: 'project', vars: project_id }, function(data)
	{
		elm.html(data).slideDown('normal', 'swing');
		
		sIFR.replace(font, {
			selector: 'h5'
			,css: [
				'.sIFR-root { color: #512470; font-size: 21px; font-weight: bold }'
			]
		});
		
		$('.colorbox').colorbox();
		$('.colorboxiframe').colorbox({ maxWidth: '75%', maxHeight: '75%' });
		$('a.flash').prettyPhoto();
		
		$('.project_popup').click(function()
		{
			getProject(elm, $(this).attr('rel'));
		});
	});
}

function getAlinea(elm, alinea_id)
{
	$.get('ajax.php', { cmd: 'alinea', vars: alinea_id }, function(data)
	{			   
		elm.html(data).slideDown('normal', 'swing');
		
		sIFR.replace(font, {
			selector: 'h5'
			,css: [
				'.sIFR-root { color: #512470; font-size: 21px; font-weight: bold }'
			]
		});
		
		$('.colorbox').colorbox();
		$('.colorboxiframe').colorbox({ maxWidth: '75%', maxHeight: '75%' });
		$('a.flash').prettyPhoto();
		
		$('.alinea_popup').click(function()
		{
			getAlinea(elm, $(this).attr('rel'));
		});
	});
}

function getVacature(elm, vacature_id)
{
	$.get('ajax.php', { cmd: 'vacature', vars: vacature_id }, function(data)
	{			   
		elm.html(data).slideDown('normal', 'swing');
		
		sIFR.replace(font, {
			selector: 'h5'
			,css: [
				'.sIFR-root { color: #512470; font-size: 21px; font-weight: bold }'
			]
		});
		
		$('.colorbox').colorbox();
		$('.colorboxiframe').colorbox({ maxWidth: '75%', maxHeight: '75%' });
		$('a.flash').prettyPhoto();
		
		$('.vacature_popup').click(function()
		{
			getVacature(elm, $(this).attr('rel'));
		});
	});
}

function getNieuwsbericht(elm, nieuwsbericht_id)
{
	$.get('ajax.php', { cmd: 'nieuwsbericht', vars: nieuwsbericht_id }, function(data)
	{   
		elm.html(data).slideDown('normal', 'swing');
		
		sIFR.replace(font, {
			selector: 'h5'
			,css: [
				'.sIFR-root { color: #512470; font-size: 21px; font-weight: bold }'
			]
		});
		
		$('.colorbox').colorbox();
		$('.colorboxiframe').colorbox({ maxWidth: '75%', maxHeight: '75%' });
		$('a.flash').prettyPhoto();
		
		$('.nieuwsbericht_popup').click(function()
		{
			getNieuwsbericht(elm, $(this).attr('rel'));
		});
																	   
	});
	
}

function resetZindex(id)
{
	if(debug)
	{
		$('#debug').html(id);
	}
		
	$('#element1').css('z-index', '4');
	$('#element2').css('z-index', '3');
	$('#element3').css('z-index', '2');
	$('#element4').css('z-index', '1');
	
	if(id)
	{
		$('#' + id).css('z-index', '5');
	}
		
	/*if(id == 'element1')
	{
		$('#element1').css('width', '801px');
	}
	else
	{
		$('#element1').css('width', 'auto');
	}
	
	if(id == 'element3')
	{
		$('#element3').css({'right': '38px', 'padding-right': '300px' });
	}	
	else
	{
		$('#element3').css({'right': '330px', 'padding-right': '0' });
	}*/
}

function closeElement($this)
{
	id = $this.attr('id');
	
	if(heights[id] > 0)
	{
		height = heights[id]+36;
	}
	else
	{
		height = 0;
	}
	
	$this.next().animate({'height': height + 'px'}, 'normal', 'swing', function()
	{
		$this.animate({'width' : '184px'}, 'normal', 'swing').next().animate({'width' : '198px'}, 'normal', 'swing', function()
		{						
			$('#' + id).removeClass('open styleactive').next().css('height', 'auto');
			$('#' + id).next().children('.more').empty();
			$('#' + id).next().children().css('display', 'block');
			$('#' + id).children('.a_popup_close').remove();
		});
	});
}

function goLightbox(url) {
	$.fn.colorbox({href: url, open:true});
}
