﻿jQuery.preloadImages = function (ele, att) {
	if (jQuery.isArray(ele)) {
		jQuery.each(ele, function () {
			jQuery('<img />').attr('src', this);
		});
	} else {
		ele.each(function () {
			jQuery('<img />').attr('src', $(this).attr(att));
		});
	}
};

jQuery.toggleArrayValue = function (value, array) {
	var index = jQuery.inArray(value, array);
	
	if (index != -1) { 
		array.splice(index, 1);
	} else { 
		array.push(value);
	}
	
	return array;
};

jQuery.getUniqueScript = function (url, callback) {
	if (jQuery.getUniqueScript.uniqueScripts.length < 1) {
		jQuery('script').each(function () {
			var src = jQuery(this).attr('src');
			if (typeof(src) != 'undefined') {
				jQuery.getUniqueScript.uniqueScripts.push(src);
			}
		})
	}
	
	if (jQuery.inArray(url, jQuery.getUniqueScript.uniqueScripts) == -1) {
		jQuery.getUniqueScript.uniqueScripts.push(url);
		return jQuery.getScript(url, callback, true);
	} else {
		if (typeof(callback) != 'undefined') {
			callback();
		}
		return false;
	}
};

jQuery.getUniqueScript.uniqueScripts = jQuery.makeArray();

jQuery.fn.addHoverState = function () { 
	this.hover(
		function () { jQuery(this).addClass('hover-active') },
		function () { jQuery(this).removeClass('hover-active') }
	);
};

jQuery.fn.matchHeight = function () {
	var maxHeight = (arguments.length > 0) ? arguments[0] : 0;
	
	this.each(function () {
		height = jQuery(this).height();
		maxHeight = (height > maxHeight) ? height : maxHeight;
	});
	
	this.height(maxHeight);
	
	return this;
};

jQuery.getScript = function (url, callback, cache) {
	jQuery.ajax({
		type: 'GET',
		url: url,
		success: callback,
		dataType: 'script',
		cache: cache
	});
};
