ww.store = {
	opentype: undefined,
	
	tinyMCEBrowser: function( field_name, url, type, win ) {

		ww.store.opentype = {
			name: 'tiny',
			field_name: field_name,
			win: win,
			type: type
		};
		
		ww.store.setupDialog( '', function() {} );
		
		return false;
	},
	
	open: function( parent_id, callback ) {
	
		ww.store.opentype = { 
			name: 'node',
			type: 'image',
			callback: callback
		};
		
		ww.store.setupDialog( parent_id, callback );
	},
	
	setupDialog: function( parent_id, callback ) {
		
		$( '#store-dialog' ).remove();
		$.get( ww.buildPath( 'admin/store/' + parent_id ), function( response ) {
			$( 'body' ).append( '<div id="store-dialog"></div>' );
			$( '#store-dialog' )
			.dialog( { 
				bgiframe: true, 
				modal: true,
				resizable: false,
				width: 620,
				height: 570,
				autoOpen: false,
				closeOnEscape: false,
				zIndex: 400000,
				title: '<h1>Médiatár</h1>'
			} )
			.append( response )
			.bind( 'dialogopen', function( event, ui ) {
				ww.store.setupStoreScrollPane();
				ww.store.setupNodeScrollPane();
			} )
			.dialog( 'open' );
			ww.store.setupLightbox();
			$( '#store-tabs' ).tabs();
		} );
	},
	
	setupStoreScrollPane: function() {
		$( '#store-path-pane' ).jScrollPane( { showArrows: true, scrollbarWidth: 12 } );
		$( '#store-file-pane' ).jScrollPane( { showArrows: true, scrollbarWidth: 12 } );
	},
	
	setupNodeScrollPane: function() {
		$( '#store-node-pane' ).jScrollPane( { showArrows: true, scrollbarWidth: 12 } );
	},
	
	setupLightbox: function() {
		$( '#store-file-pane a.lightbox' ).slimbox( { closeKeys: [88, 67] } );
	},
	
	loadStart: function() {
		$( '#store-load' ).html( 'Betöltés...' ).show();
	},
	
	loadStop: function() {
		$( '#store-load' ).html( '' ).hide();
	},
	
	loadNode: function( parent_id ) {
		
		ww.store.loadStart();
		$.get( ww.buildPath( 'admin/store/node/' + parent_id ), function( response ) {
			$( '#tab-node' ).html( response );
			
			ww.store.setupNodeScrollPane();
			
			ww.store.loadStop();
		} );
	},
	
	loadStore: function( parent_id ) {
		
		ww.store.loadStart();
		$.get( ww.buildPath( 'admin/store/store/' + parent_id ), function( response ) {
			$( '#store-content' ).html( response );
			
			ww.store.setupStoreScrollPane();
			ww.store.setupLightbox();
			
			$( '#store-upload-form-parent-id' ).attr( 'value', parent_id );
			$( '#store-directory-form-parent-id' ).attr( 'value', parent_id );
			
			ww.store.loadStop();
		} );
	},

	formUploadSuccess: function() {
	
		var parent_id = $( '#store-upload-form-parent-id' ).attr( 'value' );
		ww.store.loadStore( parent_id );
	},
	
	formDirectorySuccess: function() {
	
		var parent_id = $( '#store-directory-form-parent-id' ).attr( 'value' );
		ww.store.loadStore( parent_id );
	},
	
	remove: function( id ) {
	
		ww.store.loadStart();
		$.get( ww.buildPath( 'admin/store/delete/' + id ), function( response ) {
			ww.store.formDirectorySuccess();
			ww.store.loadStop(); 
		} );
	},
	
	pick: function( type, id ) {
	
		if( ww.store.opentype.name == 'tiny' ) {
			var win = ww.store.opentype.win;
			var input = ww.store.opentype.field_name;

			if( ww.store.opentype.type == 'file' ) {
				var url = 'store/download/' + id;
			} else {
				var url = 'store/' + id;
			}
			if( type == 'node' ) {
				url = id;
			}

			win.document.getElementById( input ).value = url;

		}
		if( ww.store.opentype.name == 'node' && typeof( ww.store.opentype.callback ) == 'function' ) {
			ww.store.opentype.callback( id );
		}
		
		$( '#store-dialog' ).dialog( 'close' );
	}
}


