var cookie_number = readCookie("style_number");
var $STYLESHEET = cookie_number ? parseFloat(cookie_number) : 1;
var $STYLESHEET_TOTAL = 3;


function change_stylesheet( action)
{
  $STYLESHEET = $STYLESHEET + action;
  if( $STYLESHEET < 1)
    $STYLESHEET = 1;
  if( $STYLESHEET > $STYLESHEET_TOTAL)
    $STYLESHEET = $STYLESHEET_TOTAL;
  
  setActiveStyleSheet( 'style'+$STYLESHEET) 
  // for (var i=0; i <= $STYLESHEET_TOTAL; i++) {
  //   if( i == $STYLESHEET)
  //     var rel = 'stylesheet';
  //   else
  //     var rel = 'alternate stylesheet';
  //   document.getElementById( 'style' + i).rel = rel;
  // };
}

function setActiveStyleSheet(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.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  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() {
  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();
  setActiveStyleSheet(title);
}

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

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

var Tabs = Class.create ();

Tabs.prototype = {
  
  initialize:  function (ids, start_on) {
			this.ids = ids;
      this.class_active = 'active';
      this.class_inactive = 'inactive';
			this.start_on = start_on && $(start_on + '_tab') ? start_on : this.ids[0];
      this.eventos();
			this.actua (false, this.start_on);
  },
          
  eventos: function () {
      for (var i = 0 ; i < this.ids.length ; i++) {
        Event.observe (this.ids [i] + '_tab', 'click', this.actua.bindAsEventListener (this, this.ids [i]), false);
      }
  },
  
  actua:    function (e, current) {        
      tab = $(current + '_tab');
  		box = $(current + '_box');
      tab.className = this.class_active; 
      Element.show (box); 
      for (var i = 0 ; i < this.ids.length ; i++)
        if( this.ids[i] != current)
  			{
  			  if( !$(this.ids[i] + '_tab'))
  			    alert( this.ids[i] + '_tab')
  				$(this.ids[i] + '_tab').className =  this.class_inactive;
        	Element.hide (this.ids[i] + '_box');
  			}
   }
};
