/** $.toURL plugin **/
(function($){
	$.toURL = function(s, noTrim) {
		s = s || '';
		// Apply mappings
		$.each($.toURL.charMaps, function(result, regexp) {
			s = s.replace(regexp, result);
		});
		// Normalize 
		s = s.replace(/[^a-z0-9]+/g, ' ');
		if(!noTrim) {
			s = $.trim(s);
		}
		return s.replace(/ /g, '-');
	};
	
	// Construct char mappings
	$.toURL.charMaps = {};
	$.each({
		'a':"A\u00aa\u00c0-\u00c5\u00e0-\u00e5\u0100-\u0105",'b':"B",'c':"C\u00a2\u00a9\u00c7\u00e7\u0106-\u010d",
		'd':"D\u00d0\u00f0\u010e-\u0111",'e':"E\u00c8-\u00cb\u00e8-\u00eb\u0112-\u011b",'f':"F\u0192",
		'g':"G\u011c-\u0123",'h':"H\u0124-\u0127",'i':"I\u00cc-\u00cf\u00ec-\u00ef\u0128-\u0131",
		'j':"J\u0134\u0135",'k':"K\u0136\u0137",'l':"L\u0139-\u0142",'m':"M",'n':"N\u00d1\u00f1\u0143-\u0149",
		'o':"O\u00ba\u00d2-\u00d6\u00d8\u00f2-\u00f6\u00f8\u014c-\u0151",'p':"P\u00b6",'q':"Q",
		'r':"R\u00ae\u0154-\u0159",'s':"S\u015a-\u0161\u017f\u0218\u0219",'t':"T\u0162-\u0167\u021a\u021b",
		'u':"U\u00b5\u00d9-\u00dc\u00f9-\u00fc\u0168-\u0173",'v':"V",'w':"W\u0174\u0175",'x':"X\u00d7",
		'y':"Y\u00dd\u00fd\u00ff\u0176-\u0178",'z':"Z\u0179-\u017e",'-0-':"\u00b0",'-1-':"\u00b9",'-2-':"\u00b2",
		'-3-':"\u00b3",'-lb-':"\u00a3",'-yen-':"\u00a5",'-paragraph-':"\u00a7",'ss':"\u00df",'-not-':"\u00ac",
		'-quarter-':"\u00bc",'-half-':"\u00bd",'-three-quarters-':"\u00be",'ae':"\u00c6\u00e6",'th':"\u00de\u00fe",
		'ij':"\u0132\u0133",'oe':"\u0152\u0153",'dz':"\u01c4-\u01c6\u01f1-\u01f3",'lj':"\u01c7-\u01c9",
		'nj':"\u01ca-\u01cc"
	}, function(result, map){
		$.toURL.charMaps[result] = new RegExp('['+map+']', 'g');
	});

})(jQuery);


/** $.getUID, $.getID, $.formatBytes, $.fn.hoverize plugin **/
(function($){
	// Get ID from string-ID
	$.getID = function(s) {
		return s.split('-').pop();
	};
	$.fn.getID = function() {
		return $.getID(this.attr('id'));
	};
	
	// Get unique ID
	var uid = 0;
	$.getUID = function() {
		return 'jUID-'+(++uid);
	};
	
	// Convert bytes to human readable format: B, KB, MB, GB, TB
	$.formatBytes = function(n) {
		var i = 0;
		while(n > 1024) {
			n /= 1024;
			i++;
		}
		if(n < 10) {
			n = Math.round(n * 100) / 100;
		} else if(n < 100) {
			n = Math.round(n * 10) / 10;
		} else {
			n = Math.ceil(n);
		}
		return ''+n+' '+(['B','KB','MB','GB','TB'])[i];
	};
	
	// Add 'hover' class on element mouse over
	$.fn.hoverize = function() {
		return this.hover(function(){
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	};
	
	// Generate thumbnail url
	$.thumbUrl = function(fileName, width, height) {
		return fileName.replace(/\.([^.]+)/, '='+(width || 0)+'x'+(height ||0)+'.$1');
	};
	
	// Get file extensions
	$.fileExtension = function(fileName) {
		var extPos = fileName.lastIndexOf('.');
		if(extPos == -1) {
			return '';
		}
		return fileName.substr(extPos + 1, 10);
	};
})(jQuery);


/** Base64 encoding **/
(function($){
	var keyString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	
	function uTF8Encode(string) {
		string = string.replace(/\x0d\x0a/g, "\x0a");
		var output = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				output += String.fromCharCode(c);
			} else if ((c > 127) && (c < 2048)) {
				output += String.fromCharCode((c >> 6) | 192);
				output += String.fromCharCode((c & 63) | 128);
			} else {
				output += String.fromCharCode((c >> 12) | 224);
				output += String.fromCharCode(((c >> 6) & 63) | 128);
				output += String.fromCharCode((c & 63) | 128);
			}
		}
		return output;
	}
	function uTF8Decode(input) {
		var string = "",
			i = 0, c = 0, c1 = 0, c2 = 0, c3 = 0;
		while ( i < input.length ) {
			c = input.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if ((c > 191) && (c < 224)) {
				c2 = input.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = input.charCodeAt(i+1);
				c3 = input.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
	
	$.base64Encode = function(input) {
		var output = "",
			chr1, chr2, chr3, enc1, enc2, enc3, enc4,
			i = 0;
		input = uTF8Encode(input);
		while (i < input.length) {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
			output = output + keyString.charAt(enc1) + keyString.charAt(enc2) + keyString.charAt(enc3) + keyString.charAt(enc4);
		}
		return output;
	};
	$.base64Decode = function(input) {
		var output = "",
			chr1, chr2, chr3,
			enc1, enc2, enc3, enc4,
			i = 0;
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while (i < input.length) {
			enc1 = keyString.indexOf(input.charAt(i++));
			enc2 = keyString.indexOf(input.charAt(i++));
			enc3 = keyString.indexOf(input.charAt(i++));
			enc4 = keyString.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output = output + String.fromCharCode(chr1);
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
		}
		output = uTF8Decode(output);
		return output;
	};
})(jQuery);