;(function($) {

if (!$.ewyseCustom) {

$.extend({
	ewyseCustom: true,
	ajaxLoad: { cache: {} },
	load: function(jquery, url, args, callback) {
		return $(jquery).load(url, args, callback);
	},
	ie6: function() {
		return $.browser.msie && parseInt($.browser.version) < 7;
	},
	activeItem: function(jquery, options) {
		return $(jquery).activeItem(options);
	},
	// Creates an url based on a given url with extra parameters given in $args. $args can be a string or an object. When it's an object, it will be processed as in $.param
	makeURL: function(url, args) {
		if (args) url += (url.match(/\?/) ? "&" : "?") + (typeof args == 'string' ? args : $.param(args, true));
		return url;
	},
	// Returns an array with either the current get-arguments or the get-arguments of the given url.
	parseGetParameters: function(url, noEnv) {
		var array = {}, index, args, arg, i, key;
		if ((url || (!noEnv && (url = window.location.search))) && (index = url.indexOf('?')) !== -1) {
			args = url.substring(index + 1).split(/&amp;|&/);
			for (i = 0; i < args.length; i++) {
				arg = args[i].split('=');
				// Remove the first entry; it's our key in this pair
				key = arg.shift();
				// If the value also contains unencoded '=' signs, we save the value by rejoining the remainings
				arg = arg.join('=');
				array[key] = (typeof(arg) != 'undefined') ? decodeURIComponent(arg) : '';
			}
		}
		return array;
	},
	// The post counterpart of getJSON
	postJSON: function(url, getParams, postParams, callback) {
		return $.ajax({
			type: "POST",
			url: $.makeURL(url, getParams),
			dataType: "json",
			data: postParams,
			success: callback
		});
	},
	// Add a stylesheet to the page
	createStyleSheet: function(url) {
		if (document.styleSheets) {
			for (var i=0; i<document.styleSheets.length; i++) {
				if (document.styleSheets[i].href.indexOf(url) >= 0) return document.styleSheets[i];
			} 
		}
		if ($.browser.msie) {
			return document.createStyleSheet(url);
		} else {
			return $('<link>').appendTo('head').attr({ href: url, rel: 'stylesheet', type: 'text/css' })[0];
		}
	}
});

if (!$.fn._load) $.fn._load = $.fn.load;

$.fn.extend({
	// Adds arguments $args to the current URL using $.makeURL(url, args).
	// Works on iframes, forms and anchors
	args: function(args) {
		return this.each(function() {
			if (/FORM|A/.test(this.nodeName)) {
				$(this).attr(this.nodeName == 'FORM' ? 'action' : 'href', $.makeURL(this.href || this.action, args));
			}
		});
	},
	// Run this over a group of images to wait until they're ready. Runs the $callback with $callbackParams (an array) when all images are loaded (or if they failed)
	imageReady: function(callback, callbackParams) {
		callbackParams = callbackParams || [];
		var images = this.filter('img'), count = images.length, loaded = 0, self = this;
		images.each(function() {
			if (this.complete) loaded++;
			else $(this).one('load error abort', function() {
				loaded++;
				if (loaded == count) callback.apply(self, callbackParams);
			});
		});
		if (loaded == count) callback.apply(self, callbackParams);
		return this;
	},
	// Perform a function if a condition is fullfilled.. (to not break chaining and
	// to prevent queries to have to run again.
	doIf: function(bool, func) {
		return (bool && $.isFunction(func)) ? func.apply(this) : this;
	},
	// Makes the selected elements positioned absolute in exactly the same place as they were
	absolutize: function() {
		return this.each(function() {
			var $this = $(this), offset = $this.position();
			$this.css({
				position: 'absolute',
				top: offset.top,
				left: offset.left,
				width: $this.width(),
				height: $this.height()
			});
		});
	},
	lightbox: function(args, options) {
		function ajaxLoad(json) {
			PopupMessage().lightbox(json.content, options);
		}
		
		this.filter('a:not(.jq_lightbox)').addClass('jq_lightbox').ajaxLoad(args, options, ajaxLoad);
		return this;
	},
	// Loads the .href attribute of the selected elements using getJSON and passes the JSON object to the callback function
	// Has cache functionality, when explicitly requested in the options; {cache: true}
	
	// The callback can also be given as a second parameter, when no options are to be set
	
	// $args		must be an object or a string added to the current url using $.makeURL(url, args) to change the request (add extra parameters for the json request, for instance)
	// $options		optional; currently only used to set the cache: true property to cache requests
	// $callback	the function to call when the json is loaded
	ajaxLoad: function(args, options, callback) {
		// shift arguments if data argument was ommited
		if ($.isFunction(options)) {
			callback = options;
			options = {};
		} else options = options || {};
		return this.each(function() {
			// Create a new url
			var url = args ? $.makeURL(this.href, args) : this.href, self = this;
			url = encodeURI(url);
			// TODO: Here goes the caching, once we fixed it
			if (false) {
			//if (callback && options.cache) {
				var callback2 = callback;
				callback = function(json) {
					$.ajaxLoad.cache[url] = json;
					callback2.call(self, json);
				};
			}
			$(self).click(function(e) {
				e.preventDefault();
				// Return cache if requested and available, otherwise load JSON through a get request
				if (callback2 && $.ajaxLoad.cache[url]) callback.call(self, $.ajaxLoad.cache[url]);
				else $.getJSON(url, callback);
			});
		});
	},
	load: function(url, args, callback) {
		// Backwards compatibility for the jquery load function
		if ($.isFunction(url)) return $(this)._load(url, args, callback);
		if (!this.length) return this;
		var url2 = url;
		if (args) url2 = $.makeURL(url, args);
		return this.each(function() {
			if (typeof this['location'] == 'object') this.location.href = url2;
			else if (typeof this['href'] != 'undefined') this.href = url2;
			else if (typeof this['src'] != 'undefined') this.src = url2;
			else $(this)._load(url, args, callback);
		});
	},
	hoverClass: function(className) {
		return this.hover( 
			function () { $(this).addClass(className); },
			function () { $(this).removeClass(className); }
		);
	},
	focusClass: function(className) {
		return this.each(function () {
			$(this)
				.focus(function() { $(this).addClass(className); })
    			.blur(function() { $(this).removeClass(className); });
		});
	},
	activeClass: function(className) {
		return this.each(function () {
			$(this)
				.mousedown(function() { $(this).addClass(className); })
    			.mouseup(function() { $(this).removeClass(className); });
		});
	},
	document: function(query) {
		if (this.length == 1 && (el = this[0]) && el.nodeName == 'IFRAME') {
			var result = $(el.contentWindow.document);
		} else {
			var result = $(document);
		}
		if (typeof query != 'undefined') return result.find(query);
		else return result;
	},
	window: function(query) {
		if (this.length == 1 && (el = this[0]) && el.nodeName == 'IFRAME') {
			var result = $(el.contentWindow);
		} else {
			var result = $(window);
		}
		if (typeof query != 'undefined') return result.find(query);
		else return result;
	},
	enable: function(setOpacity) {
		//alert('hi');
		if ($.browser.mozilla || setOpacity) { this.css('opacity', 1);};
		return this.removeAttr('disabled');
	},
	disable: function(setOpacity) {
		if ($.browser.mozilla || setOpacity) this.css("opacity", 0.4);
		return this.attr('disabled','disabled');
	},
	enabled: function(aBool, setOpacity) {
		return this[aBool === false ? 'disable' : 'enable'](setOpacity);
	},
	activeItem: function(options) {
		if (!options) options = {};
		
		var config = {
			classBase: ((options.classBase || this.className) || 'dummyClassForActiveItem'),
			classDown: options.classDown || '',
			classOver: options.classOver || '',
			classActive: options.classActive || '',
			navigation: options.navigation || false, // none, vertical, horizontal
			keyNext: options.navigation == 'horizontal' ? 39 : 40,
			keyPrev: options.navigation == 'horizontal' ? 37 : 38
		};
		
		return this.each(function (i) {
			var $this = $(this);
			// Make tab-selectable
			this.tabIndex = '0';
			
			this.className = config.classBase;
			
			if (config.classOver) $this.mouseover(function(event) {
				this.focus();
				$(this).addClass(config.classOver).siblings('.' + config.classBase).mouseout();
			});
			
			if (config.classOver || config.classDown) $this.mouseout(function (event) {
				if (!config.classActive || !$this.is('.' + config.classActive)) {
					$(this).removeClass([config.classDown, config.classOver].join(' '));
				}
			});
			
			$this.blur(function (event) {
				$(this).triggerHandler('mouseout');
			});

			$this.focus(function (event) {
				$(this).triggerHandler('mouseover');
			});

			if (config.classDown) {
				$this.mousedown(function (event) {
					$(this).addClass(config.classDown);
					return false;
				});
				$this.mouseup(function (event) {
					$(this).mouseout();
					return false;
				});
			}
			// Set general actions
			$this.keyup(function (event) {
				if (/13|32/.test(event.which)) {
					$(this).mouseup().click();
					return false;
				}
			});

			$this.keydown(function (event) {
				if (/13|32/.test(event.which)) {
					$(this).mousedown();
					return false;
				} else if (config.navigation) {
					if (event.which == config.keyNext && (nextItem = $(this).next().not(':disabled')) && nextItem.length) {
						$(this).mouseout();
						nextItem.mouseover().focus();
						return false;
					} else if (event.which == config.keyPrev && (prevItem = $(this).prev().not(':disabled')) && prevItem.length) {
						$(this).mouseout();
						prevItem.mouseover().focus();
						return false;
					}
				}
			});
			if (config.classOver) {
				$this.mouseover(function (event) {
				if (!config.classActive || !$this.is('.' + config.classActive)) {
						$this.addClass(config.classOver);
					}
				});
			}
		});
	}
});

/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-07-21 18:44:59 -0500 (Sat, 21 Jul 2007) $
 * $Rev: 2446 $
 *
 * Version 2.1.1
 */
$.fn.bgIframe = $.fn.bgiframe = function(s) {
	// This is only for IE6
	if ( $.browser.msie && $.browser.version == '6.0' ) {
		s = $.extend({
			top     : 'auto', // auto == .currentStyle.borderTopWidth
			left    : 'auto', // auto == .currentStyle.borderLeftWidth
			width   : 'auto', // auto == offsetWidth
			height  : 'auto', // auto == offsetHeight
			opacity : true,
			src     : 'javascript:false;'
		}, s || {});
		var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
		    html = '<iframe class="bgiframe" frameborder="0" tabindex="-1" src="'+s.src+'" '+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
		return this.not('.bgiframeProcessed').prepend(html).addClass('bgiframeProcessed');
	}
	return this;
};
}

})(jQuery);
