﻿// JScript File
jQuery.fn.centerScreen = function(loaded) {
	var obj = this;
   if(!loaded) {
		obj.css('top', $(window).height()/2-this.height()/2);
		obj.css('left', $(window).width()/2-this.width()/2);
		$(window).resize(function() { 
			obj.centerScreen(!loaded); });
	} else {
		obj.stop();
      obj.animate({ top: $(window).height()/2-this.height()/2, left: $(window).width()/2-this.width()/2}, 200, 'linear');
	}
}

jQuery.fn.centerVertically = function() {
	var obj = this;
	obj.css('top', $(window).height() / 2 - this.height() / 2);
}
jQuery.fn.centerHorizontally = function() {
	var obj = this;
	obj.css('left', $(window).width() / 2 - this.width() / 2);
}
jQuery.fn.center = function () {
	var obj = this;
	var top = $(window).height() / 2 - this.height() / 2 + $(window).scrollTop();
	var left = $(window).width() / 2 - this.width() / 2 + $(window).scrollLeft()
	obj.css('top', (top < 1 ? 0 : top));
	obj.css('left', (left < 1 ? 0 : left));
}

// jQuery extension - for highlighting the search field thingy
jQuery.fn.greytext = function (default_value, remove_class_on_focus, css_class_focus) {
	return this.each(function () {
		if(this.value.length == 0) // insert default value if blank
			this.value = default_value;
	}).focus(function () {
		$(this).removeClass(remove_class_on_focus).addClass(css_class_focus);
		if (this.value == default_value)
			this.value = "";

	}).blur(function () {
		$(this).removeClass(css_class_focus).addClass(remove_class_on_focus);
		if (this.value == "")
			this.value = default_value;
	});
};

// clickOnEnter performs a 'click' event on the target object when the enter key is pressed in the source object.
// for instance, in a aspx-page where a "search" button has default focus but you want the "login" button to be activated
// then 'enter' is pressed in the "username" or "password" textboxes....this is just what you need
(function ($) {
	var methods = {
		init: function (object) {
			
			var instance = this;
			var objects;
			
			if(arguments.length > 0 && (typeof(arguments[0]) == "string")) {
				objects = $(this);
				objects.attr('clickonenter', arguments[0]); // $('#' + arguments[0]);
			} else {
				objects = (this.attr('clickonenter') != undefined ? this : this.find("*[clickonenter]"));
			}

			return objects.each(function (intIndex, node) {
				var obj = $(node);
				if (obj.attr("clickonenter") !== undefined) {
					var clickTarget = $('#' + obj.attr("clickonenter"));
					if (clickTarget.length > 0) {
						obj.bind('keypress.clickOnEnter', function (event) {
							return methods["onEnterKey"].apply(
										this, new Array(
											event, clickTarget
										)
									);
						});
					}
				}
			});
		},
		onEnterKey: function (evt, target) {
			if (evt.keyCode == 13) { // enter
				$(target).click();
				return false;
			} else {
				return true;
			}
		}
	}
	$.fn.clickOnEnter = function (options) {
		return methods["init"].apply(this, arguments); // Array.prototype.slice.call(arguments, 1));
	};
})(jQuery);


// quick and dirty deferred image loading plugin
// mital, 20111101

(function ($) {
	var settings = {
		windowHeight: 0,
		documentHeight: 0,
		defimages: new Array(),
		timed: true
	}
	var methods = {
		init: function (object) {
			
			var instance = this;
			var objects;
			
			// add to array
			$(document).find('img[deferred]').each(function() {
				settings.defimages[settings.defimages.length] = new Array(this, $(this).attr('deferred'), $(this).position().top);
			});
			methods["defer"].apply(this);
			if(settings.defimages.length > 0)
				$(window).bind('scroll.deferredImageLoading', function() { methods["defer"].apply(this); });
		},

		defer: function () {
			if(!settings.timed) {
				return;
			}
			settings.timed = false;

			var start = $(document).scrollTop();
			var end = start + $(window).height();
			for(var i = 0, len = settings.defimages.length; i < len; i++) {
				var img = settings.defimages[i];
				if(img[2] > start && img[2] < end) {
					$(img[0]).attr('src', img[1]);
					settings.defimages.splice(i, 1); 
					len--;
					i--;
				}
			}
			for(var i = 0, len = settings.defimages.length; i < len; i++)
				settings.defimages[i][2] = $(settings.defimages[i][0]).position().top; // re-assign top values. They may have changed because of image loading
			
			setTimeout(function() { settings.timed = true; methods["defer"].apply(this); }, 1000);

			if(settings.defimages.length == 0) // cancel event when all images have been loaded
				$(window).unbind('scroll.deferredImageLoading');
		}
	}
	$.fn.deferredImageLoad = function (options) {
		return methods["init"].apply(this, arguments); // Array.prototype.slice.call(arguments, 1));
	};
})(jQuery);
