1 | /** |
---|
2 | * Cookie plugin |
---|
3 | * |
---|
4 | * Copyright (c) 2006 Klaus Hartl (stilbuero.de) |
---|
5 | * Dual licensed under the MIT and GPL licenses: |
---|
6 | * http://www.opensource.org/licenses/mit-license.php |
---|
7 | * http://www.gnu.org/licenses/gpl.html |
---|
8 | * |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Create a cookie with the given name and value and other optional parameters. |
---|
13 | * |
---|
14 | * @example $.cookie('the_cookie', 'the_value'); |
---|
15 | * @desc Set the value of a cookie. |
---|
16 | * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true }); |
---|
17 | * @desc Create a cookie with all available options. |
---|
18 | * @example $.cookie('the_cookie', 'the_value'); |
---|
19 | * @desc Create a session cookie. |
---|
20 | * @example $.cookie('the_cookie', null); |
---|
21 | * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain |
---|
22 | * used when the cookie was set. |
---|
23 | * |
---|
24 | * @param String name The name of the cookie. |
---|
25 | * @param String value The value of the cookie. |
---|
26 | * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. |
---|
27 | * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. |
---|
28 | * If a negative value is specified (e.g. a date in the past), the cookie will be deleted. |
---|
29 | * If set to null or omitted, the cookie will be a session cookie and will not be retained |
---|
30 | * when the the browser exits. |
---|
31 | * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). |
---|
32 | * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). |
---|
33 | * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will |
---|
34 | * require a secure protocol (like HTTPS). |
---|
35 | * @type undefined |
---|
36 | * |
---|
37 | * @name $.cookie |
---|
38 | * @cat Plugins/Cookie |
---|
39 | * @author Klaus Hartl/klaus.hartl@stilbuero.de |
---|
40 | */ |
---|
41 | |
---|
42 | /** |
---|
43 | * Get the value of a cookie with the given name. |
---|
44 | * |
---|
45 | * @example $.cookie('the_cookie'); |
---|
46 | * @desc Get the value of a cookie. |
---|
47 | * |
---|
48 | * @param String name The name of the cookie. |
---|
49 | * @return The value of the cookie. |
---|
50 | * @type String |
---|
51 | * |
---|
52 | * @name $.cookie |
---|
53 | * @cat Plugins/Cookie |
---|
54 | * @author Klaus Hartl/klaus.hartl@stilbuero.de |
---|
55 | */ |
---|
56 | jQuery.cookie = function(name, value, options) { |
---|
57 | if (typeof value != 'undefined') { // name and value given, set cookie |
---|
58 | options = options || {}; |
---|
59 | if (value === null) { |
---|
60 | value = ''; |
---|
61 | options.expires = -1; |
---|
62 | } |
---|
63 | var expires = ''; |
---|
64 | if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { |
---|
65 | var date; |
---|
66 | if (typeof options.expires == 'number') { |
---|
67 | date = new Date(); |
---|
68 | date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); |
---|
69 | } else { |
---|
70 | date = options.expires; |
---|
71 | } |
---|
72 | expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE |
---|
73 | } |
---|
74 | // CAUTION: Needed to parenthesize options.path and options.domain |
---|
75 | // in the following expressions, otherwise they evaluate to undefined |
---|
76 | // in the packed version for some reason... |
---|
77 | var path = options.path ? '; path=' + (options.path) : ''; |
---|
78 | var domain = options.domain ? '; domain=' + (options.domain) : ''; |
---|
79 | var secure = options.secure ? '; secure' : ''; |
---|
80 | document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); |
---|
81 | } else { // only name given, get cookie |
---|
82 | var cookieValue = null; |
---|
83 | if (document.cookie && document.cookie != '') { |
---|
84 | var cookies = document.cookie.split(';'); |
---|
85 | for (var i = 0; i < cookies.length; i++) { |
---|
86 | var cookie = jQuery.trim(cookies[i]); |
---|
87 | // Does this cookie string begin with the name we want? |
---|
88 | if (cookie.substring(0, name.length + 1) == (name + '=')) { |
---|
89 | cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); |
---|
90 | break; |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | return cookieValue; |
---|
95 | } |
---|
96 | }; |
---|
97 | |
---|
98 | if (jQuery.cookie('picture-menu') == 'visible') { |
---|
99 | jQuery("head").append("<style type=\"text/css\">#content.contentWithMenu, #the_page > .content {margin-left:240px;}</style>"); |
---|
100 | } else { |
---|
101 | jQuery("head").append("<style type=\"text/css\">#the_page #menubar {display:none;} #content.contentWithMenu, #the_page > .content {margin-left:35px;}</style>"); |
---|
102 | } |
---|
103 | |
---|
104 | function hideMenu(delay) { |
---|
105 | var menubar = jQuery("#menubar"); |
---|
106 | var menuswitcher = jQuery("#menuSwitcher"); |
---|
107 | var content = jQuery("#the_page > .content"); |
---|
108 | var pcontent = jQuery("#content"); |
---|
109 | |
---|
110 | menubar.hide(delay); |
---|
111 | menuswitcher.addClass("menuhidden").removeClass("menushown"); |
---|
112 | content.addClass("menuhidden").removeClass("menushown"); |
---|
113 | pcontent.addClass("menuhidden").removeClass("menushown"); |
---|
114 | jQuery.cookie('picture-menu', 'hidden', { path: "/" }); |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | function showMenu(delay) { |
---|
119 | |
---|
120 | var menubar = jQuery("#menubar"); |
---|
121 | var menuswitcher = jQuery("#menuSwitcher"); |
---|
122 | var content = jQuery("#the_page > .content"); |
---|
123 | var pcontent = jQuery("#content"); |
---|
124 | |
---|
125 | menubar.show(delay); |
---|
126 | menuswitcher.addClass("menushown").removeClass("menuhidden"); |
---|
127 | content.addClass("menushown").removeClass("menuhidden"); |
---|
128 | pcontent.addClass("menushown").removeClass("menuhidden"); |
---|
129 | jQuery.cookie('picture-menu', 'visible', { path: "/" }); |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | function hideInfo(delay) { |
---|
134 | |
---|
135 | var imageInfos = jQuery("#imageInfos"); |
---|
136 | var infoswitcher = jQuery("#infoSwitcher"); |
---|
137 | var theImage = jQuery("#theImage"); |
---|
138 | |
---|
139 | imageInfos.hide(delay); |
---|
140 | infoswitcher.addClass("infohidden").removeClass("infoshown"); |
---|
141 | theImage.addClass("infohidden").removeClass("infoshown"); |
---|
142 | jQuery.cookie('side-info', 'hidden', { path: "/" }); |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | function showInfo(delay) { |
---|
147 | |
---|
148 | var imageInfos = jQuery("#imageInfos"); |
---|
149 | var infoswitcher = jQuery("#infoSwitcher"); |
---|
150 | var theImage = jQuery("#theImage"); |
---|
151 | |
---|
152 | imageInfos.show(delay); |
---|
153 | infoswitcher.addClass("infoshown").removeClass("infohidden"); |
---|
154 | theImage.addClass("infoshown").removeClass("infohidden"); |
---|
155 | jQuery.cookie('side-info', 'visible', { path: "/" }); |
---|
156 | |
---|
157 | } |
---|
158 | |
---|
159 | jQuery("document").ready(function (jQuery) { |
---|
160 | |
---|
161 | // side-menu show/hide |
---|
162 | |
---|
163 | var sidemenu = jQuery.cookie('picture-menu'); |
---|
164 | var menubar = jQuery("#menubar"); |
---|
165 | |
---|
166 | if (menubar.length == 1) { |
---|
167 | |
---|
168 | jQuery("#menuSwitcher").html("<div class=\"switchArrow\"> </div>"); |
---|
169 | |
---|
170 | // if cookie says the menu is hiding, keep it hidden! |
---|
171 | if (sidemenu == 'visible') { |
---|
172 | showMenu(0); |
---|
173 | } else { |
---|
174 | hideMenu(0); |
---|
175 | } |
---|
176 | |
---|
177 | jQuery("#menuSwitcher").click(function () { |
---|
178 | if (jQuery("#menubar").is(":hidden")) { |
---|
179 | showMenu(0); |
---|
180 | return false; |
---|
181 | } else { |
---|
182 | hideMenu(0); |
---|
183 | return false; |
---|
184 | } |
---|
185 | }); |
---|
186 | |
---|
187 | } |
---|
188 | |
---|
189 | // info show/hide |
---|
190 | |
---|
191 | var sideinfo = jQuery.cookie('side-info'); |
---|
192 | var imageInfos = jQuery("#imageInfos"); |
---|
193 | |
---|
194 | if (imageInfos.length == 1) { |
---|
195 | |
---|
196 | jQuery("#infoSwitcher").html("<div class=\"switchArrow\"> </div>"); |
---|
197 | |
---|
198 | // if cookie says the menu is hiding, keep it hidden! |
---|
199 | if (sideinfo == 'hidden') { |
---|
200 | hideInfo(0); |
---|
201 | } else { |
---|
202 | showInfo(0); |
---|
203 | } |
---|
204 | |
---|
205 | jQuery("#infoSwitcher").click(function () { |
---|
206 | if (jQuery("#imageInfos").is(":hidden")) { |
---|
207 | showInfo(0); |
---|
208 | return false; |
---|
209 | } else { |
---|
210 | hideInfo(0); |
---|
211 | return false; |
---|
212 | } |
---|
213 | }); |
---|
214 | |
---|
215 | } |
---|
216 | |
---|
217 | // comments show/hide |
---|
218 | |
---|
219 | var commentsswicther = jQuery("#commentsSwitcher"); |
---|
220 | var comments = jQuery("#thePicturePage #comments"); |
---|
221 | |
---|
222 | commentsswicther.html("<div class=\"switchArrow\"> </div>"); |
---|
223 | |
---|
224 | if (comments.length == 1) { |
---|
225 | var comments_button = jQuery("#comments h3"); |
---|
226 | |
---|
227 | if (comments_button.length == 0) { |
---|
228 | jQuery("#addComment").before("<h3>Comments</h3>"); |
---|
229 | comments_button = jQuery("#comments h3"); |
---|
230 | } |
---|
231 | |
---|
232 | if (jQuery.cookie('comments') == 'visible') { |
---|
233 | comments.addClass("commentsshown"); |
---|
234 | comments_button.addClass("comments_toggle").addClass("comments_toggle_off"); |
---|
235 | } else { |
---|
236 | comments.addClass("commentshidden"); |
---|
237 | comments_button.addClass("comments_toggle").addClass("comments_toggle_on"); |
---|
238 | } |
---|
239 | |
---|
240 | comments_button.click(function () { commentsToggle() }); |
---|
241 | commentsswicther.click(function () { commentsToggle() }); |
---|
242 | |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | }); |
---|
247 | |
---|
248 | function commentsToggle() { |
---|
249 | var comments = jQuery("#thePicturePage #comments"); |
---|
250 | var comments_button = jQuery("#comments h3"); |
---|
251 | |
---|
252 | if (comments.hasClass("commentshidden")) { |
---|
253 | comments.removeClass("commentshidden").addClass("commentsshown"); |
---|
254 | comments_button.addClass("comments_toggle_off").removeClass("comments_toggle_on"); ; |
---|
255 | jQuery.cookie('comments', 'visible', { path: "/" }); |
---|
256 | } else { |
---|
257 | comments.addClass("commentshidden").removeClass("commentsshown"); |
---|
258 | comments_button.addClass("comments_toggle_on").removeClass("comments_toggle_off"); ; |
---|
259 | jQuery.cookie('comments', 'hidden', { path: "/" }); |
---|
260 | } |
---|
261 | |
---|
262 | } |
---|