(function($){
	/** makeChain
	  * parameters: whether or not to flush the chain (should be true) after execution
	  *
	  * the $.tps object chains together JS blocks to execute once the DOM is loaded
	  * makeChain is aliased to $.tps.init in jQuery.tps.js. 
	  * makeSinglePager is deprecated and currently unused.
	*/
	
	
	$.tps = {
		makeSinglePager: function(index, length, prev, next) {
			var pager = $("<ul/>");
			if(index > 0) {
				pager.append($("<li/>").addClass("pager-prev").append(
					$("<a href='#'>prev</a>").click(prev)
				));
			}
			pager.append($("<li>" + (index + 1) + " of " + length + "</li>"));
			if((index + 1) < length) {
				pager.append($("<li/>").addClass("pager-next").append(
					$("<a href='#'>next</a>").click(next)
				));
			}
			return pager;
		},
		makeChain: function(autoFlush) {
			var chain = [];
			return (function(a) {
				if($.isFunction(a)) {
					chain.push(a);
				} else {
					$.each(chain, function(i, f) {
						f(a);
						if(autoFlush) {
							chain[i] = null;
						}
					});
					if(autoFlush) {
						chain = [];
					}
				}
			});
		}
	};

	/** findNormalAnchor
	  * parameters: conditional to evaluate when an anchor is found
	  *
	  * used by $.fn.findBaynoteAnchor to hook anchors to execute 
	  * Baynote exit events
	*/
	
	$.fn.findNormalAnchor = function(cond) {
		return $($.grep($(this).find("a"), function(element){
			var target = $(element);
			return target.attr("href")
				&& target.attr("href") != "#"
				&& !(target.data("events") || {})['click']
				&& (cond ? cond(target) : true);
		}));
	};

	
	/** load
	  * parameters: url, params to send (optional), callback when complete
	  *
	  * modified version of jQuery's load function. see notes below
	*/
	
	$.fn.load_without_error_handler = $.fn.load;
	$.fn.load = function(url, params, callback) {
		if(params) {
			if(params) {
				if(jQuery.isFunction(params)) {
					callback = params;
					params = null;
				}
			}
		}
		var self = $(this);
		var callback_with_error_handler = callback ? function(response, status, xhr) {
			if(status == "success" || status == "notmodified") {
				// EXPERIMENTAL $.loadContext shows the current context where we apply the initialization code.
				// NOTE normally the initialization code is placed inside $(function) so that we can setup the elements.
				// In the initialzation code, we may use $(selector) to find the element which we want to setup, but it works under document element.
				// If we load the html by ajax, it should work under elements which we load not whole document elements.
				// In this case, $.loadContext shows where we eavluate the selector, if ourside of ajaxed html, it shows document itself (see the end of this code block).
				// We can use this context like: $(selector, $.loadContext).
				// FIXME We can apply this context to $(selector) automatically by overwrapping the jQuery.init.
				var c = $.loadContext;
				$.loadContext = self;
				callback.apply(self, arguments);
				$.loadContext = c;
			}
//			else {
//				alert("Connection Error, Please try again");
//			}
		} : undefined;
		$.fn.load_without_error_handler.call(this, url, params, callback_with_error_handler);
	};
	$.loadContext = $(document);

	/** serializeArray
	  * parameters: none
	  *
	  * modified version of jQuery's serializeArray function that includes the form submit element,
	  * required by Drupal
	*/

	$.fn.serializeArray_without_submit = $.fn.serializeArray;
	$.fn.serializeArray = function() {
		return ($(this).serializeArray_without_submit() || []).concat($(this).find("input:submit").map(function(i, elem){
			var val = $(this).val();
			return val == null ? null : {name: elem.name, value: val};
		}).get());
	}

	/** displayfix
	  * parameters: none
	  *
	  * forces an msie display update
	*/
	$.fn.displayfix = function() {
		if($.browser.msie) {
			var display = $(this).css("display");
			$(this).css("display", "").css("display", display);
		}
	};
})(jQuery);
