/*
	Scripts for ItbiteZ
	Design by Solid Cactus 2008
	www.SolidCactus.com
*/

// Rotating testimonials 
//JL - jeffl@solidcactus.com - Solid Cactus 

function domticker(content, divId, divClass, delay, fadethis){
	this.content=content
	this.tickerid=divId 
	this.delay=delay 
	this.mouseoverBol=0 
	this.pointer=1
	/*Need a better way for this ... */
	this.opacitystring=(typeof fadethis!="undefined")? "width: 100%; filter:progid:DXImageTransform.Microsoft.alpha(opacity=100); -moz-opacity: 1" : ""
	/* ... */
	if (this.opacitystring!="") this.delay+=500 
	this.opacitysetting=0.2 
	document.write('<div id="'+divId+'" class="'+divClass+'"><div style="'+this.opacitystring+'">'+content[0]+'</div></div>')
	var instanceOfTicker=this
	setTimeout(function(){instanceOfTicker.initialize()}, delay)
	}
	
	domticker.prototype.initialize=function(){
	var instanceOfTicker=this
	this.contentdiv=document.getElementById(this.tickerid).firstChild 
	document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
	document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
	this.rotatemsg()
	}
	
	domticker.prototype.rotatemsg=function(){
	var instanceOfTicker=this
	if (this.mouseoverBol==1) 
	setTimeout(function(){instanceOfTicker.rotatemsg()}, 100)
	else{
	this.fadetransition("reset") 
	this.contentdiv.innerHTML=this.content[this.pointer]
	this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) 
	this.pointer=(this.pointer<this.content.length-1)? this.pointer+1 : 0
	setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) 
	}
	}
	
	domticker.prototype.fadetransition=function(fadetype, timerid){
	var contentdiv=this.contentdiv
	if (fadetype=="reset")
	this.opacitysetting=0.2
	if (contentdiv.filters && contentdiv.filters[0]){
	if (typeof contentdiv.filters[0].opacity=="number") 
	contentdiv.filters[0].opacity=this.opacitysetting*100
	else 
	contentdiv.style.filter="alpha(opacity="+this.opacitysetting*100+")"
	}
	else if (typeof contentdiv.style.MozOpacity!="undefined" && this.opacitystring!=""){
	contentdiv.style.MozOpacity=this.opacitysetting
	}
	else
	this.opacitysetting=1
	if (fadetype=="up")
	this.opacitysetting+=0.2
	if (fadetype=="up" && this.opacitysetting>=1)
	clearInterval(this[timerid])
}


/* tabbed Product Details */

function getElementsByClassName(options) {
	// { className, startNode, tagName, onlyFindFirst }
	var options = options.className ? options : {className: options};
	var startNode = options.startNode || document;
	var els = startNode.getElementsByTagName(options.tagName || '*');
	options.className = typeof options.className === 'string' ? options.className : options.className.join('(\\s|$)|(\\s|^)');
	var re = new RegExp('(\\s|^)(' + options.className + ')(\\s|$)');
	if (options.onlyFindFirst === true) {
		for (var i = 0, j = els.length; i < j; i++)
			if (re.test(els[i].className))
				return els[i];
		return false;
	} else {
		var found = [];
		for (var i = 0, j = els.length; i < j; i++)
			if (re.test(els[i].className))
				found.push(els[i]);
		return found;
	}
}

function scTDinit(
		scTabDetails /* target DIV */,
		caption /* text that will be parsed into tabs */) {
	function splitCaption(caption) {
		var tabs = [],
		    /* base string to find tabs */
		    newTabTitles = "\\s*--- ?tab\\(([^\\)]*)\\)",
		    /* find tabs string turned to RegExp for matching title */
		    newTabTitle = new RegExp(newTabTitles, 'i'),
		    /* ... and find titles string turned to RegExp with global modifier turned on for finding all titles */
		    titles = caption.match(new RegExp(newTabTitles, 'gi')),
		    /* regex to split into tabs */
		    newTabSplitter = /\s*--- ?tab\([^\)]*\)/i,
		    content = caption.split(newTabSplitter),
		    emptyRegex = /^\s*$/,
		    /* for removing extra BRs at beginning and end */
		    brRemover = /^(<br ?\/?>)+|(<br ?\/?>)+$/ig;

		if (emptyRegex.test(content[0])) content.shift(); // Firefox and IE's implementations of split treat this differently
		for (var i = 0, title; title = titles[i]; i++) {
			tabs.push({
				title: title.match(newTabTitle)[1],
				content: content[i].replace(brRemover, '')
			});
		}

		return tabs;
	}

	var tabs = splitCaption(caption || scTabDetails.innerHTML);
	if (tabs.length > 0 && scTabDetails) {
		this.lis = [];
		this.divs = [];

		var sctab = document.createElement('ul'),
		    sctabinfo = document.createElement('div'),
		    titleCleanser = /[^a-zA-Z0-9]/g;

		// create tabs
		sctab.id = 'sctab';
		for (var i = 0, tab; tab = tabs[i]; i++) {
			var li = document.createElement('li'),
			    span = document.createElement('span'),
			    link = document.createElement('a');
			li.id = 'tabheader' + i;
			li.className = 'tabheader';
			link.href = '#' + tab.title.replace(titleCleanser, '');
			link.appendChild(document.createTextNode(tab.title));

			link.tabbedDetailsObject = this;
			link.onclick = function () {
				var tdObj = this.tabbedDetailsObject;
				for (var i = 0, j = tdObj.lis.length; i < j; i++) {
					var li = tdObj.lis[i],
					    div = tdObj.divs[i],
					    current = (this === li.getElementsByTagName('a')[0]);
					li.className = current ? 'tabhover' : 'tabheader';
					div.style.display = current ? 'block' : 'none';
				}
			}

			span.appendChild(link);
			li.appendChild(span);
			sctab.appendChild(li);
			this.lis.push(li);
		}
		
		// create info divs
		sctabinfo.id = 'sctabinfo';
		for (var i = 0, tab; tab = tabs[i]; i++) {
			var infobox = document.createElement('div'),
			    div = document.createElement('div');
			infobox.className = 'infobox';
			infobox.name = tab.title.replace(titleCleanser, '');
			div.innerHTML = typeof tab.content === 'object' ? tab.content.join('\n') : tab.content;
			infobox.appendChild(div);
			sctabinfo.appendChild(infobox);
			this.divs.push(infobox);
		}

		// empty the sc-tab-details div and put in our new content
		while (scTabDetails.firstChild) scTabDetails.removeChild(scTabDetails.firstChild);
		scTabDetails.appendChild(sctab);
		scTabDetails.appendChild(sctabinfo);

		this.lis[0].getElementsByTagName('a')[0].onclick();
	}
}


