(function($){
	var id = null, f = 'playlists.js', tree = null; 
	$.music = {
		jget : {
			tracks : function(l) { return $.fn.l.get.o(l,'s'); },
			playlists : function(l) { return $.fn.l.get.o(l,'f'); },
			t : function() { return tree; },
			o : function(i) { return $.jutil.jget(i,'id',tree)}
		},
		jset : function (l,o) {
			o['id'] = String( $.jutil.m('id',tree,1) * 1 + 1 ); o['order'] = String( $.music.jorder(l) * 1 + 1 ); o['folder_id'] = String( l * 1 );
			return $.jutil.jset(l,'id',tree,o);
		},
		jupdate : function (o) {
			return $.jutil.jupdate(o['id'],'id',tree,o);
		},
		jremove : function (i) {
			return $.jutil.jremove(i,'id',tree);
		},
		jorder : function (l) {
			return $.jutil.order(l,'id',tree);
		},
		create : function(d) {
			id = d;
			tree = $.jutil.load(id,1,f);
		},
		save : function() {
			return $.jutil.save(id,1,f,tree);
		}
	};
	$.fn.l = {
		get : {
			o : function(l,t) {
				var o = $.jutil.jget(l,'id',tree);
				return ( o === null || typeof o === 'undefined' ) ? null : $.map(o['childs'],function(n) { return ( n != null && typeof n['type'] != 'undefined' && n['type'] === t ) ? n : null; });
			},
			i : function (l) {
				var o = $.jutil.jget(l,'id',tree);
				return ( o === null || typeof o === 'undefined' ) ? null : o['childs'];
			},
			m : function (l) {
				var o = $.jutil.jget(l,'id',tree), t = o['childs'], n = t[0]['order'], id = t[0]['id'];
				for (i in t)
					if (t[i]['order'] * 1 < n) {
						n = t[i]['order'];
						id = t[i]['id'];
					}
				return id;
			}
		},
		f : function(o,n,r) {
			for (var i in o) {
				if (typeof o[i]['childs'] != 'undefined') {
					t = r + o[i]['title'] + '/';
					n.push([t,o[i]['id']]);
					n = $.fn.l.f(o[i]['childs'],n,t);
				}
			}
			return n;
		},
		clone : {
			o : function(o,d,p) {
				for (var i in o)
					if (o[i]!= null && typeof o[i] === 'object')
						this[i] = (o[i].length > 0) ? $.fn.l.clone.a(o[i],d * 1 + 1,d) : [];
					else if (i === 'id')
						this[i] = String( d );
					else if (i === 'folder_id')
						this[i] = String( p );
					else
						this[i] = o[i];
			},
			a : function(o,d,p) {
				var r = [];
				for (var i in o)
					for (var j in o[i])
						r[i] = new $.fn.l.clone.o(o[i],d*1+i*1,p);
				return r;
			}
		},
		asc : function (a,b) {
			var x = a['title'].toLowerCase(), y = b['title'].toLowerCase();
			return ( x > y ) ? 1 : ( x <= y ) ? -1 : 0;
		},
		des : function (a,b) {
			var x = a['title'].toLowerCase(), y = b['title'].toLowerCase();
			return ( x < y ) ? 1 : ( x >= y ) ? -1 : 0;
		}
	};
})(jQuery);