/*
	Style switcher control:
	<select id="skinSelector" onchange="setActiveStyleSheetEx(this.value);">
		<option value="Style 1">Style 1</option>
		<option value="Style 2+Liquid">Style 2</option>
		<optgroup label="Liquid">
			<option value="Liquid">Plain</option>
			<!-- for this to work correctly the components need to be in the order of appearance in the style sheets, e.g.: 1st+4th+8th NOT like: 4th+1st+8th -->
			<option value="Style 1+Liquid">Style 1</option>
			<option value="Style 2+Liquid">Style 2</option>
		</optgroup>
	</select>

*/


// function to use to combine skins
// skins is a + seperated string
function setActiveStyleSheetEx(skins) {
  var j, i, a, main;

  if (!skins) return;
  skins = skins.split("+");
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;      
    }
  }
  for (j = 0; j < skins.length; j++)
  {
	  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
		  if(a.getAttribute("title") == skins[j]) a.disabled = false;
		}
	  }  
  }
  
}

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

function getPreferredStyleSheet() {
  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) {
  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) {
  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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheetEx(title);

  // find the skin selector
  skinsel = document.getElementById('skinSelector');
  if (skinsel)
  {	  
	  for (i = 0; i < skinsel.options.length; i++)
	  {
		  if (skinsel.options[i].value == title)
		  {
			  skinsel.options[i].selected = true;
			  break;
		  }
		  skinsel.options[i].selected = false;
	  }
  }
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheetEx(title);

