﻿var idsArray = [];
var linkidsArray = [];
var starthere = '';

function addLoadEvent(func) {   
   var oldonload = window.onload;   
   if (typeof window.onload != 'function') {   
     window.onload = func;   
   } else {   
     window.onload = function() {   
       if (oldonload) {   
         oldonload();   
       }   
       func();   
     }   
   }   
} 
addLoadEvent(setUpArray);
addLoadEvent(setMouseOverEvent);
addLoadEvent(rotateContentTimed); 

function setUpArray(){
    var d = document.getElementById('mainimgnav');
	
	if(d){
	    var links = d.getElementsByTagName('a');
		for ( var i = 0; i < links.length; i++ ) {
		linkidsArray.push(links[i].getAttribute('id')); 
		idsArray.push(links[i].getAttribute('id') + "Image");
		}
    }
}


//timer functions to step through each image and text block
var t;
function rotateContentTimed() {
    setTimeout("showTwo()", 8000);
}
function showOne() {
    switchid('link1Image','link1');
    t=setTimeout("showTwo()", 8000);
}
function showTwo() {
    switchid('link2Image','link2');
    t=setTimeout("showThree()", 8000);
}
function showThree() {
    switchid('link3Image','link3');
    
    if  (document.getElementById('link4') !== null){
        t=setTimeout("showFour()", 8000);
        }
    else {
        t=setTimeout("showOne()", 8000);
    }    
}
function showFour() {
    switchid('link4Image','link4'); 
    if  (document.getElementById('link5') !== null){
        t=setTimeout("showFive()", 8000);
        }
    else {
        t=setTimeout("showOne()", 8000);
    }
}
function showFive() {
    switchid('link5Image','link5'); 
    t=setTimeout("showOne()", 8000);
}

function setMouseOverEvent() {
    for ( var i = 0; i < linkidsArray.length; i++ ) {
        document.getElementById(linkidsArray[i]).onmouseover = function(){
        switchid(this.id + "Image",this.id);
        starthere = this.id;
        setMouseOutEvent(starthere);
        clearTimeout(t);
            return false;
        }    
    }
}

function setMouseOutEvent(starthere){
    document.getElementById('mainimgnav').onmouseout = function() {
        clearTimeout(t);
        
        if(starthere !== null){
            
            if(starthere == 'link1'){
                t=setTimeout("showTwo()", 8000);
            }
            if(starthere == 'link2'){
                t=setTimeout("showThree()", 8000);
            }
            if(starthere == 'link3'){
                if  (document.getElementById('link4') !== null){
                    t=setTimeout("showFour()", 8000);
                    }
                else {
                    t=setTimeout("showOne()", 8000);
                } 
            }
            if(starthere == 'link4'){
                if  (document.getElementById('link5') !== null){
                    t=setTimeout("showFive()", 8000);
                    }
                else {
                    t=setTimeout("showOne()", 8000);
                }
            }
            if(starthere == 'link5'){
                t=setTimeout("showOne()", 8000);
            }
            
            } else {
                t=setTimeout("showOne()", 8000);
        }
       
    }
}

function switchid(id,linkid){
	hideallids();
	hidelinkids();
	showdiv(id,linkid);
}

function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<idsArray.length;i++){
		hidediv(idsArray[i]);
	}		  
}

function hidelinkids(){
	//loop through the array and hide each element by id
	for (var i=0;i<linkidsArray.length;i++){
		changeClass(linkidsArray[i]);
	}		  
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}
function changeClass(linkid) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(linkid).className = 'not-selected';
	}
	else {
		if (document.layers) { // Netscape 4
			document.linkid.className = 'not-selected';
		}
		else { // IE 4
			document.all.linkid.className = 'not-selected';
		}
	}
}

function showdiv(id,linkid) {
	//safe function to show an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		//document.getElementById(id).style.display = 'block';
		
		if( document.getElementById(linkid) )
		document.getElementById(linkid).className = 'selected';
			
		if( document.getElementById(id) )	
		new Effect.Appear(document.getElementById(id), {duration:0.40});
			
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
			document.linkid.className = 'selected';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
			document.all.linkid.className = 'selected';
		}
	}
}