var demo = {fn: function(){}};

(function($){
	
	demo.itemSimple = function()
	{
		$('#demo-contact')
			.item({
				name: "Rizqi Ahmad",
				address: "Somestrasse 50, Hamburg",
				country: "Germany"
			})
			.chain();
		demo.itemSimple = demo.fn;
	};
	
	demo.itemCustomBuilder = function()
	{
		$('#demo-contact')
			.item({
				name: "Rizqi Ahmad",
				address: "Somestrasse 50, Hamburg",
				country: "Germany"
			})
			.chain({
				'.name': 'Hello, My Name is {name}',
				'.address': 'My address is {address}',
				'.country': 'It is in {country}'
			});
		demo.itemCustomBuilder = demo.fn;
	};
	
	demo.itemsSimple = function()
	{
		$('#demo-persons')
			.items([
				{first:'Isaac', last:'Newton'},
				{first:'Johannes', last:'Keppler'},
				{first:'Alessandro', last:'Volta'},
				{first:'Blaise', last:'Pascal'}
			])
			.chain();
		demo.itemsSimple = demo.fn;
	};
	
	demo.itemsAddRemove = function()
	{
		$('#demo-persons')
			.items([
				{first:'Isaac', last:'Newton'},
				{first:'Johannes', last:'Keppler'},
				{first:'Alessandro', last:'Volta'},
				{first:'Blaise', last:'Pascal'}
			])
			.chain(function(){
				this.dblclick(function(){
					$(this).item('remove');
				});
			});
		
		demo.itemsAddRemove_add = function()
		{
			$('#demo-persons').items('add', {
				first: $('#demo-input-first').val(),
				last: $('#demo-input-last').val()
			});
		}
	};
	
	demo.eventBinding = function()
	{
		$('#demo-persons')
			.items([
				{first:'Isaac', last:'Newton'},
				{first:'Johannes', last:'Keppler'},
				{first:'Alessandro', last:'Volta'},
				{first:'Blaise', last:'Pascal'}
			])
			.chain(function(){
				var self = this;
				this.click(function(){
					var data = self.item();
					alert(data.last+', '+data.first);
				})
			});
	};
	
	demo.itemsSorting = function()
	{
		$('#demo-persons')
			.items([
				{first:'Isaac', last:'Newton'},
				{first:'Johannes', last:'Keppler'},
				{first:'Alessandro', last:'Volta'},
				{first:'Blaise', last:'Pascal'},
				{first:'Niels', last:'Bohr'},
				{first:'Albert', last:'Einstein'},
				{first:'Stephen', last:'Hawking'}
			])
			.chain();
		
		demo.itemsSorting_first = function()
		{
			$('#demo-persons').items('sort', 'first', {toggle:true});
		};
		
		demo.itemsSorting_last = function()
		{
			$('#demo-persons').items('sort', 'last', {toggle:true});
		}
	};
	
	demo.itemsFiltering = function()
	{
		$('#demo-persons')
			.items([
				{first:'Isaac', last:'Newton'},
				{first:'Johannes', last:'Keppler'},
				{first:'Alessandro', last:'Volta'},
				{first:'Blaise', last:'Pascal'},
				{first:'Niels', last:'Bohr'},
				{first:'Albert', last:'Einstein'},
				{first:'Stephen', last:'Hawking'}
			])
			.chain();
		
		$('#demo-input-filter').keydown(function(){
			var self = $(this);
			setTimeout(function(){	
				$('#demo-persons').items('filter', self.val());
			})
		})
	};
	
	demo.simpleLinking = function()
	{
		$('#demo-contact')
				.item({
					name: "John Doe",
					company: "The Example Company",
					phone: "604-555-1234",
					url: "http://example.com/"
				})
				.chain({
					'.name': '{name}',
					'.company': '{company}',
					'.phone': '{phone}',
					'.url': {href:'{url}', content:'{url}'}
				});
			
			demo.simpleLinking_link = function()
			{
				$('#demo-edit')
					.item('link', '#demo-contact', 'self')
					.chain(function(){
						var self = this;
						
						// Update item on keypress
						this.find(':input').keydown(function(){
							// You can avoid using setTimeout if
							// You use keyup instead
							setTimeout(function(){
								self.item({
									name: self.find('.name').val(),
									company: self.find('.company').val(),
									phone: self.find('.phone').val(),
									url: self.find('.url').val()
								})
							})
						})
					});
				
				demo.simpleLinking_link = demo.fn;
			}
	};
	
	
	demo.itemsLinking = function()
	{
		$('#demo-persons')
			.items([
				{first:'Isaac', last:'Newton'},
				{first:'Johannes', last:'Keppler'},
				{first:'Alessandro', last:'Volta'},
				{first:'Blaise', last:'Pascal'},
				{first:'Niels', last:'Bohr'},
				{first:'Albert', last:'Einstein'},
				{first:'Stephen', last:'Hawking'}
			])
			.chain();
		
		$('#demo-input-filter').keydown(function(){
			var self = $(this);
			setTimeout(function(){	
				$('#demo-persons').items('filter', self.val());
			})
		});
		
		$('#demo-filtered').items('link', '#demo-persons', 'hidden').chain();
	};
	
})(MyJQ);