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

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

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


$(document).ready(function() {

	/* -----------------------------------------------------------------------------
	 * Function: Open rel="external" href's in new window
	 * Purpose:  Keep html clean, open rel="external" links in new window
	 * ---------------------------------------------------------------------------*/
	$('a[rel=external], a[rel=new]').click(function() {
		window.open(this.href);
		return false;
	});


	/* -----------------------------------------------------------------------------
	 * 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('');
				}
			});

		}

	});


	/* -----------------------------------------------------------------------------
	 * Function: Word Counter
	 * Purpose:  Count words of specified field
	 * ---------------------------------------------------------------------------*/
	function countWords(field) {
		var y = field.val();
		var r = 0;
		var words_left = 0;
		a = y.replace(/\s/g,' ');
		a = a.split(' ');
		for (z=0; z < a.length; z++) {
			if (a[z].length > 0) r++;
		}

		words_left = 100-r;
		// words_used = r;

		return words_left;
		// return words_used;
	}


	/* -----------------------------------------------------------------------------
	 * Function: Live word counter
	 * Purpose:  Same
	 * ---------------------------------------------------------------------------*/
	$('.wordCount').each(function(){

		if ($(this).val() == $(this).attr("title")) {
			words_left = 100;
		} else {
			words_left = countWords($(this));
		}

		if (words_left == 1) {
			$(this).parent().find('.wordCounter').html('<strong>' + words_left + '</strong> word');
		} else {
			$(this).parent().find('.wordCounter').html('<strong>' + words_left + '</strong> words');
		}

		if (words_left < 0) {
			$(this).parent().find('span.max, span.wordCounter strong').addClass('err');
		} else {
			$(this).parent().find('span.max, span.wordCounter strong').removeClass('err');
		}

		$(this).keyup(function(){
			words_left = countWords($(this));

			if (words_left == 1) {
				$(this).parent().find('.wordCounter').html('<strong>' + words_left + '</strong> word');
			} else {
				$(this).parent().find('.wordCounter').html('<strong>' + words_left + '</strong> words');
			}

			if (words_left < 0) {
				$(this).parent().find('span.max, span.wordCounter strong').addClass('err');
			} else {
				$(this).parent().find('span.max, span.wordCounter strong').removeClass('err');
			}
		});
	});




});


