function changeBackground(div)
{
	$.ajax({
		url: 'contents.php?image=true',
		cache: false,
		success: function(html)
		{
			$(div + 'x').css('background-image','url(' + html + ')'); 
		}
	});
}
function cfunctions(div)
{
	$(document).ready(function() 
	{
		$('.refgallerie a').click(function(event) 
		{
			event.preventDefault();
		
			var previmg = parseInt($(".refgallerie .refimg").html());
			var maximg = $(".refgallerie .inactive").html();
			
			if($(this).hasClass('prev'))
			{
				var newimg = previmg - 1;
				if(newimg < 1) { newimg = 1; }
			}
			else 
			{
				var newimg = previmg + 1;
				if(newimg > maximg) { newimg = 1; }
			}
			$.ajax({
				url: 'contents.php?gallerie=true&id=' + $(this).attr('name') + '&newimg=' + newimg,
				cache: false,
				success: function(html)
				{
					$(".threex").css('background-image','url(' + html + ')'); 
				}
			});
			$(".refgallerie .refimg").html('');
			$(".refgallerie .refimg").html(newimg);
		});
		$('.backbutton').click(function(event)
		{
			event.preventDefault();
			
			if(div == 'three')
			{
				$('.' + div + 'x').css('background-image', $("#cache").css('background-image'));
				cfunctions('three');
			}
			
			$('.' + div + ' .txt').html('');
			$('.' + div + ' .txt').html($('#cache').html());
			$('#cache').html('');

			initFunctions();
		});
	});
}
function initFunctions()
{
	$(document).ready(function() 
	{
		$('a.teamimg').hover(function(event) 
		{	
			event.preventDefault();
			$('.onex').css('background-image','url(images_layout/team/' + $(this).attr('id') + '.jpg)'); 
		});		
		$('a.plan').click(function(event) 
		{	
			event.preventDefault();
			$('.fivex').css('background-image','url(images_layout/plan/1.jpg)'); 
		});
		$('a.part').click(function(event) 
		{	
			event.preventDefault();
			var name = $(this).attr('name').split('.');
			var id = name[0];
			var part = name[1];

			$.ajax({
				url: 'contents.php?child=true&ajax=true&name=' + div + '&id=' + id + '&next=' + part,
				cache: false,
				success: function(html)
				{
					$('.' + div + ' .txt').html('');
					$('.' + div + ' .txt').append(html);
					initFunctions();
				}
			});
		});
		$('a.div2').click(function(event) 
		{
			$('.txt2').show();
			
			event.preventDefault();
			var name = $(this).attr('name').split('.');
			var id = name[0];
			var part = name[1];

			$.ajax({
				url: 'contents.php?child=hide&ajax=true&name=' + div + '&id=' + id + '&next=' + part,
				cache: false,
				success: function(html)
				{
					$('.two .txt2').html('');
					$('.two .txt2').append(html);
					initFunctions();
				}
			});
		});
		
		$('.reflist a').click(function(event) 
		{
			event.preventDefault();
			$.ajax({
				url: 'contents.php?ajax=true&child=true&name=three&id=3',
				cache: false,
				success: function(html)
				{
					$('.' + div + ' .txt').html('');
					$('.' + div + ' .txt').append(html);
					cfunctions(div);
					initFunctions();
				}
			});
		});

		$('.subnavi a').click(function(event) 
		{
			event.preventDefault();
			$('.txt2').hide();
			var name = $(this).attr('name').split('.');
			var id = name[0];
			div = name[1];
			var ref = '';
			
			if(name[2] == 'ref') 
			{
				$.ajax({
					url: 'img.php?id=' + id,
					cache: false,
					success: function(html)
					{
						$("#preload").html('');	
						$("#preload").html(html);
					}
				});
				$.ajax({
					url: 'contents.php?gallerie=true&id=' + id,
					cache: false,
					success: function(html)
					{
						$(".threex").css('background-image','url(' + html + ')'); 
					}
				});
				ref = '&ref=true';
			}
			else
			{
				changeBackground('.' + div);
			}
						
			$('#cache').html('');
			$('#cache').html($('.' + div + ' .txt').html());
			$("#cache").css('background-image', $(".threex").css('background-image')); 
	
			$.ajax({
				url: 'contents.php?ajax=true' + ref + '&child=true&name=' + div + '&id=' + id,
				cache: false,
				success: function(html)
				{
					$('.' + div + ' .txt').html('');
					$('.' + div + ' .txt').append(html);
					cfunctions(div);
					initFunctions();
				}
			});
		});
	});
}

$(document).ready(function() 
{
	$('.txt2').hide();
	$('.title').click(function(event) 
	{
		event.preventDefault();
		$('.txt2').hide();
		$('#intro').animate({ width: '0px' }, 1000, function()
		{
			$('#intro').html('');
		});
		
		div = $(this).attr('id');
		
		$.ajax({
			url: 'contents.php?ajax=true&child=true&name=' + div + '&id=load',
			cache: false,
			success: function(html)
			{
				$('.' + div + ' .txt').html('');
				$('.' + div + ' .txt').append(html);
				cfunctions(div);
				initFunctions();
			}
		});
		
		var p = $(this).parent()[0];
		if(p.opened != 'undefinied')
		{
			var opendiv = p.opened;
			$(this).animate({ width: '0px' }, 300);
			$(p.opened).next('.col').animate({ width: '0px' }, 1000, function(){
				changeBackground('.' + $(opendiv).attr('id'));																  
			});
		}
		p.opened = $(this);
		$(this).next('.col').animate({ width: '887px'}, 1000);
		$('.title').not(p.opened).animate({ width: '23px' }, 300);
	});
});
