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