/*****************************************
   Date: Mar 9, 2011
   Script: Manage Gallery (v1.0)
   By: Axel
******************************************/
//Global variables
var i = 0;
var i2 = 0;
var s;
var doChange = true
var tempId;
var adsCount = 6; //Contains how many ads will be in the rotation
var myAds = new Array(); //Array the holds the images links
	myAds[0] = 'images/ads/1.jpg';
	myAds[1] = 'images/ads/2.jpg';
	myAds[2] = 'images/ads/3.jpg';
	myAds[3] = 'images/ads/4.jpg';
	myAds[4] = 'images/ads/5.jpg';
	myAds[5] = 'images/ads/6.jpg';
	
var myMaps = new Array(); //Array that holds the images maps
	myMaps[0] = '#dc-tv';
	myMaps[1] = '#res-phone';
	myMaps[2] = '#high-speed-dsl';
	myMaps[3] = '#tri-dvr';
	myMaps[4] = '#wireless-broadband';
	myMaps[5] = '#bundled-serv';
var colorSelected = 'red'; //Color for selected item in nav list
var colorOthers = 'black'; //Color for other nav list itens

/*****************************************
   Function controls the loop process
******************************************/
function photoGallery() {
	if(doChange) {
		//Get the appropiate image based on the array links
		document.getElementById('photo-gallery').src = myAds[i].toString();

		//Some control variables
		tempId = 'my';
		i2 = i;
		tempId = tempId.concat((i2 + 1).toString());
		
		//Get the appropiate map specific to the selected image
		document.getElementById('photo-gallery').useMap = theMap(i); 
		
		//Change the text colors from the nav list
		changeColors(tempId);
		
		//Check if it's the end of recursion or to continue with next
		if(i == (adsCount - 1)) {
			i = 0;
			i2 = 0;
			s = setTimeout("photoGallery()",10000);
		}
		else if(i < (adsCount - 1)) {
			i = i + 1;
			s = setTimeout("photoGallery()",10000);
		}
	}
}

/*****************************************
   Function controls the click process
******************************************/
function showPic (thepic, theid) {
	 var idWithout = '';
	 
	 if (document.getElementById) {
		  //Get the specific itemn
		  document.getElementById('photo-gallery').src = thepic.href;
		  
		  //Replace my from html document id with empty
		  idWithout = theid.replace('my', '');
		  
		  //Change the map for specific image
		  document.getElementById('photo-gallery').useMap = theMap(idWithout - 1); 
		  
		  //Change the text colors
		  changeColors(theid);
		  
		  //Set some variables
		  doChange = false;
		  s = null;
		  
		  //After the user stops the loop when click, wait 2 mins and then refresh
		  refreshPage(120000);
		  
		 //So it doesn't follow the link
		 return false;
	 } 
	 else {
		 return true;
	 }
}

/*****************************************
   Function controls the change colors 
   process
******************************************/
function changeColors(theid) {
	var j = 0;
	var tempText = 'my';
	
	for(j = 0; j <= adsCount - 1; j++) {
		tempText = tempText.concat((j + 1).toString());
		
		//Changes the nav list items based on the selected image
		if(theid == tempText) {
			document.getElementById(tempText).style.color = colorSelected;
		}
		else  {
			document.getElementById(tempText).style.color = colorOthers;
		}
		
		//Resets variable for re-use
		tempText = 'my';
	}
}

/*****************************************
   Function controls the refresh process
******************************************/
function refreshPage(timeForRefresh) {
	setTimeout("location.reload(true);",timeForRefresh);
}

/*****************************************
   Function to control images mapping
/*****************************************/
function theMap(i) {
	var map = '';
	
	//Fetches the map based on the current image value on array
	map = myMaps[i];
	
	return map;
}
