/*******************************
* Developed by Jason C Fleming *
********************************
** (c)2011 Lilaea Media LLC   **
*******************************/



/****************************
********* LOAD VARS *********
****************************/	
// determine artwork group based on current page loaded
var levels        = document.location.href.split("/");
var thispage      = levels.pop();
var thisdir       = levels.pop();

$(document).ready(function(e) {
	$(document).mousemove(isHuman); // requires a mouse move to load email address
	if (thisdir == 'contact') { // bind form submit and initialize chosen plugin
		$('#contact_form').bind('submit', submitForm);
		$('#submit_link').bind('click', submitForm);
	}
});

function isHuman(e) { // handles mouse move event, assumes must be human
	$(document).unbind('mousemove'); // unbind mouse event
	var hex = $('#obfu_0').html(); // get encrypted email address from first div
	if (hex != null) {
	var res;
	$.ajax({ // post back to footer php to get decrypt key
		url: '../inc/obfuscapery.inc.php',
		type: 'POST', // won't work with GET method
		data: { token: hex }, // pass token
		error: function() { return; }, // bail on error
		success: function(response) { // get params 
			res = response.split("\n");
		},
		complete: function() { // gracefully replace each link with mailto link
			var count = res.shift();
			var secret = res.shift();
			var selector = '';
			for (e = 0; e < count; e++) {
				var email = $('#obfu_' + e).html(); // get encrypted email address from div
				var selector = '#mailto_' + e;
				email = xor_dec(email, secret);
				swapMailTo(selector, email);
			}
		}
	});
	}
}

function swapMailTo(selector, email) 
{
	var mailto = '<a href="mailto:' + email + '">' + email + '</a>'; // customize this
	$(selector).fadeOut('slow', function(e) { $(this).html(mailto).fadeIn('slow'); } );
}

function xor_enc(string, key, times)
{
	if (string.length == 0 || key.length == 0) return; // verify input exists
 	while (key.length < string.length) key += key; // pad key to >= string
	var hex = ''; // initialize result string
	for( p = 0; p < string.length; p++)
	{ // iterate through input string by character
		var c = string.charCodeAt(p); // get ascii value of char
		var k = key.charCodeAt(p); // get ascii value of corresponding key
		var b = c ^ k; // xor bitwise conversion: b = c ^ k and c = b ^ k 
		var h = '0' + b.toString(16); // convert result to hex (left pad zeros)
		h = h.substring(h.length - 2);  // truncate to 2 chars
		hex += h; // add to result string
	}
	return hex;
}
 
function xor_dec(hex, key, times)
{
	if (hex.length == 0 || key.length == 0) return; // verify input exists
 	while (key.length < hex.length) key += key; // pad key to >= hex
	var string = ''; // initialize result string
 	for(p = 0; p < hex.length; p += 2) 
	{ // iterate through input string two chars at a time
		var h = hex.charAt(p) + hex.charAt(p + 1); // get next two hex chars
		var b = parseInt(h, 16); // convert to decimal
		var k = key.charCodeAt(p / 2); // get ascii value of corresponding key
		var c = String.fromCharCode(b ^ k); // get ascii char from xor bitwise conversion
		string += c; // add to result string
	}
	return string;
}


/******************************
** CONTACT FORM HANDLERS **
******************************/	

function submitForm(event){
	event.stopImmediatePropagation();
	event.preventDefault();
	$('#failure_container').hide('slow');
	var $response;
	var name          = $('#first_name').val();
	var company       = $('#company').val();
	var email         = $('#email').val();
	var phone         = $('#phone').val();
	var comments      = $('#comments').val();
	$.ajax({
		url: '../inc/contact_form.php',
		dataType: 'html',
		type: 'POST',
		data: { 
			name:          name,
			company:       company,
			email:         email,
			phone:         phone,
			comments:      comments,
		},
		error: function(xhr, status, error) { 
			$('#failure_container').html(error).show().slideDown('slow');
		},
		success: function(response, status, xhr){
			$response = response;
		},
		complete: function(xhr, status) {
			if ($response.indexOf('success_response') >= 0) {
				$('#form_container').hide('slow');
				$('#success_container').html($response).show('slow');
			}
			else {
				$('#failure_container').html($response).slideDown('slow');
			}
			
		}
	});
}

