/*****

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html


*****/


window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false, transitioning=false;
var quotes = new Array(), curQuote = nxtQuote = -1, canFade = -1;

function so_init() {
	if(!d.getElementById || !d.createElement)return;
	
	css = d.createElement("link");
	css.setAttribute("href","/css/xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);

	quotes = getElementsByClassName("testimonialBlock", "div", d);
	for(i=0, j=quotes.length; i<j; ++i) {quotes[i].xOpacity = 0; quotes[i].style.display = 'none';}
	
	curQuote = chooseQuote();
	quotes[curQuote].style.display = "block";
	quotes[curQuote].xOpacity = .99;
	quotes[curQuote].style.opacity = quotes[curQuote].xOpacity;
	quotes[curQuote].style.MozOpacity = quotes[curQuote].xOpacity;
	quotes[curQuote].style.filter = "none";

	setTimeout(so_xfade,5000);
}

function chooseQuote() {
	if (quotes.length > 1) {
		var a = nextQuote = 0;
		do {
/*			nextQuote = 0 + (Math.round(quotes.length * 10 * Math.random()));
			nextQuote = Math.round(nextQuote / 10);
*/
			nextQuote = 0 + (Math.floor(quotes.length * Math.random()));
		} while (nextQuote == curQuote || nextQuote >= quotes.length);
		
console.log('calculated nextQuote index:'+nextQuote+' from a stack of '+quotes.length)
		return nextQuote;
	} else {
		return 0;
	}
}

function so_xfade() {
	var increment = .05, ieExtra = '';

	if (! transitioning) {
		nxtQuote = chooseQuote();
	} else {
		if (quotes[curQuote].xOpacity == 0) {
			quotes[curQuote].style.display = 'none';
		}
	}
	
	var cOpacity = quotes[curQuote].xOpacity;
	var nOpacity = quotes[nxtQuote].xOpacity;

	if (nOpacity > .64)	{
		increment = 0.1;
	} else {
		increment = 0.05;
	}
	cOpacity-=increment;
	nOpacity+=increment;

	quotes[curQuote].xOpacity = cOpacity;
	quotes[nxtQuote].xOpacity = nOpacity;
	quotes[nxtQuote].style.display = "block";
	setOpacity(quotes[curQuote]); 
	setOpacity(quotes[nxtQuote]);
	
	if(cOpacity<=0) {
		quotes[curQuote].style.display = "none";
		curQuote = nxtQuote;
		
		transitioning = false;
		setTimeout(so_xfade,5000);
	} else {
		transitioning = true;
		setTimeout(so_xfade,50);
	}
	
	function setOpacity(obj) {
		if (canFade == -1) {
			if (navigator.userAgent.indexOf('Opera') != -1) {
				v = parseFloat(navigator.userAgent.replace(/^.*Opera ([0-9\.]+).*$/, '$1'));
				if (v < 9) {
					canFade = 0;
				}
			}else if(navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Mac') != -1) {
				canFade = 0;
			}else{
				canFade = 1;
			}
		}

		if (canFade) {
			var ieOpacity = obj.xOpacity;
			if(obj.xOpacity>.95) {
				obj.xOpacity = .99;
				obj.style.filter = "none";
				return;
			}

			if (obj.xOpacity > 0.9 && obj.tagName != 'IMG') {
				ieExtra = '';
			} else {
				ieExtra = '';
			}
			
			obj.style.opacity = obj.xOpacity;
			obj.style.MozOpacity = obj.xOpacity;
			obj.style.filter = "alpha(opacity=" + (obj.xOpacity * 100) + ", style=0)" + ieExtra;
		} else {
			if(obj.xOpacity>.99) {
				obj.xOpacity = .99;
				obj.style.display = 'block';
				return;
			}
			obj.xOpacity = -.01;
		}
	}
	
}

function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = (strTag == '*' && document.all) ? document.all : objContElm.getElementsByTagName(strTag);
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (k = 0, l = arrObjClass.length; k < l; k++) {
      for (m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}

// To cover IE 5.0's lack of the push method
Array.prototype.push = function(value) {
  this[this.length] = value;
}