/*********************************************
 *********************************************

		Author: Joe Edelmann
	   Website: www.teamdigital.com
	Copyright©: Joe Edelmann / teamDigital

 *********************************************
 *********************************************/

$(document).ready(function() {

	/* -----------------------------------------------------------------------------
	 * Function: IE6 Hack to fix target="_parent" links
	 * Purpose:  ...
	 * ---------------------------------------------------------------------------*/
	$('a.parentLink').click(function(){
		parent.location = $(this).attr("href");
	});


	/* -----------------------------------------------------------------------------
	 * Function: Live word counter
	 * Purpose:  Same
	 * ---------------------------------------------------------------------------*/
	$('.wordCount').each(function(){
		var y = $(this).val();
		var r = 0;
		a=y.replace(/\s/g,' ');
		a=a.split(' ');
		for (z=0;z<a.length;z++) {
			if (a[z].length > 0) r++;
		}
		if (r==1) {
			$(this).parent().find('.wordCounter').html('<strong>' + r + '</strong> word');
		} else {
			$(this).parent().find('.wordCounter').html('<strong>' + r + '</strong> words');
		}

		if (r != 0 && r < 25) { $(this).parent().find('span.min').addClass('err'); } else { $(this).parent().find('span.min').removeClass('err'); }
		if (r > 100) { $(this).parent().find('span.max').addClass('err'); } else { $(this).parent().find('span.max').removeClass('err'); }

		$(this).keyup(function(){
			var y = $(this).val();
			var r = 0;
			a=y.replace(/\s/g,' ');
			a=a.split(' ');
			for (z=0;z<a.length;z++) {
				if (a[z].length > 0) r++;
			}
			if (r==1) {
				$(this).parent().find('.wordCounter').html('<strong>' + r + '</strong> word');
			} else {
				$(this).parent().find('.wordCounter').html('<strong>' + r + '</strong> words');
			}

			if (r != 0 && r < 25) { $(this).parent().find('span.min').addClass('err'); } else { $(this).parent().find('span.min').removeClass('err'); }
			if (r > 100) { $(this).parent().find('span.max').addClass('err'); } else { $(this).parent().find('span.max').removeClass('err'); }
		});
	});


	/* -----------------------------------------------------------------------------
	 * Function: Text field / Textarea placeholder text swap
	 * Purpose:  User title attr to swap text field/textarea value on focus/blur
	 * ---------------------------------------------------------------------------*/
	$("input[type=text], textarea").each(function() {
		var pInput = $(this);
		var pTitle = $(this).attr("title");

		// Make sure title attr is set/not empty...
		if (pTitle != "") {

			// If text inputs value is empty...
			if ($(this).val() == '') {

				// Add class="placeholder" to input field to grey out placeholder text...
				$(this).addClass('placeholder');
		
				// If text input's value equals it's title attribute, create function...
				$(this).val(pTitle).focus(function() {
					// If input's current value equals title attribute, clear that value and remove class="placeholder"
					// so user can enter their value...
					if (this.value == pTitle) {
						$(this).val('');
						$(this).removeClass('placeholder');
					};
				// On blur event if user didn't input a value, reset placeholder text to title attribute value and add class="placeholder"...
				}).blur(function() {
					if (this.value == '') {
						$(this).val(pTitle);
						$(this).addClass('placeholder');
					};
				});
			}

			// When form is submitted, make sure to reset text input's value to
			// empty if value equals title attribute value...
			$('form').submit(function() {
				if ($(pInput).val() == pTitle) {
					$(pInput).val('');
				}
			});
		}
	});




});


