jQuery.iFisheye = {
	
	build : function(options)
	{
	
		return this.each(
			function()
			{
				var el = this;
				el.fisheyeCfg = {
					items : jQuery(options.items, this),
					container: jQuery(options.container, this),
					scrollContainer: jQuery(options.scrollContainer),
					scrollOverHeight: options.scrollOverHeight,
					scrollOverTop: options.scrollOverTop,
					scrollOverWidth: options.scrollOverWidth,
					pos : jQuery.iUtil.getPosition(this),
					itemWidth: options.itemWidth,
					itemsText: options.itemsText,
					proximity: options.proximity,
					valign: options.valign,
					halign: options.halign,
					maxWidth : options.maxWidth
				};
				
				el.fisheyeCfg.pos.x += 80;
				el.fisheyeCfg.pos.y -= 50;
				el.fisheyeCfg.container.get(0).nLasttime = new Date().getTime();
				el.fisheyeCfg.container.get(0).bMoveInProcess = false;
				el.fisheyeCfg.container.get(0).moveStep = 0;
				
				el.fisheyeCfg.scrollWidth = el.fisheyeCfg.scrollContainer.get(0).offsetWidth;
				el.fisheyeCfg.scrollPos = jQuery.iUtil.getPosition(el.fisheyeCfg.scrollContainer.get(0));
				
				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);
				
				$("#workbig img:first").fadeOut("slow");
				jQuery("a img", this).hover(
					function() {
						jQuery.iFisheye.showBigWork($(this).attr("lowsrc"), $(this).attr("content"), $(this).attr("alt"));
					},
					function() {
					}
				).click( function() {
						jQuery.iFisheye.showBigWork($(this).attr("lowsrc"), $(this).attr("content"), $(this).attr("alt"));
					}
				);
				
				
				jQuery(document).bind(
					'mousemove',
					function(e) {
						var t = new Date().getTime();
						if(t - el.fisheyeCfg.container.get(0).nLasttime > 100) {
							el.fisheyeCfg.container.get(0).nLasttime = t;
							
							var pointer = jQuery.iUtil.getPointer(e);
							el.fisheyeCfg.container.get(0).mousePosX = pointer.x;
							el.fisheyeCfg.container.get(0).mousePosY = pointer.y;
							if( pointer.x - el.fisheyeCfg.scrollPos.x > el.fisheyeCfg.scrollWidth + 30 || pointer.x - el.fisheyeCfg.scrollPos.x < -50 ) {
								el.fisheyeCfg.container.get(0).mousePosX = 0;
								el.fisheyeCfg.container.get(0).mousePosY = 0;
							}
							jQuery.iFisheye.processEye(el);
							
							
							
							
							
							el.fisheyeCfg.container.get(0).moveStep = jQuery.iFisheye.getMoveStep(el);
							jQuery.iFisheye.moveSmallWorks(el);
							
							if(el.fisheyeCfg.container.get(0).moveStep) {
								if(el.fisheyeCfg.container.get(0).bMoveInProcess != true) {
									clearInterval ( el.fisheyeCfg.container.get(0).intervalId );
									el.fisheyeCfg.container.get(0).intervalId = setInterval ( function(){jQuery.iFisheye.moveSmallWorks(el)}, 100 );
									el.fisheyeCfg.container.get(0).bMoveInProcess = true;
								}
							} else {
								if(el.fisheyeCfg.container.get(0).bMoveInProcess) {
									clearInterval ( el.fisheyeCfg.container.get(0).intervalId );
									el.fisheyeCfg.container.get(0).bMoveInProcess = false;
								}
							}
						}
					}
				);
			}
		)
	},
	
	processEye: function(el) {
		/*if(
				el.fisheyeCfg.container.get(0).mousePosX - el.fisheyeCfg.scrollPos.x < el.fisheyeCfg.scrollWidth + 50 && 
				el.fisheyeCfg.container.get(0).mousePosX - el.fisheyeCfg.scrollPos.x > -50
		)/**/ {

			var mouseX = el.fisheyeCfg.container.get(0).mousePosX;
			var mouseY = el.fisheyeCfg.container.get(0).mousePosY;
			var containerX = el.fisheyeCfg.container.get(0).offsetLeft;
			
			var toAdd = 0;
			
			var posx = mouseX - el.fisheyeCfg.pos.x - containerX;
			var posy = Math.pow(mouseY - el.fisheyeCfg.pos.y - el.offsetHeight/2, 2);
			
			el.fisheyeCfg.items.each(
				function(nr)
				{
					var distance = Math.sqrt( Math.pow(posx - nr*(el.fisheyeCfg.itemWidth + 3), 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;
					
					var extraWidth = el.fisheyeCfg.maxWidth * distance/el.fisheyeCfg.proximity;
					
					this.style.width = this.style.height = el.fisheyeCfg.itemWidth + extraWidth + 'px';
					this.style.top = -(extraWidth/2) + 'px';
//					this.style.opacity = 0.5 + (extraWidth / (el.fisheyeCfg.maxWidth * 2) );
//					this.style.filter = "alpha(opacity=" + (0.5 + (extraWidth / (el.fisheyeCfg.maxWidth * 2) ))*100 + ")";

					toAdd += extraWidth;
				}
			);
			jQuery.iFisheye.positionContainer(el, toAdd);
			el.fisheyeCfg.container.get(0).bSizingInProcess = false;
		}
		
	},
	
	moveSmallWorks : function(el) {
		var obj = el.fisheyeCfg.container.get(0);
		var l = obj.offsetLeft;
		if( (obj.moveStep > 0 && l < -obj.moveStep) || (obj.moveStep < 0 && l > 735 - (obj.moveStep + 20) - obj.offsetWidth ) ){
			obj.style.left = (l + obj.moveStep) + "px";
		} else {
			if(obj.moveStep < 0) {
				obj.style.left = (735 - obj.offsetWidth) + "px";
			}
			if(obj.moveStep > 0) {
				obj.style.left = "0px";
			}
		}
		jQuery.iFisheye.processEye(el);
	},
	
	getMoveStep : function(el) {
		var mx = el.fisheyeCfg.container.get(0).mousePosX - el.fisheyeCfg.scrollPos.x;
		var my = el.fisheyeCfg.container.get(0).mousePosY - el.fisheyeCfg.scrollPos.y - el.fisheyeCfg.scrollOverTop;
		
		var step = 0;
		if(my > 0 && my < el.fisheyeCfg.scrollOverHeight) {
			if(mx > 0 && mx < el.fisheyeCfg.scrollOverWidth) {
				step = Math.round((el.fisheyeCfg.scrollOverWidth - mx) / 20);
			} else if(mx < el.fisheyeCfg.scrollWidth && mx > el.fisheyeCfg.scrollWidth - el.fisheyeCfg.scrollOverWidth) {
				step = -Math.round((mx + el.fisheyeCfg.scrollOverWidth - el.fisheyeCfg.scrollWidth) / 20);
			}
		}
		return step;
	},
	
	positionContainer : function(el, toAdd) {
		var w = (el.fisheyeCfg.itemWidth + 3) * el.fisheyeCfg.items.size() + toAdd + 3;
		el.fisheyeCfg.container.get(0).style.width = w + 'px';
		if(el.fisheyeCfg.container.get(0).offsetLeft < 735 - w) el.fisheyeCfg.container.get(0).style.left =  735 - w + 'px';
	},
	
	positionItems : function(el) {
		el.fisheyeCfg.items.each( function(nr) {
				this.style.width = el.fisheyeCfg.itemWidth + 'px';
			}
		);
	},
	
	showBigWork : function(srcbig, link, ttl) {
		if($("#workbig img:visible").get(0) && $("#workbig img:hidden").get(0)) {
			if($("#workbig img:visible").attr("src") != srcbig) {
				$("#workbig img:hidden").attr("src", srcbig);
				$("#workbig img:visible").fadeOut("slow");
				$("#workbig img:hidden").fadeIn("slow");
				$("#workbig").attr("href", link);
				$("#workbig").attr("title", ttl);
				$("#workbig img").attr("alt", ttl);
			}
		}
	}
};

jQuery.fn.Fisheye = jQuery.iFisheye.build;
