

/*
 * Gapyear jQuery plugin
 */
(function($){
	
	/**
	 * @description applies an autocomplete to an input field, using specified source.
	 * @usage $('#inputId').gy_autocomplete({
	 * 		 	sourceUrl: '/global_misc/gy_country_list', 
	 * 		 	onAjaxLoad: function data(){  return data;  }  // manipulate data if required.
	 * 			callback: function(event, item){
	 * 				// do something with the selected 
	 * 				 console.log(item.value, item.id);  
	 * 			}
	 * 		})
	 */
	$.fn.gy_autocomplete = function(opts) {
		var _this = this;
		var source = [];
		var defaults = {
			sourceUrl: '/global_misc/gy_country_list',
			minLength: 1,
			onAjaxLoad: function(data ){
				// override this to do something with the raw list returned by the ajax call
				return data;
			},
			callback: function(event, item ){
				// override this to do something with the result
			}
			
		}
		var options = $.extend( {}, defaults, opts );
		
		
		$.getJSON(options.sourceUrl, function(data) {
			// load the ajax data 
			data = options.onAjaxLoad(data);
			
			// build the autocomplete list
			$.each(data, function(i, val){
				if(typeof(val)=="object")
				{
					source.push(  { value: val.name, id: i, url_title:val.url_title} );	
				}
				else
				{
					source.push(  { value: val, id: i, url_title: GY.urlString(val)} );
				}
			});
			
						// Set up the autcomplete on each input field specified.
			return _this.each(function(){
				var $field = $(this);
				
				$field.autocomplete({
					minLength: options.minLength,
					delay: 200,
					source: source,
					select: function(event, item) { options.callback(event, item.item) }
				})
				.focus(function(){
					this.select();
				})
				.data( 'autocomplete' )._renderItem = function( $ul, item ) {
	                // This _renderItem function highlights the searched text and brings results matched at character 0 to the top
	                if($ul.find('li#matchedFromStartSeperator').length<1 )
	                {
	                    $ul.append( '<li id="matchedFromStartSeperator" style="height:0px; visibility: hidden;">&nbsp;</li>' );
	                }
	                if(item.value.toLowerCase().indexOf( $field.val().toLowerCase()) ===0)
	                {
	                    $li = $( '<li></li>' )
	                        .data( "item.autocomplete", item )
	                        .append( $( "<a></a>" ).html(  _hightlightSearchTerm( item.value, $field.val() ) ));
	                    $ul.find('li#matchedFromStartSeperator').before($li);   
	                    return $li;
	                } 
	                else
	                {
	                    $li = $( '<li></li>' )
	                        .data( "item.autocomplete", item )
	                        .append( $( "<a></a>" ).html(_hightlightSearchTerm(item.value, $field.val())) );
	                    $ul.append($li);
	                    return $li; 
	                }
	      		};
      		});
		});
		
		// helper function which highlights search term
		function _hightlightSearchTerm(s,t){
	        var matcher = new RegExp("("+$.ui.autocomplete.escapeRegex(t)+")", "ig" );
	        return s.replace(matcher, "<strong>$1</strong>");
		}
	}; 
	
})(jQuery);

/*
 * Gapyear jQuery plugin
 */
(function($){
	
	/**
	 * @description applies an autocomplete to an input field, using specified source.
	 * @usage $('#inputId').gy_autocomplete({
	 * 		 	sourceUrl: '/global_misc/gy_country_list', 
	 * 		 	callback: function(event, item){
	 * 				// do something with the selected 
	 * 				 console.log(item.value, item.id);  
	 * 			}
	 * 		})
	 */
	 		
	 	$.fn.gy_tabs = function(opts) {	
	 		// Set up the autcomplete on each input field specified.
			return this.each(function(){
				var $field = $(this);
				$field.tabs();
				
				// Allow the tabs to record history
				(function(){
	    			var hash = '';
	    			setInterval(function(){ 
		    				if(hash!=window.location.hash)
		    				{
		    					$field.tabs( "select" , window.location.hash );
		    					hash = window.location.hash;
		    				}
		    			}, 300);
		    	})()
				
				// when the tab is selected update the url with the hash
				$field.bind("tabsselect", function(event, ui) { 
				  window.location.hash = ui.tab.hash;
				});

			});
		}
})(jQuery);


/**
 * ztInputHint Plugin
 *
 * @version 1.2 (04/03/2010)
 * @requires jQuery v1.3+
 * @author Zeljko Trulec <trulec.de>
 * @copyright Copyright (c) 2010, Zeljko Trulec
 * @see https://code.google.com/p/ztinputhint/
 * 
 * Distributed under the terms of the GNU General Public License
 * http://www.gnu.org/licenses/gpl-3.0.html
 */
(function($){$.fn.ztinputhint=function(g){var h={hint:null,sourceAttrib:'title',hintClass:false};if(g&&typeof g=='object'){$.extend(h,g)}else if(g){h.hint=g}return this.each(function(){var a=$(this);if(!a.is('input:text, input:password')){return false}var b='ztInputHint_'+a.attr('name');var c='<input type="text" value="" style="display: none;" />';var d=h.hint||a.attr(h.sourceAttrib);var e=h.hintClass||a.attr('class');$(c).insertBefore(a);var f=a.prev('input:first');if(d){f.attr('class',e);f.attr('size',a.attr('size'));f.attr('tabIndex',a.attr('tabIndex'));f.val(d);a.attr('autocomplete','off');f.focus(function(){$(this).hide();cont=$(this).next('input:first');cont.show();cont.focus()});a.blur(function(){if($(this).val()==''){$(this).prev('input:first').show();$(this).hide()}});if(h.iconClass&&!a.hasClass(h.iconClass)){a.addClass(h.iconClass)}a.blur()}})};$.fn.ztInputHint=$.fn.ztinputhint})(jQuery);

