﻿// Script for submitting a tip
$(function() {

  // Give time to update the DOM
  setTimeout(
    function() {

	    $("#inputPageNumber").focus(function() {
		    $("input, textarea").addClass("ignore");
		});

		// Allow users to 'like' a tip without causing the new tip validation to fire
		$(".goToPage").focus(function() {
		    $("input, textarea").addClass("ignore");
		});
			
	    // Set up character counter
	    $("#inputTipText")
		    .each(function() {
			    var $this = $(this), info = ".countInfo";
			    $(info).empty(); // clear out noscript text
			    $this.countable({
				    threshold: 0,
				    appendMethod: "appendTo",
				    target: info,
				    startOpacity: 1,
				    maxLength: 140,
				    interval: 500,
				    tagName: "em",
				    positiveCopy: "{n}&nbsp;characters left",
				    negativeCopy: "{n}&nbsp;characters over",
				    defaultText: $this.attr("title")
			    })
		    });
	    
	    // Set up action buttons
			
	    // Define the click actions so they can be bound/unbound easily
	    var formSubmit = {
		    newTip: function(evt) {
			    // Set the proper fields for validation
			    $(".tipForm input, .tipForm textarea").removeClass("ignore");
		    },
		    pageNumber: function(evt) {
			    // Set the proper fields for validation
			    $(".tipForm input, .tipForm textarea").addClass("ignore");
		    }
	    };
			
	    // Submit Tip
	    $(".tipForm input[type=image]").click(formSubmit.newTip);
			

	    // Set default action on ENTER press
		    
	    // Submit Tip form
	    $(".tipForm input").keydown(function(evt) {
		    if (evt.keyCode != null && ((evt.keyCode == 13) || (evt.keyCode == 10))) {
			    // Temporarily unbind the validation from other schemas
			    $(".paging input").unbind("click");
					
			    // Validate this schema
			    $(".tipForm input[type=image]").triggerHandler("click");
					
			    // Reenable the other validations after a short delay
			    setTimeout(function() {
				    $(".paging input").click(formSubmit.pageNumber);
			    }, 50);
		    }
	    });
            
	    // Page Number form
	    $(".paging input").keydown(function(evt) {
		    if (evt.keyCode != null && ((evt.keyCode == 13) || (evt.keyCode == 10))) {
			    // Temporarily unbind the validation from other schemas
			    $(".tipForm input[type=image]").unbind("click");
					
			    // This is a form submit, so no further action is necessary
			    $(this.form).get(0).submit();
		    }
	    });

    });
		
});

