/**
 * Json.Remote bug fix
 */
Json.Request = XHR.extend({
	initialize: function(url, options){
		this.url = url;
		this.addEvent('onSuccess', this.onComplete);
		this.parent(options);
		this.setHeader('X-Request', 'JSON');
	},
	send: function( queryString ){
		return this.parent( this.url, queryString );
	},
	onComplete: function(){
		this.fireEvent('onComplete', [Json.evaluate(this.response.text, this.options.secure)]);
	}
});

/*
 * initiate desktop
 *
 * @param Object options
 * @TODO set options
 * 		define options of taskbar
 */
function initiateDesktop ()
{	
	if ( typeof startmenuSettings != 'undefined' ) { preference.tools.startmenu = startmenuSettings; }
	
	var taskbar 	= new Element( 'div', { styles: preference.tools.taskbar, 'id': 'layout-taskbar' } );
	var startMenu 	= new Element( 'div', { styles: preference.tools.startmenu, 'id': 'startmenu', 'class': 'display-none' } );
	
	var startmenuSizeDiff = preference.tools.startmenu.height -
							( window.ie6 ? 0 :
								- ( preference.tools.startmenu[ 'padding-top' ] ? preference.tools.startmenu[ 'padding-top' ] : 0 )
								- ( preference.tools.startmenu[ 'padding-bottom' ] ? preference.tools.startmenu[ 'padding-bottom' ] : 0 )
							);
	
	taskbar.setStyles({ 'width': preference.container.styles.width - ( preference.tools.taskbar.padding * 2 ) });
	startMenu.setStyles({ 'left': 0, 'top': preference.container.styles.height - startmenuSizeDiff });
	
	taskbar.injectAfter( 'layout-container' );
	startMenu.injectAfter( 'layout-container' );
	
	var startButton = new Element( 'div', { 
		styles: {
			'float': 'left',
			'margin': 0,
			'padding': 0,
			'width': 190,
			'height': 27,
			'cursor': 'pointer',
			'background': 'url( /images/bio/btnStart.gif ) no-repeat center center'
		}, 
		events: { 'click': function () { $( 'startmenu' ).toggleClass( 'display-none' ); } } 
	}).inject( 'layout-taskbar' );
	
	// Contents View 메뉴생성
	if ( typeof makeStartMenuButtons == 'function' ) { makeStartMenuButtons(); }
}


function resizeDesktop ()
{	
	preference.container.styles = { 
		'width': window.getWidth() - ( window.ie ? 17 : 0 ), 
		'height': window.getHeight() - preference.tools.taskbar.height - ( preference.tools.taskbar.padding * 2 ), 
		'border': colors.container.border
	};
	if ( window.ie6 )
	{
		preference.container.styles = { 
			'width': document.body.clientWidth + 4, 
			'height': document.body.clientHeight - preference.tools.taskbar.height - ( preference.tools.taskbar.padding * 2 ), 
			'border': colors.container.border
		};
    }

	$( 'layout-container' ).setStyles( preference.container.styles );
	
	var startmenuSizeDiff = preference.tools.startmenu.height -
							( window.ie6 ? 0 :
								- ( preference.tools.startmenu[ 'padding-top' ] ? preference.tools.startmenu[ 'padding-top' ] : 0 )
								- ( preference.tools.startmenu[ 'padding-bottom' ] ? preference.tools.startmenu[ 'padding-bottom' ] : 0 )
							);
	
	$( 'layout-taskbar' ).setStyles({ 'width': preference.container.styles.width - ( preference.tools.taskbar.padding * 2 ) });
	$( 'startmenu' ).setStyles({ 'left': 0, 'top': preference.container.styles.height - startmenuSizeDiff });
	
	if ( typeof resizeDesktop == 'function' ) { resizeDesktopCustomized(); }
}


//window.addEvent( 'resize', function () { resizeDesktop(); } );


var OpenWindow = new Class({
	options: {
		windowName: null,
		mode: 'basic',
		doSave: true,
		key: null,
		isMemo: false,
		isFixed: false
	},
	
	initialize: function ( options ) {
		this.setOptions( options );
		
		this.fireEvent( 'onBeforeStart' );
		
		if ( $( 'window-' + this.options.windowName ) ) 
		{
			return;
		}
	
		var size = { x: preference.window.size.width, y: preference.window.size.height };
		if ( typeof this.options.current == 'undefined' )
		{
			this.options.current = {
				'left': preference.window.styles.left,
				'top': preference.window.styles.top,
				'width': size.x,
				'height': size.y
			};
		}
		else
		{
			this.options.current = {
				'left': ( this.options.current.left ) ? this.options.current.left : preference.window.styles.left,
				'top': ( this.options.current.top ) ? this.options.current.top : preference.window.styles.top,
				'width': ( typeof this.options.current.width	== 'undefined' || this.options.current.width 	> size.x ) ? this.options.current.width 	: size.x,
				'height': ( typeof this.options.current.height	== 'undefined' || this.options.current.height	> size.y ) ? this.options.current.height	: size.y
			};
		}
		
		if ( typeof this.options.activated == 'undefined' )
		{
			this.options.activated = { 'width': size.x + 100, 'height': size.y + 100 };
		}
		else
		{
			this.options.activated = {
				'width': ( typeof this.options.activated.width		== 'undefined' ? this.options.current.width + 100	: this.options.activated.width ),
				'height': ( typeof this.options.activated.height	== 'undefined' ? this.options.current.height + 100 	: this.options.activated.height )
			};
		}
		
		if ( typeof this.options.activated == 'undefined' )
		{
			this.options.init = {
				'width': ( this.options.current.width 	? this.options.current.width 	: this.options.zoomOut.width ),
				'height': ( this.options.current.height ? this.options.current.height	: this.options.zoomOut.height )
			};
		}
		
		this.start();
	},
	start: function () {
		var thisObject = this;
		var windowName = this.options.windowName;
		
		windows[ windowName ] = this.options;
		windowsIndex.push( windowName );
		
		var currentIndex = windowsIndex.indexOf( windowName );
		
		var titleBar = new Element( 'div', { styles: preference.window.title.styles, id: 'window-' + windowName + '-layer-title' } );
		var windowContent = new Element( 'div', { styles: preference.window.content.styles, id: 'window-' + windowName + '-layer-content' } );
		var statusBar = new Element( 'div', { styles: preference.window.status.styles, id: 'window-' + windowName + '-layer-status' } );
		var windowContainer = new Element( 'div', { styles: preference.window.styles, id: 'window-' + windowName } );
		
		titleBar.setStyles({ 'width': this.options.current.width });
		windowContent.setStyles({
			'width': this.options.current.width,
			'height': this.options.current.height - preference.bar.height
		});
		statusBar.setStyles({ 'width': this.options.current.width });
		windowContainer.setStyles({
			'width': this.options.current.width,
			'height': this.options.current.height,
			'z-index': currentIndex
		});
		
		titleBar.inject( windowContainer );
		windowContent.inject( windowContainer );
		statusBar.inject( windowContainer );
		windowContainer.inject( 'layout-container' );
		
		this.makeTitlebarElement();
		this.makeContentElement();
		this.makeStatusbarElement();
		this.makeWindowButtons();
		
	    titleBar.makeDraggable({
	    	'container': $( 'layout-container' ), 
	    	'droppables': $( 'layout-container' ), 
	    	'onBeforeStart': function () {
	    		thisObject.setFront( windowName );
	    		this.element = $( 'window-' + windowName );
	    		this.options.toCompare = { 'left': this.element.getStyle( 'left' ),  'top': this.element.getStyle( 'top' ) };
	    	},
	    	'onDrag': function () {
	    		this.element.setOpacity( 0.5 );
	    	},
	    	'onComplete': function () {
	    		this.element.setOpacity( 1 );
	    		if ( $( 'window-' + windowName ).getStyle( 'width' ).toInt() < preference.container.styles.width 
	    			 && $( 'window-' + windowName ).getStyle( 'height' ).toInt() < preference.container.styles.height ) 
	    		{
					windows[ windowName ].current = $( 'window-' + windowName ).getCoordinates();
				}
				
				if ( this.element.getStyle( 'left' ) != this.options.toCompare.left || this.element.getStyle( 'top' ) != this.options.toCompare.top ) 
				{
					thisObject.saveWindow();
				}
	    	}
	    });
	    
	    windowContainer.addEvent( 'click', function () { thisObject.setFront( windowName ); } );
		if ( this.options.mode == 'basic' 		) { this.initiateContent(); }
		else if ( this.options.mode == 'detail' ) { this.getContent(); }
		this.onComplete();
	},
	makeTitlebarElement: function () {
		var titleBarLeft 	= new Element( 'div', { styles: skins.window.titlebar.left, 'id': 'window-' + this.options.windowName + '-title-left' });
		var titleBarMiddle 	= new Element( 'div', { styles: skins.window.titlebar.middle, 'id': 'window-' + this.options.windowName + '-title' });
		var titleBarRight 	= new Element( 'div', { styles: skins.window.titlebar.right, 'id': 'window-' + this.options.windowName + '-title-right' });
		titleBarMiddle.setStyle( 'width', windows[ this.options.windowName ].current.width - windowSizeDiff.titlebar );
		
		titleBarLeft.inject( 'window-' + this.options.windowName + '-layer-title' );	
		titleBarMiddle.inject( 'window-' + this.options.windowName + '-layer-title' );
		titleBarRight.inject( 'window-' + this.options.windowName + '-layer-title' );
				
		var titleText = new Element( 'span', {
			styles: { 'position': 'absolute', 'margin-top': ( window.ie ? 5 : 4 ), 'font-size': '0.9em' },
			id: 'window-' + this.options.windowName + '-title-text'
		});
		titleText.inject( 'window-' + this.options.windowName + '-title' );
	},
	makeContentElement: function () {
		var contentLeft 	= new Element( 'div', { styles: skins.window.content.left, 'id': 'window-' + this.options.windowName + '-content-left' });
		var contentMiddle 	= new Element( 'div', { styles: skins.window.content.middle, 'id': 'window-' + this.options.windowName + '-content' });
		var contentRight 	= new Element( 'div', { styles: skins.window.content.right, 'id': 'window-' + this.options.windowName + '-content-right' });
		contentLeft.setStyles({ 'height': windows[ this.options.windowName ].current.height - preference.bar.height });
		contentMiddle.setStyles({
			'width': windows[ this.options.windowName ].current.width - windowSizeDiff.content,
			'height': windows[ this.options.windowName ].current.height - preference.bar.height
		});
		contentRight.setStyles({ 'height': windows[ this.options.windowName ].current.width - preference.bar.height });
		
		contentLeft.inject( 'window-' + this.options.windowName + '-layer-content' );	
		contentMiddle.inject( 'window-' + this.options.windowName + '-layer-content' );
		contentRight.inject( 'window-' + this.options.windowName + '-layer-content' );
	},
	makeStatusbarElement: function () {
		var statusBarLeft 	= new Element( 'div', { styles: skins.window.statusbar.left, 'id': 'window-' + this.options.windowName + '-status-left' });
		var statusBarMiddle	= new Element( 'div', { styles: skins.window.statusbar.middle, 'id': 'window-' + this.options.windowName + '-status' });
		var statusBarRight 	= new Element( 'div', { styles: skins.window.statusbar.right, 'id': 'window-' + this.options.windowName + '-status-right' });
		statusBarMiddle.setStyle( 'width', windows[ this.options.windowName ].current.width - windowSizeDiff.statusbar );
		
		statusBarLeft.inject( 'window-' + this.options.windowName + '-layer-status' );	
		statusBarMiddle.inject( 'window-' + this.options.windowName + '-layer-status' );
		statusBarRight.inject( 'window-' + this.options.windowName + '-layer-status' );
		
		var statusText = new Element( 'span', {
			styles: { 'position': 'absolute', 'margin-top': ( window.ie ? 5 : 4 ), 'font-size': '0.9em' }, 
			'id': 'window-' + this.options.windowName + '-status-text' 
		});
		statusText.inject( 'window-' + this.options.windowName + '-status' );
	},
	makeWindowButtons: function ()
	{
		var thisObject  = this;
		var windowName 	= this.options.windowName;
		var isMemo 		= this.options.isMemo;
		var isFixed		= this.options.isFixed;
	
		var buttonClose = new Element( 'span', {
			className: 'window-buttons-' + windowName,
			styles: preference.window.buttons.visible,
			events: { 
				'click': function () {
					var currentIndex = windowsIndex.indexOf( windowName );
					
					$( 'window-' + windowName ).remove();
//					$( 'task-button-' + windowName ).remove(); 
					windows[ windowName ] = null;
					windowsIndex.length = windowsIndex.length - 1;
					
					if ( isFixed == false && isMemo == false )
					{
						thisObject.saveWindow( true );
					}
					else if ( isFixed == false && isMemo == true ) 
					{
						thisObject.saveMemo( true );
					}
				}
			}
		}).setStyles( buttonSettings.close );
		
		var buttonMaximize = new Element( 'span', {
			className: 'window-buttons-' + windowName,
			styles: preference.window.buttons.visible,
			id: 'window-' + windowName + '-button-maximize',
			events: {
				'click': function () {					
					this.setStyle( 'display', 'none' );
					$( 'window-' + windowName + '-button-reset-size' ).setStyle( 'display', 'block' );
						
					if ( isFixed == false && isMemo == false )
					{
						$( 'window-' + windowName + '-button-zoom-in' ).setStyle( 'display', 'none' );
						$( 'window-' + windowName + '-button-zoom-out' ).setStyle( 'display', 'block' );
					}

					thisObject.resizeWindow( preference.container.styles, { 'top': 0, 'left': 0 } );
				}
			}
		}).setStyles( buttonSettings.maximize );
		
		var buttonResetSize = new Element( 'span', {
			className: 'window-buttons-' + windowName,
			styles: preference.window.buttons.unvisible,
			id: 'window-' + windowName + '-button-reset-size',
			events: {
				'click': function () {					
					this.setStyle( 'display', 'none' );
					$( 'window-' + windowName + '-button-maximize' ).setStyle( 'display', 'block' );
					
					thisObject.getContent();
					thisObject.resizeWindow( windows[ windowName ].current, windows[ windowName ].current );
				}
			}
		}).setStyles( buttonSettings.resetSize );
			
		var buttonMinimize = new Element( 'span', { 
			className: 'window-buttons-' + windowName,
			styles: preference.window.buttons.visible,
			events: { 'click': function () { $( 'window-' + windowName ).setStyle( 'display', 'none' ); } }
		}).setStyles( buttonSettings.minimize );
		
		if ( this.options.isFixed == false && this.options.isMemo == false )
		{
			var buttonZoomIn = new Element( 'span', {
				className: 'window-buttons-' + windowName,
				styles: ( thisObject.options.mode == 'basic' ) ? preference.window.buttons.visible : preference.window.buttons.unvisible,
				id: 'window-' + windowName + '-button-zoom-in',
				events: {
					'click': function () {					
						this.setStyle( 'display', 'none' );
						$( 'window-' + windowName + '-button-zoom-out' ).setStyle( 'display', 'block' );
						
						thisObject.getContent();
						//thisObject.resizeWindow( windows[ windowName ].activated );
					}
				}
			}).setStyles( buttonSettings.zoomIn );
			
			var buttonZoomOut = new Element( 'span', {
				className: 'window-buttons-' + windowName,
				styles: ( thisObject.options.mode == 'basic' ) ? preference.window.buttons.unvisible : preference.window.buttons.visible,
				id: 'window-' + windowName + '-button-zoom-out',
				events: {
					'click': function () {					
						this.setStyle( 'display', 'none' );
						$( 'window-' + windowName + '-button-zoom-in' ).setStyle( 'display', 'block' );
						
						$( 'window-' + windowName + '-button-maximize' ).setStyle( 'display', 'block' );
						$( 'window-' + windowName + '-button-reset-size' ).setStyle( 'display', 'none' );
						
						thisObject.initiateContent();
					}
				}
			}).setStyles( buttonSettings.zoomOut );
		}
		
		var buttonArea = new Element( 'div', { 'styles': { 'position': 'relative', 'background-color': skins.window.titlebar.middle[ 'background-color' ] } } ).setStyles( skins.window.button );
	
		buttonClose.inject( buttonArea );
		buttonMaximize.inject( buttonArea );
		buttonResetSize.inject( buttonArea );
		buttonMinimize.inject( buttonArea );
		
		if ( this.options.isFixed == false && this.options.isMemo == false )
		{
			buttonZoomIn.inject( buttonArea );
			buttonZoomOut.inject( buttonArea );
		}
		
		buttonArea.inject( 'window-' + windowName + '-title' + ( buttonSettings.position != false ? '-' + buttonSettings.position : '' ) );
		
		var buttonResize = new Element( 'span', { className: 'window-buttons-' + windowName, styles: preference.window.buttons.visible }).setStyles( buttonSettings.resize );
		buttonResize.setStyle( 'cursor', 'nw-resize' );
		buttonResize.inject( 'window-' + windowName + '-status' + ( buttonSettings.position != false ? '-' + buttonSettings.position : '' ) );
		
	    buttonResize.makeResizable({
	    	'limit': { x: [ preference.window.size.width ], y: [ preference.window.size.height ] },
	    	'onBeforeStart': function () {
	    		thisObject.setFront( windowName );
	    		this.element = $( 'window-' + windowName );
	    		this.options.limit.x[1] = preference.container.styles.width - this.element.getLeft().toInt();
	    		this.options.limit.y[1] = preference.container.styles.height - this.element.getTop().toInt();
	    	},
	    	'onDrag': function () {
	    		var currentWidth	= this.element.getStyle( 'width' ).toInt();				
	    		var currentHeight 	= this.element.getStyle( 'height' ).toInt() - preference.bar.height;
			    		
				$( 'window-' + windowName + '-title' ).setStyle( 'width', currentWidth - windowSizeDiff.titlebar );
				$( 'window-' + windowName + '-content-left' ).setStyle( 'height', currentHeight );
				$( 'window-' + windowName + '-content' ).setStyles({ 'width': currentWidth - windowSizeDiff.content, 'height': currentHeight - contentSizeDiff });
				$( 'window-' + windowName + '-content-right' ).setStyle( 'height', currentHeight );
				$( 'window-' + windowName + '-status' ).setStyle( 'width', currentWidth - windowSizeDiff.statusbar );
				
	    		$( 'window-' + windowName + '-layer-title' ).setStyle( 'width', currentWidth );
	    		$( 'window-' + windowName + '-layer-status' ).setStyle( 'width', currentWidth );
	    		$( 'window-' + windowName + '-layer-content' ).setStyles({
	    			'width': currentWidth,
	    			'height': currentHeight
	    		});
				if ( isFixed == false && isMemo == false )
				{
					$( 'window-' + windowName + '-button-zoom-in' ).setStyle( 'display', 'none' );
					$( 'window-' + windowName + '-button-zoom-out' ).setStyle( 'display', 'block' );
				}
				else if ( isFixed == false && isMemo == true )
				{ 
					$( 'textarea-' + windowName ).setStyles({ 'width': currentWidth - windowSizeDiff.content - 10, 'height': currentHeight - contentSizeDiff - 40 });
				}
	    	},
	    	'onComplete': function () {
	    		windows[ windowName ].current = $( 'window-' + windowName ).getCoordinates();
				if ( isFixed == false && isMemo == false && windows[ windowName ].status != 'activated' ) { thisObject.getContent(); }
				thisObject.saveWindow();				
	    	}
	    });
	},
	makeTaskButton: function ()
	{
		var thisObject = this;
		var windowName = this.options.windowName;
		
		var taskStyles = { 'float': 'left', 'display': 'block', 'margin-left': 2, 'padding-top': ( window.ie6 ? 0 : 4 ) };
	
	    var taskButton = new Element( 'span', {
    		id: 'task-button-' + windowName,
    		styles: taskStyles,
	    	events: { 'click': function () { $( 'window-' + windowName ).setStyle( 'display', 'block' ); thisObject.setFront( windowName ); } } 
		});
	    
		if ( typeof taskButtonSettings == 'undefined' )
		{
		    var taskButtonText = new Element( 'button', { id: 'task-button-' + windowName + '-text' });
	    	taskButtonText.setText( 'Loading...' );
	    	taskButtonText.inject( taskButton );
	    }
	    else
	    {
			new Element( 'div', { styles: taskButtonSettings.left } ).inject( taskButton );
			new Element( 'div', { styles: taskButtonSettings.middle, id: 'task-button-' + windowName + '-text' }).inject( taskButton ).setHTML( 'Loading...' );	
			new Element( 'div', { styles: taskButtonSettings.right } ).inject( taskButton );
	    }
	    
	    taskButton.inject( 'layout-taskbar' );
	},
	initiateContent: function ( appendix )
	{
		this.fireEvent( 'onInitiate' );
		
		var thisObject	= this;
		var windowName 	= this.options.windowName;

		if ( windows[ windowName ].status != 'activated' && typeof windows[ windowName ].status != 'undefined' && !appendix ) { return; }
		
		$( 'window-' + windowName + '-status-text' ).setHTML( 'Loading...' );
		$( 'window-' + windowName + '-layer-content' ).setStyle( 'cursor', 'wait' );

		var request = new Json.Remote( '/bio/content/', {
			'method': 'post',
			'onComplete': function ( result ) {
			
//				$( 'task-button-' + windowName + '-text' ).setText( result.title );
				
				$( 'window-' + windowName + '-title-text' ).setHTML( result.title );
				
				$( 'window-' + windowName + '-content' ).setHTML( result.content );
				$( 'window-' + windowName + '-status-text' ).setHTML( 'Done.' );
				$( 'window-' + windowName + '-layer-content' ).setStyle( 'cursor', 'auto' );
				
				if ( window.ie ) { result.size.height += 2; }
				if ( window.ie6 ) { result.size.height += 6; }
				
				windows[ windowName ].init = result.size;
				windows[ windowName ].activated = result.activated;
				
				if ( thisObject.options.isMemo == true && thisObject.options.key != 0  )
				{
					var request = new Json.Remote( '/bio/memo/', {
						'method': 'post',
						'onComplete': function ( result ) {
							$( 'textarea-' + windowName ).setProperty( 'value', result.content );
							$( 'span-' + thisObject.options.windowName ).setText( 'saved on ' + result.savedOn );
							thisObject.resizeWindow( result.options, result.options );
							windows[ windowName ].current = result.options;
						}
					}).send({ 'id': thisObject.options.key });
				}
				
				thisObject.resizeWindow( result.size, { 'top': windows[ windowName ].current.top, 'left': windows[ windowName ].current.left }  );
				
				if ( thisObject.options.doSave == false )
				{
					thisObject.options.doSave = true;
				}
				thisObject.setFront();
			}
		}).send({ 
			'window': windowName, 
			'width': windowSizeDiff.content + contentSizeDiff, 
			'height': preference.bar.height, 
			'padding': contentSizeDiff, 
			'appendix': ( thisObject.options.isMemo == true || thisObject.options.isFixed == true ? thisObject.options.key : appendix ), 
			'isMemo': thisObject.options.isMemo, 
			'isFixed': thisObject.options.isFixed
		});

		windows[ windowName ].status = 'init';
	},
	getContent: function ( appendix )
	{ 
		if ( this.options.isFixed == true || this.options.isMemo == true ) { return; }
		
		var thisObject	= this;
		var windowName 	= this.options.windowName;
		
		if ( windows[ windowName ].status != 'activated' )
		{
			this.resizeWindow( windows[ windowName ].activated );
		}
		appendix = ( typeof appendix == 'undefined' ) ? null : appendix;
		
		$( 'window-' + windowName + '-status-text' ).setHTML( 'Loading...' );
		$( 'window-' + windowName + '-layer-content' ).setStyle( 'cursor', 'wait' );

		$( 'window-' + windowName + '-button-zoom-in' ).setStyle( 'display', 'none' );
		$( 'window-' + windowName + '-button-zoom-out' ).setStyle( 'display', 'block' );
	
		
		var request = new Json.Remote( '/bio/detail/', {
			'method': 'post',
			'onComplete': function ( result ) {
				//$( 'task-button-' + windowName + '-text' ).setText( result.title );
				//$( 'window-' + windowName + '-title-text' ).setHTML( result.title + ( result.appendix ? ' > ' + result.appendix : '' ) );
				$( 'window-' + windowName + '-title-text' ).setHTML( result.title );
				$( 'window-' + windowName + '-content' ).setHTML( result.content );
				$( 'window-' + windowName + '-status-text' ).setHTML( 'Done.' );
				$( 'window-' + windowName + '-layer-content' ).setStyle( 'cursor', 'auto' );
								
				if ( thisObject.options.doSave == false )
				{
					thisObject.options.doSave = true;
				}
			}
		}).send({ 'window': windowName, 'width': windowSizeDiff.content + contentSizeDiff, 'height': preference.bar.height, 'padding': contentSizeDiff, 'appendix': appendix  });
		windows[ windowName ].status = 'activated';
		
		if ( this.options.mode == 'detail' )
		{
			this.resizeWindow( windows[ windowName ].current, windows[ windowName ].current  );
		}
	},
	resizeWindow: function ( size, coordi ) {
		var windowName 	= this.options.windowName; 
		
		$( 'window-' + windowName + '-title' ).setStyle( 'width', size.width - windowSizeDiff.titlebar );
		$( 'window-' + windowName + '-content-left' ).setStyle( 'height', size.height - preference.bar.height );
		$( 'window-' + windowName + '-content' ).setStyles({ 'width': size.width - windowSizeDiff.content, 'height': size.height - preference.bar.height - contentSizeDiff });
		$( 'window-' + windowName + '-content-right' ).setStyle( 'height', size.height - preference.bar.height );
		$( 'window-' + windowName + '-status' ).setStyle( 'width', size.width - windowSizeDiff.statusbar );
		
		$( 'window-' + windowName + '-layer-title' ).setStyle( 'width', size.width );
		$( 'window-' + windowName + '-layer-status' ).setStyle( 'width', size.width );
		$( 'window-' + windowName + '-layer-content' ).setStyles({ 'width': size.width, 'height': size.height - preference.bar.height });
		$( 'window-' + windowName ).setStyles({ 'width': size.width, 'height': size.height });
		
		if ( coordi ) { $( 'window-' + windowName ).setStyles({ 'left': coordi.left, 'top': coordi.top }); }

		if ( this.options.isMemo == true )
		{
			this.resizeMemo( size );
		}
		
		windows[ windowName ].current = $( 'window-' + windowName ).getCoordinates();
		this.saveWindow();
	},
	resizeMemo: function ( size )
	{
		$( 'textarea-' + this.options.windowName ).setStyles({ 'width': size.width - windowSizeDiff.content - 10, 'height': size.height - preference.bar.height - 40 });
	},
	setFront: function ()
	{
		if ( !$( 'window-' + this.options.windowName ) ) { return; }
		if ( windowsIndex.length - 1 == $( 'window-' + this.options.windowName ).getStyle( 'z-index' ) ) { return; }
		
		for ( var index = 1; index < windowsIndex.length - 1; index++ )
		{
			$( 'window-' + windowsIndex[ index + 1 ] ).setStyle( 'z-index', index );
			windowsIndex[ index ] = windowsIndex[ index + 1 ];
		}
		windowsIndex[ windowsIndex.length - 1 ] = this.options.windowName;
		$( 'window-' + windowsIndex[ index ] ).setStyle( 'z-index', windowsIndex.length - 1 );
	},
	saveMemo: function ( doDelete ) {
		doDelete = ( typeof doDelete == 'undefined' ) ? false : doDelete;
		
		if ( this.options.isMemo == true && doDelete == false )
		{
			var thisObject  = this; 
			var windowName  = this.options.windowName; 
			var content 	= encodeURIComponent( $( 'textarea-' + windowName ).getValue() );
			var sendData	= 'id=' + this.options.key + '&content=' + content + '&options=' + Json.toString( windows[ windowName ].current ) + '&doDelete=false';

			var request = new Json.Request( '/bio/memo-update/', {
				'method': 'post',
				'onComplete': function ( result ) {
					$( 'span-' + thisObject.options.windowName ).setText( 'saved on ' + result.savedOn );
					thisObject.options.key = result.key;
				}
			}).send( sendData );
		}
		else if ( this.options.isMemo == true && doDelete == true )
		{
			var thisObject  = this; 
			var sendData	= 'id=' + this.options.key + '&doDelete=true';

			var request = new Json.Request( '/bio/memo-update/', { 'method': 'post' } ).send( sendData );
		}
	},
	saveWindow: function ( doDelete ) {
		doDelete = ( typeof doDelete == 'undefined' ) ? false : doDelete;
		if ( this.options.isFixed == false && this.options.isMemo == false && this.options.doSave == true && doDelete == false )
		{
			var windowName  = this.options.windowName; 
			var request = new Json.Remote( '/bio/window/', { 'method': 'post' } ).send({
				'window': windowName, 
				'init': windows[ windowName ].init, 
				'activated': windows[ windowName ].activated, 
				'current': windows[ windowName ].current,
				'doDelete': doDelete
			});
		}
		else if ( this.options.isFixed == false && this.options.isMemo == false && this.options.doSave == true && doDelete == true )
		{
			var windowName  = this.options.windowName; 
			var request = new Json.Remote( '/bio/window/', { 'method': 'post' } ).send({ 'window': windowName, 'doDelete': doDelete });
		}
	},
	onComplete: function () {
		this.fireEvent( 'onComplete' );

		if ( buttonSettings.event == true )
		{
			$$( '.window-buttons-' + this.options.windowName ).addEvent( 'mouseleave', function () { this.setStyle( 'background-position', 'top center' ); } );
			$$( '.window-buttons-' + this.options.windowName ).addEvent( 'mouseenter', function () { this.setStyle( 'background-position', 'bottom center' ); } );
		}
		
		preference.window.styles.left += preference.nextPosition;
		preference.window.styles.top  += preference.nextPosition;
		
//		if ( !$( 'startmenu' ).hasClass( 'display-none' ) ) { $( 'startmenu' ).toggleClass( 'display-none' ); }
//		this.makeTaskButton();
		
		windows[ this.options.windowName ].handler = this;
		
		if ( this.options.mode == 'detail' )
		{
			this.options.mode = 'basic';
		}
		
		return true;
	}
});

OpenWindow.implement( new Events, new Options );

function loadWindow ( windowId )
{
	if ( typeof windowId == 'undefined' ) { return; }
	
	var request = new Json.Remote( '/bio/load/', {
		'method': 'post',
		'onComplete': function ( result ) {
			new OpenWindow( result.options );
		}
	}).send({ 'id': windowId });
}


function openMemo ( key )
{
	var key = ( typeof key == 'undefined' ) ? 0 : key;
	
	if ( key )
	{
		new OpenWindow({ 'windowName': 'memo-' + key, 'key': key, 'isMemo': true });
	}
	else
	{
		var request = new Json.Remote( '/bio/memo/', {
			'method': 'post',
			'onComplete': function ( result ) {
				new OpenWindow({ 'windowName': 'memo-' + result.windowKey, 'key': result.key, 'isMemo': true });
			}
		}).send({ 'id': key });
	}
}



