1 /**
  2  * @namespace Namce space for the CMS functions.
  3  */
  4 if (!Nornix.cms) Nornix.cms = {};
  5 
  6 Nornix.toggle = function (rootElement, tagName, toggleClass, headingClass, title)
  7 {
  8 	var elements = document.getElementById(rootElement).getElementsByTagName(tagName);
  9 	var link = document.createElement("a");
 10 	link.title = title;
 11 	link.href = "javascript:;";
 12 	var i = 0, element;
 13 	while (element = elements[i++])
 14 	{
 15 		if (Nornix.css.contains(element, toggleClass))
 16 		{
 17 			Nornix.css.add(element, "closed");
 18 			var p = element.previousSibling;
 19 			Nornix.css.add(p, headingClass);
 20 			Nornix.css.add(p, "closed");
 21 			var linkCopy = link.cloneNode(false);
 22 			while (p.childNodes.length) // move nodes "manually"
 23 			{
 24 				linkCopy.appendChild(p.firstChild);
 25 			}
 26 			p.appendChild(linkCopy);
 27 			Nornix.events.add(p, "click", toggle, true);
 28 		}
 29 	}
 30 	
 31 	function toggle ()
 32 	{
 33 		toggle = this.nextSibling;
 34 		if (Nornix.css.contains(toggle, "closed"))
 35 		{
 36 			Nornix.css.swap(toggle, "closed", "open");
 37 			Nornix.css.swap(this, "closed", "open");
 38 		}
 39 		else
 40 		{
 41 			Nornix.css.swap(toggle, "open", "closed");
 42 			Nornix.css.swap(this, "open", "closed");
 43 		}
 44 		return false;
 45 	}
 46 };
 47 
 48 
 49 /**
 50  * Get background color of element.
 51  * @param {HTMLElement} el element to get the background color from
 52  * @return color as an object like {r: XX, g: XX, b: XX} or undefined
 53  */
 54 Nornix.getBackgroundColor = function (el)
 55 {
 56 	var c = Nornix.css.getProperty(el, "background-color");
 57 	if (c === undefined || c === "" || c === "transparent") return null;
 58 	return Nornix.convert2Rgb(c);
 59 };
 60 
 61 /**
 62  * Get text color of element.
 63  * @param {HTMLElement} el element to get the text color from
 64  * @return color as an object like {r: XX, g: XX, b: XX} or undefined
 65  */
 66 Nornix.getTextColor = function (el)
 67 {
 68 	var c = Nornix.css.getProperty(el, "color");
 69 	if (c === undefined || c === "" || c === "transparent") return null;
 70 	return Nornix.convert2Rgb(c);
 71 };
 72 
 73 Nornix.convert2Rgb = function (c)
 74 {
 75 	var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
 76 	if (rgb)
 77 	{
 78 		return {r : parseInt(rgb[1]), g : parseInt(rgb[2]), b : parseInt(rgb[3])};
 79 	}
 80 	return Nornix.makeRgbColor(c);
 81 };
 82 
 83 
 84 /**
 85  * Create string representation of RGB color information.
 86  * @param {Object} rgb color information like {r: 23.342, g: 12.372, b: 34}
 87  * @return string like rgb(23, 12, 34)
 88  */
 89 Nornix.printRgbColor = function (rgb)
 90 {
 91 	return "rgb(" + Math.round(rgb.r) + "," + Math.round(rgb.g) + "," + Math.round(rgb.b) + ")";
 92 };
 93 
 94 /**
 95  * Create RGB color object from hexadecimal string represenation.
 96  * @param {String} color, must begin with "#", then 3 or 6 hexadecimal digits.
 97  * @return color as RGB object like {r: XX, g: XX, b: XX} or undefined
 98  */
 99 Nornix.makeRgbColor = function (c)
100 {
101 	if (c.charAt(0) === "#")
102 	{
103 		if (c.length === 4)
104 		{
105 			c = "#" + c.charAt(1) + c.charAt(1) + c.charAt(2) + c.charAt(2) + c.charAt(3) + c.charAt(3);
106 		}
107 		if (c.length === 7)
108 		{
109 			return {
110 				r : parseInt(c.substr(1, 2), 16),
111 				g : parseInt(c.substr(3, 2), 16),
112 				b : parseInt(c.substr(5, 2), 16)
113 				};
114 		}
115 	}
116 };
117 
118 /**
119  * Fade element from current background color to fade color and back.
120  *
121  */
122 Nornix.fader = function (el, className, fps, durationIn, durationOut)
123 {
124 	if (!fps) fps = 30;
125 	if (!durationIn) durationIn = 200;
126 	if (!durationOut) durationOut = 1500;
127 	if (!className) className = "fader";
128 
129 	var toColor = Nornix.getBackgroundColor(el);
130 	var toColorCopy = {r : toColor.r, g : toColor.g, b : toColor.b};
131 	var toTextColor = Nornix.getTextColor(el);
132 	var toTextColorCopy = {r : toTextColor.r, g : toTextColor.g, b : toTextColor.b};
133 
134 	// get colors from CSS
135 	var div = document.createElement("div");
136 	Nornix.css.add(div, className);
137 	document.documentElement.appendChild(div); // IE(7) fix
138 	var fromColor = Nornix.getBackgroundColor(div);
139 	var fromTextColor = Nornix.getTextColor(div);
140 	document.documentElement.removeChild(div);
141 	div = null;
142 
143 	var oldBackgroundColor = el.style.backgroundColor;
144 	var oldTextColor = el.style.color;
145 
146 	Nornix.fade(el,
147 	[
148 		{"property" : "background-color", "toColor" : fromColor,     "fromColor" : toColorCopy},
149 		{"property" : "color",            "toColor" : fromTextColor, "fromColor" : toTextColorCopy}
150 	],
151 	fps,
152 	durationIn,
153 	function () // function to run on stop
154 	{
155 		Nornix.fade(el,
156 		[
157 			{"property" : "background-color", "toColor" : toColor,     "fromColor" : fromColor,
158 			 "oldStyle" : oldBackgroundColor},
159 			{"property" : "color",            "toColor" : toTextColor, "fromColor" : fromTextColor,
160 			 "oldStyle" : oldTextColor}
161 		],
162 		fps,
163 		durationOut
164 		);
165 	});
166 };
167 
168 /**
169  * Fade element from one color to another.
170  */
171 Nornix.fade = function (el, fades, fps, duration, stopFunc)
172 {
173 	if (!fps) fps = 30;
174 	if (!duration) duration = 3000;
175 
176 	var framesCount = Math.round(fps * duration / 1000);
177 	var interval = Math.round(duration / framesCount);
178 	var frame = 0;
179 
180 	var aFade, i = 0;
181 	//  IN: property, toColor, fromColor, oldStyle
182 	//  PREPARE: jsPorperty, step
183 	while (aFade = fades[i++])
184 	{
185 		if (!aFade.property) aFade.property = "background-color";
186 		aFade.jsProperty = Nornix.css.prop2Js(aFade.property);
187 		aFade.step = {
188 			r : (aFade.toColor.r - aFade.fromColor.r) / framesCount,
189 			g : (aFade.toColor.g - aFade.fromColor.g) / framesCount,
190 			b : (aFade.toColor.b - aFade.fromColor.b) / framesCount
191 		};
192 	}
193 
194 	var intervalId = window.setInterval( function ()
195 		{
196 			if (++frame !== framesCount)
197 			{
198 				i = 0;
199 				while (aFade = fades[i++])
200 				{
201 					el.style[aFade.jsProperty] = Nornix.printRgbColor(aFade.fromColor);
202 					aFade.fromColor.r += aFade.step.r;
203 					aFade.fromColor.g += aFade.step.g;
204 					aFade.fromColor.b += aFade.step.b;
205 				}
206 			}
207 			else
208 			{
209 				window.clearInterval(intervalId);
210 				if (stopFunc)
211 				{
212 					stopFunc();
213 				}
214 				else
215 				{
216 					// only set old styles after the last link in the chain
217 					i = 0;
218 					while (aFade = fades[i++])
219 					{
220 						el.style[aFade.jsProperty] = aFade.oldStyle;
221 					}
222 				}
223 			}
224 		},
225 		interval
226 	); // end setInterval
227 };
228 
229 Nornix.getFileExtension = function (filename, separator)
230 {
231 	separator = separator ? separator : ".";
232 	if (filename.length < 2) return "";
233 	var pos = filename.lastIndexOf(separator); 
234 	if (pos === -1) return ""; 
235 	return filename.substr(pos + 1, filename.length); 	
236 };
237 
238 Nornix.cms.checkCookieInLogin = function (theForm, sessionName, cookieError)
239 {
240 	if (Nornix.cookies.read(sessionName)) return;
241 	p = document.createElement("p");
242 	p.className = "warning message";
243 	p.appendChild(document.createTextNode(cookieError));
244 	theForm.parentNode.insertBefore(p, theForm);
245 };
246 
247 Nornix.cms.initFileUploadForm = function (theForm, fileEdit, nameField, typeNo)
248 {
249 	Nornix.cms.fileEditField = theForm[fileEdit];
250 	Nornix.cms.nameField = theForm[nameField];
251 	Nornix.cms.typeField = theForm[typeNo];
252 	Nornix.events.add(Nornix.cms.fileEditField, "change", Nornix.cms.updateFileInfo);
253 };
254 
255 Nornix.cms.updateFileInfo = function ()
256 {
257 	// file type
258 	var value = this.value;
259 	var ext = Nornix.getFileExtension(value);
260 	if (ext)
261 	{
262 		var val = NornixDyn.fileExt[ext];
263 		if (val === null)
264 		{
265 			Nornix.cms.typeField.selectedIndex = 0;
266 		}
267 		else
268 		{
269 			Nornix.cms.typeField.selectedIndex = val;
270 		}
271 	}
272 	// file name
273 	if (Nornix.cms.nameField.value) return;
274 	var filename = Nornix.getFileExtension(value, "/");
275 	if (!filename)
276 	{
277 		filename = Nornix.getFileExtension(value, "\\");
278 	}
279 	if (!filename) return;
280 	var extLen = ext.length;
281 	if (extLen !== 0)
282 	{
283 		extLen++;
284 	}
285 	Nornix.cms.nameField.value = filename.substr(0, filename.length - extLen);
286 }
287 
288 Nornix.cms.updatePositionSelect = function ()
289 {
290 	var ups = Nornix.cms.updatePositionSelect,
291 		selectedPath = ups.pageField.options[ups.pageField.selectedIndex].text,
292 		pathLength,
293 		newSelect = ups.oldPositionField.cloneNode(true),
294 		i = 1, // don't touch option no 0
295 		option,
296 		options = newSelect.options;
297 	if (selectedPath !== "/")
298 	{
299 		selectedPath += "/";
300 	}
301 	pathLength = selectedPath.length;
302 	while (option = options[i++])
303 	{
304 		var text = option.text;
305 		if (text.substr(0, pathLength) !== selectedPath || text.substr(pathLength).indexOf("/") !== -1)
306 		{
307 			newSelect.removeChild(option);
308 			--i; // get next element right!
309 		}
310 	}
311 	ups.positionField.parentNode.replaceChild(newSelect, ups.positionField);
312 	ups.positionField = newSelect;
313 	if (newSelect.options.length === 1)
314 	{
315 		newSelect.disabled = true;
316 	}
317 	else
318 	{
319 		newSelect.disabled = false;
320 	}
321 };
322 
323 // make more generic?
324 Nornix.cms.updatePositionSelectInit = function ()
325 {
326 	var ups = Nornix.cms.updatePositionSelect;
327 	ups.positionField = document.getElementById("positionNo");
328 	ups.oldPositionField = ups.positionField.cloneNode(true);
329 //	ups.positionField.style.backgroundColor = "red"; /