
/***********************************************************************
* AutoThumb MOD - JS Functions
* by Paulo França Lacerda
/**********************************************************************/

var autothumb_img_number = 0;
var autothumb_oldOnLoad  = null;

var autothumb_is_active		= false; 	// whether AutoThumb is currently is_active (enabled).
var autothumb_blank_wnd		= false; 	// whether the full-sized image should be open in a new, blank window.
var autothumb_hint_on		= false;  	// whether a hint should be displayed on thumb mouse over (the text passed in "long_hint").
var autothumb_title_on		= false;  	// whether there should be a top bar saying "click to enlarge" (the text passed in "title_text").
var autothumb_footer_on		= false; 	// whether there should be a bottom bar showing the full-size dimage dimensions (width x height).

var autothumb_max_height		= 0;	// how tall the full-size image can be (at most) for not to be thumbnailed.
var autothumb_max_width			= 0; 	// how wide the full-size image can be (at most) for not to be thumbnailed.
var autothumb_title_opacity		= 0;	// title opacity (0-100)
var autothumb_thumb_opacity		= 0;	// thumbnail opacity (0-100)
var autothumb_footer_opacity	= 0;	// footer opacity (0-100)

var autothumb_linkout_src	= '';		// full path to AutoThumb's link-out image.
var autothumb_long_hint		= ''; 		// the localized long  hint to be displayed on thumb mouse over - if var "hint_on" is passed as true.
var autothumb_title_text	= '';		// the localized short hint to be displayed on the title bar    - if var "title_on" is passed as true.

var autothumb_hrg_array = new Array();
var autothumb_ful_array = new Array();
var autothumb_src_array = new Array();
var autothumb_tab_array = new Array();

var autothumb_idx = -1;
var autothumb_hook_count = 0;


//////////////////////////////////////////////////////////////////////////////////////////
//
// function  create_opaque()
//
// Creates a new html element with a given opacity, as specified.
//
// v1.0.0-rc3
//	Now correctly keeps the thumb in the same line
//	(without a line break) but ONLY IF both title and
//	footer are disabled.
//

function create_opaque (tag,opa)
{
	var e = document.createElement(tag);
	
	if (opa < 0)
	{
		opa = 1; 
	}
	
	if (opa > 100)
	{
		opa = 100; 
	}
	
	if (opa == 0)
	{
		opacity_percentage = 0.0;
	}
	else
	{
		opacity_percentage = opa / 10;
	}
	
	//
	// v1.0.0-rc3
	//
	// Now correctly keeps the thumb in the same line
	// (without a line break) but ONLY IF both title and
	// footer are disabled.
	//
	
	if (autothumb_title_on || autothumb_footer_on)
	{
		e.style.display = 'block';
	}
	else
	{
		e.style.display = 'inline';
	}
	
	e.style.filter = 'alpha(opacity:' + opa + ')';
	e.style.KHTMLOpacity = opacity_percentage;
	e.style.MozOpacity = opacity_percentage;
	e.style.opacity = opacity_percentage;

	return e;
}


//////////////////////////////////////////////////////////////////////////////////////////
//
// function  autothumb_make_title()
//
// Creates the title bar for the thumbnail.
// This function is used by function autothumb_makethumb().
//
// v1.0.0-rc2
//	Code cleaned up.
//
// v1.0.0-b2
//	Unchanged.
//
// v1.0.0-b1
//	First released.
//

function autothumb_make_title (
	thumb_anchor,
	thumb_width)
{
	var x = undefined;

	if (!thumb_anchor)
	{
		return x;
	}
	
	x = create_opaque ('div', autothumb_title_opacity);	// box at the top
	
	x.style.padding = '0 0 0 0'; 
	x.style.margin  = '0 0 0 0'; 
	x.style.width = thumb_width + 'px';
		
	x.style.border = '1px solid #0B5EBB';
	x.style.height = 8 + 'px';
	x.style.padding = '0 0 9px 0'; 
	x.style.margin  = '0 0 0 0'; 
	x.style.backgroundColor = '#08478E'; 
	x.style.color = '#ffffff';
	x.style.textAlign = 'center';
	x.style.fontName = 'Verdana';
	x.style.fontSize = '11px';
	x.style.overflow = 'hidden';
	x.style.whiteSpace = 'nowrap';
	
	var anchor2	= create_opaque('a',	autothumb_title_opacity);
	var title	= create_opaque('span',	autothumb_title_opacity);
	
	anchor2.href = thumb_anchor.href;
	anchor2.innerHTML = '<font color="white">' + autothumb_title_text + '</font>';	// can't get styles to work here!  \\:^(
	anchor2.target = thumb_anchor.target;

	title.style.verticalAlign = 'top';
	title.style.fontWeight = 'normal';	// this ensures the title won't get bold even when the [img] tag is embraced by a [b].
	
	title.appendChild(anchor2);
	x.appendChild(title);
	
	return x;
}

//////////////////////////////////////////////////////////////////////////////////////////
//
// function  autothumb_make_footer()
//
// Creates the footer bar for the thumbnail.
//
// This function is used by function autothumb_makethumb().
//
// v1.0.0-rc3
//	Fixed a bug: the footer was displaying the 
//	dimensions (width x height) of the thumbnail, 
//	while it should be the full-sized image's 
//	dimensions.
//
// v1.0.0-rc4
//	The footer was displaying "Height x Width" while the thumbnail hint
//	was showing "Width x Height". Now the footer shows the same as the thumb's hint.
//
// v1.0.0-rc2
//	Code cleaned up.
//
// v1.0.0-b5
//	Attempt to fix bugs in client browser other than IE6.
//
// v1.0.0-b1
//	First released.
//

function autothumb_make_footer (
	thumb_width,
	thumb_height,
	full_width,
	full_height)
{
	var x;

	x = create_opaque('div', autothumb_footer_opacity);	// box at the bottom

	x.style.padding = '0 0 0 0'; 
	x.style.margin  = '0 0 0 0'; 
	x.style.width = thumb_width + 'px';

	x.style.border = '1px solid #CDE4FD';
	x.style.height = 8 + 'px';
	x.style.padding = '0 0 9px 0'; 
	x.style.margin  = '0 0 0 0'; 
	x.style.backgroundColor = '#D3E7FD';
	x.style.color = '#08478E';
	x.style.textAlign = 'center';
	x.style.fontName = 'Verdana';
	x.style.fontSize = '9px';
	x.style.fontWeight = 'normal';
	x.style.overflow = 'hidden';
	x.style.whiteSpace = 'nowrap';

	var footer = create_opaque('span', autothumb_footer_opacity);

	footer.style.verticalAlign = 'top';
	footer.innerHTML   = '<a href="http://www.athrasoft.com/forum/viewtopic.php?f=38&amp;t=41"><img alt="Visit AutoThumb\'s home page and know how to use it in your forums for free." valign="bottom" border="0" width="9" height="8" src="'+ autothumb_linkout_src +'" /></a> <font face="Verdana"> &nbsp; '+ full_width + '<font color="#808080">x</font>' + full_height + '</font>';

	x.appendChild(footer);

	return x;
}


//////////////////////////////////////////////////////////////////////////////////////////
//
// function  autothumb_makethumb()
//
// Turns the original, full-sized image passed into a clickable thumbnail.
//
// v1.0.0-rc2
//	Code cleaned up.
//
// v1.0.0-b7
//	Rewritten. 
//
// v1.0.0-b5
//	Adjusted to the new approach: window.onload() event.
//
// v1.0.0-b1
//	First released.
//

function autothumb_makethumb (
	full_image,		// passed as "null" in the 1st pass.
	hourglass_img,	// hourglass image object.
	full_src,		// full-sized image source.
	table)			// temporary table.
{
	//
	// Validates all parameters passed.
	//

	if ((!hourglass_img) || (!full_src))
	{
		return false;
	}
	
	table.style.display = 'none';
	
	//
	// If AutoThumb is disabled, then nothing is done to the original image.
	//
	
	var w = parseInt(full_image.width);
	var h = parseInt(full_image.height);
	
	var is_active  = (autothumb_is_active == '1') && ((w > autothumb_max_width) || (h > autothumb_max_height));
	
	var real_image = hourglass_img.previousSibling;
		
	//debug: alert('wh: ' + w + ' x ' + h + '  ==  '+full_image.width+' x '+full_image.height);
	
	if (!is_active)  // perhaps it's been disabled above?!
	{
		real_image.style.padding = '0 0 0 0'; 
		real_image.style.margin  = '0 0 0 0'; 
		real_image.style.display = 'inline';  // v1.0.0-rc3: "inline" instead of "block".
		
		hourglass_img.style.display = 'none';
	
		return false;
	}
	
	//
	// Initializes variables.
	//

	var factor = 0;
	var thumb_width = 0;
	var thumb_height = 0;
	
	var show_title  = (autothumb_title_on == '1');
	var show_footer = (autothumb_footer_on == '1');
	var new_window  = (autothumb_blank_wnd == '1');
	var show_hint   = (autothumb_hint_on == '1') && (autothumb_long_hint) && (autothumb_long_hint != '');

	var thumb_anchor;
	var the_parent;

	//
	// Modifies image's original size.
	//
	
	if (w > h)
	{
		factor = w / autothumb_max_width;
		thumb_width = autothumb_max_width;
		thumb_height = h / factor;
	}
	else
	{
		factor = h / autothumb_max_height;
		thumb_width = w / factor;
		thumb_height = autothumb_max_height;
	}

	thumb_width  = parseInt(thumb_width);
	thumb_height = parseInt(thumb_height);
	
	//
	// Creates the thumb_anchor as the image's sibling, 
	// then puts the image object passed inside the thumb_anchor.
	//

	thumb_anchor = create_opaque('a', autothumb_thumb_opacity);  // creates the thumb_anchor.

	thumb_anchor.href = full_image.src;  // sets thumb_anchor's click address.
	thumb_anchor.style.padding = '0 0 0 0'; 
	thumb_anchor.style.margin  = '0 0 0 0'; 
	thumb_anchor.style.width = thumb_width + 'px';
	thumb_anchor.style.height = thumb_height + 'px';
	
	//
	// Sets thumb_anchor's target.
	//

	if (new_window)
	{
		thumb_anchor.target = '_blank';
	}
	
	//
	// Sets thumbnail's hint text.
	//

	if (show_hint)
	{
		real_image.alt = autothumb_long_hint + ': ' + w + ' x ' + h + ' pixels.';
	}
	
	//
	// Creates the title bar.
	//

	var title_bar;

	if (show_title)
	{
		title_bar = autothumb_make_title(thumb_anchor, thumb_width);
	}

	//
	// Creates the bottom bar.
	//

	var footer_bar;

	if (show_footer)
	{
		footer_bar = autothumb_make_footer(thumb_width, thumb_height, w, h);
	}

	the_parent = real_image.parentNode;

	//
	// Inserts the bottom bar as a child of the main box.
	//

	if (show_title)
	{
		the_parent.insertBefore(title_bar, real_image);
	}

	//
	// Makes the thumb_anchor embrace the thumbnail.
	//

	the_parent.insertBefore(thumb_anchor, real_image);

	if (show_footer)
	{
		the_parent.insertBefore(footer_bar, hourglass_img);
	}

	//
	// Inserts the top bar right before the thumb anchor.
	//

	//
	// Finally applies dimension changes to the image,
	// turning it into a thumbnail of itself.
	//

	hourglass_img.style.display = 'none';

	real_image.style.width  = thumb_width  + 'px';
	real_image.style.height = thumb_height + 'px';
	real_image.style.padding = '0 0 0 0'; 
	real_image.style.margin  = '0 0 0 0'; 
	real_image.style.border  = '0';
	real_image.style.position = '';
	
	thumb_anchor.appendChild(real_image); 
	real_image.style.display = 'inline';  // v1.0.0-rc3: "inline" instead of "block".

	return false;	
}


//////////////////////////////////////////////////////////////////////////////////////////
//
// function  autothumb_window_onload()
//
// Handles the document window's onload() event.
//
// This function is used by function autothumb_makethumb().
//
// v1.0.0-rc2
//	Code cleaned up.
//
// v1.0.0-b5
//	First released.
//

function autothumb_window_onload ()
{
	for (var i=0; i<autothumb_hrg_array.length; i++)
	{
		autothumb_ful_array[i] = create_opaque ('img', autothumb_thumb_opacity);
		autothumb_ful_array[i].src = autothumb_src_array[i];
		
		autothumb_makethumb (
			autothumb_ful_array[i],		// full-sized image object.
			autothumb_hrg_array[i],		// hourglass image object.
			autothumb_src_array[i],		// full-sized image source.
			autothumb_tab_array[i]);	// temporary table.
	}
}

//////////////////////////////////////////////////////////////////////////////////////////
//
// function  autothumb_prepare_item()
//
// This is a handler for the hourglass images' onload() event.
// It saves info from the image passed as param to treat it later.
// Also hooks into document window's onload() event (once).
//
// v1.0.0-rc2
//	Code cleaned up.
//
// v1.0.0-b7
//	Rewritten. 
//
// v1.0.0-b6
//	Now it clears the image's onload() event after 
//	preparation 'cause the handler was being fired endlessly.
//
// v1.0.0-b5
//	First released.
//

function autothumb_prepare_item (
	hourglass_img,		// hourglass image object.
	full_src,			// full-sized image source.
	is_active, 			// whether AutoThumb is currently is_active (enabled).
	blank_wnd, 			// whether the full-sized image should be open in a new, blank window.
	hint_on,  			// whether a hint should be displayed on thumb mouse over (the text passed in "long_hint").
	title_on,  			// whether there should be a top bar saying "click to enlarge" (the text passed in "title_text").
	footer_on, 			// whether there should be a bottom bar showing the full-size dimage dimensions (width x height).
	max_height,			// how tall the full-size image can be (at most) for not to be thumbnailed.
	max_width, 			// how wide the full-size image can be (at most) for not to be thumbnailed.
	title_opacity,		// title opacity (0-100)
	thumb_opacity,		// thumbnail opacity (0-100)
	footer_opacity,		// footer opacity (0-100)
	linkout_src,		// full path to AutoThumb's link-out image.
	long_hint, 			// the localized long  hint to be displayed on thumb mouse over - if var "hint_on" is passed as true.
	title_text)			// the localized short hint to be displayed on the title bar    - if var "title_on" is passed as true.
{
	hourglass_img.onload = null;	// withot this, the onload event is fired endlessly, and this handler is fired as such.
	
	autothumb_img_number = autothumb_img_number + 1;

	var table = document.getElementById('autothumb_img_table');
	table.id = table.id +'_'+ autothumb_img_number;

	//
	// Adds the source for the full-sized iamge as well 
	// as the hourglass image object to the array.
	//
	
	autothumb_idx = autothumb_idx + 1;
	
	autothumb_ful_array[autothumb_idx] = null;  // this will be retrieved by our window.onload() event custom handler.
	autothumb_hrg_array[autothumb_idx] = hourglass_img;
	autothumb_src_array[autothumb_idx] = full_src;
	autothumb_tab_array[autothumb_idx] = table;
		
	//
	// this block only runs at the first call.
	//
	
	if (autothumb_hook_count == 0)
	{
		autothumb_hook_count = autothumb_hook_count + 1;
	
		//
		// Saves some of the values passed into the global arrays.
		//
	
		autothumb_is_active		= is_active;
		autothumb_blank_wnd		= blank_wnd;
		autothumb_hint_on		= hint_on;
		autothumb_title_on		= title_on;
		autothumb_footer_on		= footer_on;
		autothumb_max_height	= max_height;
		autothumb_max_width		= max_width;
		autothumb_title_opacity	= title_opacity;
		autothumb_thumb_opacity	= thumb_opacity;
		autothumb_footer_opacity= footer_opacity;
		autothumb_linkout_src	= linkout_src;
		autothumb_long_hint		= long_hint;
		autothumb_title_text	= title_text;
	}
}

//
// Hooks into the phpBB's onload() handling mechanism.
//

onload_functions.push('autothumb_window_onload()');
