/*##############################################################################
#    ____________________________________________________________________
#   /                                                                    \
#  |               ____  __      ___          _____  /     ___    ___     |
#  |     ____       /  \/  \  ' /   \      / /      /__   /   \  /   \    |
#  |    / _  \     /   /   / / /    /  ___/  \__   /     /____/ /    /    |
#  |   / |_  /    /   /   / / /    / /   /      \ /     /      /____/     |
#  |   \____/    /   /    \/_/    /  \__/  _____/ \__/  \___/ /           |
#  |                                                         /            |
#  |                                                                      |
#  |   Copyright (c) 1999-2007                        MindStep SCOP SARL  |
#  |   Herve Masson                                                       |
#  |                                                                      |
#  |      www.mindstep.com                              www.mjslib.com    |
#  |   info-oss@mindstep.com                           mjslib@mjslib.com  |
#   \____________________________________________________________________/
#
#  Version: $Id: image.js 3416 2007-08-06 16:52:31Z herve $
#
#  [This product is distributed under a BSD-like license]
#  
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  
#     1. Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#  
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in
#        the documentation and/or other materials provided with the
#        distribution. 
#  
#  THIS SOFTWARE IS PROVIDED BY THE MINDSTEP CORP PROJECT ``AS IS'' AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MINDSTEP CORP OR CONTRIBUTORS
#  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
#  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
#  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
#  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
#  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#  
#  The views and conclusions contained in the software and documentation
#  are those of the authors and should not be interpreted as representing
#  official policies, either expressed or implied, of MindStep Corp.
#  
##############################################################################*/

var gIMG_morphDelay		= 30;
var gIMG_morphInc		= 33;
var gIMG_morphDec		= -10;
var gIMG_defaultEffect	= "none";
var gIMG_animateDelay	= 100;

function _img_animate(img)
{
	var delay=img._mjsanimdelay;
	var imlist=img._mjsanimimages;
	var index=img._mjsanimindex;

	index++;
	index%=imlist.length;
	img._mjsanimindex=index;

	var im=imlist[index];
	img.src=im.src;

	var id=mjs_allocateElementID(img);
	mjs_timedCall(delay,"imganimate"+id,_img_animate,img);
	return;
}

function _img_mouseOver(container)
{
	mjs_show(container._overimg);
	return true;
}

function _img_mouseOut(container)
{
	mjs_hide(container._overimg);
	return true;
}

function _img_morph(container)
{
	var opa=container._opacity;
	var newopa=container._morphinc+opa;
	var done=false;

	if(!container._visible)
	{
		mjs_setOpacity(container._overimg,0);
		mjs_show(container._overimg);
		container._visible=true;
	}

	if(container._morphinc>0)
	{
		if(newopa>100)
		{
			newopa=100;
			done=true;
		}
	}
	else
	{
		if(newopa<0)
		{
			newopa=0;
			done=true;
		}
	}

	if(newopa==0)
	{
		mjs_hide(container._overimg);
		container._visible=false;
	}
	else
	{
		mjs_setOpacity(container._overimg,newopa);
	}
	container._opacity=newopa;

	if(!done)
	{
		var id=mjs_allocateElementID(container);
		mjs_timedCall(gIMG_morphDelay,"imgMorph:"+id,_img_morph,container);
	}
	return true;
}

function _img_morphIn(container)
{
	container._morphinc=gIMG_morphInc;
	_img_morph(container);
	return true;
}

function _img_morphOut(container)
{
	container._morphinc=gIMG_morphDec;
	_img_morph(container);
	return true;
}


function _img_applyIE6pngtrick(img)
{
	var span=mjs_insertElement(img,"span");

	mjs_resize(span,mjs_width(img),mjs_height(img));
	span.style.display="inline-block";
	span.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader("+
		"src='"+img.src+"',sizingMethod='scale');";
	mjs_hide(img);
	return span;
}

function _img_parse(img)
{
	var node=img;
	var parent=img.parentNode;
	var attr;
	var href=mjs_attr(img,"mjshref");
	var doiepngtrick=false;

	if(mjs_isie)
	{
		// IE has problems with alpha transparency for years. IE7 fixed some,
		// but still has problem that makes it unsuitable in some case.
		// while able to deal with PNG32+alpha channel, IE7 is not able to mix
		// it with alpha(opacity). That's why trick below is used for any IE version
		// and not only IE<=6

		var name=img.src;
		if(name.substring(name.length-3,name.length).toUpperCase()=="PNG")
		{
			doiepngtrick=true;
		}
	}

	if(mjs_valued(attr=mjs_attr(img,"mjssrclist")))
	{
		// We have a list of src => animated images
		var list=attr.split("&");
		var count=list.length;
		var imlist=[];

		for(var i=0;i<count;i++)
		{
			imlist[i]=new Image();
			imlist[i].src=list[i];
		}

		// Starts with the first image of the serie
		img.src=list[0];

		var delay=mjs_attr(img,"mjsdelay");
		if(!mjs_valued(delay))
		{
			delay=gIMG_animateDelay;
		}

		// Keeps stuff handy
		img._mjsanimimages=imlist;
		img._mjsanimdelay=delay;
		img._mjsanimindex=0;

		// Arm the animation
		var id=mjs_allocateElementID(img);
		mjs_timedCall(delay,"imganimate"+id,_img_animate,img);
	}

	if(mjs_valued(attr=mjs_attr(img,"mjsoversrc")))
	{
		// We need a mouse-over image selection
		// => creates a DIV container with two images

		var dx=mjs_width(img),dy=mjs_height(img);
		var container=document.createElement('div');

		// Note: to position images within the container, the
		// container needs to have a relative positioning (don't ask why)

		container.style.position="relative";

		if(doiepngtrick)
		{
			img=_img_applyIE6pngtrick(img);
		}

		var overimg=document.createElement('img');
		overimg.src=attr;

		overimg.style.position="absolute";
		overimg.style.top="0px";
		overimg.style.left="0px";
		overimg.border=0;
		mjs_hide(overimg);

		img.style.position="absolute";
		img.style.top="0px";
		img.style.left="0px";
		img.border=0;

		mjs_resize(container,dx,dy);

		parent.insertBefore(container,img);
		parent.removeChild(img);

		if(mjs_valued(href))
		{
			// Add a surrounding hyperlink when necessary, and make
			// this link the container for both images
			container=mjs_addElement(container,"a");
			container.href=href;
		}

		container.appendChild(img);
		container.appendChild(overimg);

		if(doiepngtrick)
		{
			// More ugly IE<7 tricks - we need another SPAN level to apply the
			// opacity on the icon when morphing images
			overimg=_img_applyIE6pngtrick(overimg);
			overimg=mjs_insertElement(overimg,"span");
			overimg.style.position="absolute";
			mjs_resize(overimg,mjs_width(img),mjs_height(img));
			mjs_hide(overimg);
			if(mjs_valued(href))
			{
				overimg.style.cursor="hand";
			}
		}

		container._img=img;
		container._overimg=overimg;
		container._opacity=0;

		var effect=mjs_attr(img,"mjseffect");

		if(!mjs_valued(effect))
		{
			effect=gIMG_defaultEffect;
		}
		if(!strcmp(effect,"morph"))
		{
			mjs_setEventCallback("mouseover","imgover",container,_img_morphIn);
			mjs_setEventCallback("mouseout","imgout",container,_img_morphOut);
		}
		else if(!strcmp(effect,"none"))
		{
			mjs_setEventCallback("mouseover","imgover",container,_img_mouseOver);
			mjs_setEventCallback("mouseout","imgout",container,_img_mouseOut);
		}
		else
		{
			LOGERROR("unknown effect '%s'",effect);
		}
		node=container;
		parent=node.parentNode;
		doiepngtrick=false;
	}
	else if(mjs_valued(href))
	{
		// Simple image with hyperlink. Create the <a> element and
		// move the <img> inside
		var link=document.createElement('a');
		link.href=href;
		parent.insertBefore(link,node);
		parent.removeChild(node);
		link.appendChild(node);
		img.border=0;
		node=link;
	}
	if(doiepngtrick)
	{
		node=_img_applyIE6pngtrick(img);
	}
}

function _img_initialize()
{
	var list=mjs_lookupTags('img');

	for(var i=0;i<list.length;i++)
	{
		_img_parse(list[i]);
	}
}

mjs_registerModule("image",_img_initialize);

//--------------------------------------------------------------------------------
//
//	Public javascript API 
//
//--------------------------------------------------------------------------------

function mjs_setOverStateEffect(effectName)
{
	gIMG_defaultEffect=effectName;
}

