/*
 * Written by Stefano Mazzocchi <stefanom at mit.edu>
 * With code borrowed from http://www.splintered.co.uk/experiments/archives/javascript_fade/fade.js
 */

var delayInSeconds = 5;

var index;
var size;
var items;
var keepGoing;
var opacity;
var displayedItem;
var itemToDisplay;
var idle = true;
var items2;
var displayedItem2;
var itemToDisplay2;
var slideshowsecundaria;
var withlinks;
var links;
var linkToNavigate;
var enablePause=false;
var staticInit=false;
var ciclying=true;
var withControls=true;


addEvent(window,"load",slideshow);


function slideshow() {
	if(!document.getElementById("slideshow"))
	{
		return;
	}
	
	if(document.getElementById("slideshowconfig"))
	{
		if(document.getElementById("staticInit"))
		{
			staticInit=(document.getElementById("staticInit").innerHTML == 'true');
		}
		if(document.getElementById("ciclying"))
		{
			ciclying=(document.getElementById("ciclying").innerHTML == 'true');
		}
		if(document.getElementById("withControls"))
		{
			withControls=(document.getElementById("withControls").innerHTML == 'true');
		}
	}
			
	items = getItems();
	size = items.length;
	slideshowsecundaria=0;
	if(document.getElementById("slideshow2"))
	{
		slideshowsecundaria=1;
		items2 = getItems2();
	}
		
	withlinks=false;
	if(document.getElementById("slideshowlinks"))
	{
		withlinks=true;
		links = getLinks();
	}
	
	/*index = Math.floor(Math.random() * (size - 1)); //comienzo aleatorio */
	if(staticInit == true)
	{
		index = 0;
		opacity=100;
		itemToDisplay=items[0];
		itemToDisplay.style.display = "block";
		displayedItem=itemToDisplay;
		window.setTimeout("start()", 1000 * delayInSeconds);
	}
	else
	{
		index = -1;
		start();
	}
}

function getItems() 
{
	var items = [];
	var divs = document.getElementById("slideshow").getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++) 
	{
		if (divs[i].className == "item") items.push(divs[i]);
	}
	return items;
}

function getItems2() 
{
	var items = [];
	var divs = document.getElementById("slideshow2").getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++) 
	{
		if (divs[i].className == "item") items.push(divs[i]);
	}
	return items;
}

function getLinks() 
{
	var links = [];
	var as = document.getElementById("slideshowlinks").getElementsByTagName("a");
	for (var i = 0; i < as.length; i++) 
	{
		links.push(as[i]);
	}
	return links;
}

function start() 
{
	if(withControls == true)
	{
		var tables = document.getElementById("slideshow").getElementsByTagName("table");
		for (var i = 0; i < tables.length; i++) 
		{
			var id=tables[i].getAttribute('id');
			if(id != 'slideshowconfig')
				tables[i].style.display = "block";
		}
	
		document.getElementById("stop").style.display = "inline";
		if(enablePause && document.getElementById("pause"))
		{
			document.getElementById("pause").style.display = "inline";
			document.getElementById("separador").style.display = "inline";
		}
		
		document.getElementById("start").style.display = "none";
		document.getElementById("previous").style.display = "none";
		document.getElementById("next").style.display = "none";
	}
	keepGoing = true;	
	beat();
}

function stop() 
{
	document.getElementById("stop").style.display = "none";
	if(document.getElementById("pause"))
	{
		document.getElementById("pause").style.display = "none";
		document.getElementById("separador").style.display = "none";
	}
	if(enablePause)
	{	
		document.getElementById("start").style.display = "inline";
		document.getElementById("previous").style.display = "inline";
		document.getElementById("next").style.display = "inline";
	}
	keepGoing = false;
	if(withlinks)
	{
		linkToNavigate = links[index];
		/*location.href="http://javascript.internet.com";*/
		if (linkToNavigate.nodeName == 'A')
		{
			var att=linkToNavigate.getAttribute('HREF');
			location.href=att;	
		}
			
	}
}

function pause() 
{
	document.getElementById("stop").style.display = "none";
	document.getElementById("pause").style.display = "none";
	document.getElementById("separador").style.display = "none";
	
	document.getElementById("start").style.display = "inline";
	document.getElementById("previous").style.display = "inline";
	document.getElementById("next").style.display = "inline";
	keepGoing = false;
}

function beat() 
{
	if (keepGoing) 
	{
		next();
		window.setTimeout("beat()", 1000 * delayInSeconds);
	}
}

function next() 
{
	if (index < size - 1) 
	{
		show1(index + 1);
	} 
	else 
	{
		if(ciclying)
			show1(0);
	}
}

function previous() 
{
	if (index > 0) 
	{
		show1(index - 1); 
	} 
	else 
	{
		show1(size - 1);
	}
}

/*cuando se llega a estado idle, elección elemento a presentar y tipo de presentación (fade-in o fade-out)*/
function show1(newIndex) 
{
	if (idle) 
	{
		idle = false;
		index = newIndex;
		goOn = show2;
		if (displayedItem) 
		{
			opacity = 100;
			fadeOut(); // this will call goOn() when done fading
		} 
		else 
		{
			goOn();
		}
	}
}

/*presentacion de elemento*/
function show2() 
{
	goOn = show3;
	
	opacity = 0;
	itemToDisplay = items[index];
	setOpacity(itemToDisplay, opacity);
	itemToDisplay.style.display = "block";
	
	if(slideshowsecundaria)
	{
		itemToDisplay2 = items2[index];
		setOpacity2(itemToDisplay2, opacity);
		itemToDisplay2.style.display = "block";
	}
	
	fadeIn(); // this will call goOn() when done fading
	
	
}

function show3() 
{
	displayedItem = itemToDisplay; // this is the end of our item showing routine
	
	if(slideshowsecundaria)
	{
		displayedItem2 = itemToDisplay2; // this is the end of our item showing routine
	}
	
	idle = true;
}
	
function fadeIn() 
{
    opacity += 10;
    	/*no excluir IE
	if (opacity <= 100 && !document.all) {*/
	if (opacity <= 100) 
	{
		setOpacity(itemToDisplay, opacity);
		if(slideshowsecundaria)
		{
			setOpacity2(itemToDisplay2, opacity);
		}
		
		window.setTimeout("fadeIn()", 50);
	} 
	else 
	{
		if (goOn) goOn();
	}
}


function fadeOut() {
    opacity -= 10;
	/*no excluir IE
	if (opacity >= 0 && !document.all) */
	if (opacity >= 0) 
	{
		setOpacity(displayedItem, opacity);
		if(slideshowsecundaria)
		{
			setOpacity2(displayedItem2, opacity);
		}
		
		window.setTimeout("fadeOut()", 50);
	} 
	else 
	{
		displayedItem.style.display = "none";
		if(slideshowsecundaria)
		{
			displayedItem2.style.display = "none";
		}
		
		if (goOn) goOn();
	}
}

function setOpacity(target, opacity) {
	if (target && opacity <= 100) {
	
		if (target.style.opacity != null) { // CSS3 compatible
			target.style.opacity = (opacity / 100) - 0.001;
		} else if (target.style.MozOpacity != null) { // Mozilla's pre-CSS3 proprietary rule
			target.style.MozOpacity = (opacity / 100) - 0.001;
			// the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1
		}
		// yes, I know that even IE has a proprietary opacity filter, but unfortunately 
		// it works fine on images, but screws up text rendering completely, so I'm not using it.
		//MCUH 
		//cogido de http://www.splintered.co.uk/experiments/archives/javascript_fade/fade.js
		else if (target.style.filter!=null) 
		{
			/* IE's proprietary filter */
			//target.style.filter = "alpha(opacity="+opacity+")";
			for (var j=0;j<target.childNodes.length;j++)
			{
				var div = target.childNodes[j];
				if(div.childNodes == 0)
				{
					continue;
				}
				var clase=div.getAttribute('className');
				if(clase == 'icon' || clase == 'iconLong')
				{
					for (var k=0;k<div.childNodes.length;k++)
					{
						var divWk = div.childNodes[k];
						if(divWk.childNodes == 0)
						{
							continue;
						}
						//div wk
						for (var p=0;p<divWk.childNodes.length;p++)
						{
							var span = divWk.childNodes[p];
							if(span.childNodes == 0)
							{
								continue;
							}
							//span
							for (var w=0;w<span.childNodes.length;w++)
							{
								var img = span.childNodes[w];
								if(img.nodeName == 'IMG')
								{
									//cambia el filtro en la img
									img.style.filter = "alpha(opacity="+opacity+")";
									continue;
								}
							}
						}
					}	
				}
				else if(clase == 'description')
				{
					for (var k=0;k<div.childNodes.length;k++)
					{
						var divWk = div.childNodes[k];
						if(divWk.getAttribute('className') != 'wk')
						{
							continue;
						}
						//div wk
						for (var p=0;p<divWk.childNodes.length;p++)
						{
							var span = divWk.childNodes[p];
							if(span.nodeName != 'SPAN')
							{
								continue;
							}							
							//cambia el filtro en los span de la div class="description"
							span.style.filter = "alpha(opacity="+opacity+")";
							/*the element needs to be block-level with either a width or height specified. 
							Alternatively, the element will also accept the filter if its display property is set to inline-block without the need for specified dimensions*/
							span.style.display = "inline-block";
						}
					}
				}
			}
			
			/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
		}
	}
}

function setOpacity2(target, opacity) {
	if (target && opacity <= 100) {
		if (target.style.opacity != null) { // CSS3 compatible
			target.style.opacity = (opacity / 100) - 0.001;
		} else if (target.style.MozOpacity != null) { // Mozilla's pre-CSS3 proprietary rule
			target.style.MozOpacity = (opacity / 100) - 0.001;
			// the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1
		}
		// yes, I know that even IE has a proprietary opacity filter, but unfortunately 
		// it works fine on images, but screws up text rendering completely, so I'm not using it.
		//MCUH 
		//cogido de http://www.splintered.co.uk/experiments/archives/javascript_fade/fade.js
		else if (target.style.filter!=null) 
		{
			/* IE's proprietary filter */
			//target.style.filter = "alpha(opacity="+opacity+")";
			for (var j=0;j<target.childNodes.length;j++)
			{
				var child = target.childNodes[j];
				if (child.nodeName == 'H1')
				{
					child.style.filter = "alpha(opacity="+opacity+")";
					continue;
				}
				else if (child.nodeName == 'DIV')
				{
					var att=child.getAttribute("className");
					if(att == 'listaImagenes')
					{
						for(var i=0;i<child.childNodes.length;i++)
						{
							var divWk = child.childNodes[i];
							if(divWk.nodeName == 'DIV')
							{
								for (var k=0;k<divWk.childNodes.length;k++)
								{
									var img=divWk.childNodes[k];
									if(img.nodeName == 'IMG')
									{
										//cambia el filtro en la img
										img.style.filter = "alpha(opacity="+opacity+")";
									}
								}
							}
						}
					}
					else if(att == 'wk')
					{
						child.style.filter = "alpha(opacity="+opacity+")";
					}
				}
			}
			
			/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
		}
	}
}

function addEvent(elm, evType, fn, useCapture)  {
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	}
} 
