var fadedirection = 1;
var imagecount = 0;
var thumbpos = 0;
var maximages=3;
	
function switchimg(sourcecol, sourcerow) {
	if (document.getElementById("imgtable").rows[sourcerow].cells[sourcecol].innerHTML.search("TNblankthumbnail.jpg") == -1) {
		slicestart = document.getElementById("imgtable").rows[sourcerow].cells[sourcecol].innerHTML.search("/TN/TN")+6;
		slicestop = document.getElementById("imgtable").rows[sourcerow].cells[sourcecol].innerHTML.search(".jpg")+4;
		imgurl = document.getElementById("imgtable").rows[sourcerow].cells[sourcecol].innerHTML.slice(slicestart, slicestop);
		
		document.getElementById("largepic").innerHTML = "<img src='ApexProductImages/"+imgurl+"'/></img>";
	} else {
		document.getElementById("largepic").innerHTML = "<img src='ApexProductImages/blankthumbnail.jpg'/></img>";
	}
}

function SetOpacity(elem, opacityAsInt) {
	var opacityAsDecimal = opacityAsInt;
    
    if (opacityAsInt > 100) { 
		opacityAsInt = opacityAsDecimal = 100;  }
    else {
		if (opacityAsInt < 0) {
        	opacityAsInt = opacityAsDecimal = 0; 
		}
	}		
    
    opacityAsDecimal /= 100;
    if (opacityAsInt < 1) {opacityAsInt = 1; }// IE7 bug, text smoothing cuts out if 0
    
    elem.style.opacity = (opacityAsDecimal);
    elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}

function FadeOpacity(elemId, fromOpacity, toOpacity, fps) {
     var steps = Math.ceil(fps);
     var delta = (toOpacity - fromOpacity) / steps;
     
     FadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (1000 / steps));
}

function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep) {

	SetOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));

    if (stepNum < steps) {
        setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", timePerStep);}
	else //finished animating, now go on to the next stage - pause for 5 seconds
	{
		if (elemId == "roller2")
		{
			setTimeout("getthenextactionroller();",8000);
		}
		else
		{
			if (direction==-1) 
			{
				setTimeout("getthenextaction();",500);
			}
			else 
			{  
				setTimeout("getthenextaction();",5000);
			}
		}
	}
}

function getthenextaction() {
	direction*= -1;
	if (direction==1) { //moved on to fading in the next headline
		currentheadline++;
		if (currentheadline>maxheadlines) { currentheadline=0; }
		document.getElementById("headline").innerHTML = newsheadlines[currentheadline];
		FadeOpacity("headline", 0, 100, 20);
	} else { //fading out the current element
		FadeOpacity("headline", 100, 0, 20);
	}
}

function getthenextactionroller() {
	fadedirection *= -1;
	if (fadedirection ==1) 
	{
		//fp2 is fadedout so switch its image
		document.getElementById("roller2").style.backgroundPosition= "0px -"+(270*imagecount)+"px";
		imagecount++;
		if (imagecount == maximages) {imagecount = 0;}
		FadeOpacity("roller2", 0, 100, 20);
	}
	else 
	{
		document.getElementById("roller1").style.backgroundPosition= "0px -"+(270*imagecount)+"px";
		imagecount++;
		if (imagecount == maximages) {imagecount = 0;}
		FadeOpacity("roller2", 100, 0, 20);
	}
}