var tc_name = "_tc";

// ThemeChanger class
function ThemeChanger(js, ua, cookie) {
  this.initialize(js, ua, cookie);
};

ThemeChanger.prototype = {
  initialize : function(js, ua, cookie) {
    this.js = js;
    this.ua = ua;
    this.cookie = cookie;
    this.cookie_limit = 3; // day
    if (location.pathname) {
      this.cookie_path = location.pathname.replace(/[^\/]+$/, '');
    } else if (document.URL) {
      this.cookie_path = document.URL.replace(/^https?:\/\/[^\/]+/, '').replace(/[^\/]+$/, '');
    } else {
      this.cookie_path = '/';
    }
    this.default_title = "DEFAULT";
    this.default_cookie_value = "__default__";
  },
  
  write_header : function() {
    this.theme = this.cookie.get_value("theme");
    this.write_css(this.theme);
  },
  
  write_css : function(theme) {
    this.make_css(this.default_title, theme, "all");
  },

  make_css : function(title, theme, media) {
    if (theme == this.default_cookie_value) theme = this.default_theme;
    if (!theme) return;

    var attr_str = (title) ? '|title|' + title : '';

    attr_str += (media) ? '|media|'+media : '';
      
    if (!((this.ua.NS6 || this.ua.NN60) && this.ua.gecko_ver < 20020514) &&
        document.contentType &&
        document.contentType.indexOf('xml') > -1 &&
        document.createProcessingInstruction) {
      var node = document.documentElement;
      for (;node = node.previousSibling;) {
        if (node.nodeType == Node.PROCESSING_INSTRUCTION_NODE) {
          if (node.sheet.title == this.select_title) {
            var pi = node.cloneNode(false);
            var data = this.parse_data(node.data);
            var new_data = "";
            for(var j = 0; j < data.length; j += 2) {
              if (data[j] == "href") {
                if (!this.default_theme) this.default_theme = data[j+1];
                data[j+1] = theme;
              }
              new_data += data[j] + '="' + data[j+1] + '" ';
            }
            pi.data = new_data;
            document.replaceChild(pi, node);
            break;
          }
        }
      }
    } else {
      var links = document.getElementsByTagName("link");

      for(var i = 0; i < links.length; i++) {
        if (links[i].title == this.default_title) {
          var link = links[i];
          if (!this.default_theme) this.default_theme = link.href;

          if (this.ua.MacIE5) {
            attr_str += "|rel|" + link.rel;
            attr_str += "|href|" + theme;
            attr_str = "link" + attr_str;
            if (link.parentNode.removeChild) link.parentNode.removeChild(link);
            this.clear_style();
            document.write(make_node(attr_str));
          } else {
            link.href = theme;
          }

          return;
        }
      }
    }
  },

  parse_data : function(data) {
    var res = new Array;
    var attrs = data.split(" ");
    for(var i in attrs) {
      var y = attrs[i].split("=");
      if (y[i]) {
        res = res.concat([y[0], y[1].slice(1, -1)]);
      }
    }
    return res;
  },

  write_form : function() {
    var str = make_node("option|value|" + this.default_cookie_value, this.default_title);
    
    var metas = document.getElementsByTagName("meta");

    var option, content;
    for(var i = 0; i < metas.length; i++) {
      if (metas[i].getAttribute("name") == "theme") {
        str += "<option value=";
              
        content = this.parse_theme_content(metas[i].getAttribute("content"));

        str += '"' + content[1] + '" ';
              
        if (this.theme == content[1]) {
          str += 'selected="yes" ';
        }
    
        str += ">" + content[0] + '</option>';
      }
    }
    document.write(make_node("select|name|theme|onchange|" +
                             tc_name +
                             ".select_theme(this.options[this.selectedIndex].value);",
                             str));
  },

  parse_theme_content : function(content) {
    return(content.split("|"));
  },

  select_theme : function(theme) {
    this.put_cookie(theme);

    if (this.ua.overIE5 || this.ua.IE4 || this.ua.Konq) {
      location.href = location.href.match(/^[^#?]*/);
      if (this.ua.Konq) {
        location.reload(true);
      }
    } else {
      this.clear_style();
      this.write_css(theme);
    }
  },

  put_cookie : function(value) {
    if (this.cookie_path) {
      this.cookie.set_value('theme', value, this.cookie_limit, this.cookie_path);
    }
  },

  clear_style : function() {
    var sss = document.styleSheets;
    if (sss) {
      for(var i = 0; i <  sss.length; i++) {
        if (sss[i] && sss[i].title && sss[i].title == this.default_title) {
          if (sss.deleteRule) {
            sss.deleteRule(i);
          } else {
            sss[i].disabled = true;
          }
        }
      }
    }
  }
  
};

eval("var " + tc_name + " = new ThemeChanger(" + js_name + ", " + ua_name + ", " + cookie_name + ");");
eval(tc_name + ".write_header();");
