/*
 *  jQuery VN Season Picker Plugin
 *
 *  Depends on: 
 *      jquery.ajaxQueue.js
 *      jquery.autocomplete.js
 *
 *  version 1.0 (08/21/2009)
 *
 *  Copyright (c) 2003-2009 by Varsity Networks, Inc. All rights reserved.
 */


// Create an anonymous wrapper function which is self-executing. The function accepts a single argument which 
// is the dollar symbol. The function is followed by a second set of parenthesis; we use these to pass the jQuery 
// library into our plugin. This means that we can use any standard jQuery functionality using the $ sign as an 
// alias. This second set of parenthesis are also what makes our function self-executing.

(function($) {

	// define SeasonSearchHelper object with some default config settings
	$.SeasonSearchHelper = {
		defaults: {
		    id: null,   
		    name: null,   
		    selValue: 0,
		    ss: null,
		    size: 20,
		    isRequired: false,
		    requiredCaption: 'Season'
		} ,
		
		params: {
		    id: null,   
		    value: null   
		}
	};

	//extend jquery with the plugin
	$.fn.extend({
		SeasonSearchHelper:function(iconfig) {
			
			//use defaults or properties supplied by user
			var config = $.extend({}, $.SeasonSearchHelper.defaults, iconfig);
			var params = $.SeasonSearchHelper.params;
			
			params.id = this.attr("id");
			params.value = this.attr("value");
			
			// setup member search plugin
			createSeasonSearchPlugin(params,config) ;
			
			//return the jquery object for chaining
			return this;
		}
	});
  
    // create plugin
	function createSeasonSearchPlugin(params,config) {
	    var objID = (config.id == null) ? config.name : config.id;
	    
		//create input search box
		$("<input type='text' />").attr({ 
			id: "srch_" + objID, 
			name: "srch_" + config.name,
			size: config.size 
		}).appendTo("#" + params.id);			

		//create hidden input to hold selected value
		$("<input type='hidden' />").attr({ 
			id: objID, 
			name: config.name 
		}).appendTo("#" + params.id);			

		//set validation rule
		if (config.isRequired) {
			addFormValidationRule("srch_" + objID,config.requiredCaption,"string");
			addFormValidationRule(config.name,config.requiredCaption,"string");
		}

		jQuery("#srch_" + objID).change(function(){ 
			jQuery("#srch_" + objID).val(""); 
			jQuery('#' + objID).val(""); 
		});
		
        //<JK>
		var searchPlaceholderText = "<Enter 4-Digit Year>";
		jQuery("#srch_" + objID).css('color','gray').val(searchPlaceholderText);
		jQuery('#' + objID).val('');
		
		jQuery("#srch_" + objID).click(function(){
			if(this.value == searchPlaceholderText){
				this.select();
			}
		}).blur(function(){
			if(this.value == ''){
				jQuery(this).css('color','gray').val(searchPlaceholderText);
				jQuery('#' + objID).val('');
				updateScheduleSelector(jQuery('#' + objID)[0]);
				this.select();
			}
		}).focus(function(){
			if(this.value == searchPlaceholderText)
				jQuery(this).css('color','gray');
			else
				jQuery(this).css('color','');	
		}).keydown(function(){
			if(this.value == searchPlaceholderText){
				jQuery(this).css('color','').val('');
				jQuery('#' + objID).val('');
				updateScheduleSelector(jQuery('#' + objID)[0]);
			}
		}).keyup(function(e){
			var code = e.charCode || e.keyCode || 0;
			if(code==9 || code==13 || (code>15 && code<32) || (code>32 && code<46) || (code>111 && code<124)) return;
			var numcheck = /^\d+$/;
			var previousText = this.value.slice(0,this.value.length-1);
			if(numcheck.test(this.value)){
				if(this.value.length > 4)
					this.value = previousText;
			}
			else{
				if(numcheck.test(previousText))
					this.value = previousText;
				else{
					jQuery(this).css('color','gray').val(searchPlaceholderText);
					jQuery('#' + objID).val('');
					updateScheduleSelector(jQuery('#' + objID)[0]);
					this.select();
				}
			}
		});
		//</JK>
			
		jQuery("#srch_" + objID).autocomplete('/services/season.asp?a=search&ss=' + config.ss, {
			minChars: 4,
			matchContains: true,
			parse: function(data) {
				return jQuery.map(eval(data), function(row,i) {
					return {
						data: row,
						value: row.name,
						result: row.name
					}
				});
			},
			formatItem: function(item) {
				return item.name;
			}
		}).result(function(e, item) {
			jQuery('#' + objID).val(item.id);
			jQuery("#srch_" + objID).blur();
			updateScheduleSelector(jQuery('#' + objID)[0]);
		});
		
		//set inital value, need to call to the server
	    if (params.value != null) {
	        var url = "/services/season.asp?a=getname&ss=" + config.ss + "&v=" + params.value;
    		
	        jQuery.getJSON(url,function(data){
	            jQuery("#srch_" + objID).val(data.name);
	            jQuery('#' + objID).val(data.id);
	            
   	            //<JK>
	            if(data.name)
					jQuery("#srch_" + objID).css('color','');
	            //</JK>
	        });
	    }
	};

})(jQuery);  

