// JavaScript Document

function _tween_timerFunction(obj) {
	obj.timerFunction();	
}

function Tween(init) {
	
	var newtween = {
		/* public */
		from: 0,
		to: 1,
		time: 300,
		onFinish: null,
		update: function () { },
		onStart: null,
		mode: 0, // 0=normal, 1=pingpong		
		
		/* private */
		steps: 1,
		step: 0,
		timer: null,	
		isActive: function() {
			return (this.timer != null);
		},
		fps: 30,
		timerFunction: null,
		transition: function(x) { return 1 - Math.cos(Math.PI*x/2); },
		// transition: function(x) { return x; },		
		// transition: function(x) { return (Math.pow((2*x-1),3)+1)/2; },
					
		stepForward: function() {
			with(this) 
				if (step > (steps - 1)) {
					update(to);
					clearInterval(timer);
					timer = null;
					if(onFinish) onFinish();					
				} else {
					step ++;
					update(from + (to - from)*transition(step/steps));				
				}			
		},
		
		stepBackward: function() {
			with(this) 
				if (step <= 0) {
					update(from);
					clearInterval(timer);
					timer = null;
					if(onFinish) onFinish();					
				} else {
					step --;
					update(from + (to - from)*transition(step/steps));				
				}
		},
				
		programTimer: function() {
			if (this.timerFunction == null) {						
				var ref = this;
				this.timerFunction = function () {
					ref.stepFunction();
				}
			}
			this.timer = setInterval(this.timerFunction, this.time/this.steps);			
		},
		
			
		/* Public */			
		start: function() {		
			if (this.timer==null) {	
				with(this) {
					this.stepFunction = this.stepForward;
					if(onStart) onStart();
					steps = Math.round((fps*time) / 1000);
					step = 0;			
					update(from);			
					programTimer();					
				}				
			} else {
				this.stepFunction = this.stepForward;			
			}
		},
		
		rewind: function() {
			if (this.timer==null) {	
				with(this) {
					this.stepFunction = this.stepBackward;
					if(onStart) onStart();
					steps = Math.round((fps*time) / 1000);
					step = steps;			
					update(to);			
					programTimer();					
				}				
			} else {
				this.stepFunction = this.stepBackward;			
			}
		},
			
		stop: function() {
			if (this.timer) {
				clearInterval(this.timer);
				timer = null;
			}
		}
		
	}
	
	if(init) {
		for (var prop in init) 
			newtween[prop] = init[prop];					
	}
	
	return newtween;
}

/* inicia mvd_divslide */
if (typeof MVD == 'undefined') MVD = {};

MVD.extend = function (base, ext) {
	for (var i in ext) if (ext.hasOwnProperty(i)) {
		if (!base[i]) base[i] = ext[i];
	}
}

/* Objeto con la funcionalidad a aplicar a cada element (DOM) */
MVD.elemExtension = { 
	show: function() { this.style.display = ((this.nodeName == 'SPAN') || (this.nodeName == 'A') ) ? 'inline' : 'block'; return this; },
	hide: function() { this.style.display = 'none'; return this; },
	setHTML : function(txt) { this.innerHTML = txt; return this; },
	setZIndex : function (val) { this.style.zIndex = val; return this; },
	setOpacity : function(opacity) {
		opacity = (opacity >= 100) ? 99.99999 :opacity;
		this.style.filter = "alpha(opacity:"+opacity+")"; // IE/Win
		this.style.KHTMLOpacity = opacity/100; 	// Safari<1.2, Konqueror
		this.style.MozOpacity = opacity/100;  // Older Mozilla and Firefox
		this.style.opacity = opacity/100; // Safari 1.2, newer Firefox and Mozilla, CSS3
		return this;
	},
	hasClass : function(name) {
		var classes = this.className.split(" ");
		for(var i=0;i<classes.length;i++) {
			if (classes[i] === name) 
				return true;
		}
		return false;
	}
}

MVD.extendElement = function(el) {
	if(el) MVD.extend(el, MVD.elemExtension);
	return el;
}

MVD.get = function(id) {	
	return MVD.extendElement(document.getElementById(id));
}


MVD.DivSlide = function (id) {
	
	var obj = {	
		time : 14,
		setTime : function (newTime) {
			this.time = newTime;
			resetTimer();
		}
	}
	var btn = null;
	var slides = [];
	var curr = 0;
	var last = -1;	
	var slideTimer = null
		
	var fade = new Tween({
		from:0,
		to:100,
		time: 400,
		update: function (val) {
			slides[curr].setOpacity(val);
			if (last >= 0) slides[last].setOpacity(100 - val);
			},
		onFinish: function () {
			if (last >= 0) slides[last].setZIndex(0).hide();
			last = curr;			
			},
		onStart: function() {
			slides[curr].show().setZIndex(2);
			}
		});
		
	function view(nro) {
		var el, i;
		curr = nro;
		if (last >= 0) {			
			fade.start();
		} else {
			last = nro;
			slides[nro].setZIndex(1).show();
		}		
	}

	function nextSlide() {
		if (!fade.isActive()) {
			if (curr < (slides.length - 1)) {
				view(curr + 1);
			} else {
				view(0);
			}			
		}
		return false;
	}
	
	function previousSlide() {
		if (!fade.isActive()) {
			if (curr > 0) {
				view(curr - 1);
			} else {
				view(slides.length - 1);
			}						
		}
		return false;
	}
	 
	function setTimer() {
		if (slideTimer) {
			clearInterval(slideTimer);
		}
		slideTimer = setInterval(nextSlide, obj.time * 1000);		
	}
	
	/**
	 * Resetea el contador 
	 */	
	function resetTimer() {
		if (slideTimer) {
			setTimer();
		}
	}
	
	function stopTimer() {
		if (slideTimer) {
			clearInterval(slideTimer);
			slideTimer = null;
		}
	}
		
	function getSlides(cont) {
		slides = [];		
		var el;
		var list = cont.getElementsByTagName('div');
		for (var i=0;i<list.length;i++) {
			el = MVD.extendElement(list[i]);
			if (el.hasClass('divslide')) {
				slides.push(el);
				if (slides.length > 1) {
					el.hide();				
				} else {
					el.show();
				}
			}				
		}				
	}
	
	function getButtons(cont) {
		btn = {};					  
		var names = { slidePrevious : 'prev', slideNext : 'next', slidePause : 'pause', slidePlay : 'play' };
		var el, name;
		var list = cont.getElementsByTagName('a');
		
		for (var i=0;i<list.length;i++) {
			el = MVD.extendElement(list[i]);							
			for (name in names) {						
				if (el.hasClass(name)) {					
					btn[names[name]] = el;					
					break;
				}					
			}
		}		
	}
	
	function setupButtons(show) {
		if (show) {
			if(btn.prev) {
				btn.prev.onclick = function () {
					previousSlide();
					resetTimer();
					return false;
				}
			}
			if(btn.next) {
				btn.next.onclick = function () {
					nextSlide();
					resetTimer();
					return false;
				}			
			}	
			if(btn.pause) {
				btn.pause.onclick = function () {
					stopTimer();		
					btn.play.show();
					btn.pause.hide();
					return false;	
				}
			}
			if(btn.play) {			
				btn.play.onclick = function () {
					nextSlide();
					setTimer();
					btn.play.hide();
					btn.pause.show();
					return false;
				}
			}

		} else {
			if(btn.prev) btn.prev.hide();
			if(btn.next) btn.next.hide();
			if(btn.pause) btn.pause.hide();
			if(btn.play) btn.play.hide();			
		}
		
	}
	
	function init(id) {
		var cont = document.getElementById(id);		
		if (cont) {
			getSlides(cont);
			getButtons(cont);
			if (slides.length > 1) {
				view(0);
				// view(slides.length - 1);				
				setupButtons(true);				
				setTimer();
			} else {
				setupButtons(false);				
			}
		}
	}
	
	init(id);
	return obj;
}


