
/*
	@method:  Cookie toolkit.
	@version:   1.0
*/
Cookies =
{
	_version:1.0,
	getCookie:function(s)
	{
		var nameEQ = s+"=";
		var a = document.cookie.split(';');
		for (var i=0; i < a.length; i++)
		{
			var c = a[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;
	},
	setCookie:function(name, value, expires, path, domain, secure)
	{
		var curCookie = name+"="+escape(value)+((expires) ? "; expires="+expires.toGMTString() : "")+((path) ? "; path="+path : "")+((domain) ? "; domain="+domain : "")+((secure) ? "; secure" : ""); 
		document.cookie = curCookie;
	},
	isEnabled:function()
	{
		var cookies = false, exp = new Date(); exp.setTime(exp.getTime()+1800000);
		Cookies.setCookie("cookies", "cookies", exp, false, false, false);
		if (Cookies.getCookie("cookies")) cookies = true;
		exp = new Date(); exp.setTime(exp.getTime() - 1800000);
		Cookies.setCookie("cookies", "cookies", exp, false, false, false);
		return cookies;
	}
}

/*
	@method:  Style Switcher.
	@version:   1.1
*/
function styleSwitcher(prefix)
{
    this.cookie = prefix+"-style";
    this.expiration = null;
    this.index = null;
    this.prefix = prefix;
    this.stylesheets = [];
    this.location = window.location;
    
    this.init = function()
    {
        var sheets = document.getElementsByTagName("link");
        for (var i=0; i<sheets.length; i++)
        {
            var o = sheets[i], primary = (o.id.substring((o.id.length-8), o.id.length)=="-primary");
            if (o.id.substring(0, this.prefix.length)==this.prefix)
            {
                if (primary) this.index = i;
                this.stylesheets[this.stylesheets.length] = {id:o.id, primary:primary};
                this.setDisabled(o);
            }
        }
        if (this.stylesheets.length) this.setDefaults();
    }
    this.get = function(id)
    {
        return (document.getElementById)?document.getElementById(id):document.all[id];
    }
    this.getPreferred = function()
    {
        var cookie = Cookies.getCookie(this.cookie);
        if (cookie!="undefined"&&cookie!=null&&this.get(this.stylesheets[cookie].id)) { this.index = cookie; return this.stylesheets[cookie].id; }
        for (var i in this.stylesheets) { if (this.stylesheets[i].primary) { this.index = i; return this.stylesheets[i].id; } }
        this.index = 0; return this.stylesheets[0].id;
    }
    this.setDisabled = function(o)
    {
        o.setAttribute("rel", "alternative");
		o.disabled = true;
    }
    this.setEnabled = function(o)
    {
        o.setAttribute("rel", "stylesheet");
		o.disabled = false;
    }
    this.setDefaults = function()
    {
        this.setEnabled(this.get(this.getPreferred()));
		this.setExpiration();
        Cookies.setCookie(this.cookie, this.index, this.expiration, false, false, false);
    }
    this.setExpiration = function()
    {
        this.expiration = new Date(); this.expiration.setTime(this.expiration.getTime()+2592000000);
    }
	this.set = function(id, r)
	{
		if (id>(this.stylesheets.length-1)||id<0) return false;
		this.index = id;
		this.swap(this.stylesheets[this.index].id, r);
	}
    this.setNext = function(r)
    {
        if (this.index<(this.stylesheets.length-1)) this.index++;
        this.swap(this.stylesheets[this.index].id, r);
    }
    this.setPrevious = function(r)
    {
        if (this.index>0) this.index--;
        this.swap(this.stylesheets[this.index].id, r);
    }
    this.swap = function(id, r)
    {
		this.setExpiration();
        Cookies.setCookie(this.cookie, this.index, this.expiration, false, false, false);
        if (r) this.refresh();
        else for (var i in this.stylesheets) (this.stylesheets[i].id==id) ? this.setEnabled(this.get(this.stylesheets[i].id)) : this.setDisabled(this.get(this.stylesheets[i].id));
    }
    this.refresh = function()
    {
		window.location = this.location;
    }
}

styleType = new styleSwitcher("major-");
styleType.init();

textSize = new styleSwitcher("text-");
textSize.init();

function putStyleText()
{
	if (styleType.index==1) document.getElementById('majorStyle').innerHTML = "default site";
    else document.getElementById('majorStyle').innerHTML = "text version";
}

function swapMajorStyle(o)
{
    if (styleType.index==1) { styleType.setPrevious(true); o.innerHTML = "text version"; }
    else { styleType.setNext(true); o.innerHTML = "default site"; }
	return false;
}
