/* Metography Slide Show

ssInit(rID, rPath, rTotal, rWidth, rHeight, rLoop, rAlt, rTime, rFade, rFirst, rSuffix);

REQUIRED ARGUMENTS
	rID		=	id of containing div
	rPath	=	path to images + prefix (images = prefix01.jpg, prefix02.jpg, etc.)
	rTotal	=	total number of images
	rWidth	=	width of images
	rHeight	=	height of images
	
OPTIONAL ARGUMENTS
	rLoop	=	number of loops (0 = endless) [default = 2]
	rAlt	=	alt tag for all images [default = '']
	rTime	=	time to show image in milliseconds (1000 = 1 sec) [default = 3000]
	rFade	=	fade in milliseconds (1000 = 1 sec) [default = 2000]
	rFirst	=	number of first image to appear [default = 1]
	rSuffix	=	image suffix [default = 'jpg']

*/
/*global document, clearTimeout, setTimeout */


var vSS = { id:'', path:'', total:0, loop:2, time:3000, fade:2000, 
			on:1, count:0, cur:1, next:2, first:1, delta:2, speed:40, copac:100, nopac:0, pre:'ss-', tout:0 };

function ssOpacity(rID, rValue) {
	if (rValue > 100) { rValue = 100; }
	if (rValue < 0) { rValue = 0; }
    var tStyle = document.getElementById(rID).style,
		tValue = (rValue / 100);
    tStyle.opacity = tValue; 
    tStyle.MozOpacity = tValue; 
    tStyle.KhtmlOpacity = tValue; 
    tStyle.filter = 'alpha(opacity=' + rValue + ')'; 
} 

function ssPad(rNum) {
	if ((parseInt(rNum, 10) > 0) && (parseInt(rNum, 10) < 10)) { rNum = ("0" + rNum); }
	return (rNum);
}

function ssLoop() {
	if (vSS.loop === 0) {
		return (true);
	} else if (vSS.cur != vSS.first) {
		return (true);
	} else if (++vSS.count < vSS.loop) {
		return (true);
	} else {
		vSS.on = 0;
		return (false);
	}
}

function ssFade() {
	ssOpacity((vSS.pre + ssPad(vSS.cur)), vSS.copac-=vSS.delta);
	ssOpacity((vSS.pre + ssPad(vSS.next)), vSS.nopac+=vSS.delta);
	if (vSS.nopac >= 100) {
		vSS.copac = 100;
		vSS.nopac = 0;
		document.getElementById(vSS.pre + ssPad(vSS.cur)).style.display = 'none';
		vSS.cur = vSS.next;
		if (ssLoop()) {
			vSS.tout = setTimeout(ssNext, vSS.time);
		}
	} else {
		vSS.tout = setTimeout(ssFade, vSS.speed);
	}
}

function ssNext(rNumber) {
	if (vSS.on) {
		if (typeof(rNumber) != 'undefined' && rNumber !== 0) { 
			vSS.next = rNumber;
		} else {
			vSS.next = (vSS.cur + 1);
		}
		if (vSS.next > vSS.total) { vSS.next = 1; }
		ssOpacity((vSS.pre + ssPad(vSS.next)), 0);
		document.getElementById(vSS.pre + ssPad(vSS.next)).style.display = 'block';
		ssFade();
	}
}

function ssStop(rNumber) {
	var t, tID;
	clearTimeout(vSS.tout);
	if (typeof(rNumber) != 'undefined' && rNumber !== 0) { 
		for (t=1; t<=vSS.total; t++) {
			tID = (vSS.pre + ssPad(t));
			ssOpacity(tID, 0);
			document.getElementById(tID).style.display = 'none';
		}
		vSS.cur = rNumber;
		vSS.copac = 100;
		vSS.nopac = 0;
		ssOpacity((vSS.pre + ssPad(vSS.cur)), 100);
		document.getElementById(vSS.pre + ssPad(vSS.cur)).style.display = 'block';
	}
}

function ssInit(rID, rPath, rTotal, rWidth, rHeight, rLoop, rAlt, rTime, rFade, rFirst, rSuffix) {
	var t, tHTML = '';
	vSS.id = rID;
	vSS.path = rPath;
	vSS.total = parseInt(rTotal, 10);
	if (typeof(rLoop) != 'undefined' && rLoop !== 0) { vSS.loop = parseInt(rLoop, 10); }
	if (typeof(rAlt) == 'undefined' || rAlt === 0) { rAlt = ''; }	
	if (typeof(rTime) != 'undefined' && rTime !== 0) { vSS.time = parseInt(rTime, 10); }
	if (typeof(rFade) != 'undefined' && rFade !== 0) { vSS.fade = parseInt(rFade, 10); }
	if (typeof(rFirst) != 'undefined' && rFirst !== 0) { vSS.cur = parseInt(rFirst, 10); }
	if (typeof(rSuffix) == 'undefined' || rSuffix === 0 || rSuffix === '') { rSuffix = 'jpg'; }
	vSS.speed = parseInt(vSS.fade / (100 / vSS.delta), 10);
	vSS.first = vSS.cur;
	for (t=1; t<=vSS.total; t++) {
		tHTML += '<' + 'div style="position:absolute; display:';
		tHTML += (t == vSS.cur) ? 'block' : 'none';
		tHTML += '" id="' + vSS.pre + ssPad(t) + '"><';
		tHTML += 'img src="' + vSS.path + ssPad(t) + '.' + rSuffix + '" alt="' + rAlt + '" /></' + 'div>';
	}
	document.getElementById(rID).style.position = 'relative';
	document.getElementById(rID).style.width = rWidth + 'px';
	document.getElementById(rID).style.height = rHeight + 'px';
	document.getElementById(rID).innerHTML = tHTML;
	vSS.tout = setTimeout(ssNext, vSS.time);
}

