// JavaScript Document

$(function() {
  var img = new Image();
  img.src = '../images/overlay.png';
});

function sendEmail(){
  var form = $('#contactForm');
  $.post('/_email.php',
		form.serialize(),
		function(result) {
		  $('#box').animate({
			'height' : '50px'
		  },750);
		  form.html('<p style="font-size:14px; vertical-align:top;">Thanks for contacting me.  I\'ll get back to you as soon as possible.</p>');
  });	
}
function getArchive(){
  $.post('_getArchive.php',
	{},
	function(data){
	  for(x = 0; x < data.length; x++){
		$('<div>').addClass('working archEntry').appendTo('#leftCol');
		$('<a>').attr('href','articles/'+data[x].url).addClass('working postLink').appendTo('div.working');
		$('<h2>').addClass('postName').text('//  '+data[x].name).appendTo('a.working');
		$('<span>').addClass('postDate').text(data[x].date).appendTo('div.working');
		$('.working').removeClass('working');
	  }
	},
  'json');
}
function getComments(name){
  $.get('/_comments.php',
		{ url: name},
		function(data){
		  $('<div>').attr('id','comments').insertBefore('#commentFormContainer');
		  $('<h2>').text(' comments').appendTo('#comments');
		  $('<span>').attr('id','count').text(data.length).prependTo('#comments h2');
		  comment(data);
		},
  'json');
}
function postComment(){
  var text =  $('#commentForm input[name="website"]').val();
  text = text == 'Website (optional)' ? '':text;
  $('#commentForm input[name="website"]').val(text);
  $.post('/_postComment.php',
		 $('#commentForm').serialize(),
		  function(data){
			var count = (parseInt($('#count').text()) + 1);			
			$('#count').text(count);
			comment(data);
			
		}
  ,'json');
}
function comment(data){
  for(x = 0; x < data.length; x++){
	$('<div>').addClass('comment current').appendTo('#comments');
	$('<div>').addClass('authorInfo').appendTo('.current');
	
	$('<a>',{ 
	  href: data[x].website,
	  target: '_blank',
	  rel: 'nofollow'
	}).appendTo('.current .authorInfo');
	$('<img>').attr('src',data[x].gravatar).addClass('gravatar').appendTo('.current .authorInfo a');
	$('<span>').addClass('poster').text(data[x].name).appendTo('.current .authorInfo a');
	
	$('<div>').addClass('commentDate').text(data[x].date).appendTo('.current .authorInfo');
	$('<div>').addClass('commentBody').append('<p>'+data[x].comment+'</p>').appendTo('.current');
	
	$('.current').removeClass('current');		
  }	
  $('.comment:even').addClass('evenComment');
  $('a[href=""]').each(function(){
	$(this).children(':first').unwrap();							
  });
}

// Modal Script Start **********************************************************

function showBox(){
	var fields = $('#contactForm:input');
		
    $('#overlay').show();
    center('#box');	
	$('#contactForm input:first').focus();
	return false;
}

function hideBox(){
    $('#box').hide();
    $('#overlay').hide();
    return false;
}

function center(element){
	try{
		element = $(element);
	}catch(e){
		return;
	}

	var my_width  = 0;
	var my_height = 0;

	if ( typeof( window.innerWidth ) == 'number' ){
		my_width  = window.innerWidth;
		my_height = window.innerHeight;
	}else if ( document.documentElement && 
			 ( document.documentElement.clientWidth ||
			   document.documentElement.clientHeight ) ){
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
	}
	else if ( document.body && 
			( document.body.clientWidth || document.body.clientHeight ) ){
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	}

	element.css("position","absolute");
	element.css("z-index",99);

	var scrollY = 0;

	if ( document.documentElement && document.documentElement.scrollTop ){
		scrollY = document.documentElement.scrollTop;
	}else if ( document.body && document.body.scrollTop ){
		scrollY = document.body.scrollTop;
	}else if ( window.pageYOffset ){
		scrollY = window.pageYOffset;
	}else if ( window.scrollY ){
		scrollY = window.scrollY;
	}
	
	var setX = ( my_width  - element.width()  ) / 2;
	var setY = ( my_height - element.height() ) / 2 + scrollY;

	setX = ( setX < 0 ) ? 0 : setX;
	setY = ( setY < 0 ) ? 0 : setY;
	
	var newX = setX + "px";
	var newY = setY + "px";
	
	element.css("left",setX);
	element.css("top",setY);
	element.css("display","block");
}
// Modal Script End **********************************************************

