var tTime;
var tTick;
var tInt = 30;
var iSlide = 5000;
var slider = new slideshow(5);
var spf= new Array('pile','driver','screw','piledriver','piledriving');
var spm= new Array('pipe','piling','mill','manufacture','manufaturting');
var fe= new Array('foundation','engineer','design');
var ir= new Array('cctv','inspection','relining','reline');
var hy= new Array('hydrovac','excavation','daylighting');
function initSlide() 
{
    document.getElementById("idx02").style.opacity = slider.maxOpc;
    document.getElementById("idx02").style.filter = 'alpha(opacity='+ slider.maxOpc * 100 +')';
	document.getElementById("slide-text-02").style.opacity = 1.0;
    document.getElementById("slide-text-02").style.filter = 'alpha(opacity=100)';
	document.getElementById("banner-front-slide").style.left=(-1934);
    selectSld(findPage());
    clearTimeout(tTime);
    tTime = setTimeout("nextSld()", iSlide);
}
function nextSld() 
{
    var nxt = ((slider.curIdx + 1) > (slider.max + 1) ? 2 : slider.curIdx + 1);
	slider.pick(nxt);
    clearTimeout(tTime);
    clearInterval(tTick);
    tTick = setInterval("slider.tick()", tInt);
}
function selectSld(objID) 
{
	if (slider.curIdx != objID)
	{
		slider.pick(objID);
		clearTimeout(tTime);
		clearInterval(tTick);
		tTick = setInterval("slider.tick()", tInt);
	}
}
function findPage()
{
	var findURL= new RegExp('www\.','i');
	var findCOM= new RegExp('\.com','i');
	var findCA= new RegExp('\.ca','i');
	var findORG= new RegExp('\.org','i');
	var findQuery= new RegExp('q=','i');
	var pageKeywords = new Array();
	var pageRank = new Array(0,0,0,0,0);
	if (document.referrer != '')
	{
		var pageInfo=document.referrer;
		var st=pageInfo.search(findURL) + 4;
		var en=pageInfo.indexOf('.',st);
		var pageSource=pageInfo.substring(st,en);
		
		var keyst = pageInfo.search(findQuery) + 2;
		var stop= false;
		var count=-1;
		var ranked=0;
		if ((pageSource == 'google') || (pageSource == 'bing') || (pageSource == 'yahoo'))
		{
			while (stop == false)
			{
				var wst;
				if (pageSource == 'google')
				{
					wst = pageInfo.indexOf('%20',keyst);
				}
				else if ((pageSource == 'bing')  || (pageSource == 'yahoo'))
				{
					wst = pageInfo.indexOf('+',keyst);
				}
				var wen = pageInfo.indexOf('&',keyst);
				count+=1;
				if ((wst < wen) && (wst != -1))
				{
					pageKeywords[count]=pageInfo.substring(keyst,wst);
					
					if (pageSource == 'google')
					{
						keyst=wst+3;
					}
					else if ((pageSource == 'bing') || (pageSource == 'yahoo'))
					{
						keyst=wst+1;
					}
				}
				else
				{
					pageKeywords[count]=pageInfo.substring(keyst,wen);
					stop=true;
				}
			}
			for (i=0;i<pageKeywords.length;i++)
			{
				for (s=0;s<spf.length;s++)
				{
					if (pageKeywords[i] == spf[s])
					{
						pageRank[0]+=1;
					}
				}
				for (m=0;m<spm.length;m++)
				{
					if (pageKeywords[i] == spm[m])
					{
						pageRank[1]+=1;
					}
				}
				for (f=0;f<fe.length;f++)
				{
					if (pageKeywords[i] == fe[f])
					{
						pageRank[2]+=1;
					}
				}
				for (r=0;r<ir.length;r++)
				{
					if (pageKeywords[i] == ir[r])
					{
						pageRank[3]+=1;
					}
				}
				for (h=0;h<hy.length;h++)
				{
					if (pageKeywords[i] == hy[h])
					{
						pageRank[4]+=1;
					}
				}
			}
			for (i=1;i<pageRank.length;i++)
			{
				if (pageRank[ranked] < pageRank[i])
				{
					ranked=i;
				}
			}
		}
		return (ranked + 2);
	}
	else
	{
		return 2;
	}
}
/********************Application Object: Animates the slideshow*****************/
function slideshow(num) 
{
    this.curLoc = -1934;
    this.curIdx = 2;
	this.curOpc = 1;
    this.nextLoc = -2901;
    this.nextIdx = 3;
	this.nextOpc = 0;
	this.fadeInc=0.06;
    this.minTick = 2;
    this.max = num;
	this.state = 0;
	this.maxOpc = 0.6;
	this.minOpc = 0.3;

    this.update = update;
	this.pick=pick;
    this.tick = tick;
    this.render = render;
	this.tfadeIn=tfadeIn;
	this.tfadeOut=tfadeOut;
	this.rfadeIn=rfadeIn;
	this.rfadeOut=rfadeOut;
	this.tslide=tslide;
	this.rslide=rslide;
}
function pick(objID)
{
	this.state=0;
	if (((this.curIdx < 4) && (objID > 4)) || ((this.curIdx == 3) && (objID == 6)))
	{
		this.nextIdx=(objID-this.max);
	}
	else if (((this.curIdx > 4) && (objID < 4)) || ((this.curIdx == 5) && (objID == 2)))
	{
		this.nextIdx=(objID+this.max);
	}
	else
	{
		this.nextIdx=objID;
	}
	this.nextLoc=this.nextIdx * -967;
}
function update() 
{
    this.curLoc=parseInt(document.getElementById("banner-front-slide").style.left);
}
function render() 
{
    switch (this.state)
	{
		case 0:
			this.rfadeOut();
			break;
		case 1:
			this.rslide();
			break;
		case 2:
			this.rfadeIn();
			break;
		default:
			break;
	}
}
function tick() 
{
    this.update();
	switch (this.state)
	{
		case 0:
			var b=this.tfadeOut();
			this.render();
			if (b)
			{
				this.state+=1;
				document.getElementById("idx0" + this.curIdx).style.opacity = this.minOpc;
				document.getElementById("idx0" + this.curIdx).style.filter = 'alpha(opacity='+ this.minOpc * 100 +')';
				this.curOpc=1;
			}
			break;
		case 1:
			var b=this.tslide();
			this.render();
			if (b)
			{
				this.state+=1;
			}
			break;
		case 2:
			var b=this.tfadeIn();
			this.render();
			if (b)
			{
				this.state+=1;
				this.nextOpc=0;
			}
			break;
		case 3:
			if (this.nextIdx < 2)
			{
				var tmp=this.nextIdx;
				this.nextIdx+=this.max;
				this.nextLoc= this.nextIdx * -967;
				document.getElementById("slide-text-0" + this.nextIdx).style.opacity = 1.0;
				document.getElementById("slide-text-0" + this.nextIdx).style.filter = 'alpha(opacity='+ 100 +')';
				document.getElementById("banner-front-slide").style.left=this.nextLoc + "px";
				document.getElementById("slide-text-0" + tmp).style.opacity = 0;
				document.getElementById("slide-text-0" + tmp).style.filter = 'alpha(opacity='+ 0 +')';
			}
			else if (this.nextIdx > 6)
			{
				var tmp=this.nextIdx;
				this.nextIdx-=this.max;
				this.nextLoc= this.nextIdx * -967;
				document.getElementById("slide-text-0" + this.nextIdx).style.opacity = 1.0;
				document.getElementById("slide-text-0" + this.nextIdx).style.filter = 'alpha(opacity='+ 100 +')';
				document.getElementById("banner-front-slide").style.left=this.nextLoc + "px";
				document.getElementById("slide-text-0" + tmp).style.opacity = 0;
				document.getElementById("slide-text-0" + tmp).style.filter = 'alpha(opacity='+ 0 +')';
			}
			document.getElementById("idx0" + this.nextIdx).style.opacity = this.maxOpc;
			document.getElementById("idx0" + this.nextIdx).style.filter = 'alpha(opacity='+ this.maxOpc * 100 +')';
			this.state+=1;
			break;
		default:
			this.curIdx = this.nextIdx;
			clearInterval(tTick);
			clearTimeout(tTime);
			tTime = setTimeout("nextSld()", iSlide);
			this.state=-1;
			break;
	}
}
function tfadeOut()
{
	this.curOpc=( this.curOpc - this.fadeInc > 0 ? this.curOpc - this.fadeInc : 0);
	if (this.curOpc == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
function rfadeOut()
{
	document.getElementById("slide-text-0" + this.curIdx).style.opacity = this.curOpc;
	document.getElementById("slide-text-0" + this.curIdx).style.filter = 'alpha(opacity='+ this.curOpc * 100 +')';
}
function tfadeIn()
{
	this.nextOpc=( this.nextOpc + this.fadeInc < 1 ? this.nextOpc + this.fadeInc : 1);
	if (this.nextOpc == 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
function rfadeIn()
{
	document.getElementById("slide-text-0" + this.nextIdx).style.opacity = this.nextOpc;
	document.getElementById("slide-text-0" + this.nextIdx).style.filter = 'alpha(opacity='+ this.nextOpc * 100 +')';
}
function tslide()
{
	if (this.curLoc != this.nextLoc) 
	{
		var inc = (Math.abs(this.curLoc - this.nextLoc) * 0.2).toFixed(0);
		inc = (inc < this.minTick ? this.minTick : inc);
		if (this.curLoc > this.nextLoc) 
		{
			this.curLoc = (this.curLoc - inc > this.nextLoc ? this.curLoc - inc : this.nextLoc);
		}
		if (this.curLoc < this.nextLoc)
		{
			this.curLoc = (inc - Math.abs(this.curLoc) < this.nextLoc ? inc - Math.abs(this.curLoc) : this.nextLoc);
		}
		return false;
	}
	else
	{
		return true;
	}
}
function rslide()
{
	document.getElementById("banner-front-slide").style.left=this.curLoc + "px";
}

