
var interval = setInterval ("cycleFeature()", 10000 );


//switches to next feature with fade in effect
function fadeandswitch(feature_num)
{
	//clear the timer interval
	clearInterval(interval);
	//save current image of active feature
	var activediv = getElementByClass('show');
	
	//set opacity to 100% transparent on image on new feature
	changeOpac(0, document.getElementById('featureimage' + feature_num).id);
	
	//apply background of previous image to new feature
	document.getElementById('featured_image').style.backgroundImage = "url(" + activediv.firstChild.src + ")";
	
	//switch to new feature
	showfeature(feature_num);
	
	//fade in new image
	opacity('featureimage' + feature_num, 0, 100, 2000);
	
	//restore interval
	interval = setInterval ("cycleFeature()", 10000 );
}

//cycles to next feature
function cycleFeature()
{
	var count = 1;
	var active = 0;
	var end = 0;
	
	//count features on page as well as get number of active feature
	for (; end == 0; count++)
	{
		
		if (document.getElementById('feature_' + count))
		{
			if (document.getElementById('feature_' + count).className == 'show')
			{
				active = count;
				
			}
		
			
		}
		else
		{
			end = 1;
			break;
		}
		
		
	}
	
	//cycle to next feautre
	if (document.getElementById('feature_' + active).id == 'feature_' + (count - 1) )
	{
		fadeandswitch(1);
	}
	else
	{
		fadeandswitch(active + 1);
	}
	
	
	
}


//used to get Element by classname, similar to getElementById but gets class instead
function getElementByClass (elementClass)
{	
	//Create Array of All HTML Tags
	var allHTMLTags=document.getElementsByTagName('*');
	var found = 0;


	//loop through all tags using a for loop
	for (i=0; i<allHTMLTags.length; i++) 
	{
		//Get all tags with the specified class name.
		if (allHTMLTags[i].className==elementClass) 
		{
			found = 1;
			break;
		}
	}
	
	
	if (found == 1)
	{
		return allHTMLTags[i];
	}
}


//changes feature story to feature number passed here
function showfeature(feature_num) 
{
	var count = 1;
	
	//count all the DIVs on page with id="feature_x"
	while (count < 20)
	{
		if (document.getElementById('feature_' + count))
		{
			count++;
		}
		else
		{
			count--;
			break;
		}
	}
	
	//now apply a hidden class to all these DIVs
	for (x=1; x<=count; x++)
	{
		document.getElementById('feature_' + x).className = 'hidden';
		document.getElementById('featurebutton' + x).className = 'h';
		

	}
	
	//now show the DIV passed to us in this function
	document.getElementById('feature_' + feature_num).className = 'show';
	document.getElementById('featurebutton' + feature_num).className = 'active';
}


function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 


//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 


//function to blend image
function blendimage(divid, imageid, imagefile, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
    
    //set the current image as background
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
    
    //make image transparent

    changeOpac(0, imageid);
    
    //make new image
    document.getElementById(imageid).src = imagefile;

    //fade in image
    for(i = 0; i <= 100; i++) {
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
        timer++;
    }
	
	//remove background image
	document.getElementById(divid).style.backgroundImage = "none";
} 		