/*
tip_balloon.js  v. 1.4

The latest version is available at
http://www.walterzorn.com
or http://www.devira.com
or http://www.walterzorn.de

Initial author: Walter Zorn
Last modified: 7.3.2008

Extension for the tooltip library wz_tooltip.js.
Implements balloon tooltips.
*/

function Balloon_tooltip(tooltip_radius, tooltip_width, tooltip_color,tooltip_border)
{
	$('#div_tooltip').css('width',tooltip_width-(tooltip_radius/2));

	//background titlu
	$('#tooltip_title, #tooltip_title_text')
		.css('background-color',tooltip_border);
	
	$('#tooltip_text, #tooltip_middle').css('background-color',tooltip_color);

	$("#div_tooltip").css('background-color',tooltip_border);
	
	//var wrapper = 
	$.jcorners("#tooltip_middle",{radius:(tooltip_radius/2)});
	$('#div_tooltip').css('width',tooltip_width);
	$.jcorners("#div_tooltip",{radius:tooltip_radius});
	//var offset = $(wrapper).offset();
	//$('#tooltip_title_bg').addClass('tooltip_bg');
	//$('#tooltip_title_bg').css('width',$(wrapper).css('width'));
	//$('#tooltip_title_bg').css('top',offset.top);
	//$('#tooltip_title_bg').css('left',offset.left);
	//$('#tooltip_title_bg').css('width',tooltip_width);
	//$('#tooltip_title_bg').css('height','30');
	
}

// Here we define new global configuration variable(s) (as members of the
// predefined "config." class).
// From each of these config variables, wz_tooltip.js will automatically derive
// a command which can be passed to Tip() or TagToTip() in order to customize
// tooltips individually. These command names are just the config variable
// name(s) translated to uppercase,
// e.g. from config. Balloon a command BALLOON will automatically be
// created.

//===================  GLOBAL TOOPTIP CONFIGURATION  =========================//
config. Balloon				= false	// true or false - set to true if you want this to be the default behaviour
config. BalloonImgPath		= tpl_url+"images/tooltip/" // Path to images (border, corners, stem), in quotes. Path must be relative to your HTML file.
//shoty
config. BalloonCornerRadius = 10;
config. BalloonTooltipWidth = 300;
config. BalloonTitleBg = "#FFF2CC";
config. BalloonContentBg = "#57AD00";
/*var tooltip_radius = 10;
var tooltip_color  = "#FFF2CC";
var tooltip_border = "#57AD00";
var tooltip_width  = 300;
*/
//\shoty
// Sizes of balloon images
config. BalloonEdgeSize		= 6		// Integer - sidelength of quadratic corner images
config. BalloonStemWidth	= 15	// Integer
config. BalloonStemHeight	= 12	// Integer
config. BalloonStemOffset	= -7	// Integer - horizontal offset of left stem edge from mouse (recommended: -stemwidth/2 to center the stem above the mouse)
//=======  END OF TOOLTIP CONFIG, DO NOT CHANGE ANYTHING BELOW  ==============//


// Create a new tt_Extension object (make sure that the name of that object,
// here balloon, is unique amongst the extensions available for wz_tooltips.js):
var balloon = new tt_Extension();

// Implement extension eventhandlers on which our extension should react

balloon.OnLoadConfig = function()
{
	if(tt_aV[BALLOON])
	{
		// Turn off native style properties which are not appropriate
		balloon.padding = Math.max(tt_aV[PADDING] - tt_aV[BALLOONEDGESIZE], 0);
		balloon.width = tt_aV[WIDTH];
		//if(tt_bBoxOld)
		//	balloon.width += (balloon.padding << 1);
		tt_aV[BORDERWIDTH] = 0;
		tt_aV[WIDTH] = -350;
		tt_aV[PADDING] = 0;
		tt_aV[BGCOLOR] = "";
		tt_aV[BGIMG] = "";
		tt_aV[SHADOW] = false;
		// Append slash to img path if missing
		if(tt_aV[BALLOONIMGPATH].charAt(tt_aV[BALLOONIMGPATH].length - 1) != '/')
			tt_aV[BALLOONIMGPATH] += "/";
		return true;
	}
	return false;
};
balloon.OnCreateContentString = function()
{
	if(!tt_aV[BALLOON])
		return false;
		
	var aImg, sImgZ, sCssCrn, sCssImg;

	// Cache balloon images in advance:
	// Either use the pre-cached default images...
	if(tt_aV[BALLOONIMGPATH] == config.BalloonImgPath)
		aImg = balloon.aDefImg;
	// ...or load images from different directory
	else
		aImg = Balloon_CacheImgs(tt_aV[BALLOONIMGPATH]);
	sCssCrn = ' style="position:relative;width:' + tt_aV[BALLOONEDGESIZE] + 'px;padding:0px;margin:0px;overflow:hidden;line-height:0px;"';
	sCssImg = 'padding:0px;margin:0px;border:0px;';
	sImgZ = '" style="' + sCssImg + '" />';

	tt_sContent = ''+
'<div id="div_tooltip_wrapper">'+
	'<div id="div_tooltip_left" style="float:left; width:' + tt_aV[BALLOONSTEMHEIGHT] + 'px;height:auto;">'+
		//LT
		'<img id="bALlOOnLT" style="position:relative;top:' + (tt_aV[BALLOONCORNERRADIUS]) + 'px;z-index:1;display:none;' + sCssImg + '" src="' 
		+ aImg[3].src + '" width="' + tt_aV[BALLOONSTEMHEIGHT] + '" height="' + tt_aV[BALLOONSTEMWIDTH] + '" />'+
		//LM
		'<img id="bALlOOnLM" style="position:relative;top:'+ (tt_aV[BALLOONSTEMHEIGHT]
		+ tt_aV[BALLOONCORNERRADIUS]) + 'px;z-index:1;display:none;' + sCssImg + '" src="' 
		+ aImg[4].src + '" width="' + tt_aV[BALLOONSTEMHEIGHT] + '" height="' + tt_aV[BALLOONSTEMWIDTH] + '" />'+
		//LB
		'<img id="bALlOOnLB" style="position:relative;top:'+ (tt_aV[BALLOONSTEMHEIGHT]
		+ tt_aV[BALLOONCORNERRADIUS]) + 'px;z-index:1;display:none;' + sCssImg + '" src="' 
		+ aImg[5].src + '" width="' + tt_aV[BALLOONSTEMHEIGHT] + '" height="' + tt_aV[BALLOONSTEMWIDTH] + '" />'+
	'</div>'+//div_tooltip_left
	'<div id="div_tooltip_middle" style="float:left; width:auto">'+
		'<div id="div_tooltip_top" style="width:auto;height:' + tt_aV[BALLOONSTEMHEIGHT] + ';">'+
			//TL
			'<img id="bALlOOnTL" style="position:relative;left:' + tt_aV[BALLOONCORNERRADIUS] +
			'px;z-index:1;display:none;' + sCssImg + '" src="' + aImg[0].src + '" width="' + 
			tt_aV[BALLOONSTEMWIDTH] + '" height="' + tt_aV[BALLOONSTEMHEIGHT] + '" />'+
			//TM
			'<img id="bALlOOnTM" style="position:relative;left:' + (tt_aV[BALLOONCORNERRADIUS] + 
			tt_aV[BALLOONSTEMWIDTH]) + 'px;z-index:1;display:none;' + sCssImg + '" src="' + 
			aImg[1].src + '" width="' +  tt_aV[BALLOONSTEMWIDTH] + '" height="' + 
			tt_aV[BALLOONSTEMHEIGHT] + '" />'+
			//TR
			'<img id="bALlOOnTR" style="position:relative;left:' + (tt_aV[BALLOONCORNERRADIUS] +
			tt_aV[BALLOONSTEMWIDTH]*2) + 'px;z-index:1;display:none;' + sCssImg + '" src="' +
			aImg[2].src + '" width="' + tt_aV[BALLOONSTEMWIDTH] + '" height="' + 
			tt_aV[BALLOONSTEMHEIGHT] + '" />'+
		'</div>'+//div_tooltip_top
		//TOOLTIP
		'<div id="div_tooltip">'+
		'  <div id="tooltip_title">'+
		'      <div id="tooltip_title_text">'+tt_aV[TITLE]+'-'+tt_w+','+tt_h+'</div>'+
		'  </div>'+//tooltip_title
		//'<!-- MIDDLE -->'+
		'  <div id="tooltip_middle">'+
		'      <div id="tooltip_text" style="padding: 5px 2px 2px 2px;">'+tt_sContent+'</div>'+
		'  </div>'+//tooltip_middle
		//'<!-- END -->'+
		'</div>'+//div_tooltip
		'<div id="div_tooltip_bottom" style="width:auto;height:' + tt_aV[BALLOONSTEMHEIGHT] + 'px;border:0px;padding:0px;margin:0px;">'+
			//BL
			'<img id="bALlOOnBL" style="position:relative;top:-1px;left:' + tt_aV[BALLOONCORNERRADIUS] +
			'px;z-index:1;display:none;' + sCssImg 
			+ '" src="' + aImg[9].src + '" width="' + tt_aV[BALLOONSTEMWIDTH] + '" height="' 
			+ tt_aV[BALLOONSTEMHEIGHT] + '" />'+
			//BM
			'<img id="bALlOOnBM" style="position:relative;z-index:1;top:-1px;left:' + (tt_aV[BALLOONCORNERRADIUS] +
			tt_aV[BALLOONSTEMWIDTH]) + 'px;display:none;' + sCssImg 
			+ '" src="' + aImg[10].src + '" width="' + tt_aV[BALLOONSTEMWIDTH] + '" height="' 
			+ tt_aV[BALLOONSTEMHEIGHT] 	+ '" />'+
			//BR
			'<img id="bALlOOnBR" style="position:relative;z-index:1;top:-1px;left:' + (tt_aV[BALLOONCORNERRADIUS] +
			tt_aV[BALLOONSTEMWIDTH]*2) + 'px;display:none;' + sCssImg 
			+ '" src="' + aImg[11].src + '" width="' + tt_aV[BALLOONSTEMWIDTH] + '" height="' 
			+ tt_aV[BALLOONSTEMHEIGHT] 	+ '" />'+
		"</div>"+//div_tooltip_bootom
	'</div>'+//div_tooltip_middle
		'<div id="div_tooltip_right" style="float:left; width:' + tt_aV[BALLOONSTEMHEIGHT] + 'px;height:auto;">'+
			//RT
			'<img id="bALlOOnRT" style="position:relative;top:' + (tt_aV[BALLOONCORNERRADIUS]) + 
			'px;left:-1px;z-index:1;display:none;' + sCssImg + '" src="' 
			+ aImg[6].src + '" width="' + tt_aV[BALLOONSTEMHEIGHT] + '" height="' + tt_aV[BALLOONSTEMWIDTH] 
			+ '" />'+
			//RM
			'<img id="bALlOOnRM" style="position:relative;top:' + (tt_aV[BALLOONCORNERRADIUS] +
			tt_aV[BALLOONCORNERRADIUS]) + 'px;left:-1px;z-index:1;display:none;' + sCssImg + '" src="' 
			+ aImg[7].src + '" width="' + tt_aV[BALLOONSTEMHEIGHT] + '" height="' + tt_aV[BALLOONSTEMWIDTH] 
			+ '" />'+
			//RB
			'<img id="bALlOOnRB" style="position:relative;top:' + (tt_aV[BALLOONCORNERRADIUS] +
			tt_aV[BALLOONCORNERRADIUS]) + 'px;left:-1px;z-index:1;display:none;' + sCssImg + '" src="' 
			+ aImg[8].src + '" width="' + tt_aV[BALLOONSTEMHEIGHT] + '" height="' + tt_aV[BALLOONSTEMWIDTH] 
			+ '" />'+
		'</div>'+//div_tooltip_right
'</div>';

/*
	tt_sContent = '<table border="0" cellpadding="0" cellspacing="0" style="width:auto;padding:0px;margin:0px;left:0px;top:0px;"><tr>'
				// Left-top corner
				+ '<td' + sCssCrn + ' valign="bottom">'
				+ '<img src="' + aImg[1].src + '" width="' + tt_aV[BALLOONEDGESIZE] + '" height="' + tt_aV[BALLOONEDGESIZE] + sImgZ
				+ '</td>'
				// Top border
				+ '<td valign="bottom" style="position:relative;padding:0px;margin:0px;overflow:hidden;">'
				+ '<img id="bALlOOnT" style="position:relative;top:1px;z-index:1;display:none;' + sCssImg + '" src="' + aImg[9].src + '" width="' + tt_aV[BALLOONSTEMWIDTH] + '" height="' + tt_aV[BALLOONSTEMHEIGHT] + '" />'
				+ '<div style="position:relative;z-index:0;padding:0px;margin:0px;overflow:hidden;width:auto;height:' + tt_aV[BALLOONEDGESIZE] + 'px;background-image:url(' + aImg[2].src + ');">'
				+ '</div>'
				+ '</td>'
				// Right-top corner
				+ '<td' + sCssCrn + ' valign="bottom">'
				+ '<img src="' + aImg[3].src + '" width="' + tt_aV[BALLOONEDGESIZE] + '" height="' + tt_aV[BALLOONEDGESIZE] + sImgZ
				+ '</td>'
				+ '</tr><tr>'
				// Left border
				+ '<td style="position:relative;padding:0px;margin:0px;width:' + tt_aV[BALLOONEDGESIZE] + 'px;overflow:hidden;background-image:url(' + aImg[8].src + ');">'
				// Redundant image for bugous old Geckos that won't auto-expand TD height to 100%
				+ '<img width="' + tt_aV[BALLOONEDGESIZE] + '" height="100%" src="' + aImg[8].src + sImgZ
				+ '</td>'
				// Content
				+ '<td style="position:relative;line-height:normal;'
				+ ';background-image:url(' + aImg[0].src + ')'
				+ ';color:' + tt_aV[FONTCOLOR]
				+ ';font-family:' + tt_aV[FONTFACE]
				+ ';font-size:' + tt_aV[FONTSIZE]
				+ ';font-weight:' + tt_aV[FONTWEIGHT]
				+ ';text-align:' + tt_aV[TEXTALIGN]
				+ ';padding:' + balloon.padding + 'px'
				+ ';width:' + (balloon.width ? (balloon.width + 'px') : 'auto')
				+ ';">' + tt_sContent + '</td>'
				// Right border
				+ '<td style="position:relative;padding:0px;margin:0px;width:' + tt_aV[BALLOONEDGESIZE] + 'px;overflow:hidden;background-image:url(' + aImg[4].src + ');">'
				// Image redundancy for bugous old Geckos that won't auto-expand TD height to 100%
				+ '<img width="' + tt_aV[BALLOONEDGESIZE] + '" height="100%" src="' + aImg[4].src + sImgZ
				+ '</td>'
				+ '</tr><tr>'
				// Left-bottom corner
				+ '<td valign="top"' + sCssCrn + '>'
				+ '<img src="' + aImg[7].src + '" width="' + tt_aV[BALLOONEDGESIZE] + '" height="' + tt_aV[BALLOONEDGESIZE] + sImgZ
				+ '</td>'
				// Bottom border
				+ '<td valign="top" style="position:relative;padding:0px;margin:0px;overflow:hidden;">'
				+ '<div style="position:relative;left:0px;top:0px;padding:0px;margin:0px;overflow:hidden;width:auto;height:' + tt_aV[BALLOONEDGESIZE] + 'px;background-image:url(' + aImg[6].src + ');"></div>'
				+ '<img id="bALlOOnB" style="position:relative;top:-1px;left:2px;z-index:1;display:none;' + sCssImg + '" src="' + aImg[10].src + '" width="' + tt_aV[BALLOONSTEMWIDTH] + '" height="' + tt_aV[BALLOONSTEMHEIGHT] + '" />'
				+ '</td>'
				// Right-bottom corner
				+ '<td valign="top"' + sCssCrn + '>'
				+ '<img src="' + aImg[5].src + '" width="' + tt_aV[BALLOONEDGESIZE] + '" height="' + tt_aV[BALLOONEDGESIZE] + sImgZ
				+ '</td>'
				+ '</tr></table>';
*/
	return true;
};
balloon.OnSubDivsCreated = function()
{
	if(tt_aV[BALLOON])
	{
		balloon.show_iStem = 10;
		//balloon.iStem = tt_aV[ABOVE] * 1;
		balloon.aStem = [
			tt_GetElt("bALlOOnTL"), tt_GetElt("bALlOOnTM"), tt_GetElt("bALlOOnTR"),
			tt_GetElt("bALlOOnBL"), tt_GetElt("bALlOOnBM"), tt_GetElt("bALlOOnBR"),
			tt_GetElt("bALlOOnLT"), tt_GetElt("bALlOOnLM"), tt_GetElt("bALlOOnLB"),
			tt_GetElt("bALlOOnRT"), tt_GetElt("bALlOOnRM"), tt_GetElt("bALlOOnRB")];
		
		balloon.aStem[balloon.show_iStem].style.display = "inline";
		
		Balloon_tooltip(tt_aV[BALLOONCORNERRADIUS],tt_aV[BALLOONTOOLTIPWIDTH],tt_aV[BALLOONTITLEBG],tt_aV[BALLOONCONTENTBG]);
		var right_most = parseInt($('#div_tooltip').outerWidth())-(tt_aV[BALLOONCORNERRADIUS]+5);
		if(!$.browser.msie) right_most -= tt_aV[BALLOONCORNERRADIUS];
		if (balloon.show_iStem==1)
			$('#bALlOOnTM').css('left',Balloon_CalcMed(right_most,tt_aV[BALLOONCORNERRADIUS]));
		if (balloon.show_iStem==2)
			$('#bALlOOnTR').css('left',right_most);
		if (balloon.show_iStem==4)
			$('#bALlOOnBM').css('left',Balloon_CalcMed(right_most,tt_aV[BALLOONCORNERRADIUS]));
		if (balloon.show_iStem==5)
			$('#bALlOOnBR').css('left',right_most);
		
		$('#ttip').val('right_most: '+right_most+' '+$('#WzTtDiV').html());
		$('#ttip2').html($('#WzTtDiV').html());
		
		return true;
	}
	return false;
};
// Display the stem appropriately
balloon.OnMoveAfter = function()
{
	if(tt_aV[BALLOON])
	{
		var bottom_most = parseInt($('#div_tooltip_middle').height())
		-(tt_aV[BALLOONCORNERRADIUS])//bottom corner
		-tt_aV[BALLOONSTEMHEIGHT]//bottom div
		-tt_aV[BALLOONSTEMWIDTH]//it's height
		;
		if (balloon.show_iStem==7)
			$('#bALlOOnLM').css('top',Balloon_CalcMed(bottom_most,tt_aV[BALLOONCORNERRADIUS]));
		if (balloon.show_iStem==8)
			$('#bALlOOnLB').css('top',bottom_most);
		if (balloon.show_iStem==10)
			$('#bALlOOnRM').css('top',Balloon_CalcMed(bottom_most,tt_aV[BALLOONCORNERRADIUS]));
		if (balloon.show_iStem==11)
			$('#bALlOOnRB').css('top',bottom_most);

		return true;
		var iStem = (tt_aV[ABOVE] != tt_bJmpVert) * 1;

		// Tooltip position vertically flipped?
		if(iStem != balloon.iStem)
		{
			// Display opposite stem
			balloon.aStem[balloon.iStem].style.display = "none";
			balloon.aStem[iStem].style.display = "inline";
			balloon.iStem = iStem;
		}
		
		balloon.aStem[iStem].style.left = Balloon_CalcStemX() + "px";
		return true;
		//tooltip();
	}
	return false;
};
function Balloon_CalcMed(x_min,x_max)
{
	return parseInt((x_min+x_max)/2);
}
function Balloon_CalcStemX()//no longer used! (shoty)
{
	var x = tt_musX - tt_x + tt_aV[BALLOONSTEMOFFSET] - tt_aV[BALLOONEDGESIZE];
	return Math.max(Math.min(x, tt_w - tt_aV[BALLOONSTEMWIDTH] - (tt_aV[BALLOONEDGESIZE] << 1) - 2), 2);
}
function Balloon_CacheImgs(sPath)
{
	var asImg = [	"topleft.png", "topmiddle.png", "topright.png",
				 	"lefttop.png", "leftmiddle.png", "leftbottom.png",
					"righttop.png", "rightmiddle.png", "rightbottom.png",
				 	"bottomleft.png", "bottommiddle.png", "bottomright.png",
					"close.png", "close_hover.png", "loader.gif"],
	n = asImg.length,
	aImg = new Array(n),
	img;

	while(n)
	{--n;
		img = aImg[n] = new Image();
		img.src = sPath + asImg[n];
	}
	return aImg;
}
// This mechanism pre-caches the default images specified by
// congif.BalloonImgPath, so, whenever a balloon tip using these default images
// is created, no further server connection is necessary.
function Balloon_PreCacheDefImgs()
{
	// Append slash to img path if missing
	if(config.BalloonImgPath.charAt(config.BalloonImgPath.length - 1) != '/')
		config.BalloonImgPath += "/";
	// Preload default images into array
	balloon.aDefImg = Balloon_CacheImgs(config.BalloonImgPath);
}
Balloon_PreCacheDefImgs();
