$(document).ready(function(){
	
	var currentImageRel = 0;
	
	if($('#slideshow img[rel]').length > 1) {
		
		setTimeout(function(){  
            
			startFade();
			
        }, 1000);  
		
	}
	
	var speed = 3000;
	
	var startFade = function() {
		
		var image = '';
		
		//alert('volgende fade');
		
		currentImageRel++;	
		
		$('#slideshow').find('img').css({
			'z-index': '99'
		});
		
		image = $('#slideshow').find('img[rel=' + currentImageRel + ']');
		
		//$('.text').append(currentImageRel + '<br />');
		
		//alert(currentImageRel);

		//alert(speed);
		
		if(!image.length) {
			
			currentImageRel = 0;
			
			image = $('#slideshow').find('img[rel=0]');
			
			//alert(currentImageRel);
		}
		
		speed = parseInt(image.attr('speed'));
		
		image.css({
			'z-index': '999',
			'display': 'none'	
				
		}).fadeIn(speed,function () {
			
			if(currentImageRel == 0)
			{
				//alert($(this).attr('rel'));
			}
			
			$('#slideshow').find('img[rel!=' + parseInt($(this).attr('rel')) + ']').css("display","none");
			
			setTimeout(function(){  
	            
				startFade();
				
	        }, speed); 
		});
				
	}
	
	$('#contactForm input').focus(function () {
		$(this).css('background','#E9E9E9');
	});
		
	$('#contactForm').submit(function () {
				
		$.post('/contact',$('#contactForm').serialize(),function (data) {
			
			if(data.message) {
				$('dl.zend_form').html('<img id="contact-thanks" src="/static/img/contact-thanks.gif" />');
			}
			else
			{
 			
				$.each(data, function (key,error) {
					$('#' + key).css({
						'background': '#FF0000'
					});
				});
			}
			
		},'json');
		
		return false;
	});
	
	var currentSliding = false;
	
	var placeProject = function (projectId) {
		
		$.post('/projects/view',{projectId: projectId}, function(data) {
			
			//alert(data.title);
						
			$('#slideshow img').css('z-index','99');
			
			var html = '';
			var img = '';
			
			if(!$('#slideshow img[rel=' + data.project.id + ']').length)
			{
				img = $('<img/>')
					.attr('src','/uploads/projects/' + data.project.location + '.png')
					.attr('rel',data.project.id)
					.css({
						
						'z-index' : 999,
						'display' : 'none'
						
					})
				;
				
				$('#slideshow')
					.prepend(img)
				;	
			}
			else
			{
				img = $('#slideshow img[rel=' + data.project.id + ']');
					
				img.css({
					
					'z-index' : 999,
					'display' : 'none'
					
				})
			}
			
			currentSliding = data.project.id;
			
			$('#OrderLink').hide();
			$('#reddot').hide();

			img.fadeIn(1200,function () {
			
				$('#slideshow img[rel!=' + $(this).attr('rel') + ']').css("display","none");
				
				if(data.project.reddot != "0") {
					$('#reddot').css('z-index','9999').show();
				}
									
				if(data.project.orderlink.length > 0) {
					$('#OrderLink').css('z-index','9999').show().attr('href',data.project.orderlink);
					$('#OrderLink img').show();
				}
				
				$('#nextButton').removeAttr('rel');
				$('#prevButton').removeAttr('rel');
				
			});

			html += '<h2>' + data.project.title + '</h2>' + data.project.text;
			
			if(data.project.account || data.project.awards) {
				html += '<ul class="info">';
				
				html += '<li><h2>Account</h2>' + data.project.account + '</li>';	
				
				if(data.project.awards) {
					html += '<li><h2>Awards</h2><div class="award-text">' + data.project.awards + '</div></li>';
				}
				
				html += '</ul>';
			}
			

			$('#right .text').html(html);
			
			$('#nextButton').attr('nextProject',data.nextProject.id);
			$('#prevButton').attr('prevProject',data.prevProject.id);
			
			
		},'json');
		
	
	}
	
	$('#prevButton').hover(function () {
		
		$(this).find('img').attr('src','/static/img/projects/button-prev-hover.gif');
		
	},function () {
		
		$(this).find('img').attr('src','/static/img/projects/button-prev.gif');
		
	}).click(function () {
		
		var projectId = $(this).attr('prevProject');
		
		if($(this).attr('rel'))
		{
			// Nog aan het spelen
			
			return false;
		}
		else
		{
			placeProject(projectId);
		
			$(this).attr('rel','active');
			
			$(this).find('img').attr('src','/static/img/projects/button-prev-hover.gif');
			
			//$(this).hide();
			
			return false;
		}
		
	});
	
	$('#nextButton').hover(function () {
		
		$(this).find('img').attr('src','/static/img/projects/button-next-hover.gif');
		
	},function () {
		
		$(this).find('img').attr('src','/static/img/projects/button-next.gif');
		
	}).click(function () {
		
		var projectId = $(this).attr('nextProject');
		
		if($(this).attr('rel'))
		{
			// Nog aan het spelen
			
			return false;
		}
		else
		{
			placeProject(projectId);
		
			$(this).attr('rel','active');
			
			$(this).find('img').attr('src','/static/img/projects/button-next-hover.gif');
			
			//$(this).hide();
			
			return false;
		}
	});
	
});