/* To improve :
- Use event listener for links, put button instead links I think would be better
- Load picture with ajax
*/

var obj = Array();

function ie6(){return typeof ie != 'undefined';}

var slides = null;
var slideser = null;

function getID(elt){if(document.getElementById(elt)) return document.getElementById(elt); else return null;}

function insertAfter(elt, data){getID(elt).parentNode.insertBefore(data, getID(elt).nextSibling);}

function slideShow(dur, slid, slidec, htag, slides)
{
	this.id = obj.length;
	this.idl = null;
	this.currentPic = 0;
	this.nbPic = 0;
	this.slideTime = null;
	this.dur = dur;
	this.slid = slid;
	this.slidec = slidec;
	this.htag = htag;
	this.scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};
	obj.push(this);

	this.constructor(slides);
};

slideShow.prototype =
{
	constructor: function(slides)
	{
		if(document.getElementById(this.slid))
			if(img = document.getElementById(this.slid).getElementsByTagName(this.htag))
			{
				this.nbPic = img.length;
				this.idl = img.item(0).getAttribute('id').substr(0, 1);
				
				if(slides == true) this.slideShow();
				var sd = document.getElementById(this.slid);

				var sdw = document.getElementById(this.slidec).offsetWidth;
				this.navArrows();

				if(document.all) sd.style.width = (sdw*this.nbPic)+'px';
				else sd.setAttribute('style', 'width: '+(sdw*this.nbPic)+'px;');
			}
	},

	toString: function()
	{
		return "Pradeo "+this.nbPic;	
	},

	slideShow: function()
	{
		this.slideTime = setInterval('obj['+this.id+'].ArScroll("slide");', 1000*this.dur);
	},

	createArrow: function(posit)
	{
		var arr = document.createElement('a');

		if(document.all)
		{
			arr.className = posit+'_arrow';
			arr.href = 'javascript:obj['+this.id+'].ArScroll(\''+posit+'\');';
		}
		else
		{
			arr.setAttribute('class', posit+'_arrow');
			arr.setAttribute('href', 'javascript:obj['+this.id+'].ArScroll(\''+posit+'\');');
		}
		insertAfter(this.slidec, arr);
	},
	
	navArrows: function()
	{
		this.createArrow('right');
		this.createArrow('left');
	},

	clearShow: function()
	{
		if(this.slideTime) clearInterval(this.slideTime);	
	},	

	ArScroll: function(direction)
	{
		if(this.nbPic > 0)
		{
			if(direction != 'slide' && this.slideTime != null)
			{
				this.clearShow();
				this.slideShow();
			}
	
			if(direction == 'left') this.currentPic = this.currentPic-1 < 0 ? this.nbPic-1 : this.currentPic-1;
			else this.currentPic = this.nbPic > this.currentPic+1 ? this.currentPic+1 : 0;

			theScroll = document.getElementById(this.slidec);
			position = slideShow.findElementPos(document.getElementById(this.idl+this.currentPic));
	
			offsetPos = slideShow.findElementPos(document.getElementById(this.idl+'0'));
			position[0] = position[0] - offsetPos[0];
	
			this.scrollStart(theScroll, theScroll.scrollLeft, position[0]);
		}
	},

	scrollStart: function(elem, start, end)
	{
		if(this.scrollanim.timer != null)
		{
			clearInterval(this.scrollanim.timer);
			this.scrollanim.timer = null;
		}

		this.scrollanim.time = 0;
		this.scrollanim.begin = start;
		this.scrollanim.change = end-start;
		this.scrollanim.duration = 15;
		this.scrollanim.element = elem;

		this.scrollanim.timer = setInterval('obj['+this.id+'].Hscroll();', 17);
	},

	Hscroll: function()
	{
		this.Eltscroll('horizontal');
	},

	/* To adapt */
	Vscroll: function()
	{
		this.Eltscroll('vertical');
	},

	Eltscroll: function(direction)
	{
		if(this.scrollanim.time > this.scrollanim.duration)
		{
			clearInterval(this.scrollanim.timer);
			this.scrollanim.timer = null;
		}
		else
		{
			if(direction == 'horizontal')
			{
				var move = slideShow.CircularEa(this.scrollanim.time, this.scrollanim.begin, this.scrollanim.change, this.scrollanim.duration);
				this.scrollanim.element.scrollLeft = move;
			}
			else
			{
				var move = slideShow.sineInOut(this.scrollanim.time, this.scrollanim.begin, this.scrollanim.change, this.scrollanim.duration);
				this.scrollanim.element.scrollTop = move; 	
			}
			this.scrollanim.time++;
		}
	}
};

slideShow.findElementPos = function(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	
	do
	{
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while(elemFind = elemFind.offsetParent);

	return Array(elemX, elemY);
};


/*
	Easing equation from http://www.robertpenner.com/easing_terms_of_use.html
	t = time, b = begin, c = change, d = duration

	time = current frame,
	begin is fixed,
	change is generally finish - begin,
	duration is fixed (frames)
*/
slideShow.CircularEa = function(t, b, c, d)
{
    return c*((t=t/d-1)*t*t + 1) + b;
}

slideShow.sineInOut = function(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

