// reqired js | functions:  common.js | getDomElement(), turnOffSelectBoxesIE6()
// reqired js | functions:  microajax.js | microAjax()


function showPreview(e, previewURL)
	{
	var that = this;

	this.handleElement = getDomElement(e);
	this.previewElement = getDomElement("previewDIV");

	this.previewURL = previewURL;
	this.previewTimeOut = "";
	this.previewDelay = 500;

	//this.handleElement.style.cursor = "wait";

	loadPreview = function()
		{
        this.previewElement.innerHTML = '<div></div>';
		//microAjax(this.previewElement, this.previewURL);
        /* changed to prototype ajax, because of error handling*/    
        new Ajax.Request(this.previewURL,{
            method: 'get',
            onSuccess: function(response){
                this.previewElement.update(response.responseText);
            },
            onFailure: function(response){
                ErrorDialog.show(AJAX_ERROR_MSG + response.status);
            }
        });
		this.previewElement.style.display = "block";
		}

	hidePreview = function()
		{
		this.handleElement.onmousemove = null;
		this.handleElement.onmouseout = null;
		//this.handleElement.style.cursor = "default";
		if(this.previewTimeOut || this.previewTimeOut != "")
			{ window.clearTimeout(this.previewTimeOut); this.previewTimeOut = ""; }
		this.previewElement.style.visibility = "hidden";
		this.previewElement.style.display = "none";
		turnOffSelectBoxesIE6('show');
		}

	doMouseFollow = function(x, y)
		{
		
		var yOffset = 15;
		var xOffset = 15;
		
		previewDIVwidth = parseInt(this.previewElement.offsetWidth, 10);
		previewDIVHeight  = parseInt(this.previewElement.offsetHeight, 10);
		
		
		WindowInnerWidth = 0;
		WindowInnerHeight = 0;
		
		if (window.innerWidth) 															{ WindowInnerWidth = window.innerWidth; }
		else if (document.documentElement && document.documentElement.clientWidth) 		{ WindowInnerWidth = document.documentElement.clientWidth; }
		else if (document.body) 														{ WindowInnerWidth = document.body.clientWidth; }
	
		if (window.innerHeight) 														{ WindowInnerHeight = window.innerHeight; }
		else if (document.documentElement && document.documentElement.clientHeight) 	{ WindowInnerHeight = document.documentElement.clientHeight; }
		else if (document.body) 														{ WindowInnerHeight = document.body.clientHeight; }


		scrollXpos = 0;
		scrollYpos = 0;

		if (window.pageXOffset) 														{ scrollXpos = window.pageXOffset; }
		else if (document.documentElement && document.documentElement.scrollLeft) 		{ scrollXpos = document.documentElement.scrollLeft; }
		else if (document.body) 														{ scrollXpos = document.body.scrollLeft; }

		if (window.pageYOffset) 														{ scrollYpos = window.pageYOffset; }
		else if (document.documentElement && document.documentElement.scrollTop) 		{ scrollYpos = document.documentElement.scrollTop; }
		else if (document.body) 														{ scrollYpos = document.body.scrollTop; }


		xpos = scrollXpos + x - previewDIVwidth - xOffset;
		ypos = scrollYpos + y - previewDIVHeight/2;

		if( x < ( previewDIVwidth + xOffset) )
			{ xpos = scrollXpos + x + xOffset; }

		if( (y + previewDIVHeight/2) > ( WindowInnerHeight - 20) )
			{ ypos = scrollYpos + WindowInnerHeight - previewDIVHeight - 20; }  // -20

		if( ((y - previewDIVHeight/2) < 0) ||  (previewDIVHeight > WindowInnerHeight) )
			{ ypos = scrollYpos; }
		

		/*
		*/

		this.previewElement.style.left = xpos  + "px";
		this.previewElement.style.top = ypos + "px";
		}


	mouseFollow = function(MSE)
		{
		var that2 = that;
		if(window.event) { MSE = window.event; }
		MsX = MSE.clientX;
		MsY = MSE.clientY;
		that2.doMouseFollow(MsX, MsY);
		}

	doShowPreview = function()
		{
		//this.handleElement.style.cursor = "pointer";
		turnOffSelectBoxesIE6();
		this.previewElement.style.visibility = "visible";
		}

	this.loadPreview();
	this.handleElement.onmousemove = that.mouseFollow;
	that2 = that;
	this.previewTimeOut = window.setTimeout("that2.doShowPreview()", this.previewDelay);

	this.handleElement.onmouseout = function () {that.hidePreview()};
	this.handleElement.onclick = function () {that.hidePreview()};
	}

