/* 
// Document: WLL Wall Animation
// Author: Tom Belknap
// Description: provides active content on pages to prevent page refreshes when commenting
//     This version creates a OOP class from which our functions can be pulled.
// Date: December 18th, 2009
// Requires: jQuery libraries, min. v1.4.2; jQuery UI libraries, min. v1.8
*/ 


// Initialization:

$(function() {
	$('.wallcommbutton').click(function() {
		$(this).showComment();
	});
	
	
});


jQuery.fn.showComment = function(post) {
	var wallId = $(this).attr('id').replace('wallcomm','');

	if ($('#w' + wallId + ' #wallcommform').length == 0) {
		var commentDiv = jQuery('<div id="wallcomment'+wallId+'" />');
		var doscroll = !this.hasClass('noscroll');
		
		commentDiv.load('ajax/html_fragments/wall_reply_form.html', function() {
			var curContext = $('#w' + wallId + ' #wallcommform');
			curContext.append('<input type="hidden" value="'+wallId+'" name="WallID">');
			curContext.attr('action', window.location.href);
			curContext.submit(validateComment);
			$('textarea.post').hint().limitInput(500,'#wallcommform #commCount');
			$('#wallcommform input[name="Cancel"]').click(function () {hideComment('#wallcomment'+wallId);});
		});
		
		commentDiv.hide();
		$('#w'+wallId).append(commentDiv);
		commentDiv.fadeIn('slow');
		
		if ($('div[id^=wallcomment][id!=wallcomment'+wallId+']').length > 0) {

			$('div[id^=wallcomment][id!=wallcomment'+wallId+']').fadeOut('fast', function() 
				{ $('div[id^=wallcomment][id!=wallcomment'+wallId+']').remove(); 
				if (doscroll) $.scrollTo($('#w' + wallId), {duration: 1000, easing:'swing'} );

				});
		} else {
			if (doscroll) $.scrollTo($('#w' + wallId), {duration: 1000, easing:'swing'} );
		}
		
		
	} else {
		hideComment('#wallcomment'+wallId);
	}
}


// Hide then remove all comment elements:
function hideComment(selector) {
	$(selector).fadeOut('slow', function() { $(selector).remove(); });
}


// revealSpan allows text to be hidden with only a small sample visible.
jQuery.fn.revealSpan = function(span) {
	if (span.style.display != 'inline') {
		span.style.display = 'inline';
		btn = document.getElementById('reveal' + span.id);
		btn.innerHTML = '&laquo;&nbsp;less';
	} else {
		span.style.display = 'none';
		btn = document.getElementById('reveal' + span.id);
		btn.innerHTML = '&raquo;&nbsp;more';
	}
}



/*//////////////////////////////////////////////////////////////////////
//                                                                    //
//  onFocus/onBlur functions for comment and wall post fields.        //
//      Provide the ability to add default text to a textarea and     //
//      have that text disappear when the user clicks in the form.    //
//                                                                    //
//////////////////////////////////////////////////////////////////////*/

jQuery.fn.dTextOnBlur = function(field) {
	var wallDefault = 'Ask a question, post a comment or just share your thoughts...';
	var commDefault = 'Respond to this!';
	var custservDefault = 'Ask customer service a question...';
	var value = field.value;
	if ((value == '') || (value == wallDefault) || (value == commDefault) || (value == custservDefault)) {
		var fieldClass = field.getAttribute('class');
		if (fieldClass == 'comment') {
			field.value = commDefault;
		} else if (fieldClass == 'custserv') {
			field.value = custservDefault;
		} else {
			field.value = wallDefault;
		}
		/*(fieldClass == 'comment') ? field.value = commDefault : field.value = wallDefault; */
	}
	field.style.color = '#aaaaaa';
};

jQuery.fn.dTextOnFocus = function(field) {
	var wallDefault = 'Ask a question, post a comment or just share your thoughts...';
	var commDefault = 'Respond to this!';
	var custservDefault = 'Ask customer service a question...';
	var value = field.value;
	if ((value == wallDefault) || (value == commDefault) || (value == custservDefault)) {
		field.value = '';
	}
	field.style.color = '#222222';
};


jQuery.fn.limitText = function(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
};

jQuery.fn.checkPost = function(field) {
	var val = field.value;
	// There is either nothing to post or else default text in the form
	if((typeof(val) == 'undefined') || (val == 'Ask a question, post a comment or just share your thoughts...')) {
		Effect.Pulsate(field.id, { pulses:2, duration:1.0 });
	} else {
		document.wallpostform.submit();
	}
};

jQuery.fn.checkComment = function(field) {
	var val = field.value;
	// There is either nothing to post or else default text in the form
	if((typeof(val) == 'undefined') || (val == 'Respond to this!test')) {
		Effect.Pulsate(field.id, { pulses:2, duration:1.0 });
	} else {
		document.wallcommform.submit();
	}
}
;
// Animate news sections: reveal the full article
// revealSpan allows text to be hidden with only a small sample visible.
function revealSpan(span) {
	if (span.style.display != 'inline') {
		span.style.display = 'inline';
		btn = document.getElementById('reveal' + span.id);
		btn.innerHTML = '&laquo;&nbsp;less';
	} else {
		span.style.display = 'none';
		btn = document.getElementById('reveal' + span.id);
		btn.innerHTML = '&raquo;&nbsp;more';
	}
};

jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'textblur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};


jQuery.fn.limitInput = function (limit, remainingElement) {
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this);
    
    function checkLength() {
      var $remaining = jQuery(remainingElement);
      var currentVal = $input.val();
      if (currentVal.length > limit) {
          $input.val(currentVal.substring(0,limit));
      }
      $remaining.val(limit - $input.val().length);
    }

     $input.keyup(checkLength);

  });
};


function validateComment() {
  var $field = $("textarea.post[name='WallCommentText']")
  return $field.val() != '' && $field.val() != $field.attr('title');
}
