function setActiveStyleSheetTextTitle(title) {
  var i, j, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("title").indexOf("text") > -1) {
     a.disabled = true;
      if(a.getAttribute("title") == title) {
        a.disabled = false;
        for(j=0; j < textstyles.length ; j++) {
          if(textstyles[j] == title){
            styleindex = j;
            createCookie("text-size", styleindex, 365);
          }
        }
      }  
    }
  }
}

function setActiveStyleSheetTheme(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("title").indexOf("device") > -1) {
      a.disabled = true;
      if(a.getAttribute("title") == title) {
        a.disabled = false;
        createCookie("device", title, 365);
      }  
    }
  }
}

function getActiveStyleSheetTheme() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled && a.getAttribute("title").indexOf("device") > -1) return a.getAttribute("title");}
  return"theme-standard";}

function getPreferredStyleSheetTheme() {
	if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) {
		return "theme-mobile";
	}
	if (window.innerWidth) {
	if (window.innerWidth <= 480) {
		return "theme-mobile";
	} 
	}
	if (document.body && document.body.clientWidth) {
	if (document.body.clientWidth <= 480) {
		return "theme-mobile";
	}
	}
	return "theme-standard";}
  

function setActiveStyleSheetText(indexchange) {
  styleindex = parseInt(styleindex) + indexchange;
  if(styleindex < 0){
  	styleindex = 0;
  } else if(styleindex >= textstyles.length){
  	styleindex = textstyles.length - 1;
  }	
  
  createCookie("text-size", styleindex, 365);
  
  var title = textstyles[styleindex];
 
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("title").indexOf("text") > -1) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;}
  }
  return null;
}

function getActiveStyleSheetText() {
	return styleindex;}

function getPreferredStyleSheetText() {
	return 2;} 

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=/;domain=griffith.edu.au";}

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;}

function textSwitcherLoad() {
	var textcookie = readCookie("text-size");
	var texttitle = textcookie ? textcookie : getPreferredStyleSheetText();
	var styleindex = texttitle;
	setActiveStyleSheetText(0);
	var themecookie = readCookie("device");
	var themetitle = themecookie ? themecookie : getPreferredStyleSheetTheme();
	setActiveStyleSheetTheme(themetitle);
	if (themetitle == 'theme-mobile') {
		metaN = document.createElement('META');  	
		metaN.name = 'viewport';
		metaN.content = 'width=device-width; initial-scale=1.0; maximum-scale=1.0;';
		document.body.appendChild(metaN);}
	if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) {
		fullThemeLink = document.createElement('P');
		if (themetitle == 'theme-mobile') {
			fullThemeLink.innerHTML ="mobile :: <a href=\"#\" onClick=\"swapThemeButton(this);\">desktop</a>"; 
		} else {
			fullThemeLink.innerHTML ="<a href=\"#\" onClick=\"swapThemeButton(this);\">mobile</a> :: desktop"; 
		}
		
		document.getElementById('backtotop').appendChild(fullThemeLink);
	}
}

function swapThemeButton(who) {
	if (who.innerHTML=='mobile') {
		who.parentNode.innerHTML ='mobile :: <a href=\"#\" onClick=\"swapThemeButton(this);\">desktop</a>';
		setActiveStyleSheetTheme('theme-mobile');
	} else {
		who.parentNode.innerHTML = '<a href=\"#\" onClick=\"swapThemeButton(this);\">mobile</a> :: desktop';
		setActiveStyleSheetTheme('theme-standard');
	}	
		location.reload(true);
}

addLoadEvent(textSwitcherLoad);

function textSwitcherUnLoad() {
	var title = getActiveStyleSheetText();
	createCookie("text-size", title, 365);
	var title = getActiveStyleSheetTheme();
	//createCookie("device", title, 365);
}

addUnLoadEvent(textSwitcherUnLoad);

var textstyles=new Array("text-decrease2","text-decrease1","text-standard","text-increase1","text-increase2");

var textcookie = readCookie("text-size");
var texttitle = textcookie ? textcookie : getPreferredStyleSheetText();
var styleindex = texttitle;
setActiveStyleSheetText(0);

var themecookie = readCookie("device");
var themetitle = themecookie ? themecookie : getPreferredStyleSheetTheme();
setActiveStyleSheetTheme(themetitle);
