/**
 * Contains functions for showing, moving, fade'ing... a tip-box.
 */

var visibleName			= 'tip2';
var innerName			= 'tip2Text';
var toneDown 			= null;
var toneUp 				= null;
var toneUp2 			= null;
var standardTone 		=  80;
var currentTone 		=   0;
var standardTipWidth 	= 250;
var currentTipWidth		= 250;

/**
 * Brings up the tooltip.
 * 
 * @param startTimer	the delay before showing the tip. -1 brings it up direct, otherwise fades it in after this amount of seconds.
 * @param title			the title, displayed in the top of the tooltip. May be skipped by passing an empty string.
 * @param text			the text to display in the tooltip box. May be formatted by giving some extra variables after the text, separated by the char '~'. These are described at the function 'getTextFormat'.
 * @param image			the image to show, either as an title-icon (if title is passed in) or as a ordinary before the text. A second and third argument can be passed in by separating the image with the char ';'. The three optional booleans tell this: * Picture shall be resized to fit the width of the tooltip. * Picture shall be used as an icon to the left of the title. Overrides the first argument if true and also if a title exist. * Picture shall be to the left of the text; fourth argument contains the max-size in width and height. Overrides the first and second argument. Image may be fully skipped by passing an empty string.
 * @param width			the desired width of the tooltip. -1 indicates standard width.
 * @param e				the event (automatically from browser by passing the object 'event'
 */
function showTip(startTimer, title, text, image, width, e)
{
	var tip = document.getElementById(visibleName);
	var tipInner = document.getElementById(innerName);
	
	var showImage		= image != '';
	var imgResize		= false;
	var imgIcon			= false;
	var imgText			= false;
	var imgWidthMax		= -1;
	var imgHeightMax	= -1;
	if ( showImage && image.indexOf(';') >= 0 )
	{
		var imageAndResize = image.split(';');
		image = imageAndResize[0];
		imgResize = imageAndResize[1] == 'true';
		if ( imageAndResize.length > 2 )
			imgIcon = imageAndResize[2] == 'true';
		if ( imageAndResize.length > 3 )
			imgText = imageAndResize[3] == 'true';
		if ( imgText && imageAndResize.length > 4 )
		{
			imgWidthMax = parseInt(imageAndResize[4].split(',')[0]);
			imgHeightMax = parseInt(imageAndResize[4].split(',')[1]);
		}
	}

	
	if ( toneDown != null )
	{
		clearInterval(toneDown);
		toneDown = null;
		tip.style.opacity = '.'+standardTone;
		tip.style.filter = 'alpha(opacity='+standardTone+')';
	}
	
	if ( width == -1 )
		width = standardTipWidth;
	currentTipWidth = width;
	
	tip.style.width = width+'px';
		
	var contents = '';
	if ( title != '' ) // Print out title
	{
		contents += '<span class="bold" style="font-size: 115%; width: 100%; display: block; text-align: center;">';
		if ( imgIcon )
			contents += '<img src="'+image+'" style="width: 15px; height: 15px; margin-right: 6px; position: relative; top: 3px;" alt="" title="" />';
		contents += title;
		contents += '</span>';
		if ( ((showImage && (!imgIcon || (imgIcon && title == ''))) || text != '') )
			contents += '<table cellpadding="0" cellspacing="0" style="width: 100%;"><tr><td style="height: 3px;"><img src="includes/pics/misc/transPixel.png" style="width: 1px; height: 1px;" alt="" title="" /></td></tr></table>';
	}

	if ( showImage && (!imgIcon || (imgIcon && title == '')) && (!imgText) ) // Print out the requested image
	{
		contents += '<img src="'+image+'" '+(imgResize ? 'style="width: '+(width-12)+'px;"' : '')+' alt="" title="" />';
		if ( text != '' )
			contents += '<table cellpadding="0" cellspacing="0" style="width: 100%;"><tr><td style="height: 3px;"><img src="includes/pics/misc/transPixel.png" style="width: 1px; height: 1px;" alt="" title="" /></td></tr></table>';
	}
	
	if ( text != '' ) // Print out the main text
	{
		var textAttributes = new Array();
		if ( text.indexOf('~') >= 0 )
		{
			var textParts = text.split('~');
			text = textParts[0];
			textAttributes = getTextFormat(textParts);
		}
		
		if ( imgText )
		{
			var testTip = document.getElementById('testTip');
			testTip.innerHTML = '<img id="testTipImg" src="'+image+'" />';
			testTip.style.display = '';
			var imgOrigWidth = document.getElementById('testTipImg').offsetWidth;
			var imgOrigHeight = document.getElementById('testTipImg').offsetHeight;
			testTip.innerHTML = '';
			testTip.style.display = 'none';

			if ( imgWidthMax == -1 && imgHeightMax == -1 )
				imgWidthMax = parseInt(currentTipWidth / 2);
			if ( imgWidthMax == -1 || imgWidthMax > currentTipWidth )
			{
				if ( ((parseInt(imgOrigWidth*(1/(imgOrigHeight/imgHeightMax))))+4) > (currentTipWidth-12) )
				{
					imgWidthMax = parseInt(currentTipWidth / 2);
					imgHeightMax = 10000;
				}
				else
				{
					imgWidthMax = 10000;
				}
			}
			if ( imgHeightMax == -1 || imgHeightMax > 10000 )
				imgHeightMax = 10000;
				
			var percentWidth = imgOrigWidth / imgWidthMax;
			var percentHeight = imgOrigHeight / imgHeightMax;
			
			contents += '<table cellpadding="0" cellspacing="0" style="width: 100%;"><tr><td style="vertical-align: top; width: '+(percentWidth > percentHeight ? (imgWidthMax+4) : (parseInt(imgOrigWidth*(1/percentHeight)))+4)+'px;">';
			if ( percentWidth > percentHeight )
				contents += '<img src="'+image+'" alt="" title="" style="width: '+imgWidthMax+'px;" />';
			else
				contents += '<img src="'+image+'" alt="" title="" style="height: '+imgHeightMax+'px;" />';
			contents += '</td><td style="vertical-align: top;">';
		}
		contents += '<span style="width: 100%; display: block;';
		for ( var i = 0 ; i < textAttributes.length ; i++ )
			contents += ' '+textAttributes[i];
		
		while (text.indexOf('[bold]') >= 0)
		{
			text = text.replace('[bold]', '<span class="bold">');
			text = text.replace('[/bold]', '</span>');
		}
		while (text.indexOf('[ap]') >= 0)
		{
			text = text.replace('[ap]', '&#0039;');
		}
		while (text.indexOf('[dap]') >= 0)
		{
			text = text.replace('[dap]', '&quot;');
		}
		
		contents += '">';
		contents += text;
		contents += '</span>';
		if ( imgText )
		{
			contents += '</td></tr></table>';
		}
	}
	
	tipInner.innerHTML = contents;
	moveTip(e);
	
	if ( startTimer == -1 || tip.style.display == '' )
		tip.style.display = '';
	else
	{
		if ( toneDown != null )
		{
			clearInterval(toneDown);
			toneDown = null;
			toneUp2 = setInterval('toneUpTip();', 10);
		}
		else
		{
			toneUp = setInterval('startToneUpTip();', startTimer*1000);
		}
	}
}

/**
 * Returns css-styles instead of the code-words sent in:
 * Aligning: 'left', 'center', 'right'
 * Bold: 'x00', 'bold', 'bolder', 'lighter'
 * Decoration: 'underline', 'overline', 'line-through', 'blink'
 * Style: 'italic', 'oblique'
 * Stretch: 'wider', 'narrower'
 * Variant: 'small-caps'
 * Border: 'frame', 'frame-dotted', 'frame-dashed'
 */
function getTextFormat(textParts)
{
	var textAttributes = new Array();
	
	for ( var i = 1 ; i < textParts.length ; i++ )
	{
		switch( textParts[i] )
		{
			case 'center':
			case 'left':
			case 'right':		textAttributes.push('text-align: '+textParts[i]+';'); break;
			case 'lighter':
			case 'bolder':
			case 'bold':
			case '100':
			case '200':
			case '300':
			case '400':
			case '500':
			case '600':
			case '700':
			case '800':
			case '900':			textAttributes.push('font-weight: '+textParts[i]+';'); break;
			case 'overline':
			case 'line-through':
			case 'blink':
			case 'underline':	textAttributes.push('text-decoration: '+textParts[i]+';'); break;
			case 'italic':
			case 'oblique':		textAttributes.push('font-style: '+textParts[i]+';'); break;
			case 'wider':
			case 'narrower':	textAttributes.push('font-stretch: '+textParts[i]+';'); break;
			case 'small-caps':	textAttributes.push('font-variant: small-caps;'); break;
			case 'frame':		textAttributes.push('border: 1px solid black;');
			case 'frame-dotted':textAttributes.push('border: 1px dotted black;');
			case 'frame-dashed':textAttributes.push('border: 1px dashed black;');
			default:			break;
		}
	}
	
	return textAttributes;
}

/**
 * Starts toning the tooltip in to full strength.
 */
function startToneUpTip()
{
	var tip = document.getElementById(visibleName);

	clearInterval(toneUp);
	toneUp = null;

	if ( tip.style.display = '' )
		return;
	
	currentTone = 0;
	tip.style.opacity = '.'+currentTone;
	tip.style.filter = 'alpha(opacity='+currentTone+')';
	tip.style.display = '';
	if ( toneUp2 != null )
	{
		clearInterval(toneUp2);
		toneUp2 = null;
	}
	toneUp2 = setInterval('toneUpTip();', 10);
}

/**
 * Tone the tooltip in another step.
 */
function toneUpTip()
{
	var tip = document.getElementById(visibleName);

	currentTone += 5;
	
	if ( currentTone >= standardTone )
	{
		tip.style.opacity = '.'+standardTone;
		tip.style.filter = 'alpha(opacity='+standardTone+')';
		clearInterval(toneUp2);
		toneUp2 = null;
	}
	else
	{
		tip.style.opacity = '.'+currentTone;
		tip.style.filter = 'alpha(opacity='+currentTone+')';
	}
}

/**
 * Start toning the tooltip out to make it hidden.
 */
function hideTip()
{
	var tip = document.getElementById(visibleName);

	if ( toneUp != null )
	{
		clearInterval(toneUp);
		toneUp = null;
		
		if ( tip.style.display != 'none' )
			tip.style.display = 'none';
	}
	else if ( toneUp2 != null )
	{
		clearInterval(toneUp2);
		toneUp2 = null;
		toneDown = setInterval('toneDownTip();', 10);
	}
	else
	{
		currentTone = standardTone;
		toneDown = setInterval('toneDownTip();', 10);
	}
}

/**
 * Toning the tooltip out another step.
 */
function toneDownTip()
{
	var tip = document.getElementById(visibleName);

	currentTone -= 5;
	
	if ( currentTone <= 0 )
	{
		tip.style.display = 'none';
		tip.style.opacity = '.'+standardTone;
		tip.style.filter = 'alpha(opacity='+standardTone+')';
		clearInterval(toneDown);
		toneDown = null;
	}
	else
	{
		tip.style.opacity = '.'+currentTone;
		tip.style.filter = 'alpha(opacity='+currentTone+')';
	}
}

/**
 * Position the tooltip in a good position according to the cursor-point as well as visible browser area.
 */
function moveTip(e)
{
	var tip = document.getElementById(visibleName);

	getMouseXY(e);
	
	var tipWidth = currentTipWidth;
	var xPos = (mousex-parseInt(tipWidth/2));
	var yPos = (mousey+10)
	
	if ( xPos < 2 )
		xPos = 2;
	
	getDocumentSize();
	// Margins from edge (right and bottom)
	frameWidth -= 26;
	frameHeight -= 18;
	if ( (parseInt(tipWidth)+xPos) > frameWidth )
		xPos = frameWidth - parseInt(tipWidth);
		
	if ( (tip.offsetHeight+yPos) > frameHeight )
	{
		if ( mousey > (tip.offsetHeight+5) )
		{
			yPos = mousey - (tip.offsetHeight+5);
		}
		else
		{
			tip.style.display = '';
		}
	}
	
	tip.style.left 	= xPos+'px';
	tip.style.top 	= yPos+'px';
}

var loadImageBig;
var loadCheck = null;
var popupType = '';

function showPopupImage(img)
{
	popupType = "image";
	
	var backObj = document.getElementById('backToned');
	backObj.style.display = 'block';	
	document.getElementById('body').style.height = '100%';
	document.getElementById('body').style.overflow = 'hidden';

	var loadObj = document.getElementById('ontopLoadingImg');
	loadObj.style.display = 'block';

	loadImageBig = new Image();
	loadImageBig.src = img;
	
	var infoObj = document.getElementById('ontopInfo');
	infoObj.style.padding = '0px';
	infoObj.style.border = '0px solid black';
	infoObj.style.backgroundColor = 'transparent';
	infoObj.innerHTML = '<img src="'+img+'" title="" alt="" style="border: 1px solid black;" />';
	
	if ( loadImageBig.complete && infoObj != null )
	{
		infoObj.style.display = 'block';
		setTimeout("autoadjust();", 10);

		var loadObj = document.getElementById('ontopLoadingImg');
		loadObj.style.display = 'none';
	}
	else
	{
		loadCheck = setInterval("checkLoaded();", 50);	
	}
}

function showPopupInfo(infoText)
{
	popupType = "text";
	
	var backObj = document.getElementById('backToned');
	backObj.style.display = 'block';
	document.getElementById('body').style.height = '100%';
	document.getElementById('body').style.overflow = 'hidden';
	
	var infoObj = document.getElementById('ontopInfo');
	infoObj.style.padding = '6px';
	infoObj.style.border = '1px solid black';
	infoObj.style.backgroundColor = 'white';
	infoObj.innerHTML = infoText;
	infoObj.style.display = 'block';
	setTimeout("autoadjust();", 10);
}

function showPopupMixed(img, infoText)
{
	popupType = "mixed";
	
	var backObj = document.getElementById('backToned');
	backObj.style.display = 'block';	
	document.getElementById('body').style.height = '100%';
	document.getElementById('body').style.overflow = 'hidden';

	var loadObj = document.getElementById('ontopLoadingImg');
	loadObj.style.display = 'block';

	loadImageBig = new Image();
	loadImageBig.src = img;

	var infoObj = document.getElementById('ontopInfo');
	infoObj.style.padding = '6px';
	infoObj.style.border = '1px solid black';
	infoObj.style.backgroundColor = 'white';
	infoObj.innerHTML = '<table cellpadding="0" cellspacing="0" style="border: 0px;"><tr><td style="text-align: left; vertical-align: top;"><img src="'+img+'" title="" alt="" /></td><td style="text-align: left; vertical-align: top; padding-left: 10px; padding-right: 10px;">'+infoText+'</td></tr></table>';
	
	if ( loadImageBig.complete )
	{
		infoObj.style.display = 'block';
		setTimeout("autoadjust();", 10);

		var loadObj = document.getElementById('ontopLoadingImg');
		loadObj.style.display = 'none';
	}
	else
	{
		loadCheck = setInterval("checkLoaded();", 50);	
	}
}

function checkLoaded()
{
	infoObj = document.getElementById('ontopInfo');
	if ( loadImageBig.complete )
	{
		clearInterval(loadCheck);
		infoObj.style.display = 'block';
		
		var loadObj = document.getElementById('ontopLoadingImg');
		loadObj.style.display = 'none';

		setTimeout("autoadjust();", 10);
	}
}

function autoadjust()
{
	// Move all popups 10px to the right in IE
	var extraRight = (navigator.userAgent.indexOf("MSIE 6") >= 0 ? 15 : 0);
	getDocumentSize();
	var obj2Center = document.getElementById('ontopInfo');
	obj2Center.style.left = parseInt((frameWidth/2)-(obj2Center.offsetWidth/2)+extraRight)+'px';
	obj2Center.style.top = parseInt((frameHeight/2)-(obj2Center.offsetHeight/2))+'px';
}

function adjustPosition()
{
	var loadObj = document.getElementById('ontopLoadingImg');
	loadObj.style.display = 'none';
	
	var imgObj = document.getElementById('ontopImage');
	imgObj.src = loadImageBig.src;
	imgObj.style.display = 'block';
	
	setTimeout("autoadjust();", 10);
}

function hidePopup()
{
	if ( popupType == "image" )
		hidePopupImage()
	else if ( popupType == "text" )
		hidePopupInfo();
	else if ( popupType == "mixed" )
		hidePopupMixed();
}

function hidePopupImage()
{
	var backObj = document.getElementById('backToned');
	backObj.style.display = 'none';
	document.getElementById('body').style.overflow = 'auto';
	document.getElementById('body').style.height = '';

	var infoObj = document.getElementById('ontopInfo');
	infoObj.style.display = 'none';
	infoObj.style.left = '-5000px';
	infoObj.style.top = '-5000px';
	infoObj.innerHTML = '&nbsp;';

	var loadObj = document.getElementById('ontopLoadingImg');
	loadObj.style.display = 'none';

	clearInterval(loadCheck);
}

function hidePopupInfo()
{
	var backObj = document.getElementById('backToned');
	backObj.style.display = 'none';
	document.getElementById('body').style.overflow = 'auto';
	document.getElementById('body').style.height = '';

	var infoObj = document.getElementById('ontopInfo');
	infoObj.style.display = 'none';
	infoObj.style.left = '-5000px';
	infoObj.style.top = '-5000px';
	infoObj.innerHTML = '&nbsp;';
}

function hidePopupMixed()
{
	var backObj = document.getElementById('backToned');
	backObj.style.display = 'none';
	document.getElementById('body').style.overflow = 'auto';
	document.getElementById('body').style.height = '';

	var infoObj = document.getElementById('ontopInfo');
	infoObj.style.display = 'none';
	infoObj.style.left = '-5000px';
	infoObj.style.top = '-5000px';
	infoObj.innerHTML = '&nbsp;';

	var loadObj = document.getElementById('ontopLoadingImg');
	loadObj.style.display = 'none';

	clearInterval(loadCheck);
}

var closeOnClick = true;
function setCloseOnClick(value)
{
	closeOnClick = value;
	
	var backObj = document.getElementById('backToned');
	backObj.title = (value ? document.getElementById('clickText').value : '');

	var loadObj = document.getElementById('ontopLoadingImg');
	loadObj.title = (value ? document.getElementById('clickText').value : '');

	var infoObj = document.getElementById('ontopInfo');
	infoObj.title = (value ? document.getElementById('clickText').value : '');

	var imgObj = document.getElementById('ontopImage');
	imgObj.title = (value ? document.getElementById('clickText').value : '');
}

function showPageInfo()
{
	document.getElementById('cmd2').src = 'includes/misc/getPageHelp.php';
}