/**
 * Interface Elements for jQuery
 * Fisheye menu
 * 
 * http://interface.eyecon.ro
 * 
 * Copyright (c) 2006 Stefan Petre
 * Dual licensed under the MIT (MIT-LICENSE.txt) 
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 */

/**
 * Build a Fisheye menu from a list of links
 *
 * @name Fisheye
 * @description Build a Fisheye menu from a list of links
 * @param Hash hash A hash of parameters
 * @option String items items selection
 * @option String container container element
 * @option Integer itemWidth the minimum width for each item
 * @option Integer maxWidth the maximum width for each item
 * @option String itemsText selection of element that contains the text for each item
 * @option Integer proximity the distance from element that make item to interact
 * @option String valign vertical alignment
 * @option String halign horizontal alignment
 *
 * @type jQuery
 * @cat Plugins/Interface
 * @author Stefan Petre
 *
 * Modified by Sten Anderson to accommodate a vertical fisheye and positioning relative to other components (via
 * the Dimensions plugin).
 *
 * Note that halign and valign don't do anything.
 */
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.getPositionLite(this),
					itemWidth: options.itemWidth,
					itemsText: options.itemsText,
					proximity: options.proximity,
					//valign: options.valign,
					//halign: options.halign,
					maxWidth : options.maxWidth,
					left : options.left,
					top : options.top,
					parentName : options.parent,
					parentOffset : jQuery("#" + options.parent).offset(),
					parentWidth : jQuery("#" + options.parent).width()
					//parentOffset : {left:100, top:100}
				};
				jQuery.iFisheye.positionContainer(el, 0);
				jQuery(window).bind(
					'resize',
					function()
					{
						//el.fisheyeCfg.pos = jQuery.iUtil.getPosition(el);
						el.fisheyeCfg.parentOffset = jQuery("#" + el.fisheyeCfg.parentName).offset();
						el.fisheyeCfg.parentWidth = jQuery("#" + el.fisheyeCfg.parentName).width();
						jQuery.iFisheye.positionContainer(el, 0);
						jQuery.iFisheye.positionItems(el);
					}
				);

      if (window.XMLHttpRequest) {

       
	   /* The information that I was working on for the Menu goes here,.*/
				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 = 'block';
						}
					);
					}
					else {
		  
      }
				jQuery(document).bind(
					'mousemove',
					function(e)
					{
						var pointer = jQuery.iUtil.getPointer(e);
						var toAdd = 0;
						/*
						if (el.fisheyeCfg.halign && el.fisheyeCfg.halign == 'center')
							var posY = pointer.y - el.fisheyeCfg.pos.y - (el.offsetWidth - el.fisheyeCfg.itemWidth * el.fisheyeCfg.items.size())/2 - el.fisheyeCfg.itemWidth/2;
						else if (el.fisheyeCfg.halign && el.fisheyeCfg.halign == 'right')
							var posY = pointer.y - el.fisheyeCfg.pos.y - el.offsetHeight;
						else 
							var posx = pointer.x - el.fisheyeCfg.pos.x;
							*/
						
						// This is the y coordinate of the mouse pointer relative to the top of the fisheye.
						// Due to some oddities with CSS that I don't understand, the "top" of the fisheye, that is, 
						// the y offset by which to subtract from the parent, is sometimes quite large to get it
						// to position correctly on the page.
						var posY = pointer.y - (el.fisheyeCfg.parentOffset.top + el.fisheyeCfg.top);
						//console.log("pointer: " + pointer.x + "," + pointer.y + " PosY: " + posY +
						//			" parentoffset.top: " + el.fisheyeCfg.parentOffset.top + " top: " + el.fisheyeCfg.top);
						
						// this is the x coordinate of where the mouse pointer is relative to center x 
						// coordinate of the fisheye (where max magnification happens). That is, posX is zero
						// when the mouse pointer is in the middle of the fisheye.
						var offsetLeft = parseInt(el.fisheyeCfg.container.get(0).style.left);
						var posX = pointer.x - offsetLeft - el.fisheyeCfg.itemWidth / 2;
						//console.log("posY " + posY + ", posX " + posX + " offsetWidth " + el.offsetWidth +
						//			" pos.x " + el.fisheyeCfg.pos.x + " pageX " + el.fisheyeCfg.left);
						posX = Math.pow(posX,2);
						el.fisheyeCfg.items.each(
							function(nr)
							{
								distance = Math.sqrt(
									Math.pow(posY - nr*el.fisheyeCfg.itemWidth, 2)
									+ posX
								);
								distance -= el.fisheyeCfg.itemWidth/2;
								
								distance = distance < 0 ? 0 : distance;
								distance = distance > el.fisheyeCfg.proximity ? el.fisheyeCfg.proximity : distance;
								distance = el.fisheyeCfg.proximity - distance;
								
								extraHeight = el.fisheyeCfg.maxWidth * distance/el.fisheyeCfg.proximity;
								
								this.style.width = el.fisheyeCfg.itemWidth + extraHeight + 'px';
								this.style.top = el.fisheyeCfg.itemWidth * nr + toAdd + 'px';
								toAdd += extraHeight;
							}
						);
						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.fisheyeCfg.parentOffset.left + el.fisheyeCfg.parentWidth + el.fisheyeCfg.left) + 'px';
		el.fisheyeCfg.container.get(0).style.top = (el.fisheyeCfg.parentOffset.top + el.fisheyeCfg.top) + 'px';		
		el.fisheyeCfg.container.get(0).style.height = 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.top = el.fisheyeCfg.itemWidth * nr + 'px';
				//console.log("position items : " + this + " id " + this.class);
			}
		);
	},
	
	parseInt : function(str)
	{
		var intVal = 0;
		var tempStr = "";
		for (i = 0; i < str.length; i++) {
		  if (!isNaN(str.charAt(i))) {
			tempStr += str.charAt(i);
		  }
		}
		intVal = tempStr;
		return intVal;
	}
};

jQuery.fn.Fisheye = jQuery.iFisheye.build;