/* for Mozilla */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
document.write("<script defer src=js/ie_onload.js><"+"/script>");
/*@end @*/

/* for other browsers */
window.onload = init;

function init()
{
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	
	// Use jQuery via jQuery(...)
	jQuery.noConflict();

	// create the "page loaded" message
	xform();
	comportamiento();
};

function xform()
{
	// Hide forms
	jQuery('form.xform').hide().end();

	// Processing
	// Firefox
	if(BrowserDetect.browser == "Firefox")
	{
		jQuery('form.xform').find('li label').not('.nocmx').each( function(i)
		{
			
			var labelContent = this.innerHTML;
			var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
			var labelSpan = document.createElement( 'span' );
			labelSpan.style.display = 'block';
			labelSpan.style.width = labelWidth;
			labelSpan.innerHTML = labelContent;
			this.style.display = '-moz-inline-box';
			this.innerHTML = null;
			this.appendChild( labelSpan );
		}).end();
	}
	// Resto de navegadores
	else
	{
		jQuery('form.xform').find('li label').not('.nocmx').each( function(i)
		{
			this.style.display = 'inline-block';
		}).end();
	}
	
	// Show forms
	jQuery('form.xform').show().end();
}

function comportamiento()
{
	jQuery('#familia').change(function()
	{
		xajax_carga_subfamilias(this.value);
	});	
	
	if(jQuery("#imagenes").length)
	{
		jQuery(".imagen > img").each( function()
		{
			if(jQuery(this).attr("longDesc"))
			{
				// Si es Firefox, usa el cursor de zoom
				if(BrowserDetect.browser == "Firefox")
					jQuery(this).css('cursor','-moz-zoom-in');
				else
					jQuery(this).css('cursor','pointer');
	
				jQuery(this).click(function()
				{
					jQuery("div.ampliada").empty();
					jQuery("div.ampliada").append("<img src=\""+jQuery(this).attr("longDesc")+"\" alt=\""+jQuery(this).attr("alt")+"\" /><span>&nbsp;</span>");
					jQuery('div.ampliada').fadeIn();
				});
			}
		});

		if(jQuery(".ampliada").length)
		{
			// Si es Firefox, usa el cursor de zoom
			if(BrowserDetect.browser == "Firefox")
				jQuery(".ampliada").css('cursor','-moz-zoom-out');
			else
				jQuery(".ampliada").css('cursor','pointer');

			jQuery(".ampliada").click(function()
			{
				jQuery(this).fadeOut();
			});
		}
	}

	if(jQuery('#acciones').length)
	{
		/* Impresion */
		jQuery("#acciones").append("<img src=\"img/iconos/imprimir.gif\" id=\"imprimir\" alt=\"Imprimir\" />");

		if(jQuery('#imprimir').length)
		{			
			jQuery('#imprimir').show();
			jQuery("#imprimir").css('cursor','pointer');
			jQuery('#imprimir').click(function()
			{
				window.print();
				return false;
			});
		}
		
		/* Enlazar receta */
		if(jQuery("#receta").length)
		{
			jQuery("#acciones").append("<img src=\"img/iconos/enlazar.gif\" id=\"enlazar\" alt=\"Enlazar\" />");
			jQuery("#enlazar").css('cursor','pointer');
			jQuery("#enlazador").hide();
			jQuery('#enlazar').click(function()
			{
				if(jQuery("#enlazador").css('display') == "none")
				{
					jQuery(this).attr('src','img/iconos/enlazarx.gif');
					jQuery('#enlazador').fadeIn();
				}
				else
				{
					jQuery(this).attr('src','img/iconos/enlazar.gif');
					jQuery('#enlazador').fadeOut();
				}
			});
		}
	}
	
	jQuery('.external').click(function ()
	{
		window.open(this.href);
		return false;
   	});
}