	/* 	
	
	t i p X  
	
	houston@tanerinkiler.com
	X series continues :)
	20070831: ilk denemeler
	20070917: nihayet amazonvari
	20070926: withoptions
	20071010: position:relative IE z-index bug
	*/

	var _highestZIndex = 9999;
	
	function GetProperties(t)
	{
		if(!t) return;
		var Pointer=t, x=0, y=0;
		while(Pointer)
		{ 
			x += Pointer.offsetLeft; 
			y += Pointer.offsetTop;
			Pointer=Pointer.offsetParent; 
		}			
		this.x = x;
		this.y = y;
		this.w = t.offsetWidth;
		this.h = t.offsetHeight;
	}
	

	function tipX(id, options)
	{

		//----------------------------------------------------------------
		var _source, _target, _fakeDiv, _zIndexParent,
			_NowZoomingIn = false, _NowZoomingOut = false, 
			_closeTimer1, _closeTimer2;
		var _id = id
		var _matchWidths	= typeof options.matchWidths != "undefined" ? options.matchWidths : false;
		var _steps 			= options.steps ? parseInt(100/options.steps) : 20;
		var _delay			= options.delay ? options.delay : 1;
		var _fakeClassName	= options.fakeClassName ? options.fakeClassName : "fake";
		var _zoomInOn		= options.zoomInOn ? options.zoomInOn : "mouseover";
		var _zoomOutOn		= options.zoomOutOn ? options.zoomOutOn : "mouseout";
		
		//----------------------------------------------------------------
		var expand_ = 1, collapse_ = 0


		//----------------------------------------------------------------
		this.setSource = setSource;
		this.setTarget = setTarget;
		this.zoomIn = zoomIn;
		this.zoomOut = zoomOut;
		this.scaleUntilFit = scaleUntilFit;
		

		function SnapFakeDiv2Source()
		//----------------------------------------------------------------
		{
			var sP = new GetProperties(_source);
			with(_fakeDiv.style)
			{
				left = sP.x + "px";
				top = sP.y + "px";
				width = sP.w + "px";
				height = sP.h + "px";
			}		
		}

		
		function setSource(s)
		//----------------------------------------------------------------
		{
			s = document.getElementById(s);
			if (!s){alert("Source could not be set!");return;}
			_source = s;
			
			var elements2Check = s.parentNode.getElementsByTagName("DIV");
			for (var i=0;i<elements2Check.length;i++)
				if (elements2Check[i].className=="fake")
					{_fakeDiv = elements2Check[i]; break;}
			
			if(!_fakeDiv)
			{
				_fakeDiv = document.createElement('DIV');
				_fakeDiv.appendChild(document.createTextNode(""));				
				_fakeDiv.className = _fakeClassName;
				document.getElementById("tipX").appendChild(_fakeDiv); /* cunku IE body'e ancak onload ile div ekletiyor! */
			}

			SnapFakeDiv2Source();
			switch(_zoomInOn)
			{
				case "click":
					_source.onclick = zoomIn;			
					break;
				case "mouseover":
					_source.onmouseover = zoomIn;			
					break;
			}
		}

		
		function setTarget(t)
		//----------------------------------------------------------------
		{
			t = document.getElementById(t);
			if (!t){alert("Target could not be set!");return;}
			_target = t;			
			_target.style.visibility = "hidden";			

			// IE'nin "position:relative"li layerlarin z-indexlerini yorumlamada farkli davranmasi sorunsali yuzunden
			// _zIndexParent olarak, var ise position:relative olan en yakin parent bulunacak. Yok ise kendisi kalacak.
			_zIndexParent = _target;			
			var pointer, 
				cssPropertyText = document.all 
									? 'pointer.currentStyle["position"]'
									: 'document.defaultView.getComputedStyle(pointer, "").getPropertyValue("position")';
			pointer = _target;
			while(pointer)
			{
				if (eval(cssPropertyText)=="relative")
				{
					_zIndexParent = pointer;
					break;
				}
				pointer = pointer.offsetParent; 
			}
			
			switch(_zoomOutOn)
			{
				case "click":
					_target.onclick = zoomOut;
//					_target.onmouseout = function(){_closeTimer1 = window.setTimeout(_id + ".zoomOut()", 5000)};
//					window.clearTimeout(_closeTimer1); window.clearTimeout(_closeTimer2); 
					break;
				case "mouseout":
					_target.onmouseout = function(){_closeTimer1 = window.setTimeout(_id + ".zoomOut()", 500)};
					break;
			}
			
			_target.onmouseover = function(){window.clearTimeout(_closeTimer1); window.clearTimeout(_closeTimer2); };
			if (_matchWidths)
				_target.style.width = _source.offsetWidth + "px";
				
		}

		
		function zoomOut()
		//----------------------------------------------------------------
		{	
			if (!_NowZoomingOut && !_NowZoomingIn)
			{
				_target.style.visibility = "hidden";
				_fakeDiv.style.visibility = "visible";					
				var srcP = new GetProperties(_fakeDiv);
				var trgP = new GetProperties(_source);				
				_NowZoomingOut = true;	
				scaleUntilFit( [srcP.x, srcP.y, srcP.w, srcP.h, trgP.x, trgP.y, trgP.w, trgP.h], 0, _steps, collapse_ );
			}
			return false;
		}



		function zoomIn()
		//----------------------------------------------------------------
		{	
			if (!_NowZoomingOut && !_NowZoomingIn)
			{
				// prepare fake div				
				SnapFakeDiv2Source();
				_fakeDiv.style.visibility = "visible";
				_source.style.visibility = "hidden";
				var srcP = new GetProperties(_fakeDiv);
				var trgP = new GetProperties(_target);

				switch(_zoomOutOn)
				{
					case "click":
						break;
					case "mouseout":
						_closeTimer2 = setTimeout(_id + ".zoomOut()", 2000);
						break;
				}
				
				_NowZoomingIn = true;

				scaleUntilFit( [srcP.x, srcP.y, srcP.w, srcP.h, trgP.x, trgP.y, trgP.w, trgP.h], 0, _steps, expand_ );
			}
			return false;
		}

	
		function scaleUntilFit(vA, percentCompleted, step, action)
		//----------------------------------------------------------------
		{
			with(_fakeDiv.style)
			{
				if(action==expand_)
				{
					left = vA[0] - ((vA[0]-vA[4])/100*percentCompleted) + "px";
					top = vA[1] - ((vA[1]-vA[5])/100*percentCompleted) + "px";					
					width = vA[2] + ((vA[6]-vA[2])/100*percentCompleted) + "px";
					height = vA[3] + ((vA[7]-vA[3])/100*percentCompleted) + "px";
				}
				else
				{
					left = vA[0] + ((vA[4]-vA[0])/100*percentCompleted) + "px";
					top = vA[1] + ((vA[5]-vA[1])/100*percentCompleted) + "px";
					width = vA[2] - ((vA[2]-vA[6])/100*percentCompleted) + "px";
					height = vA[3] - ((vA[3]-vA[7])/100*percentCompleted) + "px";
				}
			}

			percentCompleted += step;				
			
			if (percentCompleted > 100)
			{
				if (action == expand_)
					{
						_target.style.visibility = "visible";
						_fakeDiv.style.visibility = "hidden";
						_NowZoomingIn = false;						
						_zIndexParent.style.zIndex = ++_highestZIndex;
						return;
					}
				else
					{
						_source.style.visibility = "visible";
						_fakeDiv.style.visibility = "hidden";
						_NowZoomingOut = false;
						window.clearTimeout(_closeTimer1); 
						window.clearTimeout(_closeTimer2); 
						return;
					}
			}			
			setTimeout(_id + '.scaleUntilFit([' + vA + '],' + percentCompleted + ',' + step + ',' + action + ');', _delay);
		}
	}
	
	