// I wrote this to eliminate the inline event calls within HTML (separation of layers) 
// edited by aevo digital for special purpose
function initSwitcher() {
    // Finds the links that are used to change styles (identified by title tag)
    var links = document.getElementsByTagName("a");
    for (var i=0; i < links.length; i++) { // loops through array
       if (links[i].getAttribute("title") && (links[i].getAttribute("title") == 'standard' || links[i].getAttribute("title") == 'gross' || links[i].getAttribute("title") == 'riesig')) { // have a title attribute?
           links[i].onclick = function() { // setting onclick event of each node
                setActiveStyleSheet(this.getAttribute("title")); // in the node itself, so 'this' is used
                return false; // cancels default onclick behavior
           } 
	   }
    } 
}

// Enables the stylesheet that was selected last time (from cookie)
// First it disables all the stylesheet, then re-enables the one that was passed
function setActiveStyleSheet(title) {
 
    var i, a, main;
   
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     
       if(a.getAttribute("rel").indexOf("style") != -1 // is it a stylesheet?;
           && a.getAttribute("title")) { // has a title attribute?
    
           // disable stylesheet
           a.disabled = true;
      
           // Matches the passed alternative style name?; if so re-enable it 
           if(a.getAttribute("title") == title) {
				a.disabled = false;
				switchImageClass(title);
			}
       }
    }
}
 
// Gets the 'active' stylesheet that is defined in the HTML file  
function getActiveStyleSheet() {

  var i, a;
  
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  
     if(a.getAttribute("rel").indexOf("style") != -1 // is it a stylesheet?
         && a.getAttribute("title") // has a title attribute
         && !a.disabled) { // and not disabled?
    
         return a.getAttribute("title");
     }
  }
  
  return null; // not found 
}

// Sets the default stylesheet (rel="stylesheet" NOT rel="alternate stylesheet"). 
// note: the main.css is your "sticky" stylesheet that will never be changed (no title attribute)
function getPreferredStyleSheet() {

  var i, a;

  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  
  	  // Returns default stylesheet with title attribute 
      if(a.getAttribute("rel").indexOf("style") != -1 // is it a stylesheet?
      	 && a.getAttribute("rel").indexOf("alt") == -1 // is it NOT alternative style?
         && a.getAttribute("title"))  // does it have the title attribte present
       
         return a.getAttribute("title");
  }
  
  return null; // if no "link" elements found, return null
  
}




// created by aevo digital for special purpose
function switchImageClass(title)
{
	for(i=0; (a = document.getElementsByTagName("img")[i]); i++)
	{
		if(title == "standard")
		{
			if(a.className == "standard_inactive")
			{
				a.className = "standard_active";
			}
			else if(a.className == "gross_active")
			{
				a.className = "gross_inactive";
			}
			else if(a.className == "riesig_active")
			{
				a.className = "riesig_inactive";
			}
		}
		else if(title == "gross")
		{
			if(a.className == "standard_active")
			{
				a.className = "standard_inactive";
			}
			else if(a.className == "gross_inactive")
			{
				a.className = "gross_active";
			}
			else if(a.className == "riesig_active")
			{
				a.className = "riesig_inactive";
			}
		}
		else if(title == "riesig")
		{
			if(a.className == "standard_active")
			{
				a.className = "standard_inactive";
			}
			else if(a.className == "gross_active")
			{
				a.className = "gross_inactive";
			}
			else if(a.className == "riesig_inactive")
			{
				a.className = "riesig_active";
			}
		}
	}
}




// Created by Peter-Paul Koch (http://www.quirksmode.org/)
function createCookie(name,value,days) {
  
  if (days) {
  
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    
  } else {
  
    expires = "";  
  }
    document.cookie = name+"="+value+expires+"; path=/"; 
}

// Created by Peter-Paul Koch (http://www.quirksmode.org/)
function readCookie(name) {

  var nameEQ = name + "=";
  var ca = document.cookie.split(';');

  for(var i=0;i < ca.length;i++) {
  
    var c = ca[i];
    
    while (c.charAt(0)==' ') {
    
        c = c.substring(1,c.length);
    
    }
    
    if (c.indexOf(nameEQ) == 0) {
    
        return c.substring(nameEQ.length,c.length);
    }
  
  }
  
  return null;
}

/****************** Functions for page load and unload ******************/

// Setting up links, and default stylesheets when loaded
window.onload = function() {

    initSwitcher(); // set the links

    var cookie = readCookie("style"); // name of cookie that would be saved
  
    // If cookie not set, get the preferred stylesheet defined in the HTML file
    var title = cookie ? cookie : getPreferredStyleSheet();  
    
    setActiveStyleSheet(title);
}

// Saving selected stylesheet (before closing browser or going to new web page)
window.onunload = function() {

    var title = getActiveStyleSheet();
    
    createCookie("style", title, 365); // 365 day cookie
}
