/*************************************/
/* imagemenus.js                     */
/* (c) 2009 billandcori.com          */
/*                                   */
/* Used for structure on index pages */
/*                                   */
/*************************************/

// holds last selected since refresh 
var lastSelectedTab = "initial"; 



function getArea(place){

 // change visually selected element 
 setActiveTab(place);
 
 
 // change content
 var myText = document.getElementById('selectedRegion').innerHTML;
 
 // old code
 //document.getElementById('selectedRegion').innerHTML = eval(place);

 // this also works - keep fo debug
 //var x = getVTable("bikesacansa");
 //document.getElementById('selectedRegion').innerHTML = x;
 
 document.getElementById('selectedRegion').innerHTML = getVisuals(place);
 
 }
 

 
/*********************************************************/ 
/* setActiveTab										     */
/* selectedTabID: string matching object id              */
/*                                                       */
/* make clicked tab shaded and unshade last selected tab */
/*********************************************************/ 
 
function setActiveTab(selectedTabID) {

  var curTabElem = document.getElementById(selectedTabID);
  var curTabElemL = document.getElementById(selectedTabID+"l");
  var curTabElemR = document.getElementById(selectedTabID+"r");  
  
  var lstTabElem = document.getElementById(lastSelectedTab);
  var lstTabElemL = document.getElementById(lastSelectedTab+"l");
  var lstTabElemR = document.getElementById(lastSelectedTab+"r");
  
  if (curTabElem != null)
  {
	 curTabElem.setAttribute("class", "selected");
	 curTabElem.setAttribute("className", "selected");
  	 // LEFT
	 curTabElemL.setAttribute("class", "leftselected");
     curTabElemL.setAttribute("className", "leftselected");
	 // RIGHT
	 curTabElemR.setAttribute("class", "rightselected");
     curTabElemR.setAttribute("className", "rightselected");
  
     if (selectedTabID != lastSelectedTab)
	 {
        lstTabElem.setAttribute("class", "unselected");
        lstTabElem.setAttribute("className", "unselected");
		// LEFT
		lstTabElemL.setAttribute("class", "sideunselected");
        lstTabElemL.setAttribute("className", "sideunselected");
		// RIGHT
		lstTabElemR.setAttribute("class", "sideunselected");
        lstTabElemR.setAttribute("className", "sideunselected");

        lastSelectedTab = selectedTabID;
	 }
  }
  
return;
} 

// Main function to get all of the tables into one write
function getVisuals(setofgroups){
	var setarray = (window[setofgroups]);
	var mytables = "";
	var count = setarray.length;
	for(i=0; i<=setarray.length-1; i++){
		mytables = mytables + getVTable(setarray[i]);
	}
	return mytables;
}


// Return the table of visuals
function getVTable(grouping){
    var myarray = (window[grouping]);
	var header = "<table border=0 cellspacing='5' cellpadding='5' width='100%'><tr class='country' align='right' valign='top'>";
	var numtitles = 1;

	if ((myarray[0].length) == 2)
     var header = header + "<td class='smfont' colspan='4'><a name='" + myarray[0][1] + "'></a>" + myarray[0][0] + "</td></tr>";
	// shared header
	else
	 {j=0;
	  numtitles=0;
	   while ((myarray[j].length)== 3 && myarray[j][2].length==1){	
	     var header = header + "<td class='smfont' colspan='" + myarray[j][2] + "'><a name='"+ myarray[j][1] +"'></a>" + myarray[j][0]  + "</td>";
		 j++;
		 numtitles++;
		 }
	   var header = header + "</tr>";
	 } 
	var visualcont = "<tr valign='top' class='visuals'>";
	
	// manage rows
	cell=1;

	for (k=numtitles; k<(myarray.length); k++)
	{
	  // populate cell
	  if ((myarray[k][0]).length != 0)
	   visualcont = visualcont + "<td><a href='" + myarray[k][0] + "'><img src='" + myarray[k][1] + "' width='185' height='105' alt='"+ myarray[k][2] + "' border='0'><br>" + myarray[k][3] + "</a></td>";
	   // empty cell
	  else
	   visualcont = visualcont + "<td width='200'></td>";

	  // new row 
      if (cell % 4 == 0)
	   visualcont = visualcont + "</tr><tr valign='top' class='visuals'>";
	  cell ++;
	}
	 var finaltable = header + visualcont + "</tr></table>";  
	 return(finaltable);
}


// This is a menu to be used as a singular set of images without a dynamic menu selecting the set
//
// Accompanying script sample:
// var bikesacansa = [["Main Title 1","anchor","# of images under this title"],["Main Title 1","anchor","# of images under this title"],["Main Title 1","anchor","# of images under this title"],
//     ["page.htm","images/image.jpg","description of image","Title"],
//     ["page.htm","images/image.jpg","description of image","Title"],
//     ["page.htm","images/image.jpg","description of image","Title"],
//	   ["page.htm","images/image.jpg","description of image","Title"]];

function writeVTable(curRegion){
    var myarray = (window[curRegion]);
	var header = "<table border=0 cellspacing='5' cellpadding='5' width='100%'><tr class='country' align='right' valign='top'>";
	var numtitles = 1;

	if ((myarray[0].length) == 2)
     var header = header + "<td class='smfont' colspan='4'><a name='" + myarray[0][1] + "'></a>" + myarray[0][0] + "</td></tr>";
	// shared header
	else
	 {j=0;
	  numtitles=0;
	   while ((myarray[j].length)== 3 && myarray[j][2].length==1){	
	     var header = header + "<td class='smfont' colspan='" + myarray[j][2] + "'><a name='"+ myarray[j][1] +"'></a>" + myarray[j][0]  + "</td>";
		 j++;
		 numtitles++;
		 }
	   var header = header + "</tr>";
	 } 
	var visualcont = "<tr valign='top' class='visuals'>";
	
	// manage rows
	cell=1;
	for (i=numtitles; i<(myarray.length); i++)
	{
	  // populate cell
	  if ((myarray[i][0]).length != 0)
	   visualcont = visualcont + "<td><a href='" + myarray[i][0] + "'><img src='" + myarray[i][1] + "' width='185' height='105' alt='"+ myarray[i][2] + "' border='0'><br>" + myarray[i][3] + "</a></td>";
	   // empty cell
	  else
	   visualcont = visualcont + "<td width='200'></td>";

	  // new row 
      if (cell % 4 == 0)
	   visualcont = visualcont + "</tr><tr valign='top' class='visuals'>";
	  cell ++;
	}
	 var finaltable = header + visualcont + "</tr></table>";   

	 document.write(finaltable);
}