////////////////////////////////////////
// ALL RIGHTS RESERVED COMTRON d.o.o. //
// COPY OR REDISTRIBUTION OF THIS     //
// CONTENT IS NOT ALLOWED.            //
//									  //
// ITEM PREVIEW						  //
// Dejan Božič                        //
////////////////////////////////////////


var globalClassRef = null;

function Item_Preview(classRef)
{
	// ATTRIBUTES user defined
	this.PreviewContainerID	= null;
	this.MaxAlpha			= 100;
	this.MinAlpha			= 0;
	this.FadeInTime			= 500;
	this.FadeOutTime		= 100;
	this.xOffset			= 300;
	this.yOffset			= 0;
	this.Height				= 200;
	this.Width				= 300;
	this.URL				= "/Include/QuickBuy_ArticleAdvancedPreview.asp";
	this.BGColor			= "#FFFFFF";
	this.Ajax				= false;
	
	// function defined
	this.Item				= new Item_PreviewItem();
	this.PreviewContainer	= null;
	this.arrItems			= new Array();
	this.isMouseOver		= false;
	this.isMouseOut			= true;
	this.MouseEvent			= false;
	this.mousePos			= new Array();
	this.SelectedItem		= null;
	this.IFRAME				= null;


	// METHODS
	this.SetIFRAME = SetIFRAME
	function SetIFRAME(string)
	{
		this.IFRAME = string;
	}
	
	this.AddItem = AddItem
	function AddItem(item)
	{
		this.arrItems.push(item);
	}
	
	this.Create = Create
	function Create()
	{
		
		this.PreviewContainer = GetElement(this.PreviewContainerID);

		if (this.PreviewContainer == null)
		{
			this.PreviewContainer					= document.createElement("div");
			this.PreviewContainer.Id				= this.PreviewContainerID;
			this.PreviewContainer.style.position	= "absolute";
			this.PreviewContainer.style.left		= "-100px";
			this.PreviewContainer.style.top			= "-100px";
			this.PreviewContainer.style.opacity		= 0;
			this.PreviewContainer.style.border		= "1px solid #000000";
			this.PreviewContainer.style.zIndex		= 32000;
			document.body.appendChild(this.PreviewContainer);
			
			
		}
		this.RegisterMouse();
		globalClassRef = classRef;
	}
	
	this.RegisterMouse = RegisterMouse;
	function RegisterMouse()
	{
		Events.Register("onmousemove", classRef + ".TraceMouse(e)");
	}
	
	
	this.GetAjaxContent = GetAjaxContent;
	function GetAjaxContent(ArticleID, PictureURL)
	{
		
		var aRequest = new System.Net.Ajax.Request("POST","/include/ArticleBigImageViewPreview.asp", GetAjaxContent_CallBack, true);
            aRequest.AddParam("ArticleID", ArticleID);
            aRequest.AddParam("PictureURL", PictureURL);
            
		var aPageRequest = new System.Net.Ajax.PageRequests(aRequest);
		var aConnection = new System.Net.Ajax.Connection(aPageRequest);

            aConnection.Open();

	}
	
	
	function GetAjaxContent_CallBack(src)
	{
		
		if(src.ReadyState==4)
		{
			if(src.Status==200)
			{
				eval(globalClassRef).PreviewContainer.innerHTML = src.ResponseText;
				if(src.Complete)
					{
						eval(globalClassRef).PreviewContainer.style.display = '';
					}
				}
		}

	}
	
	this.Show = Show;
	this.arrOverTimeouts = new Array();
	function Show()
	{
		if (this.Ajax == false)
		{
			this.IFRAME	= "<iframe src=\"" + this.URL + eval(classRef).SelectedItem.IString + "\" Height=\"" + this.Height + "\" Width=\"" + this.Width + "\" marginheight=\"0px\" marginwidth=\"0\" scrolling=\"no\" frameborder=\"0\">";
			this.PreviewContainer.innerHTML = this.IFRAME;
		}
		else
		{
			this.PreviewContainer.innerHTML = "Nalagam...";
			this.GetAjaxContent(eval(classRef).SelectedItem.ID, eval(classRef).SelectedItem.IString);
		}
		this.ClearTimeouts();
		var speed = Math.round(this.FadeInTime / 100);
		var timer = 0;

		
		for (i = (this.PreviewContainer.style.opacity * 100); i <= this.MaxAlpha; i++)
		{
			var t = setTimeout(classRef + ".changeOpac(" + i + ",'" + this.PreviewContainer.Id + "', true)",(timer * speed))
			this.arrOverTimeouts.push(t);
			timer++;
		}
	}
	
	this.Hide = Hide;
	this.arrOutTimeouts = new Array();
	function Hide()
	{
		this.ClearTimeouts();
		var speed = Math.round(this.FadeOutTime / 100);
		var timer = 0;

		for (i = (this.PreviewContainer.style.opacity * 100); i >= this.MinAlpha; i--) 
		{
			var t = setTimeout(classRef + ".changeOpac(" + i + ",'" + this.PreviewContainer.Id + "', false)",(timer * speed))
			this.arrOutTimeouts.push(t);
			timer++;
		}
	}
	
	this.changeOpac = changeOpac;
	function changeOpac(opacity, id, direction) 
	{
		if (GetElement(id) != null)
		{
			if (direction)
			{
				if (opacity < this.MaxAlpha)
					this.PreviewContainer.style.display = "";
			}
			else
			{
				if (opacity <= this.MinAlpha)
					this.PreviewContainer.style.display = "none";
			}
		
			var object = GetElement(id).style;
				object.opacity = (opacity / 100);
				object.MozOpacity = (opacity / 100);
				object.KhtmlOpacity = (opacity / 100);
				object.filter = "alpha(opacity=" + opacity + ")";
		}
	} 
	
	this.ClearTimeouts = ClearTimeouts
	function ClearTimeouts()
	{
		for (i = 0; i <= this.arrOverTimeouts.length; i++)
			clearTimeout(this.arrOverTimeouts[i]);
			
		for (i = 0; i <= this.arrOutTimeouts.length; i++)
			clearTimeout(this.arrOutTimeouts[i]);
			
		this.arrOutTimeouts = new Array();
		this.arrOverTimeouts = new Array();
	}
	
	this.TraceMouse = TraceMouse
	function TraceMouse(e)
	{

		try
		{
			if (typeof e != "undefined") { eval(classRef).mousePos[0] = e.pageX; eval(classRef).mousePos[1] = e.pageY; } 
			if (typeof window.event != "undefined") { eval(classRef).mousePos[0] = event.clientX; eval(classRef).mousePos[1] = event.clientY; }

			if (e.pageX == undefined) // IE
			{
				eval(classRef).mousePos[0] += truebody().scrollLeft;
				eval(classRef).mousePos[1] += truebody().scrollTop;
			}
			/*else if (event == "undefined") // MOZ, OP
			;*/

			// check if any item has mouse over and store it
			if (!eval(classRef + ".isMouseOver"))
			{
				var tmpItems = eval(classRef + ".arrItems");
				var tmpPreview = eval(classRef);
				for (i in tmpItems)
				{
					if (isMouseBound(tmpItems[i].ID, tmpPreview.mousePos))
					{
						tmpPreview.isMouseOver	= true;
						tmpPreview.SelectedItem	= tmpItems[i];
						tmpPreview.MouseOver(classRef);
						break;
					}
				}
			}
			
			// Check if the current mouse over item is still active
			if (eval(classRef + ".isMouseOver"))
			{
				if (!isMouseBound(eval(classRef).SelectedItem.ID, eval(classRef).mousePos))
				{
					eval(classRef).isMouseOver = false;
					eval(classRef).MouseOut();
				}
			}
		} catch(ex) {};
	}
	
	this.MouseOver = MouseOver
	function MouseOver(classRef)
	{
		this.SnapToItem();
		this.Show();
		
		// Check if item is out of user window and adjust
		var item = getElementPosition(this.PreviewContainer);
		var elem = this.PreviewContainer;
		var winwh = getClientWindowDimensions();
		var yx = [parseFloat(this.PreviewContainer.style.left.replace("px", "")), parseFloat(this.PreviewContainer.style.top.replace("px", ""))];
		var wh = [this.Width, this.Height];

		var xyOffset = [parseFloat(this.PreviewContainer.xOffset), parseFloat(this.PreviewContainer.yOffset)];
		
		if (parseFloat((yx[0] + wh[0])) - truebody().scrollLeft > parseFloat(winwh[0]) - 20)
			SetElementPosition(this.PreviewContainer, "left,top", item[0] - ((yx[0] + wh[0]) - truebody().scrollLeft - winwh[0]) - 20 + "," + item[1] );

/*		if (parseFloat((yx[1] + wh[1])) - truebody().scrollTop > parseFloat(winwh[1]) - 20)
		{
			SetElementPosition(this.PreviewContainer, "left,top", + item[0] + "," + (parseFloat(item[1]) - parseFloat(parseFloat((yx[1] + wh[1])) - parseFloat(truebody().scrollTop) - parseFloat(winwh[1]) - 20)));
		}
*/
	}
	
	this.MouseOut = MouseOut
	function MouseOut()
	{
		this.Hide();
	}
	
	this.SnapToItem = SnapToItem
	function SnapToItem()
	{
		var pos = getElementPosition(GetElement(eval(classRef).SelectedItem.ID));
		this.PreviewContainer.style.left	= pos[0] + this.xOffset;
		this.PreviewContainer.style.top		= pos[1] + this.yOffset;
	}
	
	this.UnSnapToItem = UnSnapToItem
	function UnSnapToItem()
	{
		this.PreviewContainer.style.left	= -300;
		this.PreviewContainer.style.top		= -300;
	}
}

function Item_PreviewItem()
{
	// ATTRIBUTES
	this.ID = null;
	this.IString = null;
	
	// METHODS
	this.Create = Create;
	function Create(_ID, _IString)
	{
		var newItem			= new Item_PreviewItem();
			newItem.ID		= _ID;
			newItem.IString	= _IString;
			
		return newItem;
	}
}