/**
 * ioFadeFlow
 * 
 * Simple image gallery. 
 * Boris Cruchet.
 * 
 */

var ioTimer = null;
var i = 0;
var imagenes = new Array();
var map = new Array();

function initFlow(conf)
{
	var j = 0;
	var items = "";

	if (conf.id    == undefined) { return 0 };
	if (conf.timer == undefined) { conf.timer = 3000;}
	if (conf.fadet == undefined) { conf.fadet = 1000;}
	
	$("#"+conf.id).css("width", conf.width);
	$("#"+conf.id).css("height", conf.height);
	
	$("#"+conf.id+" img").each(function() {
		imagenes.push(this.src);
		map.push($(this).attr("usemap"));
	});
	
	if (imagenes.length > 1) 
	{
		for (j=0; j < imagenes.length; j++)
		{
			items +="<span ";
			
			if (j == 0) {
				items += " class=\"ioFlowItem ioFlowItemSelected\" ";
			}
			else {
				items += " class=\"ioFlowItem\" ";
			}
			items += ">"+(j+1)+"</span>";
		}
		$("#"+conf.id+"_").html(items);
		$("#"+conf.id+"_").addClass("ioFlowItems");
		
		$("#"+conf.id+"_ span").each(function(ind, ele){
			$(this).click( function() {
				i = ind;
				doSelect(conf);
			});
		});
			
		$("#"+conf.id).html("<img src=\""+imagenes[i]+"\" usemap=\""+map[i]+"\" border=\"0\"/>");
		doTimer(conf);
	}
}

function doTimer(conf)
{
	var next = 0;

	if(ioTimer != null) clearInterval(ioTimer);

	ioTimer = setInterval(function()
	{
		$("#"+conf.id).html("<img src=\""+imagenes[i]+"\" usemap=\""+map[i]+"\" border=\"0\"/>");

		$("#"+conf.id+"_ span").each(function(ind, ele)
		{
			if (ind == (i+1)%imagenes.length) {
				$(this).addClass("ioFlowItemSelected");
			}
			else {
				$(this).removeClass("ioFlowItemSelected");
			}
		});
		
		i = (i+1)%imagenes.length;

		$("#"+conf.id).css("background-image", "url('"+imagenes[i]+"')");
		$("#"+conf.id+" img").fadeOut(conf.fadet);
		
		setTimeout(function()
		{
			$("#"+conf.id).html("<img src=\""+imagenes[i]+"\" usemap=\""+map[i]+"\" border=\"0\"/>");
		}, conf.fadet);
		//doNext(conf);
	}, conf.timer);
}

function doSelect(conf)
{
	$("#"+conf.id).html("<img src=\""+imagenes[i]+"\" usemap=\""+map[i]+"\" border=\"0\"/>");
	
	$("#"+conf.id+"_ span").each(function(ind, ele)
	{
		if (ind == (i)%imagenes.length)
		{
			$(this).addClass("ioFlowItemSelected");
		}
		else
		{
			$(this).removeClass("ioFlowItemSelected");
		}
	});
	
	doTimer(conf);
}
