String.format = function(tmpl) {
	for(var i=1; i<arguments.length; i++) {
		re = new RegExp('\\\{' + (i-1) + '\\\}', 'gi');
		tmpl = tmpl.replace(re, arguments[i]);
	}
	return tmpl;
}

String.toJSON = function(str) {
	var _isd = null;
	
	if(typeof str == 'string') {
		try {
			eval('_isd=' + str);
		} catch(e) {}
	}
	
	return _isd;
}

String.toQueryString = function(obj) {
	var data = [], key;
	
	for(key in obj) {
		if(obj.hasOwnProperty(key)) data.push(key + '=' + obj[key]);
	};
	
	return data.join('&');
}

if(!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(value) {
		for(var i=0, length=this.length; i<length; i++) {
			if(this[i] == value) return i;
		}
		return -1;
	}
}

jQuery.fn.extend({
	// Bind method/function to scope
	'hitch': function(scope, method, argv) {
		return (function() {
			var _argv = (argv || []).concat(jQuery.makeArray(arguments));
			
			if(typeof method == 'string' && typeof scope[method] == 'function') {
				return scope[method].apply(scope, _argv);
			} else
			
			if(typeof method == 'function') {
				return method.apply(scope, _argv);
			}
		});
	},
	
	// Helper to bind ENTER press in input field
	'enter': function(fn, steals) {
		return this.keypress(function(event) {
			if(event.which == 13) {
				fn(event);
				if(steals == true) event.stopPropagation();
			}
		});
	},
	
	// Set/unset checked for input[type=checkbox]
	'checked': function(checked) {
		return this.each(function() { this.checked = checked; });
	},
	
	// Add data to SELECT
	'attach': function(data, isAppend) {
		this.each(function(index, node) {
			if(node.tagName.toUpperCase() == 'SELECT') {
				if(isAppend != false)  $(node).empty();
				
				for(var i=0; i<data.length; i++) {
					/*
					var option		= new Option();
					option.text		= data[i].text;
					option.value	= data[i].value;
					node.options[node.options.length] = option;
					
					if(data[i].disabled == true) option.disabled = true;
					*/
					
					var option = $(String.format('<option value="{0}">{1}</option>', data[i].value, data[i].text));
					if(data[i].disabled == true) option.attr('disabled', 'disabled');
					
					$(node).append(option);
				}
				
				$(node).change();
			}
		});
		
		return this;
	},
	
	'disable': function(disable) {
		  return this.each(function() {
			if(disable == true)	$(this).attr('disabled', true).addClass('disabled');
			else					$(this).removeAttr('disabled').removeClass('disabled'); 
		  })
	},
	
	'insertText': function(value) {
		this.each(function(index, node) {
			var success = false;
			 
			this.focus();
			 
			if(!success && document.selection) {
				var sel = document.selection;
				var rng = sel.createRange();
				rng.colapse;
				
				if((sel.type == "Text" || sel.type == "None") && rng != null) {
					rng.text = value;
					success = true;
				}
			}
			
			if(!success && this.selectionStart) {
				var ss = this.selectionStart;
				var st = this.scrollTop;
				var es = this.selectionEnd;
				
				var start  = (this.value).substring(0, ss);
				var end    = (this.value).substring(ss, this.textLength);
				
				this.value = start + value + end;
				
				var cpos = ss + (value.length);
				
				this.selectionStart = cpos;
				this.selectionEnd   = cpos;
				this.scrollTop      = st;
				
				success = true;
			}
			
			if(!success) {
				this.value += value;
			}			 
		});
		
		return this;
	}
});

jQuery.byId = function(id) {
	return $(String.format('#{0}', id));
}

// Slider for index page
jQuery.fn.extend({
	'slider': function(method) {
		$(this).each(function() {
			var node = $(this);
			var slides	= node.find('.slide'), 
				length	= slides.length, 
				width	= $(slides.get(0)).width();
			
			if(node.data('animation') != true) {
				switch(method) {
					case 'initialize':
						slides.first().before(slides.last());
						slides	= node.find('.slide');
						
						slides.each(function(index) {
							$(this).css('left', width * index - width);
						});
						
						break;
					
					case 'prev':
						node.data('animation', true);
						slides.animate({
							'left': '+=' + width
						}, 500, function() {
							if(slides.first().position().left == 0) slides.first().before(slides.last().css('left', -width));
							node.data('animation', false);
						});
						break;
					
					case 'next':
						node.data('animation', true);
						slides.animate({
							'left': '-=' + width
						}, 500, function() {
							if(slides.last().position().left == 0) slides.last().after(slides.first().css('left', width));
							node.data('animation', false);
						});
						break;
					
					case 'arrange':
						slides.each(function(index) {
							$(this).css('left', width * index - width);
						});
						
						break;
				}
			}
		});
		
		return this;
	},
	
	'first': function() {
		return $($(this).get(0));
	},
	
	'last': function() {
		return $($(this).get($(this).length - 1));
	}
});
