
var currentPhotoLoaderIterator = 0;
var currentPhotoAnimationIterator = 0;

var INITIAL_CIRCLE_RADIUS = 200;
var INITIAL_CIRCLE_ITEMS_AMOUNT = 5;
var CIRCLE_SPACING = 225;

var factor = 1.75;
var circleRadius = INITIAL_CIRCLE_RADIUS;
var circleAmount = INITIAL_CIRCLE_ITEMS_AMOUNT;
var circlePositions = [];


var namePlaceholder = "name";
var emailPlaceholder = "email";

$(document).ready(function() 
{
	var isHovered = false;
	var isWrongInputted = false;
	$('#socialpopup').stop(true, true).fadeOut(0);
	$('#subscribed-label').stop(true, true).fadeOut(0);
	showPlaceholder('#nameinput', namePlaceholder);
	showPlaceholder('#emailinput', emailPlaceholder);
	
	$('#barcenter-link').hover(function(){
		isHovered = true;
		$('#socialpopup').stop(true, true).fadeIn(300);
	}, function(){
		isHovered = false;
		
		$("#socialpopup").delay(175, "idleQueue").queue("idleQueue", function(){ 
			if(!isHovered) fadePopupOut();
		}).dequeue("idleQueue");
	});
	
	$('#socialpopup').hover(function(){
		isHovered = true;
	}, function(){
		isHovered = false;
		if(!isHovered) fadePopupOut();
	});
	
	function fadePopupOut()
	{
		$('#socialpopup').stop(true, true).fadeOut(150);	
	}
	
	function validateEmail()
	{
	    var a = $('#emailinput').val();
	    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	    if (filter.test(a) && a != emailPlaceholder) return true;
	    
	    $('#email-textfield').css("background-image", "url(img/inputfielderror.png)");
	    
		return false;
	}
	
	function validateName()
	{
		var a = $('#nameinput').val();
		if(a != "" && a != namePlaceholder) return true;
		
	    $('#name-textfield').css("background-image", "url(img/inputfielderror.png)");
		
		return false;
	}
	
	
	function resetFields()
	{
	    $('#name-textfield').css("background-image", "url(img/inputfield.png)");
	    $('#email-textfield').css("background-image", "url(img/inputfield.png)");
	}
	
	
	function showSuccess()
	{
		$('#sendbutton').fadeOut(150);
		$('#subscribed-label').delay(150).fadeIn(300);
		$('#nameinput').attr('disabled','disabled');
		$('#emailinput').attr('disabled','disabled');
	}
	
	function showPlaceholder(id, placeholder)
	{
		$(id).val(placeholder);
		$(id).css('color','#aaaaaa');
	}
	
	function removePlaceholder(id)
	{
		$(id).val('');
		$(id).css('color','#ffffff');
	}
	
	
	$('#nameinput').focus(function(){
		if($('#nameinput').val() == namePlaceholder) removePlaceholder('#nameinput');
	});

	$('#nameinput').focusout(function(){
		if($('#nameinput').val() == '' || $('#nameinput').val() == namePlaceholder) showPlaceholder('#nameinput', namePlaceholder);
	});

	$('#emailinput').focus(function(){
		if($('#emailinput').val() == emailPlaceholder) removePlaceholder('#emailinput');
	});
	
	$('#emailinput').focusout(function(){
		if($('#emailinput').val() == '' || $('#emailinput').val() == emailPlaceholder) showPlaceholder('#emailinput', emailPlaceholder);
	});
	
	$('#nameinput').keyup(function(){
		if(isWrongInputted)
		{
			if(validateName())
			{
			    $('#name-textfield').css("background-image", "url(img/inputfield.png)");
			}
		}
	});
	
	$('#emailinput').keyup(function(){
		if(isWrongInputted)
		{
			if(validateEmail())
			{
			    $('#email-textfield').css("background-image", "url(img/inputfield.png)");
			}
		}
	});
	
	$('#sendbtn').click(function(event){
    	event.preventDefault();
    	
    	isWrongInputted = false;
    	resetFields();
    
		if(validateEmail() & validateName())
		{
			//submitloader.stop(true, false).fadeTo(500, 1);
			
		    var dataString = "name=" + Base64.encode($('#nameinput').val()) + "&email=" + Base64.encode($('#emailinput').val());
		
		    $.ajax({
		        type: "POST",
		        url: "backend/subscribe.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data)
				{
		            if (data.success) 
					{
		                showSuccess();
		            }
		            else 
					{
		                alert("Woops, our server thought it was time to take a break. Please try again later.");
		            }
		        }
		    });
		}
		else
		{
			isWrongInputted = true;
		}
		
		return false;
	});
});

/*
$(document).ready(function() 
{
	// Circle positions for first circle
	circlePositions = setupCirclePositions(circleAmount, circleRadius);



	// Load first 20 images
	for(currentPhotoLoaderIterator ; currentPhotoLoaderIterator < 80 ; currentPhotoLoaderIterator++)
	{
		loadImage('img/photos/' + photos[currentPhotoLoaderIterator], $('#photocontainer'));
	}


	
	// Load image
	function loadImage(imagePath, holder) {
	
		var imageTemp = new Image();
		var image = $(imageTemp);
		
		image.load(function() {
		
			// Default photo init
			$(this).fadeTo(0, 0);
			$(this).addClass("photo");
			$(this).attr("animated", "false");
			
			// Add to container
			holder.append(this);
		});
		
		image.attr('src', imagePath);
	}
	
	
	
	// User scrolls wheel
	$('body').mousewheel(function(event, delta, deltaX, deltaY) {
    	
    	// User scrolls the wheel, get first image that has the attr "ready"
    	$('.photo').each(function(index){
    	
    		var found = false;
    		
    		if($(this).attr('animated') == "false")
    		{
    			found = true;
    			$(this).attr('animated', 'true');
    			
				// Get end positions
				var positionObject = circlePositions[currentPhotoAnimationIterator];
				currentPhotoAnimationIterator++;
				
				// If circle is full, setup next circle
				if(currentPhotoAnimationIterator >= circlePositions.length)
				{
					circleAmount = Math.ceil(circleAmount * factor);
					circleRadius += CIRCLE_SPACING;
					currentPhotoAnimationIterator = 0;
					circlePositions = setupCirclePositions(circleAmount, circleRadius);
				} 
    			
    			// Set start properties for animation    			
    			var horizontalPos = positionObject.x >= 0 ? Math.random() * $(window).width() : -Math.random() * $(window).width();
    			var verticalPos = 0;
    			
    			if(horizontalPos > $(window).width() / 2 || horizontalPos < $(window).width / 2)
    			{
					verticalPos = positionObject.y >= 0 ? Math.random() * $(window).height() : -Math.random() * $(window).height();
    			}
    			else
    			{
    				verticalPos = positionObject.y >= 0 ? $(window).height() : -$(window).height();
    			}
    			
    			var index = Math.floor(Math.random() * $('.photo[animated=true]').size());
				$(this).css({'left':horizontalPos + 'px', 'top':verticalPos + 'px', 'z-index':index});
				$(this).rotate({angle:(Math.random() * 40) - 20});
				
				// Animate in
				$(this).fadeTo(0, 1);
				$(this).rotate({animateTo:(Math.random() * 40) - 20, duration:350});
				$(this).animate({
    				left: positionObject.x + 'px',
		    		top: positionObject.y + 'px'
  				}, 350, function() {
    				// Animation complete.
		  		});
    		}
    		
    		if(found) return false;
    	});
	});
});




function degToRad(degrees)
{
	return degrees * (Math.PI / 180);
}



function setupCirclePositions(amount, radius)
{
	// Calculate properties for end state of animation
	var anglePart = 360 / amount;
	var angle = 0;
	var positions = [];
	
	// Calculate possible angle positions
	for(var i = 0 ; i < amount ; i++)
	{
		// X and Y position on the circle
		var xpos = angle == 90 || angle == 270 ? 0 : Math.cos(degToRad(angle)) * radius;
		var ypos = angle == 0 || angle == 180 ? 0 : Math.sin(degToRad(angle)) * radius;
		
		// Randomize position a bit
		var xpos = xpos + ((Math.random() * 100) - 50);
		var ypos = ypos + ((Math.random() * 100) - 50);
		
		// Position object
		positions.push(new Position(xpos, ypos));
		angle += anglePart;
	}
	
	// Shuffle positions
	positions.shuffle();
	
	return positions;
}



function Position(xpos, ypos)
{
	this.x = xpos;
	this.y = ypos;
}



Array.prototype.shuffle = function() 
{
	var s = [];
	while (this.length) s.push(this.splice(Math.random() * this.length, 1)[0]);
	while (s.length) this.push(s.pop());
	return this;
}
*/


