PageSourceSearch

https://thaibird.nl/wp-content/themes/crab-spice/fw/js/core.utils.js

js thaibird.nl collected 2026-10-07 03:07:37 UTC 37,046 bytes, 1,141 lines download raw bytes

1/**
2 * Crab Spice Framework: Utilities
3 *
4 * @package	crab_spice
5 * @since	crab_spice 1.0
6 */
7
8
9/* Global variables manipulations
10---------------------------------------------------------------- */
11
12// Global variables storage
13if (typeof CRAB_SPICE_STORAGE == 'undefined') var CRAB_SPICE_STORAGE = {};
14
15// Get global variable
16function crab_spice_storage_get(var_name) {
17	return crab_spice_isset(CRAB_SPICE_STORAGE[var_name]) ? CRAB_SPICE_STORAGE[var_name] : '';
18}
19
20// Set global variable
21function crab_spice_storage_set(var_name, value) {
22	CRAB_SPICE_STORAGE[var_name] = value;
23}
24
25// Inc/Dec global variable with specified value
26function crab_spice_storage_inc(var_name) {
27	var value = arguments[1]==undefined ? 1 : arguments[1];
28	CRAB_SPICE_STORAGE[var_name] += value;
29}
30
31// Concatenate global variable with specified value
32function crab_spice_storage_concat(var_name, value) {
33	CRAB_SPICE_STORAGE[var_name] += ''+value;
34}
35
36// Get global array element
37function crab_spice_storage_get_array(var_name, key) {
38	return crab_spice_isset(CRAB_SPICE_STORAGE[var_name][key]) ? CRAB_SPICE_STORAGE[var_name][key] : '';
39}
40
41// Set global array element
42function crab_spice_storage_set_array(var_name, key, value) {
43	if (!crab_spice_isset(CRAB_SPICE_STORAGE[var_name])) CRAB_SPICE_STORAGE[var_name] = {};
44	CRAB_SPICE_STORAGE[var_name][key] = value;
45}
46
47// Inc/Dec global array element with specified value
48function crab_spice_storage_inc_array(var_name, key) {
49	var value = arguments[2]==undefined ? 1 : arguments[2];
50	CRAB_SPICE_STORAGE[var_name][key] += value;
51}
52
53// Concatenate global array element with specified value
54function crab_spice_storage_concat_array(var_name, key, value) {
55	CRAB_SPICE_STORAGE[var_name][key] += ''+value;
56}
57
58
59
60/* PHP-style functions
61---------------------------------------------------------------- */
62function crab_spice_isset(obj) {
63	return typeof(obj) != 'undefined';
64}
65
66function crab_spice_empty(obj) {
67	return typeof(obj) == 'undefined' || (typeof(obj)=='object' && obj == null) || (typeof(obj)=='array' && obj.length == 0) || (typeof(obj)=='string' && crab_spice_alltrim(obj)=='') || obj===0;
68}
69
70function crab_spice_is_array(obj)  {
71	"use strict";
72	return typeof(obj)=='array';
73}
74
75function crab_spice_is_object(obj)  {
76	"use strict";
77	return typeof(obj)=='object';
78}
79
80function crab_spice_clone_object(obj) {
81	if (obj == null || typeof(obj) != 'object') {
82		return obj;
83	}
84	var temp = {};
85	for (var key in obj) {
86		temp[key] = crab_spice_clone_object(obj[key]);
87	}
88	return temp;
89}
90
91function crab_spice_merge_objects(obj1, obj2)  {
92	"use strict";
93	for (var i in obj2) obj1[i] = obj2[i];
94	return obj1;
95}
96
97// Generates a storable representation of a value
98function crab_spice_serialize(mixed_val) {
99	"use strict";
100	var obj_to_array = arguments.length==1 || argument[1]===true;
101
102	switch (typeof(mixed_val)) {
103
104		case "number":
105			if (isNaN(mixed_val) || !isFinite(mixed_val))
106				return false;
107			else
108				return (Math.floor(mixed_val) == mixed_val ? "i" : "d") + ":" + mixed_val + ";";
109
110		case "string":
111			return "s:" + mixed_val.length + ":\"" + mixed_val + "\";";
112
113		case "boolean":
114			return "b:" + (mixed_val ? "1" : "0") + ";";
115
116		case "object":
117			if (mixed_val == null)
118				return "N;";
119			else if (mixed_val instanceof Array) {
120				var idxobj = { idx: -1 };
121				var map = [];
122				for (var i=0; i<mixed_val.length; i++) {
123					idxobj.idx++;
124					var ser = crab_spice_serialize(mixed_val[i]);
125					if (ser)
126						map.push(crab_spice_serialize(idxobj.idx) + ser);
127				}                                      
128				return "a:" + mixed_val.length + ":{" + map.join("") + "}";
129			} else {
130				var class_name = crab_spice_get_class(mixed_val);
131				if (class_name == undefined)
132					return false;
133				var props = new Array();
134				for (var prop in mixed_val) {
135					var ser = crab_spice_serialize(mixed_val[prop]);
136					if (ser)
137						props.push(crab_spice_serialize(prop) + ser);
138				}
139				if (obj_to_array)
140					return "a:" + props.length + ":{" + props.join("") + "}";
141				else
142					return "O:" + class_name.length + ":\"" + class_name + "\":" + props.length + ":{" + props.join("") + "}";
143			}
144
145		case "undefined":
146			return "N;";
147	}
148	return false;
149}
150
151// Returns the name of the class of an object
152function crab_spice_get_class(obj) {
153	"use strict";
154	if (obj instanceof Object && !(obj instanceof Array) && !(obj instanceof Function) && obj.constructor) {
155		var arr = obj.constructor.toString().match(/function\s*(\w+)/);
156		if (arr && arr.length == 2) return arr[1];
157	}
158	return false;
159}
160
161
162
163/* String functions
164---------------------------------------------------------------- */
165
166function crab_spice_in_list(str, list) {
167	"use strict";
168	var delim = arguments[2] ? arguments[2] : '|';
169	var icase = arguments[3] ? arguments[3] : true;
170	var retval = false;
171	if (icase) {
172		if (typeof(str)=='string') str = str.toLowerCase();
173		list = list.toLowerCase();
174	}
175	var parts = list.split(delim);
176	for (var i=0; i<parts.length; i++) {
177		if (parts[i]==str) {
178			retval=true;
179			break;
180		}
181	}
182	return retval;
183}
184
185function crab_spice_alltrim(str) {
186	"use strict";
187	var dir = arguments[1] ? arguments[1] : 'a';
188	var rez = '';
189	var i, start = 0, end = str.length-1;
190	if (dir=='a' || dir=='l') {
191		for (i=0; i<str.length; i++) {
192			if (str.substr(i,1)!=' ') {
193				start = i;
194				break;
195			}
196		}
197	}
198	if (dir=='a' || dir=='r') {
199		for (i=str.length-1; i>=0; i--) {
200			if (str.substr(i,1)!=' ') {
201				end = i;
202				break;
203			}
204		}
205	}
206	return str.substring(start, end+1);
207}
208
209function crab_spice_ltrim(str) {
210	"use strict";
211	return crab_spice_alltrim(str, 'l');
212}
213
214function crab_spice_rtrim(str) {
215	"use strict";
216	return crab_spice_alltrim(str, 'r');
217}
218
219function crab_spice_padl(str, len) {
220	"use strict";
221	var ch = arguments[2] ? arguments[2] : ' ';
222	var rez = str.substr(0,len);
223	if (rez.length < len) {
224		for (var i=0; i<len-str.length; i++)
225			rez += ch;
226	}
227	return rez;
228}
229
230function crab_spice_padr(str, len) {
231	"use strict";
232	var ch = arguments[2] ? arguments[2] : ' ';
233	var rez = str.substr(0,len);
234	if (rez.length < len) {
235		for (var i=0; i<len-str.length; i++)
236			rez = ch + rez;
237	}
238	return rez;
239}
240
241function crab_spice_padc(str, len) {
242	"use strict";
243	var ch = arguments[2] ? arguments[2] : ' ';
244	var rez = str.substr(0,len);
245	if (rez.length < len) {
246		for (var i=0; i<Math.floor((len-str.length)/2); i++)
247			rez = ch + rez + ch;
248	}
249	return rez+(rez.length<len ? ch : '');
250}
251
252function crab_spice_replicate(str, num) {
253	"use strict";
254	var rez = '';
255	for (var i=0; i<num; i++) {
256		rez += str;
257	}
258	return rez;
259}
260
261
262
263/* Numbers functions
264---------------------------------------------------------------- */
265
266// Round number to specified precision. 
267// For example: num=1.12345, prec=2,  rounded=1.12
268//              num=12345,   prec=-2, rounded=12300
269function crab_spice_round_number(num) {
270	"use strict";
271	var precision = arguments[1] ? arguments[1] : 0;
272	var p = Math.pow(10, precision);
273	return Math.round(num*p)/p;
274}
275
276// Clear number from any characters and append it with 0 to desired precision
277// For example: num=test1.12dd, prec=3, cleared=1.120
278function crab_spice_clear_number(num) {
279	"use strict";
280	var precision = arguments[1] ? arguments[1] : 0;
281	var defa = arguments[2] ? arguments[2] : 0;
282	var res = '';
283	var decimals = -1;
284	num = ""+num;
285	if (num=="") num=""+defa;
286	for (var i=0; i<num.length; i++) {
287		if (decimals==0) break;
288		else if (decimals>0) decimals--;
289		var ch = num.substr(i,1);
290		if (ch=='.') {
291			if (precision>0) {
292				res += ch;
293			}
294			decimals = precision;
295		} else if ((ch>=0 && ch<=9) || (ch=='-' && i==0))
296			res+=ch;
297	}
298	if (precision>0 && decimals!=0) {
299		if (decimals==-1) {
300			res += '.';
301			decimals = precision;
302		}
303		for (i=decimals; i>0; i--)
304			res +='0'; 
305	}
306	//if (isNaN(res)) res = clearNumber(defa, precision, defa);
307	return res;
308}
309
310// Convert number from decimal to hex
311function crab_spice_dec2hex(n) { 
312	"use strict";
313	return Number(n).toString(16);
314}
315
316// Convert number from hex to decimal
317function crab_spice_hex2dec(hex) {
318	"use strict";
319	return parseInt(hex,16); 
320}
321
322
323
324/* Array manipulations
325---------------------------------------------------------------- */
326
327function crab_spice_in_array(val, thearray)  {
328	"use strict";
329	var rez = false;
330	for (var i=0; i<thearray.length-1; i++)  {
331		if (thearray[i] == val)  {
332			rez = true;
333			break;
334		}
335	}
336	return rez;
337}
338
339function crab_spice_sort_array(thearray)  {
340	"use strict";
341	var caseSensitive = arguments[1] ? arguments[1] : false;
342	for (var x=0; x<thearray.length-1; x++)  {
343		for (var y=(x+1); y<thearray.length; y++)  {
344			if (caseSensitive) {
345				if (thearray[x] > thearray[y])  {
346					tmp = thearray[x];
347					thearray[x] = thearray[y];
348					thearray[y] = tmp;
349				}  
350			} else {
351				if (thearray[x].toLowerCase() > thearray[y].toLowerCase())  {
352					tmp = thearray[x];
353					thearray[x] = thearray[y];
354					thearray[y] = tmp;
355				}  
356			}
357		}  
358	}
359	return thearray;
360}
361
362
363
364/* Date manipulations
365---------------------------------------------------------------- */
366
367// Return array[Year, Month, Day, Hours, Minutes, Seconds]
368// from string: Year[-/.]Month[-/.]Day[T ]Hours:Minutes:Seconds
369function crab_spice_parse_date(dt) {
370	"use strict";
371	dt = dt.replace(/\//g, '-').replace(/\./g, '-').replace(/T/g, ' ').split('+')[0];
372	var dt2 = dt.split(' ');
373	var d = dt2[0].split('-');
374	var t = dt2[1].split(':');
375	d.push(t[0], t[1], t[2]);
376	return d;
377}
378
379// Return difference string between two dates
380function crab_spice_get_date_difference(dt1) {
381	"use strict";
382	var dt2 = arguments[1]!==undefined ? arguments[1] : '';
383	var short_date = arguments[2]!==undefined ? arguments[2] : true;
384	var sec = arguments[3]!==undefined ? arguments[3] : false;
385	var a1 = crab_spice_parse_date(dt1);
386	dt1 = Date.UTC(a1[0], a1[1], a1[2], a1[3], a1[4], a1[5]);
387	if (dt2 == '') {
388		dt2 = new Date();
389		var a2 = [dt2.getFullYear(), dt2.getMonth()+1, dt2.getDate(), dt2.getHours(), dt2.getMinutes(), dt2.getSeconds()];
390	} else
391		var a2 = crab_spice_parse_date(dt2);
392	dt2 = Date.UTC(a2[0], a2[1], a2[2], a2[3], a2[4], a2[5]);
393	var diff = Math.round((dt2 - dt1)/1000);
394	var days = Math.floor(diff / (24*3600));
395	diff -= days * 24 * 3600;
396	var hours = Math.floor(diff / 3600);
397	diff -= hours * 3600;
398	var minutes = Math.floor(diff / 60);
399	diff -= minutes * 60;
400	rez = '';
401	if (days > 0)
402		rez += (rez!='' ? ' ' : '') + days + ' day' + (days > 1 ? 's' : '');
403	if ((!short_date || rez=='') && hours > 0)
404		rez += (rez!='' ? ' ' : '') + hours + ' hour' + (hours > 1 ? 's' : '');
405	if ((!short_date || rez=='') && minutes > 0)
406		rez +=  (rez!='' ? ' ' : '') + minutes + ' minute' + (minutes > 1 ? 's' : '');
407	if (sec || rez=='')
408		rez +=  rez!='' || sec ? (' ' + diff + ' second' + (diff > 1 ? 's' : '')) : 'less then minute';
409	return rez;
410}
411
412
413
414/* Colors functions
415---------------------------------------------------------------- */
416
417function crab_spice_hex2rgb(hex) {
418	hex = parseInt(((hex.indexOf('#') > -1) ? hex.substring(1) : hex), 16);
419	return {r: hex >> 16, g: (hex & 0x00FF00) >> 8, b: (hex & 0x0000FF)};
420}
421
422function crab_spice_rgb2hex(color) {
423	"use strict";
424	var aRGB;
425	color = color.replace(/\s/g,"").toLowerCase();
426	if (color=='rgba(0,0,0,0)' || color=='rgba(0%,0%,0%,0%)')
427		color = 'transparent';
428	if (color.indexOf('rgba(')==0)
429		aRGB = color.match(/^rgba\((\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?)\)$/i);
430	else	
431		aRGB = color.match(/^rgb\((\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?)\)$/i);
432	
433	if(aRGB) {
434		color = '';
435		for (var i=1; i<=3; i++) 
436			color += Math.round((aRGB[i][aRGB[i].length-1]=="%"?2.55:1)*parseInt(aRGB[i])).toString(16).replace(/^(.)$/,'0$1');
437	} else 
438		color = color.replace(/^#?([\da-f])([\da-f])([\da-f])$/i, '$1$1$2$2$3$3');
439	return (color.substr(0,1)!='#' ? '#' : '') + color;
440}
441
442function crab_spice_components2hex(r,g,b) {
443	"use strict";
444	return '#'+
445		Number(r).toString(16).toUpperCase().replace(/^(.)$/,'0$1') +
446		Number(g).toString(16).toUpperCase().replace(/^(.)$/,'0$1') +
447		Number(b).toString(16).toUpperCase().replace(/^(.)$/,'0$1');
448}
449
450function crab_spice_rgb2components(color) {
451	"use strict";
452	color = crab_spice_rgb2hex(color);
453	var matches = color.match(/^#?([\dabcdef]{2})([\dabcdef]{2})([\dabcdef]{2})$/i);
454	if (!matches) return false;
455	for (var i=1, rgb = new Array(3); i<=3; i++)
456		rgb[i-1] = parseInt(matches[i],16);
457	return rgb;
458}
459
460function crab_spice_hex2hsb(hex) {
461	"use strict";
462	return crab_spice_rgb2hsb(crab_spice_hex2rgb(hex));
463}
464
465function crab_spice_hsb2hex(hsb) {
466	var rgb = crab_spice_hsb2rgb(hsb);
467	return crab_spice_components2hex(rgb.r, rgb.g, rgb.b);
468}
469
470function crab_spice_rgb2hsb(rgb) {
471	"use strict";
472	var hsb = {};
473	hsb.b = Math.max(Math.max(rgb.r,rgb.g),rgb.b);
474	hsb.s = (hsb.b <= 0) ? 0 : Math.round(100*(hsb.b - Math.min(Math.min(rgb.r,rgb.g),rgb.b))/hsb.b);
475	hsb.b = Math.round((hsb.b /255)*100);
476	if ((rgb.r==rgb.g) && (rgb.g==rgb.b))  hsb.h = 0;
477	else if (rgb.r>=rgb.g && rgb.g>=rgb.b) hsb.h = 60*(rgb.g-rgb.b)/(rgb.r-rgb.b);
478	else if (rgb.g>=rgb.r && rgb.r>=rgb.b) hsb.h = 60  + 60*(rgb.g-rgb.r)/(rgb.g-rgb.b);
479	else if (rgb.g>=rgb.b && rgb.b>=rgb.r) hsb.h = 120 + 60*(rgb.b-rgb.r)/(rgb.g-rgb.r);
480	else if (rgb.b>=rgb.g && rgb.g>=rgb.r) hsb.h = 180 + 60*(rgb.b-rgb.g)/(rgb.b-rgb.r);
481	else if (rgb.b>=rgb.r && rgb.r>=rgb.g) hsb.h = 240 + 60*(rgb.r-rgb.g)/(rgb.b-rgb.g);
482	else if (rgb.r>=rgb.b && rgb.b>=rgb.g) hsb.h = 300 + 60*(rgb.r-rgb.b)/(rgb.r-rgb.g);
483	else 								   hsb.h = 0;
484	hsb.h = Math.round(hsb.h);
485	return hsb;
486}
487
488function crab_spice_hsb2rgb(hsb) {
489	var rgb = {};
490	var h = Math.round(hsb.h);
491	var s = Math.round(hsb.s*255/100);
492	var v = Math.round(hsb.b*255/100);
493	if (s == 0) {
494		rgb.r = rgb.g = rgb.b = v;
495	} else {
496		var t1 = v;
497		var t2 = (255-s)*v/255;
498		var t3 = (t1-t2)*(h%60)/60;
499		if (h==360) h = 0;
500		if (h<60) 		{ rgb.r=t1;	rgb.b=t2;   rgb.g=t2+t3; }
501		else if (h<120) { rgb.g=t1; rgb.b=t2;	rgb.r=t1-t3; }
502		else if (h<180) { rgb.g=t1; rgb.r=t2;	rgb.b=t2+t3; }
503		else if (h<240) { rgb.b=t1; rgb.r=t2;	rgb.g=t1-t3; }
504		else if (h<300) { rgb.b=t1; rgb.g=t2;	rgb.r=t2+t3; }
505		else if (h<360) { rgb.r=t1; rgb.g=t2;	rgb.b=t1-t3; }
506		else 			{ rgb.r=0;  rgb.g=0;	rgb.b=0;	 }
507	}
508	return { r:Math.round(rgb.r), g:Math.round(rgb.g), b:Math.round(rgb.b) };
509}
510
511function crab_spice_color_picker(){
512	"use strict";
513	var id = arguments[0] ? arguments[0] : "iColorPicker"+Math.round(Math.random()*1000);
514	var colors = arguments[1] ? arguments[1] : 
515	'#f00,#ff0,#0f0,#0ff,#00f,#f0f,#fff,#ebebeb,#e1e1e1,#d7d7d7,#cccccc,#c2c2c2,#b7b7b7,#acacac,#a0a0a0,#959595,'
516	+'#ee1d24,#fff100,#00a650,#00aeef,#2f3192,#ed008c,#898989,#7d7d7d,#707070,#626262,#555,#464646,#363636,#262626,#111,#000,'
517	+'#f7977a,#fbad82,#fdc68c,#fff799,#c6df9c,#a4d49d,#81ca9d,#7bcdc9,#6ccff7,#7ca6d8,#8293ca,#8881be,#a286bd,#bc8cbf,#f49bc1,#f5999d,'
518	+'#f16c4d,#f68e54,#fbaf5a,#fff467,#acd372,#7dc473,#39b778,#16bcb4,#00bff3,#438ccb,#5573b7,#5e5ca7,#855fa8,#a763a9,#ef6ea8,#f16d7e,'
519	+'#ee1d24,#f16522,#f7941d,#fff100,#8fc63d,#37b44a,#00a650,#00a99e,#00aeef,#0072bc,#0054a5,#2f3192,#652c91,#91278f,#ed008c,#ee105a,'
520	+'#9d0a0f,#a1410d,#a36209,#aba000,#588528,#197b30,#007236,#00736a,#0076a4,#004a80,#003370,#1d1363,#450e61,#62055f,#9e005c,#9d0039,'
521	+'#790000,#7b3000,#7c4900,#827a00,#3e6617,#045f20,#005824,#005951,#005b7e,#003562,#002056,#0c004b,#30004a,#4b0048,#7a0045,#7a0026';
522	var colorsList = colors.split(',');
523	var tbl = '<table class="colorPickerTable"><thead>';
524	for (var i=0; i<colorsList.length; i++) {
525		if (i%16==0) tbl += (i>0 ? '</tr>' : '') + '<tr>';
526		tbl += '<td style="background-color:'+colorsList[i]+'">&nbsp;</td>';
527	}
528	tbl += '</tr></thead><tbody>'
529		+ '<tr style="height:60px;">'
530		+ '<td colspan="8" id="'+id+'_colorPreview" style="vertical-align:middle;text-align:center;border:1px solid #000;background:#fff;">'
531		+ '<input style="width:55px;color:#000;border:1px solid rgb(0, 0, 0);padding:5px;background-color:#fff;font:11px Arial, Helvetica, sans-serif;" maxlength="7" />'
532		+ '<a href="#" id="'+id+'_moreColors" class="iColorPicker_moreColors"></a>'
533		+ '</td>'
534		+ '<td colspan="8" id="'+id+'_colorOriginal" style="vertical-align:middle;text-align:center;border:1px solid #000;background:#fff;">'
535		+ '<input style="width:55px;color:#000;border:1px solid rgb(0, 0, 0);padding:5px;background-color:#fff;font:11px Arial, Helvetica, sans-serif;" readonly="readonly" />'
536		+ '</td>'
537		+ '</tr></tbody></table>';
538	//tbl += '<style>#iColorPicker input{margin:2px}</style>';
539
540	jQuery(document.createElement("div"))
541		.attr("id", id)
542		.css('display','none')
543		.html(tbl)
544		.appendTo("body")
545		.addClass("iColorPickerTable")
546		.on('mouseover', 'thead td', function(){
547			"use strict";
548			var aaa = crab_spice_rgb2hex(jQuery(this).css('background-color'));
549			jQuery('#'+id+'_colorPreview').css('background',aaa);
550			jQuery('#'+id+'_colorPreview input').val(aaa);
551		})
552		.on('keypress', '#'+id+'_colorPreview input', function(key){
553			"use strict";
554			var aaa = jQuery(this).val()
555			if (aaa.length<7 && ((key.which>=48 && key.which<=57) || (key.which>=97 && key.which<=102) || (key.which===35 || aaa.length===0))) {
556				aaa += String.fromCharCode(key.which);
557			} else if (key.which == 8 && aaa.length>0) {
558				aaa = aaa.substring(0, aaa.length-1);
559			} else if (key.which===13 && (aaa.length===4 || aaa.length===7)) {
560				var fld  = jQuery('#'+id).data('field');
561				var func = jQuery('#'+id).data('func');
562				if (func!=null && func!='undefined') {
563					func(fld, aaa);
564				} else {
565					fld.val(aaa).css('backgroundColor', aaa).trigger('change');
566				}
567				jQuery('#'+id+'_Bg').fadeOut(500);
568				jQuery('#'+id).fadeOut(500);
569				
570			} else {
571				key.preventDefault();
572				return false;
573			}
574			if (aaa.substr(0,1)==='#' && (aaa.length===4 || aaa.length===7)) {
575				jQuery('#'+id+'_colorPreview').css('background',aaa);
576			}
577		})
578		.on('click', 'thead td', function(e){
579			"use strict";
580			var fld  = jQuery('#'+id).data('field');
581			var func = jQuery('#'+id).data('func');
582			var aaa  = crab_spice_rgb2hex(jQuery(this).css('background-color'));
583			if (func!=null && func!='undefined') {
584				func(fld, aaa);
585			} else {
586				fld.val(aaa).css('backgroundColor', aaa).trigger('change');
587			}
588			jQuery('#'+id+'_Bg').fadeOut(500);
589			jQuery('#'+id).fadeOut(500);
590			e.preventDefault();
591			return false;
592		})
593		.on('click', 'tbody .iColorPicker_moreColors', function(e){
594			"use strict";
595			var thead  = jQuery(this).parents('table').find('thead');
596			var out = '';
597			if (thead.hasClass('more_colors')) {
598				for (var i=0; i<colorsList.length; i++) {
599					if (i%16==0) out += (i>0 ? '</tr>' : '') + '<tr>';
600					out += '<td style="background-color:'+colorsList[i]+'">&nbsp;</td>';
601				}
602				thead.removeClass('more_colors').empty().html(out+'</tr>');
603				jQuery('#'+id+'_colorPreview').attr('colspan', 8);
604				jQuery('#'+id+'_colorOriginal').attr('colspan', 8);
605			} else {
606				var rgb=[0,0,0], i=0, j=-1;	// Set j=-1 or j=0 - show 2 different colors layouts
607				while (rgb[0]<0xF || rgb[1]<0xF || rgb[2]<0xF) {
608					if (i%18==0) out += (i>0 ? '</tr>' : '') + '<tr>';
609					i++;
610					out += '<td style="background-color:'+crab_spice_components2hex(rgb[0]*16+rgb[0],rgb[1]*16+rgb[1],rgb[2]*16+rgb[2])+'">&nbsp;</td>';
611					rgb[2]+=3;
612					if (rgb[2]>0xF) {
613						rgb[1]+=3;
614						if (rgb[1]>(j===0 ? 6 : 0xF)) {
615							rgb[0]+=3;
616							if (rgb[0]>0xF) {
617								if (j===0) {
618									j=1;
619									rgb[0]=0;
620									rgb[1]=9;
621									rgb[2]=0;
622								} else {
623									break;
624								}
625							} else {
626								rgb[1]=(j < 1 ? 0 : 9);
627								rgb[2]=0;
628							}
629						} else {
630							rgb[2]=0;
631						}
632					}
633				}
634				thead.addClass('more_colors').empty().html(out+'<td  style="background-color:#ffffff" colspan="8">&nbsp;</td></tr>');
635				jQuery('#'+id+'_colorPreview').attr('colspan', 9);
636				jQuery('#'+id+'_colorOriginal').attr('colspan', 9);
637			}
638			jQuery('#'+id+' table.colorPickerTable thead td')
639				.css({
640					'width':'12px',
641					'height':'14px',
642					'border':'1px solid #000',
643					'cursor':'pointer'
644				});
645			e.preventDefault();
646			return false;
647		});
648	jQuery(document.createElement("div"))
649		.attr("id", id+"_Bg")
650		.on('click', function(e) {
651			"use strict";
652			jQuery("#"+id+"_Bg").fadeOut(500);
653			jQuery("#"+id).fadeOut(500);
654			e.preventDefault();
655			return false;
656		})
657		.appendTo("body");
658	jQuery('#'+id+' table.colorPickerTable thead td')
659		.css({
660			'width':'12px',
661			'height':'14px',
662			'border':'1px solid #000',
663			'cursor':'pointer'
664		});
665	jQuery('#'+id+' table.colorPickerTable')
666		.css({'border-collapse':'collapse'});
667	jQuery('#'+id)
668		.css({
669			'border':'1px solid #ccc',
670			'background':'#333',
671			'padding':'5px',
672			'color':'#fff',
673			'z-index':999999
674		});
675	jQuery('#'+id+'_colorPreview')
676		.css({'height':'50px'});
677	return id;
678}
679
680function crab_spice_color_picker_show(id, fld, func) { 
681	"use strict";
682	if (id===null || id==='') {
683		id = jQuery('.iColorPickerTable').attr('id');
684	}
685	var eICP = fld.offset();
686	var w = jQuery('#'+id).width();
687	var h = jQuery('#'+id).height();
688	var l = eICP.left + w < jQuery(window).width()-10 ? eICP.left : jQuery(window).width()-10 - w;
689	var t = eICP.top + fld.outerHeight() + h < jQuery(document).scrollTop() + jQuery(window).height()-10 ? eICP.top + fld.outerHeight() : eICP.top - h - 13;
690	jQuery("#"+id)
691		.data({field: fld, func: func})
692		.css({
693			'top':t+"px",
694			'left':l+"px",
695			'position':'absolute',
696			'z-index':100001
697		})
698		.fadeIn(500);
699	jQuery("#"+id+"_Bg")
700		.css({
701			'position':'fixed',
702			'z-index':100000,
703			'top':0,
704			'left':0,
705			'width':'100%',
706			'height':'100%'
707		})
708		.fadeIn(500);
709	var def = fld.val().substr(0, 1)=='#' ? fld.val() : crab_spice_rgb2hex(fld.css('backgroundColor'));
710	jQuery('#'+id+'_colorPreview input,#'+id+'_colorOriginal input').val(def);
711	jQuery('#'+id+'_colorPreview,#'+id+'_colorOriginal').css('background',def);
712}
713
714
715
716/* Cookies manipulations
717---------------------------------------------------------------- */
718
719function crab_spice_get_cookie(name) {
720	"use strict";
721	var defa = arguments[1]!=undefined ? arguments[1] : null;
722	var start = document.cookie.indexOf(name + '=');
723	var len = start + name.length + 1;
724	if ((!start) && (name != document.cookie.substring(0, name.length))) {
725		return defa;
726	}
727	if (start == -1)
728		return defa;
729	var end = document.cookie.indexOf(';', len);
730	if (end == -1)
731		end = document.cookie.length;
732	return unescape(document.cookie.substring(len, end));
733}
734
735
736function crab_spice_set_cookie(name, value, expires, path, domain, secure) {
737	"use strict";
738	var expires = arguments[2]!=undefined ? arguments[2] : 0;
739	var path    = arguments[3]!=undefined ? arguments[3] : '/';
740	var domain  = arguments[4]!=undefined ? arguments[4] : '';
741	var secure  = arguments[5]!=undefined ? arguments[5] : '';
742	var today = new Date();
743	today.setTime(today.getTime());
744	if (expires) {
745		expires = expires * 1000 * 60 * 60 * 24;
746	}
747	var expires_date = new Date(today.getTime() + (expires));
748	document.cookie = name + '='
749			+ escape(value)
750			+ ((expires) ? ';expires=' + expires_date.toGMTString() : '')
751			+ ((path)    ? ';path=' + path : '')
752			+ ((domain)  ? ';domain=' + domain : '')
753			+ ((secure)  ? ';secure' : '');
754}
755
756
757function crab_spice_del_cookie(name, path, domain) {
758	"use strict";
759	var path   = arguments[1]!=undefined ? arguments[1] : '/';
760	var domain = arguments[2]!=undefined ? arguments[2] : '';
761	if (crab_spice_get_cookie(name))
762		document.cookie = name + '=' + ((path) ? ';path=' + path : '')
763				+ ((domain) ? ';domain=' + domain : '')
764				+ ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
765}
766
767
768
769/* ListBox and ComboBox manipulations
770---------------------------------------------------------------- */
771
772function crab_spice_clear_listbox(box) {
773	"use strict";
774	for (var i=box.options.length-1; i>=0; i--)
775		box.options[i] = null;
776}
777
778function crab_spice_add_listbox_item(box, val, text) {
779	"use strict";
780	var item = new Option();
781	item.value = val;
782	item.text = text;
783    box.options.add(item);
784}
785
786function crab_spice_del_listbox_item_by_value(box, val) {
787	"use strict";
788	for (var i=0; i<box.options.length; i++) {
789		if (box.options[i].value == val) {
790			box.options[i] = null;
791			break;
792		}
793	}
794}
795
796function crab_spice_del_listbox_item_by_text(box, txt) {
797	"use strict";
798	for (var i=0; i<box.options.length; i++) {
799		if (box.options[i].text == txt) {
800			box.options[i] = null;
801			break;
802		}
803	}
804}
805
806function crab_spice_find_listbox_item_by_value(box, val) {
807	"use strict";
808	var idx = -1;
809	for (var i=0; i<box.options.length; i++) {
810		if (box.options[i].value == val) {
811			idx = i;
812			break;
813		}
814	}
815	return idx;
816}
817
818function crab_spice_find_listbox_item_by_text(box, txt) {
819	"use strict";
820	var idx = -1;
821	for (var i=0; i<box.options.length; i++) {
822		if (box.options[i].text == txt) {
823			idx = i;
824			break;
825		}
826	}
827	return idx;
828}
829
830function crab_spice_select_listbox_item_by_value(box, val) {
831	"use strict";
832	for (var i = 0; i < box.options.length; i++) {
833		box.options[i].selected = (val == box.options[i].value);
834	}
835}
836
837function crab_spice_select_listbox_item_by_text(box, txt) {
838	"use strict";
839	for (var i = 0; i < box.options.length; i++) {
840		box.options[i].selected = (txt == box.options[i].text);
841	}
842}
843
844function crab_spice_get_listbox_values(box) {
845	"use strict";
846	var delim = arguments[1] ? arguments[1] : ',';
847	var str = '';
848	for (var i=0; i<box.options.length; i++) {
849		str += (str ? delim : '') + box.options[i].value;
850	}
851	return str;
852}
853
854function crab_spice_get_listbox_texts(box) {
855	"use strict";
856	var delim = arguments[1] ? arguments[1] : ',';
857	var str = '';
858	for (var i=0; i<box.options.length; i++) {
859		str += (str ? delim : '') + box.options[i].text;
860	}
861	return str;
862}
863
864function crab_spice_sort_listbox(box)  {
865	"use strict";
866	var temp_opts = new Array();
867	var temp = new Option();
868	for(var i=0; i<box.options.length; i++)  {
869		temp_opts[i] = box.options[i].clone();
870	}
871	for(var x=0; x<temp_opts.length-1; x++)  {
872		for(var y=(x+1); y<temp_opts.length; y++)  {
873			if(temp_opts[x].text > temp_opts[y].text)  {
874				temp = temp_opts[x];
875				temp_opts[x] = temp_opts[y];
876				temp_opts[y] = temp;
877			}  
878		}  
879	}
880	for(var i=0; i<box.options.length; i++)  {
881		box.options[i] = temp_opts[i].clone();
882	}
883}
884
885function crab_spice_get_listbox_selected_index(box) {
886	"use strict";
887	for (var i = 0; i < box.options.length; i++) {
888		if (box.options[i].selected)
889			return i;
890	}
891	return -1;
892}
893
894function crab_spice_get_listbox_selected_value(box) {
895	"use strict";
896	for (var i = 0; i < box.options.length; i++) {
897		if (box.options[i].selected) {
898			return box.options[i].value;
899		}
900	}
901	return null;
902}
903
904function crab_spice_get_listbox_selected_text(box) {
905	"use strict";
906	for (var i = 0; i < box.options.length; i++) {
907		if (box.options[i].selected) {
908			return box.options[i].text;
909		}
910	}
911	return null;
912}
913
914function crab_spice_get_listbox_selected_option(box) {
915	"use strict";
916	for (var i = 0; i < box.options.length; i++) {
917		if (box.options[i].selected) {
918			return box.options[i];
919		}
920	}
921	return null;
922}
923
924
925
926/* Radio buttons manipulations
927---------------------------------------------------------------- */
928
929function crab_spice_get_radio_value(radioGroupObj) {
930	"use strict";
931	for (var i=0; i < radioGroupObj.length; i++)
932		if (radioGroupObj[i].checked) return radioGroupObj[i].value;
933	return null;
934}
935
936function crab_spice_set_radio_checked_by_num(radioGroupObj, num) {
937	"use strict";
938	for (var i=0; i < radioGroupObj.length; i++)
939		if (radioGroupObj[i].checked && i!=num) radioGroupObj[i].checked=false;
940		else if (i==num) radioGroupObj[i].checked=true;
941}
942
943function crab_spice_set_radio_checked_by_value(radioGroupObj, val) {
944	"use strict";
945	for (var i=0; i < radioGroupObj.length; i++)
946		if (radioGroupObj[i].checked && radioGroupObj[i].value!=val) radioGroupObj[i].checked=false;
947		else if (radioGroupObj[i].value==val) radioGroupObj[i].checked=true;
948}
949
950
951
952/* Form manipulations
953---------------------------------------------------------------- */
954
955/*
956// Usage example:
957var error = crab_spice_form_validate(jQuery(form_selector), {				// -------- Options ---------
958	error_message_show: true,									// Display or not error message
959	error_message_time: 5000,									// Time to display error message
960	error_message_class: 'sc_infobox sc_infobox_style_error',	// Class, appended to error message block
961	error_message_text: 'Global error text',					// Global error message text (if don't write message in checked field)
962	error_fields_class: 'error_fields_class',					// Class, appended to error fields
963	exit_after_first_error: false,								// Cancel validation and exit after first error
964	rules: [
965		{
966			field: 'author',																// Checking field name
967			min_length: { value: 1,	 message: 'The author name can\'t be empty' },			// Min character count (0 - don't check), message - if error occurs
968			max_length: { value: 60, message: 'Too long author name'}						// Max character count (0 - don't check), message - if error occurs
969		},
970		{
971			field: 'email',
972			min_length: { value: 7,	 message: 'Too short (or empty) email address' },
973			max_length: { value: 60, message: 'Too long email address'},
974			mask: { value: '^([a-z0-9_\\-]+\\.)*[a-z0-9_\\-]+@[a-z0-9_\\-]+(\\.[a-z0-9_\\-]+)*\\.[a-z]{2,6}$', message: 'Invalid email address'}
975		},
976		{
977			field: 'comment',
978			min_length: { value: 1,	 message: 'The comment text can\'t be empty' },
979			max_length: { value: 200, message: 'Too long comment'}
980		},
981		{
982			field: 'pwd1',
983			min_length: { value: 5,	 message: 'The password can\'t be less then 5 characters' },
984			max_length: { value: 20, message: 'Too long password'}
985		},
986		{
987			field: 'pwd2',
988			equal_to: { value: 'pwd1',	 message: 'The passwords in both fields must be equals' }
989		}
990	]
991});
992*/
993
994function crab_spice_form_validate(form, opt) {
995	"use strict";
996	var error_msg = '';
997	form.find(":input").each(function() {
998		"use strict";
999		if (error_msg!='' && opt.exit_after_first_error) return;
1000		for (var i = 0; i < opt.rules.length; i++) {
1001			if (jQuery(this).attr("name") == opt.rules[i].field) {
1002				var val = jQuery(this).val();
1003				var error = false;
1004				if (typeof(opt.rules[i].min_length) == 'object') {
1005					if (opt.rules[i].min_length.value > 0 && val.length < opt.rules[i].min_length.value) {
1006						if (error_msg=='') jQuery(this).get(0).focus();
1007						error_msg += '<p class="error_item">' + (typeof(opt.rules[i].min_length.message)!='undefined' ? opt.rules[i].min_length.message : opt.error_message_text ) + '</p>'
1008						error = true;
1009					}
1010				}
1011				if ((!error || !opt.exit_after_first_error) && typeof(opt.rules[i].max_length) == 'object') {
1012					if (opt.rules[i].max_length.value > 0 && val.length > opt.rules[i].max_length.value) {
1013						if (error_msg=='') jQuery(this).get(0).focus();
1014						error_msg += '<p class="error_item">' + (typeof(opt.rules[i].max_length.message)!='undefined' ? opt.rules[i].max_length.message : opt.error_message_text ) + '</p>'
1015						error = true;
1016					}
1017				}
1018				if ((!error || !opt.exit_after_first_error) && typeof(opt.rules[i].mask) == 'object') {
1019					if (opt.rules[i].mask.value != '') {
1020						var regexp = new RegExp(opt.rules[i].mask.value);
1021						if (!regexp.test(val)) {
1022							if (error_msg=='') jQuery(this).get(0).focus();
1023							error_msg += '<p class="error_item">' + (typeof(opt.rules[i].mask.message)!='undefined' ? opt.rules[i].mask.message : opt.error_message_text ) + '</p>'
1024							error = true;
1025						}
1026					}
1027				}
1028				if ((!error || !opt.exit_after_first_error) && typeof(opt.rules[i].equal_to) == 'object') {
1029					if (opt.rules[i].equal_to.value != '' && val!=jQuery(jQuery(this).get(0).form[opt.rules[i].equal_to.value]).val()) {
1030						if (error_msg=='') jQuery(this).get(0).focus();
1031						error_msg += '<p class="error_item">' + (typeof(opt.rules[i].equal_to.message)!='undefined' ? opt.rules[i].equal_to.message : opt.error_message_text ) + '</p>'
1032						error = true;
1033					}
1034				}
1035				if (opt.error_fields_class != '') jQuery(this).toggleClass(opt.error_fields_class, error);
1036			}
1037		}
1038	});
1039	if (error_msg!='' && opt.error_message_show) {
1040		var error_message_box = form.find(".result");
1041		if (error_message_box.length == 0) error_message_box = form.parent().find(".result");
1042		if (error_message_box.length == 0) {
1043			form.append('<div class="result"></div>');
1044			error_message_box = form.find(".result");
1045		}
1046		if (opt.error_message_class) error_message_box.toggleClass(opt.error_message_class, true);
1047		error_message_box.html(error_msg).fadeIn();
1048		setTimeout(function() { error_message_box.fadeOut(); }, opt.error_message_time);
1049	}
1050	return error_msg!='';
1051}
1052
1053
1054
1055/* Document manipulations
1056---------------------------------------------------------------- */
1057
1058// Animated scroll to selected id
1059function crab_spice_document_animate_to(id) {
1060	if (id.indexOf('#')==-1) id = '#' + id;
1061	var obj = jQuery(id).eq(0);
1062	if (obj.length == 0) return;
1063	var oft = jQuery(id).offset().top;
1064	var st  = jQuery(window).scrollTop();
1065	var speed = Math.min(1600, Math.max(400, Math.round(Math.abs(oft-st) / jQuery(window).height() * 100)));
1066	jQuery('body,html').animate( {scrollTop: oft - jQuery('#wpadminbar').height() - jQuery('header.fixedTopMenu .topWrap').height()}, speed, 'swing');
1067}
1068
1069// Change browser address without reload page
1070function crab_spice_document_set_location(curLoc){
1071	try {
1072		history.pushState(null, null, curLoc);
1073		return;
1074	} catch(e) {}
1075	location.href = curLoc;
1076}
1077
1078// Add hidden elements init functions after tab, accordion, toggles activate
1079function crab_spice_add_hidden_elements_handler(key, handler) {
1080	crab_spice_storage_set_array('init_hidden_elements', key, handler);
1081}
1082
1083// Init hidden elements after tab, accordion, toggles activate
1084function crab_spice_init_hidden_elements(cont) {
1085	if (CRAB_SPICE_STORAGE['init_hidden_elements']) {
1086		for (key in CRAB_SPICE_STORAGE['init_hidden_elements']) {
1087			CRAB_SPICE_STORAGE['init_hidden_elements'][key](cont);
1088		}
1089	}
1090}
1091
1092
1093
1094/* Browsers detection
1095---------------------------------------------------------------- */
1096
1097function crab_spice_browser_is_mobile() {
1098	var check = false;
1099	(function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4)))check = true})(navigator.userAgent||navigator.vendor||window.opera);
1100	return check;
1101}
1102function crab_spice_browser_is_ios() {
1103	return navigator.userAgent.match(/iPad|iPhone|iPod/i) != null;
1104}
1105function crab_spice_is_retina() {
1106	var mediaQuery = '(-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (min-resolution: 1.5dppx)';
1107	return (window.devicePixelRatio > 1) || (window.matchMedia && window.matchMedia(mediaQuery).matches);
1108}
1109
1110
1111/* File functions
1112---------------------------------------------------------------- */
1113
1114function crab_spice_get_file_name(path) {
1115	path = path.replace(/\\/g, '/');
1116	var pos = path.lastIndexOf('/');
1117	if (pos >= 0)
1118		path = path.substr(pos+1);
1119	return path;
1120}
1121
1122function crab_spice_get_file_ext(path) {
1123	var pos = path.lastIndexOf('.');
1124	path = pos >= 0 ? path.substr(pos+1) : '';
1125	return path;
1126}
1127
1128
1129
1130/* Image functions
1131---------------------------------------------------------------- */
1132
1133// Return true, if all images in the specified container are loaded
1134function crab_spice_check_images_complete(cont) {
1135	var complete = true;
1136	cont.find('img').each(function() {
1137		if (!complete) return;
1138		if (!jQuery(this).get(0).complete) complete = false;
1139	});
1140	return complete;
1141}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.