﻿(function($) {
	$.fn.isEmpty = function() {
		var elem = $(this[0]);
		
		var v = $.trim(elem.focus().val());
		elem.blur();
		
		return v === "";
	}
	$.fn.blurText = function(options) {

		if (typeof options === "string")
			options = { text: options };

		var defaults = {
			blurClass: 'bt-class',
			text: 'Type Here...'
		};
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var elem = $(this);
			if (elem.is('input[type=text], textarea')) {
			
				elem.blur(function() {
					if ($.trim(elem.val()) === "") {
						elem.val(options.text);
						elem.addClass(options.blurClass);
					}
				}).focus(function() {
					if ($.trim(elem.val()) === options.text) {
						elem.val('');
						elem.removeClass(options.blurClass);
					}
				}).blur();
				
				var form  = elem.parents('form:first');
				form.submit(function() {
					if(elem.isEmpty())
						elem.val('');
				});
			}
		});
	};
})(jQuery);


$(function() {
	$('.skills-category h4').click(function() {
		var changingEl = $(this).next();
		if (changingEl.is(":hidden")) {
			$(this).children('.expand-collapse').addClass('expanded');
			changingEl.slideDown('fast', function() {
				$(this).css('margin-bottom', 10);
			});
		} else {
			$(this).children('.expand-collapse').removeClass('expanded');
			changingEl.css('margin-bottom', 0);
			changingEl.slideUp('fast');
		}
	});

	// Move project filter dropdown to the correct location
	var filterEl = $('#work_filter_current');
	var offset = filterEl.offset();

	var newTop = offset.top + filterEl.outerHeight() - parseInt(filterEl.css('padding-bottom'), 10);
	var newLeft = offset.left + parseInt(filterEl.css('padding-left'), 10) - 10;

	$('#filter_dropdown').css('top', newTop);
	$('#filter_dropdown').css('left', newLeft);

	// EVENTS
	$('#work_filter').click(function() {
		$('#filter_dropdown').slideToggle('fast');
	});

	$('#filter_dropdown').add('#work_filter').mouseleave(function() {
		var timeout = setTimeout(function() {
			clearTimeout($('#filter_dropdown').slideUp('fast').data('filterTimeout'));
		}, 1000);
		$('#filter_dropdown').data('filterTimeout', timeout);
	});
	$('#filter_dropdown').add('#work_filter').mouseenter(function() {
		clearTimeout($('#filter_dropdown').data('filterTimeout'));
	});

});

Shadowbox.init({
	players: ['img', 'html'],
	enableKeys: false,
	overlayOpacity: 0.6,
	onFinish: function() {
		$('.contact_name:visible').blurText('Name');
		$('.contact_email:visible').blurText('Email');
		$('.contact_message:visible').blurText('Message...');
		$('.contact_send:visible').click(function() {
			if($('.contact_name:visible').isEmpty()) {
				alert('Please enter your name');
				return;
			}
			if($('.contact_email:visible').isEmpty()) {
				alert('Please enter an email address');
				return;
			}
			if($('.contact_message:visible').isEmpty()) {
				alert('Please enter a message');
				return;
			}
		
			
			$.ajax({
				type: 'POST',
				url: '/wp-portfolio/wp-content/themes/portfolio-v1/contact.php',
				data: $('#sb-content form').serialize(),
				success: function(msg) {
					$('#sb-content fieldset').css('display', 'none');
					$('#sb-content form').append("<h3 class='contact_thanks'>" + msg+ "</h3>");
					setTimeout(Shadowbox.close, 1000);
				}
			});
		});
	}

});


