// ################ style sheet switcher code ################

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {		// loop through all linked style sheets
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;			// if this is the requested one, switch it on
    }
  }
}

function getActiveStyleSheet() {										// find currently active style
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {									// find preferred style (doesn't say "alternate" in the rel attribute)
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {								// write a cookie
  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=/";
}

function readCookie(name) {											// read a cookie
  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;
}


tstyles = new Array();
tstyles[1] = 'text1default';
tstyles[2] = 'text2bigger';
tstyles[3] = 'text3biggest';

function text_size(ts) {												// set styles based on user input
	setActiveStyleSheet(tstyles[ts]);
	var resobj = YAHOO.util.Dom.get('TRa1');
	resobj.style.backgroundColor = 'transparent';
	var resobj = YAHOO.util.Dom.get('TRa2');
	resobj.style.backgroundColor = 'transparent';
	var resobj = YAHOO.util.Dom.get('TRa3');
	resobj.style.backgroundColor = 'transparent';
	var resobj = YAHOO.util.Dom.get('TRa' + ts);
	resobj.style.backgroundColor = '#93b066';
}


// ############## print style switcher ################

var currentStyle;

// disable all styles, remember the active ones, switch to print style and show print widget
function printPage() {
	tmploc = this.location.href;
	if (tmploc.indexOf('#') != -1) {
		tmploc = tmploc.substring(0,tmploc.indexOf('#'));
	}
	if (tmploc.indexOf('?') == -1) {
		poploc = tmploc + '?print=yes';
	} else {
		poploc = tmploc + '&print=yes';
	}
	popprop = 'toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,resizable=yes,scrollbars=yes,width=900,height=600';
	var newwin=window.open(poploc,'printablePage',popprop);
}

// use black and white version of logo in header
function setLogoBW () {
	document.getElementById('logoPCimg').src = '/media/images/logo_pc_big_bw.gif';
	YAHOO.util.Dom.setStyle('logoPCimg','width','140px');
	YAHOO.util.Dom.setStyle('logoPCimg','height','32px');
}

function printRestyle() {
	currentStyle = getActiveStyleSheet();
	var i, a, main;
	for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {	// loop through all linked style sheets
		if (a.getAttribute('rel').indexOf('style') != -1) {
			a.disabled = true;											// switch off all other styles
			if (a.getAttribute('title') == 'print') {
				a.disabled = false;										// switch on print style only
			}
		}
	}
}

// ########## check if print style is required - run this code during load #########

paraPrint = extractParameter('print'); // extractParameter(param) is from includes/js-query-string-inc.jsp
if (paraPrint == 'yes') {
    printRestyle();
	YAHOO.util.Event.onDOMReady(setLogoBW);					// use b&w logo on printable page
	setActiveStyleSheet('print');
	this.print();
} else {
    var cookie = readCookie("PsoriasisConnectionsStyle");
    var title = cookie ? cookie : getPreferredStyleSheet();				// return cookie style or preferred style in "title"
	setActiveStyleSheet(title);										// set style
}

window.onunload = function(e) {										// on page unload save style setting
  var title = getActiveStyleSheet();
  if (title != 'print') {
  	createCookie('PsoriasisConnectionsStyle', title, 355);
  }
}