/*************************************************************
* jQuery FishEye plugin v0.5.0
* Copywright (c) 2008 Ian Suttle
* http://www.iansuttle.com/blog
*
* Licensed under the GNU General Public License: 
* http://www.gnu.org/licenses/gpl.html
**************************************************************/
;(function($) {
		jQuery.iFishEye = {
		build: function (options) {
			return this.each(function () {
				var el = this;
				el.fisheyeCfg = {
					items: jQuery(options.items, this),
					container: jQuery(options.container, this),
					pos: jQuery.iUtil.getPosition(this),
					itemWidth: options.itemWidth,
					itemsText: options.itemsText,
					proximity: options.proximity,
					valign: options.valign,
					halign: options.halign,
					maxWidth: options.maxWidth
				};
				jQuery.iFishEye.positionContainer(el, 0);
				jQuery(window).bind('resize', function () {
					el.fisheyeCfg.pos = jQuery.iUtil.getPosition(el);
					jQuery.iFishEye.positionContainer(el, 0);
					jQuery.iFishEye.positionItems(el)
				});
				jQuery.iFishEye.positionItems(el);
				el.fisheyeCfg.items.bind('mouseover', function () {
					jQuery(el.fisheyeCfg.itemsText, this).get(0).style.display = 'block'
				}).bind('mouseout', function () {
					jQuery(el.fisheyeCfg.itemsText, this).get(0).style.display = 'none'
				});
				jQuery(document).bind('mousemove', function (e) {
					var pointer = jQuery.iUtil.getPointer(e);
					var toAdd = 0;
					if (el.fisheyeCfg.halign && el.fisheyeCfg.halign == 'center') var posx = pointer.x - el.fisheyeCfg.pos.x - (el.offsetWidth - el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size()) / 2 - el.fisheyeCfg.itemWidth / 2;
					else if (el.fisheyeCfg.halign && el.fisheyeCfg.halign == 'right') var posx = pointer.x - el.fisheyeCfg.pos.x - el.offsetWidth + el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size();
					else var posx = pointer.x - el.fisheyeCfg.pos.x;
					var posy = Math.pow(pointer.y - el.fisheyeCfg.pos.y - el.offsetHeight / 2, 2);
					el.fisheyeCfg.items.each(function (nr) {
						distance = Math.sqrt(Math.pow(posx - nr * el.fisheyeCfg.itemWidth, 2) + posy);
						distance -= el.fisheyeCfg.itemWidth / 2;
						distance = distance < 0 ? 0 : distance;
						distance = distance > el.fisheyeCfg.proximity ? el.fisheyeCfg.proximity : distance;
						distance = el.fisheyeCfg.proximity - distance;
						extraWidth = el.fisheyeCfg.maxWidth * distance / el.fisheyeCfg.proximity;
						this.style.width = el.fisheyeCfg.itemWidth + extraWidth + 'px';
						//$(this).animate({width: el.fisheyeCfg.itemWidth + extraWidth + 'px'},1);
						this.style.left = el.fisheyeCfg.itemWidth * nr + toAdd + 'px';
						//$(this).animate({left: el.fisheyeCfg.itemWidth * nr + toAdd + 'px'},1);
						toAdd += extraWidth
					});
					jQuery.iFishEye.positionContainer(el, toAdd)
				})
			})
		},
		positionContainer: function (el, toAdd) {
			if (el.fisheyeCfg.halign) if (el.fisheyeCfg.halign == 'center') el.fisheyeCfg.container.get(0).style.left = (el.offsetWidth - el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size()) / 2 - toAdd / 2 + 'px';
			else if (el.fisheyeCfg.halign == 'left') el.fisheyeCfg.container.get(0).style.left = -toAdd / el.fisheyeCfg.items.size() + 'px';
			else if (el.fisheyeCfg.halign == 'right') el.fisheyeCfg.container.get(0).style.left = (el.offsetWidth - el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size()) - toAdd / 2 + 'px';
			el.fisheyeCfg.container.get(0).style.width = el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size() + toAdd + 'px'
		},
		positionItems: function (el) {
			el.fisheyeCfg.items.each(function (nr) {
				this.style.width = el.fisheyeCfg.itemWidth + 'px';
				this.style.left = el.fisheyeCfg.itemWidth * nr + 'px'
			})
		}
	};
	jQuery.fn.FishEye = jQuery.iFishEye.build;
})(jQuery);