// IXF1.0 :: Image cross-fade 
//******************************************************
//global object
var ixw = { 'clock' : null, 'count' : 1 }
/*******************************************************

/*****************************************************************************
*****************************************************************************/

function load_images(IMGS)
{
	ixw.imgs = IMGS;
	//cache the images
	ixw.imgsLen = ixw.imgs.length;
	ixw.cache = [];
	for(var i=0; i<ixw.imgsLen; i++)
	{
		ixw.cache[i] = new Image;
		ixw.cache[i].src = ixw.imgs[i];
	}
	updatePages(1, ixw.cache.length);
}


//crosswipe setup function
function crosswipe()
{
	//if the timer is not already going
	if(ixw.clock == null)
	{
		//copy the image object 
		ixw.obj = arguments[0];
		
		//get its dimensions
		ixw.size = { 'w' : ixw.obj.width, 'h' : ixw.obj.height };
		
		//copy the image src argument 
		ixw.src = arguments[1];
		
		//change the image alt text if defined
		if(typeof arguments[4] != 'undefined' && arguments[4] != '')
		{
			ixw.obj.alt = arguments[4];
		}

		//if dynamic element creation is supported
		if(typeof document.createElementNS != 'undefined' || typeof document.createElement != 'undefined')
		{
			//create a new image object and append it to body
			//detecting support for namespaced element creation, in case we're in the XML DOM
			ixw.newimg = (typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img');
			
			//set positioning classname
			ixw.newimg.style.position = "absolute";
			ixw.newimg.style.zIndex = 30000;
			ixw.newimg.style.visibility = "hidden";

			//set src to new image src
			ixw.newimg.src = ixw.src

			//move it to superimpose original image
			ixw.newimg.style.left = ixw.getRealPosition(ixw.obj, 'x') + 'px';
			ixw.newimg.style.top = ixw.getRealPosition(ixw.obj, 'y') + 'px';

			document.getElementsByTagName('body')[0].appendChild(ixw.newimg);
			
			//set it to be completely hidden with clip
			ixw.newimg.style.clip = 'rect(0, 0, 0, 0)';

			//show the image 
			ixw.newimg.style.visibility = 'visible';

			//copy and convert fade duration argument 
			ixw.length = parseInt(arguments[2], 10) * 1000;

			//create fade resolution argument as 20 steps per transition
			ixw.resolution = parseInt(arguments[2], 10) * 20;

			//copy slide direction argument
			ixw.dir = arguments[3];

			//start the timer
			ixw.clock = setInterval('ixw.crosswipe()', ixw.length/ixw.resolution);
		}
		
		//otherwise if dynamic element creation is not supported
		else
		{
			//just do the image swap
			ixw.obj.src = ixw.src;
		}
		
	}
};


//crosswipe timer function
ixw.crosswipe = function()
{
	//decrease the counter on a linear scale
	ixw.count -= (1 / ixw.resolution);
	
	//if the counter has reached the bottom
	if(ixw.count < (1 / ixw.resolution))
	{
		//clear the timer
		clearInterval(ixw.clock);
		ixw.clock = null;
		
		//reset the counter
		ixw.count = 1;
		
		//set the original image to the src of the new image
		ixw.obj.src = ixw.src;
	}
	
	//animate the clip of the new image
	//using the width and height properties we saved earlier
	ixw.newimg.style.clip = 'rect('
		+ ( (/bt|bltr|brtl/.test(ixw.dir)) ? (ixw.size.h * ixw.count) : (/che|cc/.test(ixw.dir)) ? ((ixw.size.h * ixw.count) / 2) : (0) )
		+ 'px, '
		+ ( (/lr|tlbr|bltr/.test(ixw.dir)) ? (ixw.size.w - (ixw.size.w * ixw.count)) : (/cve|cc/.test(ixw.dir)) ? (ixw.size.w - ((ixw.size.w * ixw.count) / 2)) : (ixw.size.w) )
		+ 'px, '
		+ ( (/tb|tlbr|trbl/.test(ixw.dir)) ? (ixw.size.h - (ixw.size.h * ixw.count)) : (/che|cc/.test(ixw.dir)) ? (ixw.size.h - ((ixw.size.h * ixw.count) / 2)) : (ixw.size.h) )
		+ 'px, '
		+ ( (/lr|tlbr|bltr/.test(ixw.dir)) ? (0) : (/tb|bt|che/.test(ixw.dir)) ? (0) : (/cve|cc/.test(ixw.dir)) ? ((ixw.size.w * ixw.count) / 2) : (ixw.size.w * ixw.count) ) 
		+ 'px)';
			
	//keep new image in position with original image
	//in case text size changes mid transition or something
	ixw.newimg.style.left = ixw.getRealPosition(ixw.obj, 'x') + 'px';
	ixw.newimg.style.top = ixw.getRealPosition(ixw.obj, 'y') + 'px';
	
	//if the counter is at the top, which is just after the timer has finished
	if(ixw.count == 1)
	{
		//remove the duplicate image
		ixw.newimg.parentNode.removeChild(ixw.newimg);
	}
};



//get real position method
ixw.getRealPosition = function()
{
	this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	this.tmp = arguments[0].offsetParent;
	while(this.tmp != null)
	{
		this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}
	
	return this.pos;
};

/* ------------------------- */

var CURRENT_SLIDE = 0;

function nextSlide(id,alt,sec)
{
	if(!alt) alt = "";
	if(!sec) sec = 1;
	
	//FILTER: ; 
	
	img = document.getElementById(id);
	CURRENT_SLIDE++;
	if(CURRENT_SLIDE+1 > ixw.cache.length)
		CURRENT_SLIDE = 0;
	
	if(img.filters)
	{
		img.style.filter = "progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=0.50,wipestyle=0,motion=reverse,duration="+sec+")";
		img.filters[0].apply();
		img.src = ixw.cache[CURRENT_SLIDE].src;
		img.filters[0].play();
	}
	else
		crosswipe(img, ixw.cache[CURRENT_SLIDE].src, sec, 'rl', alt);
		
	updatePages(CURRENT_SLIDE+1, ixw.cache.length);
}

function previousSlide(id,alt,sec)
{
	if(!alt) alt = "";
	if(!sec) sec = 1;
	
	img = document.getElementById(id);
	CURRENT_SLIDE--;
	if(CURRENT_SLIDE < 0)
		CURRENT_SLIDE = ixw.cache.length - 1;

	if(img.filters)
	{
		img.style.filter = "progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.00,wipestyle=0,motion=forward,duration="+sec+")";
		img.filters[0].apply();
		img.src = ixw.cache[CURRENT_SLIDE].src;
		img.filters[0].play();
	}
	else
		crosswipe(img, ixw.cache[CURRENT_SLIDE].src, sec, 'lr', alt);
	
	updatePages(CURRENT_SLIDE+1, ixw.cache.length);
}

function updatePages(page, nb_page)
{
	document.getElementById("pagination").innerHTML = nb_page + " " + page;
}


/* ------------------------- */
/* PARTIES CONTENUS
*/
CURRENT_PAGE = 0;

function load_pages(PAGE)
{
	ixw.pages = Array();
	ixw.pages = PAGE.split('<hr />');
	document.getElementById("ZONE_CONTENU").innerHTML = ixw.pages[0];
	updatePages(1, ixw.pages.length);
}


function nextPage(sec)
{
	if(!sec) sec = 1;
	
	//FILTER: ; 
	zone = document.getElementById("ZONE_CONTENU");
	CURRENT_PAGE++;
	if(CURRENT_PAGE+1 > ixw.pages.length)
		CURRENT_PAGE = 0;
	
	if(zone.filters)
	{
		zone.style.filter = "progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=0.50,wipestyle=0,motion=reverse,duration="+sec+")";
		zone.filters[0].apply();
		zone.innerHTML = ixw.pages[CURRENT_PAGE];
		zone.filters[0].play();
	}
	else
		zone.innerHTML = ixw.pages[CURRENT_PAGE];
		
	updatePages(CURRENT_PAGE+1, ixw.pages.length);
}


function previousPage(sec)
{
	if(!sec) sec = 1;
	
	//FILTER: ; 
	zone = document.getElementById("ZONE_CONTENU");
	CURRENT_PAGE--;
	if(CURRENT_PAGE < 0)
		CURRENT_PAGE = ixw.pages.length - 1;
	
	if(zone.filters)
	{
		zone.style.filter = "progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=0.50,wipestyle=0,motion=forward,duration="+sec+")";
		zone.filters[0].apply();
		zone.innerHTML = ixw.pages[CURRENT_PAGE];
		zone.filters[0].play();
	}
	else
		zone.innerHTML = ixw.pages[CURRENT_PAGE];
		
	updatePages(CURRENT_PAGE+1, ixw.pages.length);
}
