// JavaScript Document
var errors = new Array();
var escapedHash;

var Browser = {
  Version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}

function findHash() {
    if(window.location.hash) {
		$(".progress").show('fade');
        $(".content").hide('slide', {direction: 'left'});
        
        escapedHash = window.location.hash.replace("#!", "");
        
        $.get("includes/data.php?_escaped_fragment_=" + escapedHash, function(data) {
            $(".content").html(data);
			if (escapedHash == "main"){
				$('.nav ul').hide('hide', function(){
					$('.nav h4').show('fade');
				});
			} else {
				$('.nav h4').hide('hide', function(){
					var div = escapedHash.toString().split('-');												   
					$('.nav ul li').each(function(){
						$(this).removeClass('active');
					});
					$('.nav ul li').each(function(){
						var this_link = $(this).children('a').attr('href');
						if (div[0] == "portfolio_details"){
							var escapedHash_div = "portfolio-1";
						} else if (div[0] == "portfolio"){
							var escapedHash_div = "portfolio-1";
						} else {
							var escapedHash_div = div[0];
						}
						if (this_link == '#!'+escapedHash_div){
							$(this).addClass('active');
						}
					});												   
					$('.nav ul').show('fade');
				});
			}
			$(".content").show('slide', {direction: 'right'});
			$(".progress").hide('fade');
        });
    }
}

$(document).ready(function() {
	if (Browser.Version() == 8) {
		//Do nothing since IE8 sucks at HTML 5
	} else {
		snowfall();
	}
    findHash();
});

$(window).bind('hashchange', function() {
    findHash();
});

function showRecaptcha(element) {
	Recaptcha.create("6Leu0bkSAAAAACOEWlIeQxaU70VvrGeQgT67lFyB", element, {
		theme: "red",
		callback: Recaptcha.focus_response_field
	});
}

function notEmpty(elem, formitem){
	if(elem == ""){
		errors.push(formitem);
		return false;
	}
	return true;
}

function close_popup(divname) {
	$('#' + divname).fadeOut(1000);
}
	
function processForm(){
	challengeField = $("input#recaptcha_challenge_field").val();
	responseField = $("input#recaptcha_response_field").val();
	//alert(challengeField);
	//alert(responseField);
	//return false;
	var html = $.ajax({
	type: "POST",
	url: "includes/ajax.recaptcha.php",
	data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
	async: false
	}).responseText;
	
	if (html.replace(/^\s+|\s+$/, '') == "success")
	{
		// Uncomment the following line in your application
		var name = $('#name').val();
		var email = $('#email').val();
		var message = $('#message').val();
		name = encodeURI(name);
		email = encodeURI(email);
		message = encodeURI(message);
		
		notEmpty(name, "Name");
		notEmpty(email, "Email");
		notEmpty(message, "Message");
		
		if (errors.length > 0) {
			var buffer = "<div class=\"popup_close\"><a href=\"#!contact\" onclick=\"close_popup('errors')\"><img src=\"images/close.png\"></a></div>Problem(s) were found in the following boxes. Please correct before continuing:<br /><ul>";
			var num = 0;
			while (num <= errors.length){
				if (errors[num] != undefined){
					buffer = buffer + "<li>" + errors[num] + "</li>";
				}
			   num = num + 1;	
			}
			buffer = buffer + "</ul>";
			$('#errors').html(buffer);
			$('#errors').fadeIn(1000);
			errors = new Array();
			Recaptcha.reload();
			return false;
		} else {
			$.post("includes/process.php?name="+name+"&email="+email+"&message="+message+"&safetyword=0ca12b2ec773d31ca016cdb3d5688f17", function(data) {
				$('#top_error').html("<div class=\"popup_close\"><a href=\"#!contact\" onclick=\"close_popup('top_error')\"><img src=\"images/close.png\"></a></div>Thank you for your response. Your message has been sent to Michael.");
				$('#top_error').fadeIn(1000);
				$('#name').val("");
				$('#email').val("");
				$('#message').val("");
				Recaptcha.reload();
			});
			return true;
		}
	}
	else
	{
		alert("Your captcha is incorrect. Please try again");
		Recaptcha.reload();
		return false;
	}
}

function snowfall() {
	var SCREEN_WIDTH = window.innerWidth;
	var SCREEN_HEIGHT = window.innerHeight;
	
	var container;
	
	var particle;
	
	var camera;
	var scene;
	var renderer;
	
	var mouseX = 0;
	var mouseY = 0;
	
	var windowHalfX = window.innerWidth / 2;
	var windowHalfY = window.innerHeight / 2;
	
	var particles = []; 
	var particleImage = new Image();
	particleImage.src = 'images/ParticleSmoke.png'; 
	
	
	init();
	setInterval( loop, 1000 / 60 );
	
	function init() {
	
		container = document.createElement('div');
		$('.skyline_inner').append(container);
	
		camera = new THREE.Camera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
		camera.position.z = 1000;
	
		scene = new THREE.Scene();
	
		renderer = new THREE.CanvasRenderer();
		renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	
		for (var i = 0; i < 500; i++) {
	
			particle = new Particle3D( new THREE.ParticleBitmapMaterial( particleImage));
			particle.position.x = Math.random() * 2000 - 1000;
			particle.position.y = Math.random() * 2000 - 1000;
			particle.position.z = Math.random() * 2000 - 1000;
			particle.scale.x = particle.scale.y =  1;
			scene.addObject( particle );
			
			particles.push(particle); 
		}
	
		container.appendChild( renderer.domElement );
	
	
		document.addEventListener( 'mousemove', onDocumentMouseMove, false );
		document.addEventListener( 'touchstart', onDocumentTouchStart, false );
		document.addEventListener( 'touchmove', onDocumentTouchMove, false );
	}
	
	//
	
	function onDocumentMouseMove( event ) {
	
		mouseX = event.clientX - windowHalfX;
		mouseY = event.clientY - windowHalfY;
	}
	
	function onDocumentTouchStart( event ) {
	
		if ( event.touches.length == 1 ) {
	
			event.preventDefault();
	
			mouseX = event.touches[ 0 ].pageX - windowHalfX;
			mouseY = event.touches[ 0 ].pageY - windowHalfY;
		}
	}
	
	function onDocumentTouchMove( event ) {
	
		if ( event.touches.length == 1 ) {
	
			event.preventDefault();
	
			mouseX = event.touches[ 0 ].pageX - windowHalfX;
			mouseY = event.touches[ 0 ].pageY - windowHalfY;
		}
	}
	
	//
	
	function loop() {
	
		for(var i = 0; i<particles.length; i++)
		{
		
			var particle = particles[i]; 
			particle.update(); 
			
			with(particle.position)
			{
				if(y<-1000) y+=2000; 
				if(x>1000) x-=2000; 
				else if(x<-1000) x+=2000; 
				if(z>1000) z-=2000; 
				else if(z<-1000) z+=2000; 
			}				
		}
	
		camera.position.x += ( mouseX - camera.position.x ) * 0.05;
		camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
		
	
		renderer.render( scene, camera );
	
		
	}
}
