Upgrade to ExtJS 3.2.1 - Released 04/27/2010
[extjs.git] / examples / themes / styleswitcher.js
1 /*!
2  * Ext JS Library 3.2.1
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 function setActiveStyleSheet(title) {
8     var i,
9         a,
10         links = document.getElementsByTagName("link"),
11         len = links.length;
12     for (i = 0; i < len; i++) {
13         a = links[i];
14         if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
15             a.disabled = true;
16             if (a.getAttribute("title") == title) a.disabled = false;
17         }
18     }
19 }
20
21 function getActiveStyleSheet() {
22     var i,
23         a,
24         links = document.getElementsByTagName("link"),
25         len = links.length;
26     for (i = 0; i < len; i++) {
27         a = links[i];
28         if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
29             return a.getAttribute("title");
30         }
31     }
32     return null;
33 }
34
35 function getPreferredStyleSheet() {
36     var i,
37         a,
38         links = document.getElementsByTagName("link"),
39         len = links.length;
40     for (i = 0; i < len; i++) {
41         a = links[i];
42         if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) {
43             return a.getAttribute("title");
44         }
45     }
46     return null;
47 }
48
49 function createCookie(name, value, days) {
50     if (days) {
51         var date = new Date();
52         date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
53         var expires = "; expires=" + date.toGMTString();
54     } else {
55         expires = "";
56     }
57     document.cookie = name + "=" + value + expires + "; path=/";
58 }
59
60 function readCookie(name) {
61     var nameEQ = name + "=",
62         ca = document.cookie.split(';'),
63         i,
64         c,
65         len = ca.length;
66     for ( i = 0; i < len; i++) {
67         c = ca[i];
68         while (c.charAt(0) == ' ') {
69             c = c.substring(1, c.length);
70         }
71         if (c.indexOf(nameEQ) == 0) {
72             return c.substring(nameEQ.length, c.length);
73         }
74     }
75     return null;
76 }
77
78 window.onload = function (e) {
79     var cookie = readCookie("style");
80     var title = cookie ? cookie : getPreferredStyleSheet();
81     setActiveStyleSheet(title);
82 }
83
84 window.onunload = function (e) {
85     var title = getActiveStyleSheet();
86     createCookie("style", title, 365);
87 }
88
89 var cookie = readCookie("style");
90 var title = cookie ? cookie : getPreferredStyleSheet();
91 setActiveStyleSheet(title);