
/***********************************************************************
* 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;

/***********************************************************************
* Creates a new html element with a given opacity, as specified.
*/

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;
	}
	
	e.style.display = 'block';
	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-b2
*	Unchanged.
*
* v1.0.0-b1
*	First released.
*/

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

if (!thumb_anchor)
{
	return x;
}

x = create_opaque ('div', 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 #718e4d';
x.style.height = 8 + 'px';
x.style.padding = '0 0 9px 0'; 
x.style.margin  = '0 0 0 0'; 
x.style.backgroundColor = '#718e4d'; 
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',	title_opacity);
var title	= create_opaque('span',	title_opacity);

anchor2.href = thumb_anchor.href;
anchor2.innerHTML = '<font color="white">' + 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-b5
*	Attemps to fix bugs in client browser other than IE6.
*
* v1.0.0-b1
*	First released.
*/

function autothumb_make_footer (
	footer_opacity,
	linkout_src,
	thumb_width,
	thumb_height)
{
var x;

x = create_opaque('div', 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 #718e4d';
x.style.height = 8 + 'px';
x.style.padding = '0 0 9px 0'; 
x.style.margin  = '0 0 0 0'; 
x.style.backgroundColor = '#718e4d';
x.style.color = '#ffffff';
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', footer_opacity);

footer.style.verticalAlign = 'top';
//footer.innerHTML = h + '<span style:"color: #C0C0C0">' + 'x' + '</span>' + w;  --> this breaks JS!  \\:^(
//footer.innerHTML = '<a href="'+ 'http://www.athrasoft.com/forum/viewtopic.php?f=38&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="'+ linkout_src +'" /></a> <font face="Verdana"> AutoThumb </font>';// &nbsp; '+ h + '<font color="#808080">' + 'x' + '</font>' + w; // + '&nbsp; pixels';
footer.innerHTML   = '<font face="Verdana">Imagen reducida a '+ thumb_height + '<font color="#ffffff">x</font>' + thumb_width + '</font>';

x.appendChild(footer);

return x;
}


/***********************************************************************
* function autothumb_makethumb()
*
* Turns the original, full-sized image passed into a clickable thumbnail.
*
* Internals:
* 	o Image passed is not  cloned, nor manipulated at all.
*	o If AutoThumb is disabled in the config, nothing is done.
*	o In order to create the thumbnail, all this function do is
*	  calculate the appropriate display dimensions anf let browser's
*	  rendering system to do the rest.
*	o The HTML elements title and footers bars are only instantiated
*	  if they are enabled in the config.
*	o The title/thumbnail hints are localized (language-aware).
*	o A distinct/exclusive opacity percentage is applied to title, 
*	  thumbnail and footer boxes.
*	o The same dimension up-most limits (for the full-sized original's
*	  image) are used for calculating thumbnail's factor (ratio).
*
* NOTE:
*	Do not change the order in which the boxes are added 
*	to the parent of the original image. This would mess up things.
*
* 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.

	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.
{

//
// 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  = (is_active == '1') && ((w > max_width) || (h > max_height));

var real_image = hourglass_img.previousSibling;
	
//alert('wh: ' + h + ' x ' + h + '  real_image = '+real_image+'  table = '+table);

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 = 'block';
	
	hourglass_img.style.display = 'none';

	return false;
}

//
// Initializes variables.
//

var factor = 0;
var thumb_width = 0;
var thumb_height = 0;

var show_title  = (title_on == '1');
var show_footer = (footer_on == '1');
var new_window  = (blank_wnd == '1');
var show_hint   = (hint_on == '1') && (long_hint) && (long_hint != '');

var thumb_anchor;
var the_parent;

//
// Ensures this image will have a unique id.
//

//autothumb_img_number = autothumb_img_number + 1;
//full_image.id = full_image.id + autothumb_img_number;
//full_image.id = 'autothumb_thumbnail_' + autothumb_img_number;

//
// Modifies image's original size.
//
	
if (w > h)
{
	factor = w / max_width;
	thumb_width = max_width;
	thumb_height = h / factor;
}
else
{
	factor = h / max_height;
	thumb_width = w / factor;
	thumb_height = 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', 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 = long_hint + ': ' + w + ' x ' + h + ' pixels.';
}
	
//
// Creates the title bar.
//

var title_bar;

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

//
// Creates the bottom bar.
//

var footer_bar;

if (show_footer)
{
	footer_bar = autothumb_make_footer(footer_opacity, linkout_src, thumb_width, thumb_height);
}

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 = '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-b5
*	First released.
*/

function autothumb_window_onload ()
{
	/*
	window.onload = null;
	
	if (autothumb_oldOnLoad)
	{
		autothumb_oldOnLoad();			// fires the original handler..
		autothumb_oldOnLoad = null;		// ..once only
	}
	*/

	//alert('autothumb_hrg_array.length = '+autothumb_hrg_array.length);
	
	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_ful_array[i] = autothumb_hrg_array[i].previousSibling;
		
		//alert('autothumb_ful_array['+i+']: ' + autothumb_ful_array[i].width + ' x ' + autothumb_ful_array[i].height);
		
		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.

			autothumb_is_active, 		// whether AutoThumb is currently is_active (enabled).
			autothumb_blank_wnd, 		// whether the full-sized image should be open in a new, blank window.
			autothumb_hint_on,  		// whether a hint should be displayed on thumb mouse over (the text passed in "long_hint").
			autothumb_title_on,  		// whether there should be a top bar saying "click to enlarge" (the text passed in "title_text").
			autothumb_footer_on, 		// whether there should be a bottom bar showing the full-size dimage dimensions (width x height).
			autothumb_max_height,		// how tall the full-size image can be (at most) for not to be thumbnailed.
			autothumb_max_width, 		// how wide the full-size image can be (at most) for not to be thumbnailed.
			autothumb_title_opacity,	// title opacity (0-100)
			autothumb_thumb_opacity,	// thumbnail opacity (0-100)
			autothumb_footer_opacity,	// footer opacity (0-100)
			autothumb_linkout_src,		// full path to AutoThumb's link-out image.
			autothumb_long_hint, 		// the localized long  hint to be displayed on thumb mouse over - if var "hint_on" is passed as true.
			autothumb_title_text);		// the localized short hint to be displayed on the title bar    - if var "title_on" is passed as true.
	}
}

/***********************************************************************
* 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-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.
	
	var full_image;
	
	// full_image = document.getElementById('autothumb_fullsized_img');
	autothumb_img_number = autothumb_img_number + 1;

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

	//alert('table='+table);
	
	//
	// 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;  //full_image;
	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 window's onload() eventm in case this function 
		// is being called by the first [img] BBCode in the html.
		//
	
		/*
		autothumb_oldOnLoad = window.onload;
		window.onload = autothumb_window_onload;
		*/
	}
}

onload_functions.push('autothumb_window_onload()');
