var styleSwitch = {
	  "styleOptions":  [["Web 2.0","http://www.optikjunk.de/portfolio/css/portfolio.css","zwei"], ["Web 1.0","http://www.optikjunk.de/portfolio/css/portfolio_2003.css","eins"], ["Web 0.1","http://www.optikjunk.de/portfolio/css/nostyle.css","null"]],
	  init: function() {
	  	  var keks = styleSwitch.readCookie();
    	  if(keks !== false) {
    	  	styleSwitch.setActiveStyleSheet(keks);
    	    //styleSwitch.setBodyClass(styleSwitch.getActiveStyleSheet(), styleSwitch.styleOptions);
    	  }
    	  styleSwitch.setBodyClass(styleSwitch.getActiveStyleSheet(), styleSwitch.styleOptions);
    		var sideBar = document.getElementById("sidebar").getElementsByTagName("ul")[0];
        var styleNavLi = document.createElement("li");
        styleNavLi.setAttribute('id', 'styleswitch');
        var styleNavH3 = document.createElement("h3");
        styleNavH3.appendChild(document.createTextNode("Style Switch"));
        styleNavLi.appendChild(styleNavH3);
        var StyleNavUl = document.createElement("ul");
        for(var i=0; i< styleSwitch.styleOptions.length; i++) {
            var li = document.createElement("li");
            li.setAttribute('id', styleSwitch.styleOptions[i][2]);
            var a = document.createElement("a");
            var stringA = styleSwitch.styleOptions[i][0];
            //das kann ja wohl zusammengefasstwerden
            var textA = document.createTextNode(stringA);
            a.appendChild(textA);
            a.href = styleSwitch.styleOptions[i][1];
            a.setAttribute("rel", "styleswitch");
            li.appendChild(a);
            StyleNavUl.appendChild(li);
        }
        styleNavLi.appendChild(StyleNavUl);
        sideBar.appendChild(styleNavLi);

        var styleLinks = styleNavLi.getElementsByTagName('a');
        for (var j = 0; j < styleLinks.length; j++)
        {
           styleLinks[j].onclick = styleSwitch.clickHandler;
        }
    },

    clickHandler: function()
    {
   	  if(this.firstChild.nodeValue !== styleSwitch.readCookie()) {
        var linkText = this.firstChild.nodeValue;
        styleSwitch.setActiveStyleSheet(linkText);
        styleSwitch.setBodyClass(linkText, styleSwitch.styleOptions);
        styleSwitch.writeCookie(linkText);
      }
      return false;
    },

    setActiveStyleSheet: function(title)
    {
      var i, a;
      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;
          }
        }
      }
    },

    getActiveStyleSheet: function()
		{
		  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;
		},

    readCookie: function()
    {
        var myCookie = document.cookie.split("=");
            if(myCookie[1]) {
                return unescape(myCookie[1]);
            }else {
                return false;
            }
    },

    writeCookie: function(theStyleName)
    {
			if(styleSwitch.readCookie !== theStyleName) {
      	var myCookie = 'stylePref=' + escape(theStyleName) +';';
        myCookie = myCookie + 'path=/;';
        myCookie = myCookie + 'domain=optikjunk.de;';
      //myCookie = myCookie + 'domain=/;';
     		document.cookie = myCookie;
      }
  	},

  	setBodyClass: function (styleName, styleArray)
  	{
  		for (var i=0; i<styleArray.length; i++) {
  			if (styleName == styleArray[i][0]) {
  				var body = document.getElementsByTagName('body');
  				Core.addClass(body[0], styleArray[i][2]);
  				//body[0].setAttribute('class', styleArray[i][2])
  			}
  		}
  	}
};

var externalLinks = {
    init: function()
    {
        var extLinks = Core.getElementsByClass("external");
        for (var i = 0; i < extLinks.length; i++)
        {
            extLinks[i].onclick = externalLinks.clickHandler;

        }
    },
    clickHandler: function()
    {
        open(this.href);
        return false;
    }
};

//Core.start(styleSwitch);
Core.start(externalLinks);
