
// constant

E_ERROR = 1; // fatal error, show up
E_WARNING = 2; // warning message, show up
E_NOTICE = 4; // notice message, not show up
E_LOG = 8; // log only, not show up
BS = String.fromCharCode(8);

// pad

if (!String.prototype.padder) {
  String.prototype.pad = function() {
		var s = this;
		if (arguments.length > 0 && (n = arguments[0] - s.length) && n > 0)
			s = (arguments[1] || '0').toString().repeat(n).substr(0, n) + s;
		return s;
  }
}

if (!String.prototype.repeat) {
  String.prototype.repeat = function() {
  	var s = this;
  	var n = arguments.length ? parseInt(arguments[0]) : 1;
  	while (--n > 0) s += this;
  	return s;
  }
}

function get_function_name(args)
{
	return args.callee.toString().match(/function\s([^\(]+).+/i)[1];
}

// addon

jQuery.fn.extend({

	supersleight: function(settings)
	{
		settings = jQuery.extend({
			imgs: true,
			backgrounds: true,
			shim: 'img/blank.gif',
			apply_positioning: true
		}, settings);

		return this.each(function(){
			if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7 && parseInt(jQuery.browser.version) > 4) {
				jQuery(this).find('*').each(function(i,obj) {
					var self = jQuery(obj);
					// background pngs
					if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
						var bg = self.css('background-image');
						var src = bg.substring(5,bg.length-2);
						var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
						var styles = {
							'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
							'background-image': 'url('+settings.shim+')'
						};
						self.css(styles);
					};
					// image elements
					if (settings.imgs && self.is('img[src$=png]')){
						var styles = {
							'width': self.width() + 'px',
							'height': self.height() + 'px',
							'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
						};
						self.css(styles).attr('src', settings.shim);
					};
					// apply position to 'active' elements
					if (settings.applyPositioning && self.is('a, input') && self.css('position') === ''){
						self.css('position', 'relative');
					};
				});
			};
		});
	},

	asButton: function()
	{
		var newCSSClass = ['btnblue'];
		newCSSClass.push(arguments[0] || '');
		newCSSClass.push($(this).is(':disabled') ? 'disable' : '');
		return this
			.addClass(newCSSClass.join(' '))
			.mouseover(function(){ $(this).addClass('focus'); })
			.mouseout(function(){ $(this).removeClass('focus focused'); })
			.mousedown(function(){ $(this).addClass('focused'); })
			.mouseup(function(){ $(this).removeClass('focused').blur(); });
	},
	
	adjust_style: function()
	{
		this
			.find(':button')
				.addClass('btnblue')
				.mouseover(function(){ $(this).addClass('focus'); })
				.mouseout(function(){ $(this).removeClass('focus focused'); })
				.mousedown(function(){ $(this).addClass('focused'); })
				.mouseup(function(){ $(this).removeClass('focused').blur(); })
			.end()
			.find('textarea')
				.focus(function(){ $(this).addClass('focus'); })
				.blur(function(){ $(this).removeClass('focus'); })
			.end();
		return this;
	},
	
	/*
	set css property background-position is cross-browser
	- this.css('background-position', z)
	but get value is not
	- ff: this.css('background-position')
	- ie: this.css('background-positionX') & this.css('background-positionY')
	*/
	
	background_position: function()
	{
		if (arguments[0] && $.isArray(arguments[0]))
		{
			var x = arguments[0][0];
			var y = arguments[0][1];
			var z = this.css('background-position')
				? this.css('background-position').split(' ')
				: [this.css('background-positionX'), this.css('background-positionY')];
			if (x !== null) z[0] = x;
			if (y !== null) z[1] = y;
			z = z.join(' ');
			this.css('background-position', z);
			return this;
		}
		else
		{
			return this.css('background-position')
				|| this.css('background-positionX') + ' ' + this.css('background-positionY');
		}
	}


});

// ui & message

function movr() { if ($(this).hasClass('focused')) return false; $(this).addClass('focus'); }
function mout() { if ($(this).hasClass('focused')) return false; $(this).removeClass('focus'); }
function mm(msg) { try { console.info(msg); } catch(e) {}; $.jGrowl(msg); }

// localization

function lc()
{
	if (lang === undefined || arguments.length == 0)
		return '';

	var ret = [];
	var i_max = arguments.length;
	for (var i = 0; i < i_max; i++)
	{
		var l = lang[arguments[i]] === undefined ? arguments[i] : lang[arguments[i]];
		if (/^x_|_x_|_x$/.test(arguments[i]))
		{
			i++;
			if (i < i_max)
			{
				var l2 = lang[arguments[i]] === undefined ? arguments[i] : lang[arguments[i]];
				l = $.sprintf(l, l2);
			}
		}
		ret.push(l);
	}

	return ret.join('');
}
