/// <reference path="jquery.js" />

/* ============================
DotControl copyright 2010

Version 0.5

==============================*/

/* FULL DIV LINK */
(function ($) {
	$.fn.MakeLink = function () {
		return this.each(function (i) {
			$(this).click(function () { window.location = $(this).find("a").attr("href"); return false; });
		});
	};
})(jQuery);

/* EQUAL HEIGHT */
(function ($) {
	$.fn.equalHeight = function () {
		var tallest = 0;
		var thisHeight = 0;
		this.each(function (i) {
			thisHeight = $(this).height();
			if (thisHeight > tallest) {
				tallest = thisHeight;
		}
		});
		return this.each(function (i) {
			$(this).height(tallest);
		});
	};
})(jQuery);

function equalHeight(group) {
	var tallest = 0;
	var thisHeight = 0;
	group.each(function () {
		thisHeight = $(this).height();
		if (thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.each(function () {
		$(this).height(tallest);
	});
}

/* VERTICALLY ALIGN FUNCTION */
(function ($) {
	$.fn.vAlign = function () {
		return this.each(function (i) {
			$(this).load( function() {
				var ah = $(this).height();
				var ph = $(this).parent().parent().height();
				var mh = (ph - ah) / 2;
				$(this).css('top', mh);
			});
		});
	};
})(jQuery);

/* OPTIMIZED LIST */
(function ($) {
	$.fn.toOpimizedList = function () {
		return this.each(function (i) {
			$(this).children('li').Optimize();
		});
	};
})(jQuery);

/* OPTIMIZED TABLE */
(function ($) {
	$.fn.toOpimizedTable = function () {
		return this.each(function (i) {
			$(this).children('tr').Optimize();
		});
	};
})(jQuery);


/* OPTIMIZE */
(function ($) {
	$.fn.Optimize = function () {
		this.first().addClass('first');
		this.last().addClass('last');
		this.addClass('alt');
		this.hover(function () {
			$(this).addClass('hover');
		}, function () {
			$(this).removeClass('hover');
		});
		return this;
	};
})(jQuery);




/* txtbox hints */

jQuery.fn.hint = function (text) {

	var blurClass = 'blur';

	return this.each(function () {
		// get jQuery version of 'this'
		var $input = jQuery(this);

		// capture the rest of the variable to allow for reuse
		if (!text) {
			var title = $input.attr('title');
		} else {
			var title = text;
		}
		var $form = jQuery(this.form);
		var $win = jQuery(window);

		function remove() {
			if ($input.val() === title && $input.hasClass(blurClass)) {
				$input.val('').removeClass(blurClass);
			}
		}

		// only apply logic if the element has the attribute
		if (title) {
			// on blur, set value to title attr if text is blank
			$input.blur(function () {
				if (this.value === '') {
					$input.val(title).addClass(blurClass);
				}
			}).focus(remove).blur(); // now change all inputs to title

			// clear the pre-defined text when form is submitted
			$form.submit(remove);
			$win.unload(remove); // handles Firefox's autocomplete
		}
	});
};

/* VALIDATORS */

function ValidateRequired(sender, args) {
	var $val = args.Value;
	args.IsValid = requiredValidate($val, sender);
}

function ValidateEmail(sender, args) {
	var $val = args.Value;
	args.IsValid = validateEmail($val, sender);

}

function requiredValidate(value, sender) {
	isValid = !(value == "" || value == false);
	highlightError($(sender).siblings('input, textarea'), isValid);
	return isValid;
}

function validateEmail(value, sender) {
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var isValid = emailPattern.test(value);
	highlightError($(sender).siblings('input, textarea'), isValid);
	return isValid;
}

function highlightError(object, isValid) {
	if (!isValid) {
		$(object).addClass('invalid');
	} else {
		$(object).removeClass('invalid');
	}
	$('.invalid').first().focus();
}


