// a separate variable is set if this PNG image needs to have an Internet Explorer hack applied to it
var hackIE = false;

// add this script to be run when the window is loaded
if(window.addEventListener)
{
    window.addEventListener("load",changeItems,true);
}
else
{
    window.attachEvent("onload",changeItems);
}

// write out a style rule to hide all lists that are going to be changed
document.write("<style> ul.dropdown { display:none; } </style>");

// run functions to add images to links, and convert lists to dropdown menus
function changeItems(event)
{
  changeLinks();
  changeLists();
}

function changeLists()
{
  // get all the uls on the page, and iterate through them
  var allULLists = document.getElementsByTagName("ul");
  
  var lists = [];
  
  for (i=0; i < allULLists.length; i++)
  {
  	var current = allULLists[i];
  	
  	// change all lists with a class of dropdown to a dropdown menu
  	if(current.className == "dropdown")
  	{
  		lists.push(current);
  	}
  }
  
  for (i=0; i < lists.length; i++)
  {
  	changeToDropdown(lists[i]);
  }
}

function changeLinks()
{
 // get all of the <a> tags on the page, and iterate through them
 allATags = document.getElementsByTagName("a");
 for(i=0; i < allATags.length; i++)
 {
  var current = allATags[i];
  
  // if it has an href attribute, check to see if it needs an icon added to it
  if(current.href)
  {
      // check to see if the extension of the link is .pdf
      if(current.href.slice(current.href.length-4) == ".pdf")
      {
       addPicture(current,"http://www.georgefox.edu/template/i/common/page_white_acrobat.png","Adobe Acrobat document");
      }
      // check to see if the extension of the link is .doc or .docx
      else if(current.href.slice(current.href.length-4) == ".doc" || current.href.slice(current.href.length-5) == ".docx")
      {
       addPicture(current,"http://www.georgefox.edu/template/i/common/page_word.png","Microsoft Word document");
      }
      // check to see if the extension of the link is .xls
      else if(current.href.slice(current.href.length-4) == ".xls")
      {
       addPicture(current,"http://www.georgefox.edu/template/i/common/page_white_excel.png","Microsoft Excel document");
      }
      // check to see if the link is a powerpoint file
      else if(current.href.slice(current.href.length-4) == ".xls")
      {
       addTitle(current,"Microsoft Powerpoint document");
      }
      // check to see if the link starts with mailto:
      else if(current.href.slice(0,7) == "mailto:")
      {
       addPicture(current,"http://www.georgefox.edu/template/i/common/email.png","email address");
      }
      else if(current.className && current.className == "kfoxLink")
      {
       addPicture(current,"http://www.georgefox.edu/template/i/common/sound.png","listen on KFOX");
      }
      
      // check to see if this is the "print this page" box on the tilikum site
      else if(current.parentNode && current.parentNode.id == "printThis")
      {
       addPicture(current,"http://www.georgefox.edu/template/i/common/printer.png","print this page");
      }
  }
 }
}

// a function to add a graphic icon to the end of an <a> tag
function addPicture(current,picture,title)
{
   // only add images to links that don't already have images in them
   // don't add images to links with the class name of noImage
   if(current.getElementsByTagName("img").length == 0 && (!current.className || current.className.indexOf("noImage") == -1) )
   {
    // create a new image, set the class to linkImage, and set the source
    var newImage = document.createElement("img");
    newImage.height = "16";
    newImage.width = "16";
    newImage.className = "linkImage";
    
    // if the browser is Internet Explorer, do some funky-ness to make sure that alpha transparency will work
    if(hackIE)
    {
     newImage.src = "http://www.georgefox.edu/template/i/common/blank.gif";
     newImage.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\""+picture+"\", sizingMethod=\"scale\")";
    }
    // otherwise, just set the source
    else
    {
     newImage.src = picture;
    }
    
    // add the image to the end of the link
    current.appendChild(newImage);
   }
   
   // add a title to the current link
   addTitle(current,title)
}

function addTitle(current,title)
{
   // add the title to the link, or append it if it already has a title
   if(current.title && current.title.indexOf(title) == -1)
   {
    current.title += " - "+title;
   }
   else
   {
    current.title = title;
   }
}

function changeLocation(event)
{
	// if this isn't the first item in the select menu, go to that location
	if(this.selectedIndex != 0)
	{
		document.location.href = this[this.selectedIndex].value;
	}
}

function changeToDropdown(list)
{
	// get an array of all items in the current list
	var lis = list.getElementsByTagName("li");
	
	// create a new select element and add an onchange function to it
	var newDropdown = document.createElement("select");
	newDropdown.style.display = "block";
	newDropdown.onchange = changeLocation;
	
	// iterate through all list items, adding options to the select element
	for(j=0; j < lis.length; j++)
	{
		// get the current list item
		var currentItem = lis[j];
		
		// find any a tags within the current list item
		var currentLinks = currentItem.getElementsByTagName("a");
		
		// create a new option element
		var newOption = document.createElement("option");

		// if there are no a tags within it, make the option blank (ie, not have a value attached to it)
		if(currentLinks.length == 0)
		{
			newOption.appendChild(currentItem.firstChild);
			// if it's the first option, make sure it's selected
			if(j==0){newOption.selected = "true";}
		}
		// create a normal option, add a value, and add the text to it
		else
		{
			var currentLink = currentLinks[0];
			newOption.appendChild(currentLink.firstChild);
			newOption.value = currentLink.href;
		}
		
		// add our newly created option to the select menu		
		newDropdown.appendChild(newOption);
	}
	
	// replace the ul with the newly created select menu
	list.parentNode.replaceChild(newDropdown,list);
}