$(window).load(function() {
	if ($('body').attr('id') == "portfolio") {
		$().qMosaic({
			color : "#000",
			opacity : 0.75,
			rows : 5,
			columns: 7,
			speed : 20,
			shuffle : true,
			overStartCallback : showViewProject,
			outStartCallback : hideViewProject
		});
	}
	// qMosaic Callback Functions
	var showViewProjectTimeout;
	function showViewProject(obj) {
		var msg = '<p style="text-align:center;position:absolute;right:170px;bottom:40px;z-index:9999;color:#eee;font-size:12px"><img src="img/zoom_icon1.gif" alt="Zoom Image" /></p>';
		var func = function() {
			doShowViewProject(obj, msg);
		};
		showViewProjectTimeout = setTimeout(func, 500);
	}
	function hideViewProject(obj) {
		clearTimeout(showViewProjectTimeout);
		obj.siblings('p').detach();
	}
	function doShowViewProject(obj, msg) {
		obj.after(msg);
	}
});
$(document).ready(function () {
	$('#bobs').mouseenter(function() {
		$('#phelpsImg').qMosaic('show');
	});
	$('#bobs').mouseleave(function() {
		$('#phelpsImg').qMosaic('hide');
	});
	var body_id = $('body').attr('id');
	// set up code formatting (syntax highlight)
	// -- snippet
	if (body_id == "blog") {
		$("pre").filter('.css').snippet('css', {style: 'acid'});
		$("pre").filter('.html').snippet('html', {style: 'acid'});
		$("pre").filter('.php').snippet('php', {style: 'acid'});
		$("pre").filter('.js').snippet('javascript', {style: 'acid'});
		$("pre").filter('.jsdom').snippet('javascript_dom', {style: 'acid'});
		$("pre").filter('.as').snippet('javascript', {style: 'acid'});
		// show/hide javascript required items
		$('#comment-no-js-error').css('display', 'none');
		$('#commentForm').css('display', 'inline');
		$("#commentForm").qValidateForm({
			submitOnValidate : true,
			callbackFunction : commentValidated,
			error_name : "Enter your name, nickname, pseudonyn, whatever",
			error_email : "It will not be posted, but we still need it.",
			error_text : "It's pointless posting a comment if you have no comment."
		});
		// Set up Reply to Comment Form
		// reply to comment button is hidden on load
		// for those with JS disabled
		$('.comment-reply-to').css('display', 'inline-block');
		$('a.comment-reply-to').toggle(
			function() {
				// get the parent ID
				var parent_id = $(this).attr('rel');
				var post_id = $('#comment_post_ID').val();
				var user_ID = $('#user_ID').val();
				if (user_ID != '') {
					user_ID_field = '<input type="hidden" id="user_ID" name="user_ID" value="' + user_ID + '" />';
				} else {
					user_ID_field = '';
				}
				// add the form
				var replyForm = '<form id="replyForm" action="post-comment-house6.php" method="post" style="margin-top:10px"> \
									<fieldset> \
										<ol> \
											<li> \
												<label for="comment_author">Name <span style="color:#FF0000">*</span></label> \
												<input id="comment_author" class="required name" name="comment_author" type="text" /> \
											</li> \
											<li> \
												<label for="comment_author_email">Email <span style="color:#FF0000">*</span></label> \
												<input id="comment_author_emaill" class="required email" name="comment_author_email" type="text" /> \
											</li> \
											<li> \
												<label for="comment_author_url">Website</label> \
												<input id="comment_author_url" class="" name="comment_author_url" type="text" /> \
											</li> \
											<li> \
												<label for="comment_content">Message <span style="color:#FF0000">*</span></label> \
												<textarea id="comment_content" class="required text" name="comment_content" rows="10" cols="55"></textarea> \
											</li> \
											<li class="comment-footnote">You can use special language classes inside a &lt;pre&gt; tag to format code.  Languages include: as, css, html, js, jsdom, and php.  Example: &lt;pre class="css"&gt;Code Here&lt;/pre&gt;</li> \
											<li style="text-align:center"> \
												' + user_ID_field + ' \
												<input type="hidden" id="comment_parent" name="comment_parent" value="' + parent_id + '" /> \
												<input type="hidden" id="comment_post_ID" name="comment_post_ID" value="' + post_id + '" /> \
												<input id="replySubmit" type="submit" value="Post It" /> <input id="replyReset" type="reset" value="Clear" /> \
											</li> \
										</ol> \
									</fieldset> \
								</form>';
				$(this).parent('div').after(replyForm);
				$(this).html('Close Form');
				//alert($('#replyForm').html());
				// add validation to the new form
				$(this).parent('div').siblings('#replyForm').qValidateForm({
					submitOnValidate : true,
					callbackFunction : commentValidated,
					error_name : "Enter your name, nickname, pseudonyn, whatever",
					error_email : "It will not be posted, but we still need it.",
					error_text : "It's pointless posting a comment if you have no comment."
				});
			},
			function() {
				$(this).html('Reply to This Comment');
				$(this).parent('div').siblings('#replyForm').detach();
			}
		);
	}
	// set up form validation if on contact page
	if (body_id == "contact") {
		$("#contactForm").qValidateForm({
			submitOnValidate : false,
			callbackFunction : formValidated,
			error_name : "It makes introductions difficult if you have no name",
			error_email : "I need to know where to send my reply",
			error_text : "C'mon now ... You must have <i>something</i> to say"
		});
	}
	// set up lightbox (prettyPhoto)
	if (body_id == "portfolio") {
		$("a[rel^='pplb']").prettyPhoto({
			theme : 'facebook',
			show_title : false
		});
	}
	// replace email addresses (spam bot protection)
	var email_links = $('body').find('.email_link');
	email_links.each(function() {
		var makeLink = true;
		var link_label = null;
		var pattern = /([-a-z0-9]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*\|[a-z0-9][-a-z0-9]+(\.[-a-z0-9]+)*[a-z0-9]\|[a-z]{2,6}(\.[a-z]{2,6})*)/i;
		var val = $(this).html();
		if (pattern.test(val)) {
			var addy = val.match(pattern);
			var email_text = addy[0];
			addy = email_text.split("|");
			addy = addy[0] + "@" + addy[1] + "." + addy[2];
			if (makeLink) {
				if (link_label != null) {
					addy = '<a href="mailto:' + addy + '">' + link_label + '</a>';
				} else {
					addy = '<a href="mailto:' + addy + '">' + addy + '</a>';
				}
			}
			val = val.replace(email_text, addy);
			$(this).html(val);
		}
	});
	// Header Link
	$('#header').mouseenter(function() {
		$(this).css("cursor", "pointer");
	});
	$('#header').click(function() {
		if (body_id == "project_page") {
			window.open("/index.php", "_self");
		} else {
			window.open("/index.php", "_self");
		}
	});
	// portfolio overlay links
	if (body_id == "portfolio") {
		$('#content').find('.portfolio_viewit').each(function() {
			var theLink = $(this).parent().find('a').attr('href');
			$(this).mouseenter(function() {
				$(this).css("cursor", "pointer");
			});
			$(this).click(function() {
				window.open(theLink, "_self");
			});
		});
	}
	// ======================================================
	// Button Effects
	// ------------------------------------------------------
	// -- Navigation Menu - Hover Effect
	$('#navContainer').children().hover(function() {
    	var img = $(this).find('a').find('img');
    	img.stop().fadeTo(500, 0.1);
	}, function () {
    	var img = $(this).find('img');
    	img.stop().fadeTo(500, 1);
	});
	// -- Latest Project - Hover Effect
	$('#latest_project').hover(function() {
    	var img = $(this).find('a').find('img');
    	img.stop().fadeTo(500, 0.1);
	}, function () {
    	var img = $(this).find('img');
    	img.stop().fadeTo(500, 1);
	});
	// -- Blog Button - Hover Effect
	$('#nav_blog').hover(function() {
    	var img = $(this).find('a').find('img');
    	img.stop().fadeTo(350, 0.1);
	}, function () {
    	var img = $(this).find('img');
    	img.stop().fadeTo(350, 1);
	});
	/*
	// -- portfolio thumbs
	$('#portfolio_web').children('div').find('a').siblings('.portfolio_viewit').css("opacity", 0.01);
	$('#portfolio_web').children('div').hover(function() {
		var overlay = $(this).children('.portfolio_viewit');
		overlay.stop().fadeTo(300, 0.8);
	}, function() {
		var overlay = $(this).children('.portfolio_viewit');
		overlay.stop().fadeTo(300, 0.01);
	});
	$('#portfolio_flash').children('div').find('a').siblings('.portfolio_viewit').css("opacity", 0.01);
	$('#portfolio_flash').children('div').hover(function() {
		var overlay = $(this).children('.portfolio_viewit');
		overlay.stop().fadeTo(300, 0.8);
	}, function() {
		var overlay = $(this).children('.portfolio_viewit');
		overlay.stop().fadeTo(300, 0.01);
	});
	*/
	
	// ================================================
	// FORM VALIDATION AND SUBMISSION
	// ------------------------------------------------
	function commentValidated() {
		//alert("Comment Validated, w00t!");
	}
	function formValidated() {
		//alert("Form Validated, Woohoo!");
		var userName = $('#userName').val();
		var userEmail = $('#userEmail').val();
		var userWebsite = $('#userWebsite').val();
		var userMessage = $('#userMessage').val();
		$.ajax({
			type : "POST",
			url : "contact_form.php",
			dataType : "html",
			data : "userName=" + userName + "&userEmail=" + userEmail + "&userWebsite=" + userWebsite + "&userMessage=" + userMessage + "&js=true",
			success : processFormResponse
		});
	}
	function processFormResponse(data, textStatus, jqXHR) {
		//alert(data);
		if (data == 'success') {
			showFormSuccess();
		} else {
			showFormErrors(data);
		}
	}
	function showFormSuccess() {
		var msg = '<p style=""><img src="img/contact_success.gif" alt="Success" /> &nbsp;Your message has been sent ...  thank you for contacting me.  I will be in touch as soon as I can.</p>';
		$('#formContainer').before(msg);
		$('#formContainer').css("display", "none");
	}
	//
	function showFormErrors(info) {
		var msg = '<p style="">' + info + '</p>';
		$('#formContainer').before(msg);
		$('#formContainer').css("display", "none");
	}
	// ======================================================
	// Dynamic Dates
	// ------------------------------------------------------
	// -- my years of experience
	var theDate = new Date();
	$('#years_of_experience').html(parseFloat(theDate.getFullYear()) - 2002);
	// -- current year (for copyright)
	$('#current_year').html(theDate.getFullYear());
	// -- current age
	if (theDate.getMonth() < 8) {
		var age_offset = -1;
	} else {
		var age_offset = 0;
	}
	$('#current_age').html(parseFloat(theDate.getFullYear()) - 1975 + age_offset);
	// ======================================================
	// That's All Folks!
	// ------------------------------------------------------
});
