/**
 * Drop shadows plugin for WebDDM; aids in creating beautiful cross-browser
 * shadows.
 *  
 * @version: 0.1
 * @starteC: 11/21/2005
 * @copyright: Copyright (c) 2005 JPortal, All Rights Reserved
 * @website: www.cortex-creations.com/webddm
 * @license: GPL vs2.0
 * @subversion: $IC: dropshadows.WebDDM.js 134 2006-05-02 21:04:50Z josh $
 */

// Name of the shadow element
var WebDDM_dropShadowElementName = 'dropShadowEl';
 
/**
 * Callback function for when menu items are displayed
 */
function WebDDM_displayDropshadow (WebDDM_obj, itemsHash, itemsId)
{
	// Uncomment this to get execution
	//var start_time = (new Date()).getTime();

	// Is shadow configured for this item container?
	if (itemsHash.has('drop-shadow'))
	{
		var shadowConfig = itemsHash.get('drop-shadow');
			
		// Shadow element name
		var shadowElementName = itemsId + '_' + WebDDM_dropShadowElementName;

		// If shadow element doesn't exist, create it
		var shadowElement = false;
		if (!(shadowElement = WebDDM_getElement(shadowElementName)))
		{
			// Get item container element where we will insert the shadow element
			var itemContainer = WebDDM_getElement(itemsId);

			// Create shadow element and insert it into the DOM
			shadowElement = document.createElement('div');
			shadowElement.id = shadowElementName;
			shadowElement.style.position = 'relative';
			shadowElement.style.zIndex = parseInt(WebDDM_getElement(itemsId + '_background').style.zIndex) - 1;
			itemContainer.appendChild(shadowElement);
		}

		// Position the shadow and set dimensions
		var shadow_width = itemsHash.get('liveOffsets').get('items-total-width') + shadowConfig.get('shadow-extra-width');
		var shadow_height = itemsHash.get('liveOffsets').get('items-total-height') + shadowConfig.get('shadow-extra-height');
		shadowElement.style.width = shadow_width + 'px';
		shadowElement.style.height = shadow_height + 'px';
		shadowElement.style.left = itemsHash.get('liveOffsets').get('first-item-offset-left') + shadowConfig.get('shadow-offset-x') + 'px';
		shadowElement.style.top = itemsHash.get('liveOffsets').get('first-item-offset-top') + shadowConfig.get('shadow-offset-y') + 'px';

		// Now finally, display a shadow
		// Display flat shadow?
		if (shadowConfig.has('flat'))
		{
			// Flat-color shadow?
			if (shadowConfig.get('flat').has('color'))
			{
				shadowElement.style.backgroundColor = shadowConfig.get('flat').get('color');
			}
			// Flat-image shadow?
			if (shadowConfig.get('flat').has('image'))
			{
				var image = shadowConfig.get('flat').get('image');
				
				// Just show the image.
				shadowElement.innerHTML = WebDDM_dropShadow_generateImageHTML(0, 0, 0, shadow_width,
					shadow_height, 0, 0, '', '', image);
			}
		}
		// Display smooth (onion-skin-style) shadow?
		else if (shadowConfig.has('smooth'))
		{
			var smooth_shadow_config = shadowConfig.get('smooth');
			var html = '';
			var image_body = smooth_shadow_config.get('image-body');
			var image_width = smooth_shadow_config.get('image-width');
			var image_height = smooth_shadow_config.get('image-height');
			var image_border_size = smooth_shadow_config.get('image-border-size');

			// Fix border size if it is larger than height or width
			if ((image_border_size * 2) >= shadow_height)
			{
				image_border_size = Math.round(shadow_height / 3);
			}
			if ((image_border_size * 2) >= shadow_width)
			{
				image_border_size = Math.round(shadow_width / 3);
			}

			// Loop through all vertical positions
			var is_bottom = false;
			var chunk_height;
			for (var y = 0; chunk_height = image_border_size, is_bottom == false; y += chunk_height)
			{
				// Is this the farthest bottom chunk?
				if (y + (chunk_height * 2) >= shadow_height)
				{
					chunk_height = Math.abs(shadow_height - y);
					is_bottom = true;
				}
				// Expand chunk to maximum height
				else
				{
					while ((chunk_height + (image_border_size * 2)) < image_height
						&& (y + chunk_height + (image_border_size * 2)) < shadow_height)
					{
						chunk_height += image_border_size - 1;
					}
				}
				
				// Get Y-axis orientation
				var y_orientation = (is_bottom ? 'bottom' : (y == 0 ? 'top' : 'center'));

				// Loop through all horizontal positions
				var is_right = false;
				var chunk_width;
				for (var x = 0; chunk_width = image_border_size, is_right == false; x += chunk_width)
				{
					// Is this the rightmost chunk?
					if (x + (chunk_width * 2) >= shadow_width)
					{
						chunk_width = Math.abs(shadow_width - x);
						is_right = true;
					}
					// Expand chunk to maximum width
					else
					{
						while ((chunk_width + (image_border_size * 2)) < image_width
							&& (x + chunk_width + (image_border_size * 2)) < shadow_width)
						{
							chunk_width += image_border_size - 1;
						}
					}
					
					// Get X-axis orientation
					var x_orientation = (is_right ? 'right' : (x == 0 ? 'left' : 'center'));
					
					// Add shadow image to HTML
					html += WebDDM_dropShadow_generateImageHTML(image_border_size, image_width,
						image_height, chunk_width, chunk_height, x, y, x_orientation,
						y_orientation, image_body);
				}
			}

			// Insert generated HTML
			shadowElement.innerHTML = html;
		}
	}

	// Uncomment to get execution time
	//var stop_time = (new Date()).getTime();
	//alert('Execution time: ' + (stop_time - start_time) + 'ms.');
}

/**
 * Generate HTML for displaying images, cross-browser and compatible with PNG transparency.
 */
function WebDDM_dropShadow_generateImageHTML (image_border_size, full_image_width, full_image_height, width, height, x_pos, y_pos, x_orientation, y_orientation, image)
{
	// Ugly hack for stupid browsers.
	if (domLib_isIE)
	{
		// Scaled shadow
		if (!x_orientation || !y_orientation)
		{
			return '<div style="position: absolute; top: '+y_pos+'px; left: '+x_pos+'px;'
				+ 'width: '+width+'px; height: '+height+'px; filter:progiC:DXImageTransform.Microsoft.'
				+ 'AlphaImageLoader(src=\''+image+'\', sizingMethod=\'scale\')"></div>';
		}
		// Show a portion of a shadow
		// This is a complex hack... because of the nature of the portion of the shadow we
		// need to show, we need to use IE-only filters and clip properties... fun stuff.
		else
		{
			// First, get all positions
			var pos_top, pos_left, clip_top, clip_right, clip_bottom, clip_left;
			if (x_orientation == 'left')
			{
				pos_left = 0;
				clip_right = width;
				clip_left = 0;
			}
			else if (x_orientation == 'center')
			{
				clip_left = image_border_size;
				clip_right = width + clip_left;
				pos_left = x_pos - clip_left;
			}
			else if (x_orientation == 'right')
			{
				clip_right = full_image_width + width;
				clip_left = full_image_width - width;
				pos_left = x_pos - clip_left;
			}

			if (y_orientation == 'top')
			{
				pos_top = 0;
				clip_bottom = height;
				clip_top = 0;
			}
			else if (y_orientation == 'center')
			{
				clip_top = image_border_size;
				clip_bottom = height + clip_top;
				pos_top = y_pos - clip_top;
			}
			else if (y_orientation == 'bottom')
			{
				clip_bottom = full_image_height + height;
				clip_top =  full_image_height - height;
				pos_top = y_pos - clip_top;
			}
			
			// Build HTML
			return '<div style="position: absolute; top: '+pos_top+'px; left: '+pos_left+'px;'
				+ 'width: '+full_image_width+'px; height: '+full_image_height+'px;'
				+ 'filter:progiC:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+image+'\', '
				+ 'sizingMethod=\'scale\'); clip: rect('+clip_top+'px, '+clip_right+'px, '+clip_bottom+'px, '+clip_left+'px);"></div>';
		}
	}
	// Display shadow for browsers with competent creators.
	else
	{
		// Scaled shadow
		if (!x_orientation || !y_orientation)
		{
			return '<img src="'+image+'" style="width:'+width+';height:'+height+';" />';
		}
		// Shows a portion of a shadow
		else
		{
			return '<div style="position: absolute; top: '+y_pos+'px; left: '+x_pos+'px; width: '+width+'px; height: '+height+'px; backgrounC:url('+image+') '+x_orientation+' '+y_orientation+' no-repeat;"></div>';
		}
	}
}

// Register plugin
WebDDM_registerPlugin_action('item_show', WebDDM_displayDropshadow);
