(function($){
	$.jutil = {
		jget : function(d,p,o) {
			for (var i in o) {
				if ( typeof o[i][p] != 'undefined' && String( o[i][p] ) === String( d ) )
					return o[i];
				else if ( typeof o[i]['childs'] != 'undefined' ) {
					var r = $.jutil.jget(d,p,o[i]['childs']);
					if (r != 'undefined' && r != null) return r;
				}
			}
		},
		jset : function(d,p,o,n) {
		 	for (var i in o) {
				if ( typeof o[i][p] != 'undefined' && o[i][p] === String( d ) && typeof o[i]['childs'] != 'undefined' )
					return Boolean( o[i]['childs'].push(n) );
				else if ( typeof o[i]['childs'] != 'undefined' ) {
					r = $.jutil.jset(d,p,o[i]['childs'],n);
					if (r) return r;
				}
			}
			return false;
		},
		jremove : function(d,p,o) {
		 	for (var i in o) {
				if ( typeof o[i][p] != 'undefined' && o[i][p] === String( d ) )
					return Boolean( (typeof o.splice === 'undefined') ? delete o[i] : o.splice(i,1) );
				else if ( typeof o[i]['childs'] != 'undefined' ) {
					var t = o[i], r = Boolean( $.jutil.jremove(d,p,o[i]['childs']) && t['childs'].sort($.jutil.s) );
					if (r) return r;
					//return Boolean( $.jutil.jremove(d,p,o[i]['childs']) && t['childs'].sort($.jutil.s) );
				}
			}
			return false;
		},
		jupdate : function(d,p,o,n) {
		 	for (var i in o) {
				if ( typeof o[i][p] != 'undefined' && o[i][p] === String( d ) )
					return Boolean( o[i] = n );
				else if ( typeof o[i]['childs'] != 'undefined' ) {
					var t = o[i], r = Boolean( $.jutil.jupdate(d,p,o[i]['childs'],n) && t['childs'].sort($.jutil.s) );
					if (r) return r;
					//return Boolean( $.jutil.jupdate(d,p,o[i]['childs'],n) && t['childs'].sort($.jutil.s) );
				}
			}
			return false;
		},
		s : function(a,b) {
			return ( a['order'] * 1 > b['order'] * 1 ) ? 1 : ( a['order'] * 1 <= b['order'] * 1 ) ? -1 : 0;  
		},
		load : function(i,p,t) {
			// p -> code
			// t-> file
			// i-> id
			var result;
			$.ajax({
				async: false,
				type: 'get',
				url: '../ajax/get-data.php',
				data: { "id" : i, "file" : t, "code": p || 1 },
				dataType: 'json',
				success: function( e ) {
					result = e || [];
				}
			});
			return result;
			/*return eval($.ajax({
				async:false,
				type: 'get',
				url: '/ajax/get-data.php',
				data: { "id" : i, "file" : t },
				dataType:'json'
				}).responseText);*/
		},
		save : function (i,p,t,d) {
			// p -> code
			// t-> file
			// i-> id
			// d->data
			var result = $.ajax({
				async:false,
				type: 'post',
				url: '../ajax/set-data.php',
				data: { "id" : i, "file" : t, "data" : escape( json.encode( d ) ), "code": p },
				dataType:'json'
			}).responseText;
			return Boolean( result );
		},
		order : function(d,p,o) {
			o = $.jutil.jget(d,p,o), m = 1;
			for (i in o['childs']) {
				if ( typeof o['childs'][i]['order'] != 'undefined' && o['childs'][i]['order'] * 1 > m )
					m = o['childs'][i]['order'];
			}
			return m;
		},
		m : function(p,o,m) {
			for (var i in o) {
				if ( typeof o[i][p] != 'undefined' && o[i][p] * 1 > m * 1 )
					m = o[i][p];
				if ( typeof o[i]['childs'] != 'undefined' )
					m = $.jutil.m(p,o[i]['childs'],m);
			}
			return m;
		},
		count : function( o, m ) {
			for ( var i in o )
				m = ( typeof o[i]["childs"] != "undefined" ) ? $.jutil.count( o[i]['childs'], m ) : ( typeof o[i] != "undefined" ) ? ++m : m;
			return m;
		}
	};
})(jQuery);
(function($){
	var id = null, f = 'favorites.js', tree = null; 
	$.jspeed = {
		jget : {
			speedlinks : function(l) { return $.fn.l.get.o(l,'s'); },
			folders : function(l) { return $.fn.l.get.o(l,'f'); },
			defaulttab : function() { return $.fn.l.get.m(0); },
			items : function (l) { return $.fn.l.get.i(l); },
			ftree : function() { return tree === null ? null : $.fn.l.f(tree[0]['childs'],[],''); },
			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['folder_id'] = String( l * 1 );
			o['order'] = String( $.jspeed.jorder(l) * 1 + 1 );
			if ( o['type'] === 'f' ) {
				var obj = $.jutil.jget(l,'id',tree),
					index = $.fn.l.lastIndexOf( obj['childs'], 'type', 'f' );
				obj['childs'].splice( ++index, 0, o );
				return true;
			}
			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);
		},
		jduplicate : function (l,i) {
			var t = $.jutil.jget(i,'id',tree), m = $.jutil.m('id',tree,1) * 1 + 1, o = new $.fn.l.clone.o(t,m,l);
			o['order'] = String( $.jspeed.jorder(l) * 1 + 1 );
			o['title'] = 'Copia de ' + o['title']; 
			return $.jutil.jset(l,'id',tree,o);
		},
		jorder : function (l) {
			return $.jutil.order(l,'id',tree);
		},
		jsort : function( id, type ) {
			var obj = $.jutil.jget( id, 'id', tree),
				links = $.fn.l.get.o(id,'s'),
				folders = $.fn.l.get.o(id,'f');
			if ( typeof obj === 'object' && typeof links === 'object' && typeof folders === 'object' ) {
				obj['childs'] = type ? folders.sort($.fn.l.asc).concat(links.sort($.fn.l.asc)) : folders.sort($.fn.l.des).concat(links.sort($.fn.l.des));
				return true;
			} else {
				return false;
			}
		},
		create : function(d) {
			id = d;
			tree = $.jutil.load(id,1,f);
		},
		createAnon : function(obj) {
			tree = obj;
		},
		createShare : function(alias) {
			var file_id = ( alias + ".js" ) || "";
			$.ajax({
				async: false,
				type: 'get',
				url: '../ajax/get-data.php',
				data: { 'file': file_id, 'code': 2 },
				dataType: 'json',
				success: function( e ) {
					tree = e;
				}
			});
			return tree;
		},
		save : function( ) {
			return $.jutil.save(id,1,f,tree);
			//return $.jutil.save(id,1,f,tree) && $.jspeed.savePublicShare();
		},
		savePublicShare : function( alias ) {
			var obj = $.fn.l.getShareFolders( [], tree[0].childs ),
				result = false;
			if ( typeof obj !== "undefined" ) {
				$.ajax({
					async: false,
					global: false,
					type: "post",
					url: "../ajax/set-public.php",
					data: { "data": escape( json.encode( [ { "id": 0, "title": "root", "childs": obj } ] ) ), "alias": alias || "" },
					dataType: "json",
					success : function ( e ) {
						result = e;
					}
				});
			}
			return result;
		},
		getSharedParent : function( id ) {
			var parent = $.jspeed.jget.o( id );
			if ( typeof parent === "object" && "folder_id" in parent && "type" in parent && parent.type === "f" ) {
				if ( "shared" in parent && parent.shared * 1 === 1 ) {
					return true;
				} else if ( parent.folder_id * 1 === 0 ) {
					return false;
				} else {
					return $.jspeed.getSharedParent( parent.folder_id );
				}
			} else {
				return false;
			}
		},
		setChildsShared : function ( actual, mode ) {
			var obj = actual.childs || [];
			for ( var i = 0; i < obj.length; i++ ) {
				if ( "type" in obj[i] && obj[i].type === "f" ) {
					obj[i]["shared"] = String( mode );
					return ( "childs" in obj[i] && obj[i].childs.length > 0 ) ? $.jspeed.setChildsShared( obj[i], mode ) : true;
				}
			}
			return true;
		}
	};
	$.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), id;
				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;
		},
		getShareFolders : function( vec, tree ) {
			var obj;
			for ( var i = 0; i < tree.length; i++ ) {
				obj = tree[i];
				if ( "type" in obj && obj.type === "f" ) {
					if ( "shared" in obj && obj.shared * 1 === 1 ) {
						vec.push( obj );
					} else if ( "childs" in obj && obj.childs.length > 0 ) {
						vec = $.fn.l.getShareFolders( vec, obj.childs );
					}
				}
			}
			return vec;
		},
		lastIndexOf : function( obj, proper, value ) {
			var index = -1;
			for ( var i = 0; i < obj.length; i++ ) {
				if ( typeof obj[i][proper] !== 'undefined' && obj[i][proper] === value ) {
					index = i;
				}
			}
			return index;
		}
	};
})(jQuery);