/*!
 * Ext JS Library 3.2.1
 * Copyright(c) 2006-2010 Ext JS, Inc.
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
 
var firstLoad = true;
var manualUnload = false;

function setActiveStyleSheet(title) {
	// Wenn es kein IE ist oder bei IEs wenns beim ersten laden ist (da funktionierts)
	if (!Ext.isIE || firstLoad)
	{
		var i,
			a,
			links = document.getElementsByTagName("link"),
			len = links.length;
		for (i = 0; i < len; i++) {
			a = links[i];
			if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
				a.disabled = true;
				if (a.getAttribute("title") == title) a.disabled = false;
			}
		}
		firstLoad = false;
	}
	else
	{
		// IE
		// Cookie manuell setzen und Seite neu laden
		createCookie("style", title, 365);
		manualUnload = true;
		location.reload();
	}
}

function getActiveStyleSheet() {
    var i,
        a,
        links = document.getElementsByTagName("link"),
        len = links.length;
    for (i = 0; i < len; i++) {
        a = links[i];
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
            return a.getAttribute("title");
        }
    }
    return null;
}

function getNextStyleSheet ()
{
	var nextSS;
	
	switch (getActiveStyleSheet())
	{
		case 'blau':
			nextSS = 'grau';
			break;
		case 'grau':
			nextSS = 'schwarz';
			break;
		case 'schwarz':
			nextSS = 'blau';
			break;
		default:
			nextSS = 'blau';
			break;
	}

	return nextSS;
}

function getPreferredStyleSheet() {
    var i,
        a,
        links = document.getElementsByTagName("link"),
        len = links.length;
    for (i = 0; i < len; i++) {
        a = links[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 + "=",
        ca = document.cookie.split(';'),
        i,
        c,
        len = ca.length;
    for ( i = 0; i < len; i++) {
        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);
	Ext.getCmp ('btnChangeTemplate').setText (Ext.variables.styles[getNextStyleSheet()]);
}

window.onunload = function (e) {
	if (!manualUnload)
	{
		var title = getActiveStyleSheet();
		if (title === null)	title = 'grau';
		createCookie("style", title, 365);
	}
}

// var cookie = readCookie("style");
// var title = cookie ? cookie : getPreferredStyleSheet();
// setActiveStyleSheet(title);

