OSDN Git Service

version 0.5.32, UI.FileInput was fixed for Opera9.
[pettanr/clientJs.git] / 0.5.x / javascripts / peta.apps.js
1 /*
2  * pettanR peta.apps.js
3  *   version 0.5.32
4  *   
5  * author:
6  *   itozyun
7  * licence:
8  *   3-clause BSD
9  */
10
11 ( function( pettanr, gOS, window, document, undefined ){
12 /*
13  * PettanR service driver.
14  */
15         var MyAuthorID = 'current_author' in window ? current_author.id : ( pettanr.CONST.SERVER_SUPPORT === false ? 1 : -1 ),
16                 MyArtistID = 'current_artist' in window ? current_artist.id : ( pettanr.CONST.SERVER_SUPPORT === false ? 1 : -1 ),
17                 Driver     = null,
18                 FileAPI    = gOS.registerDriver( function(){
19                         var self = Driver = this;
20                         
21                         function onLoadJson( _file, _json ){
22                                 var _access = FileAPI.getFileDataAccess( _file ),
23                                         _data   = _access !== null ? _access.DATA : null;
24                                 if( _data === null ){
25                                         onErrorJson( _file );
26                                         return;
27                                 };
28                                 _data.state = Const.FILE.STATE.OK;
29                                 
30                                 if( Type.isArray( _json ) === true ){
31                                         for( var i = 0, l = _json.length; i < l; ++i ){
32                                                 buildFileData( _json[ i ], _data );
33                                         };
34                                 } else
35                                 if( Type.isNumber( _json.id ) === true ){
36                                         buildFileData( _json, _data );
37                                 };
38                                 _file.dispatchEvent( FileAPI.createFileEvent( Const.FILE.EVENT.GET_SEQENTIAL_FILES, _file, 'children', null ) );
39                         };
40                         function onErrorJson( _file ){ 
41                                 var _data = FileAPI.getFileData( _file);
42                                 if( _data !== null){
43                                         _data.state = Const.FILE.STATE.ERROR;
44                                 };
45                                 _file.dispatchEvent( FileAPI.createFileEvent( Const.FILE.EVENT.GET_SEQENTIAL_FILES, _file, 'children', null ) );
46                         };
47                         function buildFileData( _data, _parent ){
48                                 var _array, i, l;
49                                 // Panel
50                                 if( _parent === FILE_DATA_PANELS_ROOT || _parent === FILE_DATA_MY_PANELS_ROOT ){
51                                         _data.type = FILE_TYPE.PANEL;
52                                         _array = PANEL_ARRAY;
53                                 } else
54                                 // Comic
55                                 if( _parent === FILE_DATA_COMICS_ROOT ){
56                                         _data.type = FILE_TYPE.COMIC;
57                                         _array = COMIC_ARRAY;
58                                 } else
59                                 if( _parent.type === FILE_TYPE.COMIC ){
60                                         _array = COMIC_ARRAY;
61                                 } else
62                                 // Lisence
63                                 if( _parent === FILE_DATA_LISENCE_ROOT ){
64                                         _data.type = FILE_TYPE.LICENSE;
65                                         _array = ORIGINAL_LICENSE_ARRAY;
66                                 } else
67                                 // Author
68                                 if( _parent === FILE_DATA_AUTHOR_ROOT ){
69                                         _data.type = FILE_TYPE.AUTHOR;
70                                         _array = AUTHOR_ARRAY;
71                                 } else
72                                 // Artist
73                                 if( _parent === FILE_DATA_ARTIST_ROOT ){
74                                         _data.type = FILE_TYPE.ARTIST;
75                                         _array = ARTIST_ARRAY;
76                                 } else          
77                                 // Picture
78                                 if( _parent === FILE_DATA_PICTURE_ROOT || _parent === FILE_DATA_MY_PICTURES_ROOT ){
79                                         _data.type = FILE_TYPE.PICTURE;
80                                         _array = RESOURCE_PICTURE_ARRAY;
81                                         // original_license を含まなければ、license object を削除して ビットデータ で保持
82                                         // original_license なら ファイルを作る buildFileData( _license, FILE_DATA_LISENCE_ROOT)
83                                         var _license = _data.license,
84                                                 _rule,
85                                                 _Math_pow = Math.pow,
86                                                 _bits = 0;
87                                         if( typeof _license === 'object' ){
88                                                 for( i=0, l=BASIC_LICENSES.length; i<l; ++i ){
89                                                         _rule = _license[ BASIC_LICENSES[ i]];
90                                                         if( typeof _rule === 'number' && _rule === 1 ){
91                                                                 _bits += _Math_pow( 2, i );
92                                                         };
93                                                 };
94                                                 _data.license = _bits;
95                                         };
96                                 } else {
97                                         alert( 'error' );
98                                 };
99                                 
100                                 _data.driver = Driver;
101                                 // alert( ' _array ' + _data.id + Type.isNumber( _data.id ) );
102                                 // _array に _data を格納 または 上書き
103                                 if( Type.isNumber( _data.id ) === true && _data.id > 0 ){
104                                         var _id       = _data.id - 1,
105                                                 __data    = _array[ _id ],
106                                                 _reserved = Const.FILE.DATA_PROPERTY_RESERVED.join( ', ' );
107                                         if( __data ){
108                                                 for( var key in _data ){
109                                                         if( _reserved.indexOf( key ) === -1 ){
110                                                                 __data[ key ] = _data[ key ];
111                                                         };
112                                                 };
113                                                 _data = __data; // このタイミングで参照が切れるので注意!!
114                                         } else {
115                                                 _array[ _id ] = _data;
116                                         };
117                                 } else {
118                                         // alert( 'error' + _parent.name + ' ' + _data.id );
119                                 };
120                 
121                                 // Author
122                                 // Artist
123                                 if( _parent === FILE_DATA_AUTHOR_ROOT || _parent === FILE_DATA_ARTIST_ROOT ){
124                                         addChildData( _parent, _data );
125                                 } else
126                                 // Comic + Panels
127                                 if( _parent.type === FILE_TYPE.COMIC || _parent === FILE_DATA_COMICS_ROOT ){
128                                         var _panels = _data.stories,
129                                                 _panel;
130                                         if( Type.isArray( _panels ) === true ){
131                                                 
132                                                 for( i=0, l=_panels.length; i<l; ++i ){
133                                                         _panel = buildFileData( _panels[ i ], FILE_DATA_PANELS_ROOT );
134                                                         /*
135                                                          * 間違い! t 順に格納
136                                                          */
137                                                         addChildData( _data, _panel );
138                                                 };
139                                                 delete _data.stories;
140                                         } else {
141                                                 if( _data.json !== null ){
142                                                         _data.json = true;
143                                                 };
144                                                 if( Type.isArray( _data.children ) === false ){
145                                                         _data.children = [];
146                                                 };
147                                         };
148                                         var _author = _data.author || getResource( AUTHOR_ARRAY, _data.author_id );
149                                         if( _author ){
150                                                 _data.author = _author = buildFileData( _author, FILE_DATA_AUTHOR_ROOT );
151                                                 addChildData( _author, _data );
152                                                 _author.id === MyAuthorID && addChildData( FILE_DATA_MY_COMICS_ROOT, _data );
153                                         };
154                                         if( _parent === FILE_DATA_COMICS_ROOT ){
155                                                 addChildData( FILE_DATA_LATEST_COMICS, _data );
156                                         };
157                                 } else
158                                 // Panel
159                                 if( _parent === FILE_DATA_PANELS_ROOT || _parent === FILE_DATA_MY_PANELS_ROOT ){
160                                          _data.comic && buildFileData( _data.comic, FILE_DATA_COMICS_ROOT );
161                                         _data.author && buildFileData( _data.author, FILE_DATA_AUTHOR_ROOT );
162                                         
163                                         _data.comic  = getResource( COMIC_ARRAY, _data.comic_id );
164                                         _data.author = getResource( AUTHOR_ARRAY, _data.author_id );
165                                         
166                                         // picture data をファイルに取り出し
167                                         var _elements = _data.panel_elements,
168                                                 _elm;
169                                         if( Type.isArray( _elements ) === true ){
170                                                 for( i=0, l=_elements.length; i<l; ++i ){
171                                                         _elm = _elements[ i];
172                                                         if( _elm.resource_picture ){
173                                                                 _elm.resource_picture = buildFileData( _elm.resource_picture, FILE_DATA_PICTURE_ROOT ); // 上記参照切れに備えてここで上書き
174                                                         } else {
175                                                                 _elm.resource_picture = getResource( RESOURCE_PICTURE_ARRAY, _elm.picture_id );
176                                                         };
177                                                 };
178                                         };
179                                         _data.publish === 1 && addChildData( FILE_DATA_LATEST_PANELS, _data );
180                                         _data.author_id === MyAuthorID && addChildData( FILE_DATA_MY_PANELS_ROOT, _data );
181                                 } else
182                                 // Picture
183                                 if( _data.type == FILE_TYPE.PICTURE ){
184                                         var _artist = _data.artist || getResource( ARTIST_ARRAY, _data.artist_id );
185                                         if( _artist){
186                                                 _data.artist = _artist = buildFileData( _artist, FILE_DATA_ARTIST_ROOT );
187                                                 addChildData( _artist, _data );
188                                                 if( _artist.id === MyArtistID ){
189                                                         addChildData( FILE_DATA_MY_PICTURES_ROOT, _data );
190                                                         //FILE_DATA_MY_PICTURES_ROOT.type = FILE_TYPE.ARTIST;
191                                                         //FILE_DATA_MY_PICTURES_ROOT.id = MyArtistID;
192                                                 };
193                                         };
194                                 };
195                                 return _data;
196                         };
197                         function addChildData( _parent, _child ){
198                                 if( Type.isArray( _parent.children ) === false ){
199                                         _parent.children = [];
200                                 };
201                                 Util.getIndex( _parent.children, _child ) === -1 && _parent.children.push( _child );
202                         };
203                         function getResource( _array, _id ){
204                                 if( Type.isArray( _array ) === false || Type.isNumber( _id ) === false || _id < 1 ) return null;
205                                 var _data = _array[ _id - 1 ];
206                                 if( !_data ){
207                                         _data = _array[ _id - 1 ] = {};
208                                 };
209                                 return _data;
210                         };
211                         
212                         this.getSeqentialFiles = function( _file ){
213                                 var _data = FileAPI.getFileData( _file ),
214                                         _json = _data !== null ? _data.json : null;
215                                 if( _json === true && _data.type === FILE_TYPE.COMIC ){
216                                         if( pettanr.CONST.SERVER_SUPPORT === false ){
217                                                 _json = [ 'json\/comics_', _data.id, '.json' ].join( '' );
218                                         } else {
219                                                 _json = [ pettanr.CONST.PETTANR_ROOT_PATH, 'comics\/', _data.id, '.json' ].join( '' );
220                                         };
221                                 };
222                                 if( typeof _json === 'string' ){
223                                         FileAPI.getJson( _file, _json, onLoadJson, onErrorJson );
224                                         _data.state = Const.FILE.STATE.LOADING;
225                                         _data.json  = null;
226                                         return;
227                                 };
228                         };
229                         this.getName = function( _file ){
230                                 var _data = FileAPI.getFileData( _file ),
231                                         _type = _data !== null ? _data.type : null;
232                                 if( _type === FILE_TYPE.PICTURE ){
233                                         return [ _data.id, _data.ext ].join( '.' );
234                                 } else
235                                 if( _type === FILE_TYPE.PANEL ){
236                                         return [ 'id:' + _data.id + ', ' + ( _data.t || '' ) + ':', _data.comic ? _data.comic.title : 'no comic' ].join( '' );
237                                 } else
238                                 if( _type === FILE_TYPE.COMIC ){
239                                         return _data.title;
240                                 } else
241                                 if( _type === FILE_TYPE.ARTIST ){
242                                         return [ _data.name, '画伯' ].join( '' );
243                                 } else
244                                 if( _type === FILE_TYPE.AUTHOR ){
245                                         return [ _data.name, '先生' ].join( '' );
246                                 };
247                                 return _data.name;
248                         };
249                         this.getThumbnail = function( _file ){
250                                 var _data = FileAPI.getFileData( _file ),
251                                         _type = _data !== null ? _data.type : null;
252                                 if( _type === FILE_TYPE.PICTURE ){
253                                         return { image: [ pettanr.CONST.THUMBNAIL_PATH, _data.id, '.', _data.ext ].join( '' )};
254                                 };
255                                 if( _data === FILE_DATA_COMICS_ROOT ){
256                                         return { className: 'file-type-cabinet' };
257                                 };
258                                 if( _type === FILE_TYPE.COMIC ){
259                                         return { className: 'file-type-comic' };
260                                 };
261                                 if( _type === FILE_TYPE.PANEL ){
262                                         return { className: 'file-type-panel' };
263                                 };
264                                 if( _type === FILE_TYPE.AUTHOR ){
265                                         return { className: 'file-type-author' };
266                                 };
267                                 if( _type === FILE_TYPE.ARTIST ){
268                                         return { className: 'file-type-artist' };
269                                 };
270                                 if( _type === FILE_TYPE.FOLDER){
271                                         return { className: 'file-type-folder' };
272                                 };
273                                 return { className: 'file-type-broken' };
274                         };
275                         this.getSummary = function( _file ){
276                                 var _data = FileAPI.getFileData( _file ),
277                                         _type = _data !== null ? _data.type : null;
278                                 if( _type === FILE_TYPE.PICTURE ){
279                                         return [ _data.width, 'x', _data.height, ', filesize:', _data.filesize, ', lisence:', _data.license ].join( '' );
280                                 };
281                                 if( _data === FILE_DATA_COMICS_ROOT ){
282                                         return 'cabinet file';
283                                 };
284                                 if( _type === FILE_TYPE.COMIC ){
285                                         return 'comic file, id:' + _data.id;
286                                 };
287                                 if( _type === FILE_TYPE.PANEL ){
288                                         return [ _data.width, 'x', _data.height ].join( '' );
289                                 };
290                                 if( _type === FILE_TYPE.AUTHOR ){
291                                         return 'author file, id:' + _data.id;
292                                 };
293                                 if( _type === FILE_TYPE.ARTIST ){
294                                         return [ 'id:', _data.id, ' Email:', _data.email || 'empty' , ', HP:', _data.homepage_url || 'empty' ].join( '' );
295                                 };
296                                 if( _type === FILE_TYPE.FOLDER ){
297                                         return 'pettanR folder';
298                                 };
299                                 return 'pettanR unknown file';
300                         };
301                         this.read = function( _file ){
302                                 var _data = FileAPI.getFileData( _file ),
303                                         _type = _data !== null ? _data.type : null,
304                                         ret;
305                                 if( _type === FILE_TYPE.COMIC ){
306                                         // children を panels に deepcopy
307                                         ret = {};
308                                         for( var key in _data ){
309                                                 ret[ key ] = _data[ key ]
310                                         }
311                                         ret.stories = _data.children;
312                                         return ret;
313                                 }
314                                 if( _type === FILE_TYPE.PANEL ){
315                                 };
316                                 if( _type === FILE_TYPE.PANEL_PICTURE ){
317                                         
318                                 };
319                                 if( _type === FILE_TYPE.BALLOON ){
320                                 };
321                                 if( _type === FILE_TYPE.PICTURE ){
322                                 };
323                         };
324                         this.write = function( _file, _newData, _onUpdate ){
325                                 var _data = FileAPI.getFileData( _file ),
326                                         _type = _data !== null ? _data.type : null;
327                                 if( _type === FILE_TYPE.COMIC ){
328                                 };
329                                 if( _type === FILE_TYPE.PANEL ){
330                                 };
331                                 if( _type === FILE_TYPE.PANEL_PICTURE ){
332                                         
333                                 };
334                                 if( _type === FILE_TYPE.BALLOON ){
335                                 };
336                                 if( _type === FILE_TYPE.PICTURE ){
337                                 };
338                         };
339                         this.viewerApplicationList = function( _file ){
340                                 var _data = FileAPI.getFileData( _file ),
341                                         _type = _data !== null ? _data.type : null;
342                                 if( _type === FILE_TYPE.PANEL ){
343                                         return [ Reader ];
344                                 };
345                                 if( _type === FILE_TYPE.COMIC ){
346                                         return [ Reader ];
347                                 };
348                                 if( _data === FILE_DATA_MY_PICTURES_ROOT ){
349                                         return [ PremiumSatge ];
350                                 };
351                                 if( _type === FILE_TYPE.ARTIST ){
352                                         return [ PremiumSatge ];
353                                 };
354                                 return [];
355                         };
356                         this.editorApplicationList = function( _file ){
357                                 var _data = FileAPI.getFileData( _file ),
358                                         _type = _data !== null ? _data.type : null;
359                                 if( _type === FILE_TYPE.PANEL ){
360                                         return [ Editor ];
361                                 }
362                                 if( _type === FILE_TYPE.COMIC ){
363                                         return [Editor, ComicConsole ];
364                                 }
365                                 return [];
366                         }
367                 }),
368                 Const = FileAPI.getConst(),
369                 FILE_TYPE = Util.extend(
370                         Const.FILE.TYPE,
371                         {
372                                 COMIC:                          FileAPI.createFileTypeID(),
373                                 PANEL:                          FileAPI.createFileTypeID(),
374                                 PICTURE:                        FileAPI.createFileTypeID(),
375                                 PANEL_PICTURE:          FileAPI.createFileTypeID(),
376                                 BALLOON:                        FileAPI.createFileTypeID(),
377                                 AUTHOR:                         FileAPI.createFileTypeID(),
378                                 ARTIST:                         FileAPI.createFileTypeID(),
379                                 LICENSE:                        FileAPI.createFileTypeID()
380                         }
381                 ),
382                 FILE_DATA_SERVICE_ROOT = {
383                         name:           'PettanR root',
384                         type:           FILE_TYPE.FOLDER,
385                         children:       []
386                 },
387                 FILE_DATA_COMICS_ROOT = {
388                         name:           'Comics',
389                         type:           FILE_TYPE.FOLDER,
390                         children:       [],
391                         driver:         Driver,
392                         json:           pettanr.CONST.URL_COMICS_JSON
393                 },
394                 FILE_DATA_PANELS_ROOT = {
395                         name:           'Panels',
396                         type:           FILE_TYPE.FOLDER,
397                         children:       [],
398                         driver:         Driver,
399                         json:           pettanr.CONST.URL_PANELS_JSON
400                 },
401                 FILE_DATA_PICTURE_ROOT = {
402                         name:           'Picutures',
403                         type:           FILE_TYPE.FOLDER,
404                         children:       [],
405                         driver:         Driver,
406                         json:           pettanr.CONST.URL_RESOURCE_PICTURES_JSON
407                 },
408                 FILE_DATA_MY_COMICS_ROOT = {
409                         name:           'My Comics',
410                         type:           FILE_TYPE.FOLDER,
411                         children:       [],
412                         driver:         Driver,
413                         id:                     MyAuthorID
414                 },
415                 FILE_DATA_LATEST_COMICS = {
416                         name:           'Latest Comics',
417                         type:           FILE_TYPE.FOLDER,
418                         children:       []
419                 },
420                 FILE_DATA_LATEST_PANELS = {
421                         name:           'Latest Panels',
422                         type:           FILE_TYPE.FOLDER,
423                         children:       []
424                 },
425                 FILE_DATA_MY_PANELS_ROOT = {
426                         name:           'My Panels',
427                         type:           FILE_TYPE.FOLDER,
428                         children:       [],
429                         driver:         Driver,
430                         json:           pettanr.CONST.URL_MY_PANELS_JSON
431                 },
432                 FILE_DATA_MY_PICTURES_ROOT = {
433                         name:           'My Pictures',
434                         type:           FILE_TYPE.FOLDER,
435                         children:       [],
436                         driver:         Driver,
437                         json:           pettanr.CONST.URL_ORIGINAL_PICTURES_JSON,
438                         id:                     MyArtistID
439                 },
440                 FILE_DATA_AUTHOR_ROOT = {
441                         name:           'Authors',
442                         type:           FILE_TYPE.FOLDER,
443                         children:       []
444                 },
445                 FILE_DATA_ARTIST_ROOT = {
446                         name:           'Artists',
447                         type:           FILE_TYPE.FOLDER,
448                         children:       []
449                 },
450                 FILE_DATA_LISENCE_ROOT = {
451                         name:           'Original Lisences',
452                         type:           FILE_TYPE.FOLDER,
453                         children:       []
454                 },
455                 FILE_DATA_BALLOON_ROOT = {
456                         name:           'Balloon templetes',
457                         type:           FILE_TYPE.FOLDER,
458                         children:       []
459                 },
460                 AUTHOR_ARRAY = [],
461                 ARTIST_ARRAY = [],
462                 PANEL_ARRAY  = [],
463                 COMIC_ARRAY  = [],
464                 RESOURCE_PICTURE_ARRAY = [],
465                 BALLOON_TEMPLETE_ARRAY = [],
466                 ORIGINAL_LICENSE_ARRAY = [],
467                 BASIC_LICENSES = 'cc_by,cc_nc,cc_nd,cc_sa,keep_aspect_ratio,no_convert,no_flip,no_resize'.split( ',');
468         FILE_DATA_SERVICE_ROOT.children.push( FILE_DATA_COMICS_ROOT, FILE_DATA_PICTURE_ROOT, FILE_DATA_PANELS_ROOT, FILE_DATA_LISENCE_ROOT, FILE_DATA_BALLOON_ROOT );
469         FILE_DATA_COMICS_ROOT.children.push( FILE_DATA_MY_COMICS_ROOT, FILE_DATA_LATEST_COMICS, FILE_DATA_AUTHOR_ROOT );
470         FILE_DATA_PANELS_ROOT.children.push( FILE_DATA_LATEST_PANELS, FILE_DATA_MY_PANELS_ROOT );
471         FILE_DATA_PICTURE_ROOT.children.push( FILE_DATA_MY_PICTURES_ROOT, FILE_DATA_ARTIST_ROOT );
472         
473         FileAPI.createFolderUnderRoot( FILE_DATA_SERVICE_ROOT );
474
475         Driver.isPettanrFileInstance = function( _file ){
476                 if( FileAPI.isFileInstance( _file ) === true ){
477                         var _data = FileAPI.getFileData( _file.getUID() );// _file でなく  _file.getUID()
478                         return _data !== null && _data.driver === Driver;
479                 }
480                 return false;
481         };
482
483 var Cabinet = gOS.registerApplication( function(){
484         var self         = this,
485                 finder       = null,
486                 tree         = null,
487                 nodeClose    = null,
488                 nodePath     = null,
489                 nodeBody     = null,
490                 headerH      = 0,
491                 eventRoot    = null;
492
493         this.bgColor     = '#FFFFFF';
494         this.MIN_WIDTH   = 500;
495         this.MIN_HEIGHT  = 300;
496         this.onInit = function(){
497                 self.rootElement.id = 'cabinet-root';
498                 self.rootElement.innerHTML = [
499                         '<div id="cabinet-header">',
500                                 '<div class="header-title">Cabinet</div>',
501                                 '<div id="cabinet-close-button">x</div>',
502                                 '<div id="cabinet-path" class="finder-path"></div>',
503                         '</div>',
504                         '<div id="cabinet-container" class="finder-container"></div>'
505                 ].join( '' );
506                 
507                 self.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
508                 
509                 tree      = FileAPI.createTree( FILE_DATA_SERVICE_ROOT );
510                 eventRoot = self.getPointingDeviceEventTreeRoot();
511                 
512                 delete self.onInit;
513         };
514         this.onOpen = function( _w, _h ){
515                 headerH = Util.getElementSize( document.getElementById( 'cabinet-header' ) ).height;
516                 
517                 nodeClose = eventRoot.createNode( document.getElementById( 'cabinet-close-button' ), false, false, 'close-button-hover', 'pointer' );
518                 nodeClose.addEventListener( 'click', Cabinet.shutdown );
519                 nodePath  = eventRoot.createNode( document.getElementById( 'cabinet-path' ), false, false );
520                 nodeBody  = eventRoot.createNode( document.getElementById( 'cabinet-container' ), false, true, null, '', true );
521                 
522                 finder = self.createFinder( nodeBody, tree );
523                 finder.createPath( nodePath );
524                 self.onPaneResize( _w, _h );
525         };
526         this.onClose = function(){
527                 finder.destroy();
528                 tree.destroy();
529                 finder = tree = null;
530         };
531         this.onPaneResize = function( w, h ){
532                 nodePath.width( w );
533                 nodeBody.update( 0, headerH, w, h - headerH );
534                 finder.resize( w, h - headerH );
535         };
536 }, false, true, 'Cabinet', 'cabinet', null, '#1C1C1C' );
537
538 var Gallery = gOS.registerApplication( function(){
539         var self         = this,
540                 finder       = null,
541                 tree         = null,
542                 nodeClose    = null,
543                 nodePath     = null,
544                 nodeBody     = null,
545                 headerH      = 0,
546                 eventRoot    = null;
547
548         this.bgColor     = '#FFFFFF';
549         this.MIN_WIDTH   = 500;
550         this.MIN_HEIGHT  = 300;
551         this.onInit = function(){
552                 self.rootElement.id        = 'gallery-root';
553                 self.rootElement.innerHTML = [
554                         '<div id="gallery-header">',
555                                 '<div class="header-title">Gallery</div>',
556                                 '<div id="gallery-close-button">x</div>',
557                                 '<div id="gallery-path" class="finder-path"></div>',
558                         '</div>',
559                         '<div id="gallery-container" class="finder-container"></div>'
560                 ].join( '' );
561                 
562                 self.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
563                 
564                 tree = FileAPI.createTree( FILE_DATA_PICTURE_ROOT );
565                 var     _root  = tree.getRootFile(),
566                         _myPic = _root.getChildFileByIndex( 0 ),
567                         _pic   = _root.getChildFileByIndex( 1 );
568                 _myPic.getSeqentialFiles();
569                 _pic.getSeqentialFiles();
570                 _myPic.destroy();
571                 _pic.destroy();
572                 
573                 eventRoot = self.getPointingDeviceEventTreeRoot();
574         };
575         this.onOpen = function( w, h ){
576                 headerH = Util.getElementSize( document.getElementById( 'gallery-header' ) ).height;
577                 
578                 nodeClose = eventRoot.createNode( document.getElementById( 'gallery-close-button' ), false, false, 'close-button-hover', 'pointer' );
579                 nodeClose.addEventListener( 'click', Gallery.shutdown );
580                 nodePath = eventRoot.createNode( document.getElementById( 'gallery-path' ), false, false );
581                 nodeBody = eventRoot.createNode( document.getElementById( 'gallery-container' ), false, true, null, '', true );
582                 
583                 finder = self.createFinder( nodeBody, tree );
584                 finder.createPath( nodePath );
585                 self.onPaneResize( w, h );
586         };
587         this.onClose = function(){
588                 // finder.destroy();
589                 // tree.destroy();
590                 finder = tree = null;
591         };
592         this.onPaneResize = function( w, h ){
593                 nodePath.width( w );
594                 nodeBody.update( 0, headerH, w, h - headerH );
595                 finder.resize( w, h - headerH );
596         };
597 }, false, true, 'Gallery', 'gallery', null, '#01A31C' );
598
599 var Backyard = gOS.registerApplication( function(){
600         var self         = this;
601         
602         this.bgColor     = '#FFFFFF';
603         this.MIN_WIDTH   = 500;
604         this.MIN_HEIGHT  = 300;
605         this.onInit = function(){
606         };
607         this.onOpen = function( _w, _h, _option ){
608         }
609         this.onClose = function(){
610         }
611         this.onPaneResize = function( _w, _h){
612         }
613 }, false, false, 'Settings', 'settings', null, '#DDDDDD' );
614
615 if( pettanr.DEBUG === true){
616         var Debug = gOS.registerApplication( function(){
617                 var self = this,
618                         elmDl,
619                         data = ( function(){
620                                 var data = {
621                                         pettanR:       pettanr.version,
622                                         ua:            navigator.userAgent,
623                                         platform:      navigator.platform,
624                                         appVersion:    navigator.appVersion,
625                                         appCodeName:   navigator.appCodeName,
626                                         appName:       navigator.appName,
627                                         language:      navigator.browserLanguage || navigator.language,
628                                         ActiveX:       UA.ACTIVEX,
629                                         RenderingMode: UA.isStanderdMode === true ? 'Standerd' : 'Quirks'
630                                 };
631                                 if( UA.IE ){
632                                         data.version = UA.IE;
633                                         if( UA.ieVersion >= 8 ) data.RenderingVersion = UA.ieRenderingVersion;
634                                         data.browserType = UA.STANDALONE === true ? 'Standalone' : 'bundle';
635                                         if( UA.ieVersion < 9 ) {
636                                                 data.vml = UA.VML;
637                                         } else {
638                                                 data.svg = UA.SVG;
639                                         }
640                                 };
641                                 return data;
642                         })();
643
644                 this.bgColor     = '#FFFFFF';
645                 this.MIN_WIDTH   = 500;
646                 this.MIN_HEIGHT  = 300;
647                 this.onInit = function(){
648                         self.rootElement.id = 'debug-root';
649                         self.rootElement.innerHTML = '<dl id="useragent" class="dl-table clearfix"></dl>';
650                 };
651                 this.onOpen = function( _w, _h, _option ){
652                         elmDl = document.getElementById( 'useragent' );
653                         var elmDt, elmDd;
654                         for( var key in data ){
655                                 elmDt = document.createElement( 'dt' );
656                                 elmDt.innerHTML = key;
657                                 elmDd = document.createElement( 'dd' );
658                                 elmDd.innerHTML = '' + data[ key];
659                                 if( !data[ key ] ) elmDd.style.color = 'red';
660                                 elmDl.appendChild( elmDt );
661                                 elmDl.appendChild( elmDd );
662                         }
663                 }
664                 this.onClose = function(){
665                         
666                 }
667                 this.onPaneResize = function( _w, _h ){
668                         
669                 }
670         }, false, true, 'Debug', 'debug', null, '#01A31C' );
671 }
672
673 /* ----------------------------------------
674  * Image Group Exproler
675  *  - overlay
676  */
677 var PremiumSatge = gOS.registerApplication( function(){
678         var BASE_PATH      = pettanr.CONST.RESOURCE_PICTURE_PATH,
679                 THUMB_PATH     = pettanr.CONST.THUMBNAIL_PATH,
680                 LIMIT_FILESIZE = 1024 * 100,
681                 ICON_ARRAY     = [];
682                 
683         var self             = this,
684                 tree, rootFile,
685                 winW, winH, wrapX,
686                 elmContainer, elmIconOrigin, elmName, elmButton,
687                 containerW, containerH, 
688                 itemW, itemH, buttonW,
689                 onUpdate        = null,
690                 onUpdateData    = null,
691                 onUpdateContext = null,
692                 artistID        = -1;
693
694         var ImageGroupIconClass = function( index, data ){
695                 var elmIconWrap     = elmIconOrigin.cloneNode( true ),
696                         elmIconTitle    = Util.getElementsByClassName( elmIconWrap, 'image-group-item-title' )[ 0 ],
697                         SRC             = [ BASE_PATH, data.id, '.', data.ext ].join( ''),
698                         LOW_SRC         = data.filesize && data.filesize > LIMIT_FILESIZE ? [ THUMB_PATH, data.id, '.', data.ext ].join( '') : null,
699                         reversibleImage = null,
700                         timer           = null,
701                         onEnterFlag     = false,
702                         instance        = this;
703                 elmContainer.appendChild( elmIconWrap );
704                 elmIconWrap.style.left = ( index * itemW ) + 'px';
705                 elmIconTitle.appendChild( document.createTextNode( data.filesize + 'bytes' ) );
706                 
707                 function onImageLoad( url, _imgW, _imgH ){
708                         data.width  = _imgW = _imgW || data.width  || 64;
709                         data.height = _imgH = _imgH || data.height || 64;
710                         elmIconTitle.firstChild.data = _imgW + 'x' + _imgH;
711                         var zoom = 128 /( _imgW > _imgH ? _imgW : _imgH ),
712                                 MATH_FLOOR = Math.floor,
713                                 h = MATH_FLOOR( _imgH * zoom ),
714                                 w = MATH_FLOOR( _imgW * zoom );
715                         reversibleImage.elm.style.cssText = [
716                                 'width:',  w, 'px;',
717                                 'height:', h, 'px;',
718                                 'margin:', MATH_FLOOR( itemH / 2 - h / 2 ), 'px ', MATH_FLOOR( itemW / 2 - w / 2 ), 'px 0'
719                         ].join('');
720                         reversibleImage.resize( w, h );
721                         self.addEventListener( elmIconWrap, 'click', onClick );
722                 };
723                 
724                 function onClick(){
725                         onUpdateData = data;
726                         PremiumSatge.shutdown();
727                 };
728                 
729                 function asyncDraw(){
730                         reversibleImage = pettanr.image.createReversibleImage( LOW_SRC || SRC, itemW, itemH, onImageLoad );
731                         elmIconWrap.appendChild( reversibleImage.elm );
732                         onEnterFlag = true;
733                         timer = null;
734                 };
735                 
736                 this.onEnter = function( delay ){
737                         self.addTimer( asyncDraw, delay, true );
738                         delete instance.onEnter;
739                 };
740                 this.destroy = function(){
741                         delete instance.destroy;
742                         // timer && window.clearTimeout( timer );
743                         self.removeTimer( asyncDraw );
744                         self.removeEventListener( elmIconWrap );
745                         reversibleImage !== null && reversibleImage.destroy();
746                         // Util.removeAllChildren( elmIconWrap );
747                         // elmContainer.removeChild( elmIconWrap );
748                         reversibleImage = elmIconWrap = elmIconTitle = data = timer = null;
749                 };
750         };
751         
752         function onEnterShowImage(){
753                 var l = ICON_ARRAY.length,
754                         _start = -wrapX /itemW -1,
755                         _end = _start + winW /itemW +1,
756                         _icon;
757                 for( var i=0, c = 0; i<l; ++i){
758                         _icon = ICON_ARRAY[ i ];
759                         if( _start < i && i < _end && _icon.onEnter ){
760                                 _icon.onEnter( c * 100 );
761                                 c++;
762                         }
763                 }
764                 //onEnterInterval !== null && window.clearTimeout( onEnterInterval );
765                 //onEnterInterval = null;
766                 self.removeTimer( onEnterShowImage );
767         };
768         function clickClose(){
769                 PremiumSatge.shutdown();
770         };
771         function onMouseWheel( e ){
772                 if( winW < containerW ){
773                         wrapX += e.wheelDelta / 2;
774                         wrapX = wrapX > 0 ? 0 : wrapX < winW -containerW ? winW -containerW : wrapX;
775                         elmContainer.style.left = wrapX + 'px';
776                         
777                         self.removeTimer( onEnterShowImage );
778                         self.addTimer( onEnterShowImage, 500 );
779                 }
780                 return false;                   
781         }
782         
783         function drawIcons(){
784                 while( ICON_ARRAY.length > 0 ){
785                         ICON_ARRAY.shift().destroy();
786                 }
787                 var _index = rootFile.search( {
788                                 id:   artistID,
789                                 type: FILE_TYPE.ARTIST
790                         })[ 0 ],
791                         _artistFile = rootFile.getChildFileByIndex( _index ),
792                         _file;
793                 if( _artistFile !== null ){
794                         for(var i=0, l=_artistFile.getChildFileLength(); i<l; ++i ){
795                                 _file = _artistFile.getChildFileByIndex( i );
796                                 ICON_ARRAY.push( new ImageGroupIconClass( i, FileAPI.getFileData( _file ) ));
797                                 _file.destroy();
798                         }
799                         elmName.firstChild.data = _artistFile.getName();
800                         _artistFile.destroy();
801                 }
802         }
803         
804         function onFadeout(){
805                 while( ICON_ARRAY.length > 0 ){
806                         ICON_ARRAY.shift().destroy();
807                 }
808                 onUpdate !== null && onUpdateData !== null && onUpdate.call( onUpdateContext, onUpdateData );
809                 onUpdate = onUpdateData = onUpdateContext = null;
810                 PremiumSatge.shutdown();
811         }
812         
813         
814         this.MIN_WIDTH   = 320;
815         this.MIN_HEIGHT  = 320;
816         this.onInit = function(){
817                         self.rootElement.id = 'image-group-wrapper';
818
819                         self.rootElement.innerHTML = [
820                                 '<div id="image-group-icon-container"></div>',
821                                 '<div id="image-group-name">NO DATA...</div>',
822                                 '<div id="image-group-button" class="button">close</div>'
823                         ].join( '' );
824                         
825                         self.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
826                         
827                         tree      = FileAPI.createTree( FILE_DATA_ARTIST_ROOT );
828                         rootFile  = tree.getRootFile();
829         };
830         this.onOpen = function( _windowW, _windowH, _ARTISTIDorFILE, _onUpdate, opt_thisObj ){
831                 elmContainer  = document.getElementById( 'image-group-icon-container' );
832                 containerH    = Util.getElementSize( elmContainer ).height;
833                 
834                 elmIconOrigin = ( function(){
835                         var ret  = document.createElement( 'div' ),
836                                 data = document.createElement( 'div' );
837                         ret.appendChild( data );
838                         ret.className  = 'image-group-item';
839                         data.className = 'image-group-item-title';
840                         return ret;
841                 })();
842
843                 var size      = Util.getElementSize( elmIconOrigin );
844                 itemW         = size.width;
845                 itemH         = size.height;
846
847                 elmName       = document.getElementById( 'image-group-name' );
848                 elmButton     = document.getElementById( 'image-group-button' );
849                 
850                 buttonW       = Util.getElementSize( elmButton ).width;
851                 
852                 self.addEventListener( elmContainer, 'mousewheel', onMouseWheel );
853                 self.addEventListener( elmButton, 'click', clickClose );
854                 tree.addTreeEventListener( Const.TREE.EVENT.UPDATE, drawIcons );
855                 
856                 if( Driver.isPettanrFileInstance( _ARTISTIDorFILE ) === true ){
857                         var _data = FileAPI.getFileData( _ARTISTIDorFILE );
858                         if( _ARTISTIDorFILE.getType() === FILE_TYPE.ARTIST || FILE_DATA_MY_PICTURES_ROOT === _data ){
859                                 artistID = _data.id || -1;
860                         }
861                 } else
862                 if( Type.isNumber( _ARTISTIDorFILE ) === true ){
863                         artistID = _ARTISTIDorFILE;
864                 }
865                 
866                 onUpdate = _onUpdate || null;
867                 onUpdateContext = opt_thisObj || null;
868                 onUpdateData = null;
869                 
870                 drawIcons();
871                 
872                 wrapX = 0;
873                 containerW = ICON_ARRAY.length * itemW;
874                 
875                 winW = _windowW;
876                 winH = _windowH;
877                 var w = winW > containerW ? winW : containerW,
878                         h = _windowH > containerH ? containerH : _windowH,
879                         MATH_FLOOR = Math.floor;
880
881                 $( elmContainer ).css( {
882                         width:          w,
883                         height:         0,
884                         left:           0,
885                         top:            MATH_FLOOR( _windowH /2 )
886                 }).stop().animate( {
887                         height:         h,
888                         top:            MATH_FLOOR( _windowH /2 - h /2 )
889                 }, onEnterShowImage );
890                 
891                 elmButton.style.cssText = [
892                         'left:', MATH_FLOOR( _windowW /2 - buttonW /2 ), 'px;',
893                         'top:',  MATH_FLOOR( _windowH /2 + containerH /2 +10 ), 'px'
894                 ].join('');
895         }
896         this.onPaneResize = function( _windowW, _windowH ){
897                 var w = _windowW > containerW ? _windowW : containerW,
898                         h = _windowH > containerH ? containerH : _windowH,
899                         MATH_FLOOR = Math.floor,
900                         offsetW = MATH_FLOOR( _windowW /2 -winW /2 );
901                         
902                 winW = _windowW;
903                 winH = _windowH;
904                 if( offsetW <= 0 ){ // smaller
905                         $( elmContainer ).stop().css( {
906                                 left:                           offsetW,
907                                 width:                          w
908                         }).animate( {
909                                 left:                           0,
910                                 top:                            MATH_FLOOR( _windowH /2 -h /2 )
911                         });                                     
912                 } else {
913                         $( elmContainer ).stop().css( { // bigger
914                                 left:                           0,
915                                 width:                          w,
916                                 borderLeftWidth:        offsetW
917                         }).animate( {
918                                 top:                            MATH_FLOOR( _windowH /2 -h /2 ),
919                                 borderLeftWidth:        0
920                         });
921                 }
922                 elmButton.style.cssText = [
923                         'left:', MATH_FLOOR( _windowW /2 -buttonW /2 ), 'px;',
924                         'top:',  MATH_FLOOR( _windowH /2 +containerH /2 + 10 ), 'px'
925                 ].join('');
926                 onEnterShowImage();
927         }
928         this.onClose = function(){
929                 if( tree === null ) return true;
930                 $( elmContainer ).stop().animate( {
931                                 height: 0,
932                                 top:    Math.floor( winH / 2 )
933                         }, onFadeout );
934                 // onEnterInterval !== null && window.clearTimeout( onEnterInterval );
935                 // onEnterInterval = null;
936                 self.removeTimer();
937                 
938                 tree.removeTreeEventListener( Const.TREE.EVENT.UPDATE, drawIcons );
939                 tree.destroy();
940                 tree = rootFile = null;
941                 
942                 return false;
943         }
944 }, true, true, 'Premium Stage', 'premiumStage', null, '#C3325F' );
945
946
947 /* ----------------------------------------
948  * Text Editor
949  *  - overlay
950  */
951 var TextEditor = gOS.registerApplication( function(){
952         var elmTextarea, elmButton,
953                 textElement, onUpdate,
954                 ID = 'textEditor',
955                 panelX, panelY,
956                 self = this;
957         
958         function clickOK(){
959                 textElement && textElement.text( elmTextarea.value );
960                 self.addAsyncCall( asyncCallback );
961         };
962         
963         function asyncCallback(){
964                 onUpdate && onUpdate( textElement );
965                 onUpdate = textElement = null;
966                 TextEditor.shutdown();
967         };
968         
969
970         function textareaFitHeight(){
971                 var rows = 0;
972                 while( elmTextarea.offsetHeight < textElement.h ){
973                         rows++;
974                         elmTextarea.rows = rows;
975                 };
976                 if( rows > 1 ) elmTextarea.rows = --rows;
977         };
978         
979         /* grobal method */
980         
981         this.MIN_WIDTH   = 320;
982         this.MIN_HEIGHT  = 320;
983         this.onInit = function(){
984                 self.rootElement.id        = 'speach-editor-wrapper';
985                 self.rootElement.innerHTML = '<textarea id="speach-editor"></textarea><div id="speach-edit-complete-button" class="button">OK</div>';
986         };
987         this.onOpen = function( _w, _h, _panelX, _panelY, _textElement, _onUpdate ){
988                 elmTextarea = document.getElementById( 'speach-editor' );
989                 elmButton   = document.getElementById( 'speach-edit-complete-button' );
990                 
991                 self.addKeyEventListener( 'keydown', new Function( 'return false' ), 69, false, true );
992                 self.addEventListener( elmButton, 'click', clickOK );
993                 
994                 panelX = _panelX;
995                 panelY = _panelY;
996                 textElement = _textElement;
997                 onUpdate = _onUpdate || null;
998                 
999                 self.onPaneResize( _w, _h );
1000                 elmTextarea.value = _textElement.content;
1001                 elmTextarea.focus();
1002                 
1003                 /*
1004                  * ie6,7は、textarea { width:100%}でも高さが変わらない。rowsを設定。
1005                  */
1006                 UA.isIE === true && UA.ieVersion <= 7 && self.addAsyncCall( textareaFitHeight );
1007         };
1008         this.onPaneResize = function( _w, _h ){
1009                 self.rootElement.style.cssText = [
1010                         'left:', textElement.x + panelX, 'px;',
1011                         'top:',  textElement.y + panelY, 'px;',
1012                         'width:', textElement.w, 'px;',
1013                         'height:', textElement.h, 'px;'
1014                 ].join( '' );
1015         };
1016         this.onClose = function(){
1017                 self.removeKeyEventListener();
1018                 self.removeEventListener( elmButton );
1019                 
1020                 elmTextarea = elmButton = onUpdate = textElement = self = null;
1021         };
1022 }, true, false, 'Tetxt Editor', 'texteditor', null, '#DDDDDD' );
1023
1024
1025 var Reader = gOS.registerApplication( function(){
1026         var windowW, windowH,
1027                 headerH,
1028                 consoleH,
1029                 panelMargin,
1030                 elmContainer, elmTitle, elmAuthor, elmBackButton, elmNextButton,
1031                 bindWorker    = null,
1032                 currentFile   = null,
1033                 comicData     = null,
1034                 currentPanel  = null,
1035                 currentIndex  = 0,
1036                 numPanel      = 0,
1037                 self          = this;
1038
1039         function onBackClick(){
1040                 currentIndex -= ( currentIndex > 0 ? 1 : 0 );
1041                 slide();
1042                 return false;
1043         }
1044         function onNextClick(){
1045                 currentIndex += ( currentIndex < numPanel - 1 ? 1 : 0 );
1046                 slide();
1047                 return false;
1048         }
1049         function slide(){
1050                 var elm    = elmContainer.childNodes[ currentIndex ],
1051                         h      = windowH - headerH - consoleH,
1052                         top    = headerH;
1053                 if( elm ){
1054                         top =  headerH - elm.offsetTop + Math.floor( ( h - elm.offsetHeight ) / 2 );
1055                 }
1056                 
1057                 $( elmContainer ).stop().animate( {
1058                         top:    top
1059                 });
1060         }
1061         function getCurrentTopPosition(){
1062
1063         }
1064         function draw(){
1065                 var fileData, title, author;
1066                 
1067                 if( Driver.isPettanrFileInstance( currentFile ) === true ){
1068                         if( currentFile.getType() === FILE_TYPE.COMIC ){
1069                                 fileData    = currentFile.read();
1070                                 title       = fileData.title;
1071                                 author      = fileData.author.name;
1072                                 comicData   = fileData;
1073                                 numPanel    = currentFile.getChildFileLength();
1074                         } else
1075                         if( currentFile.getType() === FILE_TYPE.PANEL ){
1076                                 fileData    = currentFile.read();
1077                                 title       = fileData.comic.title;
1078                                 author      = fileData.comic.author.name;
1079                                 comicData   = fileData;
1080                                 numPanel    = 1;
1081                         }
1082                 } else {
1083                         
1084                 };
1085                 if( comicData !== null ){
1086                         elmTitle.data  = title;
1087                         elmAuthor.data = author;
1088                         bindWorker.json( comicData );
1089                         self.addAsyncCall( asyncResize );
1090                 };
1091         }
1092         function asyncResize(){
1093                 self.onPaneResize( windowW, windowH );
1094         };
1095         
1096         /* grobal method */
1097         
1098         this.MIN_WIDTH   = 320;
1099         this.MIN_HEIGHT  = 320;
1100         this.onInit = function(){
1101                 self.rootElement.id = 'comic-reader-wrapper';
1102                 self.rootElement.innerHTML = [
1103                         '<div id="comic-reader-panel-container"></div>',
1104                         '<div class="comic-reader-shadow" style="top:0;height:40px;"></div>',
1105                         '<div id="comic-reader-header">',
1106                                 '<div id="comic-reader-header-content">',
1107                                         '<span id="comic-reader-title">NO DATA...</span>',
1108                                         '<span id="comic-reader-author">NO DATA...</span>',
1109                                 '</div>',
1110                         '</div>',
1111                         '<div class="comic-reader-shadow" style="bottom:0;height:100px;"></div>',
1112                         '<div id="comic-reader-console">',
1113                                 '<div id="comic-reader-button-centering">',
1114                                         '<a href="#" id="comic-reader-back-button">▲</da>',
1115                                         '<a href="#" id="comic-reader-forward-button">▼</a>',
1116                                 '</div>',
1117                         '</div>'
1118                 ].join( '' );
1119                 
1120                 self.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
1121                 
1122         };
1123         this.onOpen = function( _w, _h, _file ){
1124                 headerH       = Util.getElementSize( document.getElementById( 'comic-reader-header' ) ).height;
1125                 consoleH      = Util.getElementSize( document.getElementById( 'comic-reader-console' ) ).height;
1126                 elmContainer  = document.getElementById( 'comic-reader-panel-container' );
1127                 elmTitle      = document.getElementById( 'comic-reader-title' ).firstChild;
1128                 elmAuthor     = document.getElementById( 'comic-reader-author' ).firstChild;
1129                 elmBackButton = document.getElementById( 'comic-reader-back-button' );
1130                 elmNextButton = document.getElementById( 'comic-reader-forward-button' );
1131
1132                 bindWorker = pettanr.bind.createBindWorker( elmContainer, null, false, false );
1133                 
1134                 self.addEventListener( elmBackButton, 'click', onBackClick );
1135                 self.addEventListener( elmNextButton, 'click', onNextClick );
1136                 
1137                 numPanel = currentIndex = 0;
1138                 
1139                 elmContainer.style.cssText = 'left:' + ( _w / 2 )  + 'px;' + 'top:' + _h + 'px;';
1140                 
1141                 windowW = _w;
1142                 windowH = _h;
1143                 if( FileAPI.isFileInstance( _file ) === true ){
1144                         currentFile = _file;
1145                         _file.addEventListener( Const.FILE.EVENT.GET_SEQENTIAL_FILES, draw );
1146                         _file.getSeqentialFiles();
1147                         draw();
1148                 };
1149         };
1150         this.onPaneResize = function( _windowW, _windowH ){
1151                 windowW = _windowW;
1152                 windowH = _windowH;
1153                 var panelH = elmContainer.offsetHeight,
1154                         panelW = elmContainer.offsetWidth,
1155                         h      = _windowH - headerH - consoleH;
1156                 $( elmContainer ).stop().animate(
1157                         {
1158                                 left:   Math.floor( ( _windowW - panelW ) / 2 ),
1159                                 top:    headerH + ( panelH < h ? Math.floor( ( h - panelH ) / 2 ) : 0 )
1160                         }
1161                 );
1162         };
1163         this.onClose = function(){
1164                 self.removeEventListener( elmBackButton );
1165                 self.removeEventListener( elmNextButton );
1166                 
1167                 bindWorker.destroy();
1168                 bindWorker = null;
1169                 
1170                 currentFile && currentFile.removeEventListener( Const.FILE.EVENT.GET_SEQENTIAL_FILES, draw );
1171                 currentFile = comicData = currentPanel = null;
1172                 
1173                 elmContainer = elmTitle = elmAuthor = elmBackButton = elmNextButton = null;
1174         };
1175 }, true, true, 'Comic Reader', 'comicreader', null, '#01A31C' );
1176
1177
1178 var Editor = gOS.registerApplication( function(){
1179
1180         var PANEL_ELEMENT_TYPE_IMAGE = 0,
1181                 PANEL_ELEMENT_TYPE_TEXT  = 1,
1182                 MODULE_ARRAY             = [],
1183                 PANEL_ELEMENT_ARRAY      = [],
1184                 MIN_PANEL_HEIGHT         = 20,
1185                 MIN_ELEMENT_SIZE         = 19,
1186                 MOUSE_HIT_AREA           = 10,
1187                 windowW, windowH,
1188                 app                      = this,
1189                 eventRoot                = null,
1190                 option,
1191                 comicID                  = -1,
1192                 panelID                  = -1,
1193                 panelTimming             = -1,
1194                 phase                    = -1;
1195
1196         var kill = function(){
1197                 var o = this, v;
1198                 for( var p in o ){
1199                         if( o.hasOwnProperty && !o.hasOwnProperty( p ) ) continue;
1200                         v = o[ p ];
1201                         delete o[ p ];
1202                 };
1203         };
1204 /* ----------------------------------------
1205  * MENU BAR
1206  *  - mouseEventListener
1207  *  - controler
1208  * 
1209  * div
1210  *   div.title
1211  *   ul
1212  *     li
1213  *        a
1214  *          span
1215  *          kbd shortcut
1216  */
1217         var MENU_BAR_CONTROL = ( function(){
1218                 var ELM_ITEM_CLASSNAME = 'menubar-item',
1219                         currentMenu        = null,
1220                         elmBar, elmBox,
1221                         nodeBar, nodeBox,
1222                         barH, menuW;
1223         /** -----------------------------------------
1224          *  PrivateOptionDataClass
1225          */
1226                 var PrivateOptionDataClass = function( menuData, option, title, shortcut, callback, visible, separateAfter, thisObject ){
1227                         this.menuData      = menuData;
1228                         this.option        = option;
1229                         this.title         = title;
1230                         this.shortcut      = shortcut || '';
1231                         this.callback      = callback;
1232                         this.thisObject    = thisObject;
1233                         this.visible       = visible;
1234                         this.separateAfter = separateAfter;
1235                 };
1236                 PrivateOptionDataClass.prototype = {
1237                         menuData      : null,
1238                         option        : null,
1239                         elm           : null,
1240                         node          : null,
1241                         visible       : undefined,
1242                         border        : false,
1243                         title         : null,
1244                         shortcut      : null,
1245                         callback      : null,
1246                         thisObject    : null,
1247                         separateAfter : false,
1248                         show : function( elm ){
1249                                 if( this.elm === elm ) return;
1250                                 if( !elm ){
1251                                         elm = document.createElement( 'div' );
1252                                         elmBox.appendChild( elm );
1253                                         elm.appendChild( document.createElement( 'span' ) );
1254                                         elm.appendChild( document.createElement( 'kbd' ) );
1255                                 };
1256                                 this.elm      = elm;
1257                                 this.option.title( this.title );
1258                                 this.option.visible( !!this.visible );
1259                                 elm.lastChild.innerHTML = this.shortcut;
1260                                 elm.style.borderStyle = this.border === true ? 'solid' : 'none';
1261                                 
1262                                 this.node     = nodeBox.createNode( elm, false, true, 'menubar-option-hover', 'pointer' );
1263                                 this.node.disabled( !this.visible );
1264                         },
1265                         hide : function(){
1266                                 this.node.remove();
1267                                 delete this.elm;
1268                                 delete this.node;
1269                         },
1270                         fire : function(){
1271                                 this.callback.call( this.thisObject || this.option, Util.getIndex( this.menuData.optionDataList, this ) );
1272                         },
1273                         remove : function(){
1274                                 var list = PrivateOptionDataClass.list;
1275                                 list.splice( Util.getIndex( list, this ), 1 );
1276                         }
1277                 };
1278                 PrivateOptionDataClass.list = [];
1279                 PrivateOptionDataClass.get = function( OptionOrElm ){
1280                         var list = PrivateOptionDataClass.list,
1281                                 i    = list.length,
1282                                 data;
1283                         for( ; i; ){
1284                                 data = list[ --i ];
1285                                 if( data.option === OptionOrElm || data.elm === OptionOrElm ) return data;
1286                         };
1287                         return null;
1288                 };
1289
1290         /** -----------------------------------------
1291          *  OptionClass
1292          */     
1293                 var OptionClass = function( menuData, title, shortcut, callback, visible, separateAfter, thisObject ){
1294                         PrivateOptionDataClass.list.push( new PrivateOptionDataClass( menuData, this, title, shortcut, callback, visible, separateAfter, thisObject ) );
1295                 };
1296                 OptionClass.prototype = {
1297                         title: function( v ){
1298                                 var data = PrivateOptionDataClass.get( this );
1299                                 if( Type.isString( v ) === true ){
1300                                         data.title = v;
1301                                         if( data.elm ) data.elm.firstChild.innerHTML = v;
1302                                 };
1303                                 return data.title;
1304                         },
1305                         visible : function( v ){
1306                                 var data = PrivateOptionDataClass.get( this );
1307                                 if( Type.isBoolean( v ) === true ){
1308                                         data.visible = v;
1309                                         data.elm && Util.toggleClass( data.elm, 'menubar-option-disabled', !v );
1310                                 };
1311                                 return data.visible;
1312                         }
1313                 };
1314         /** -----------------------------------------
1315          *  AsyncOptionClass
1316          */
1317                 var AsyncOptionClass = function( menuData, callback, visible, separateAfter, thisObject ){
1318                         var data  = new PrivateOptionDataClass( menuData, this, null, null, callback, visible, separateAfter, thisObject );
1319                         data.show = AsyncOptionClass.show;
1320                         data.hide = AsyncOptionClass.hide;                      
1321                         PrivateOptionDataClass.list.push( data );
1322                 };
1323                 AsyncOptionClass.prototype = {
1324                         title    : function(){},
1325                         visible  : OptionClass.prototype.visible
1326                 };
1327                 AsyncOptionClass.show = function( elm ){
1328                         if( this.elm === elm ) return;
1329                         if( !elm ){
1330                                 elm = document.createElement( 'div' );
1331                                 elmBox.appendChild( elm );
1332                                 elm.appendChild( document.createElement( 'span' ) );
1333                                 elm.appendChild( document.createElement( 'kbd' ) );             
1334                         };
1335                         this.elm         = elm;
1336                         elm.className    = 'loading';
1337                         elm.style.height = '90px';
1338                         elm.firstChild.innerHTML = this.elm.lastChild.innerHTML = '';
1339                         elm.style.borderStyle    = this.border === true ? 'solid' : 'none';
1340                         
1341                         this.callback();                        
1342                 };
1343                 AsyncOptionClass.hide = function(){
1344                         this.elm.className    = '';
1345                         this.elm.style.height = '';
1346                         delete this.elm;
1347                 };
1348                 
1349         /** -----------------------------------------
1350          *  MenuPrivateDataClass
1351          */
1352                 var MenuPrivateDataClass = function( menu, title ){
1353                         this.menu           = menu;
1354                         this.elm            = document.createElement( 'div' );
1355                         this.optionDataList = [];
1356                         
1357                         elmBar.appendChild( this.elm );
1358                         this.elm.className  = ELM_ITEM_CLASSNAME;
1359                         this.elm.innerHTML  = title;
1360                 };
1361                 MenuPrivateDataClass.prototype = {
1362                         menu           : null,
1363                         elm            : null,
1364                         node           : null,
1365                         visible        : false,
1366                         currentOption  : -1,
1367                         index          : -1,
1368                         optionDataList : null,
1369                         open : function(){
1370                                 this.elm.style.left = ( menuW * Util.getIndex( MenuPrivateDataClass.list, this ) ) + 'px';
1371                                 this.node           = nodeBar.createNode( this.elm, false, false, ELM_ITEM_CLASSNAME + '-hover', 'pointer' );
1372                                 // this.node.addEventListener( 'click', this.onClick, this );
1373                                 this.node.addEventListener( 'click', this.onClick, this );                      
1374                         },                      
1375                         close : function(){
1376                                 var o;
1377                                 while( o = this.optionDataList.shift() ) o.remove();
1378                                 this._kill = kill;
1379                                 this._kill();
1380                         },
1381                         onClick : function( e ){
1382                                 if( currentMenu !== this.menu ){
1383                                         currentMenu && currentMenu.hide();
1384                                         currentMenu = this.menu;
1385                                         this.menu.show();
1386                                 };
1387                         },
1388                         onOptionClick : function( e ){
1389                                 var target = e.target,
1390                                         i      = target.nodeIndex(),
1391                                         option = this.optionDataList[ i ];
1392                                 if( target === nodeBox ) return true;
1393                                 option.fire();
1394                         }
1395                 };
1396                 MenuPrivateDataClass.list = [];
1397                 MenuPrivateDataClass.get  = function( menu ){
1398                         var list = MenuPrivateDataClass.list,
1399                                 i    = list.length;
1400                         for( ; i; ){
1401                                 if( list[ --i ].menu === menu ) return list[ i ];
1402                         };
1403                         return null;
1404                 };
1405         
1406         /** -----------------------------------------
1407          *  MenuClass
1408          */
1409                 var MenuClass = function( title ){
1410                         MenuPrivateDataClass.list.push( new MenuPrivateDataClass( this, title ) );
1411                 };
1412                 MenuClass.prototype = {
1413                         show: function(){
1414                                 data = MenuPrivateDataClass.get( this );
1415                                 if( data.visible === true ) return;
1416                                 
1417                                 data.elm.className = ELM_ITEM_CLASSNAME + '-focus';
1418                                 
1419                                 if( !elmBox ){
1420                                         elmBox   = document.createElement( 'div' );
1421                                         elmBar.appendChild( elmBox );
1422                                         elmBox.className = 'menubar-option-box';
1423                                         nodeBox  = nodeBar.createNode( elmBox, false, false, 'menubar-option-box-hover' );
1424                                 };
1425                                 nodeBox.disabled( false );
1426                                 elmBar.parentNode.insertBefore( elmBox, elmBar.nextSibling ); // ie6 では elmBar の 子にすると 選択肢が表示されない
1427                                 
1428                                 nodeBox.setPosition( data.node.x(), barH );
1429                                 
1430                                 var i,
1431                                         children = elmBox.childNodes,
1432                                         list     = data.optionDataList
1433                                         l        = list.length;
1434                                 while( l < children.length ){
1435                                         elmBox.removeChild( elmBox.firstChild );
1436                                 };
1437                                 for( i = 0; i < l; ++i ){
1438                                         list[ i ].show( children[ i ] );
1439                                 };
1440                                 nodeBox.mesure();
1441
1442                                 nodeBar.addEventListener( 'mouseout', this.hide, this );
1443                                 nodeBox.addEventListener( 'click', data.onOptionClick, data );
1444                                 data.visible = true;                                                            
1445                         },
1446                         hide: function(){
1447                                 data = MenuPrivateDataClass.get( this );
1448                                 if( data.visible === false ) return;
1449                                 
1450                                 data.elm.className = ELM_ITEM_CLASSNAME;
1451                                 for( var i = data.optionDataList.length; i; ){
1452                                         data.optionDataList[ --i ].hide();
1453                                 };
1454                                 elmBar.parentNode.removeChild( elmBox );
1455                                 nodeBox.disabled( true );
1456
1457                                 nodeBar.removeEventListener( 'mouseout', this.hide );
1458                                 nodeBox.removeEventListener( 'click', data.onOptionClick );
1459                                 data.visible = false;
1460                                 currentMenu  = null;
1461                         },
1462                         createOption: function( title, shortcut, callback, visible, separateBefore, separateAfter, thisObject ){
1463                                 var data       = MenuPrivateDataClass.get( this ),
1464                                         before     = data.optionDataList[ data.optionDataList.length - 1 ],
1465                                         ret        = new OptionClass( data, title, shortcut, callback, visible, separateAfter, thisObject ),
1466                                         dataOption = PrivateOptionDataClass.get( ret );
1467                                 if( before ) before.border = separateBefore === true || before.separateAfter === true;
1468                                 data.optionDataList.push( dataOption );
1469                                 if( data.visible === true ){
1470                                         dataOption.show();
1471                                         nodeBox.mesure();
1472                                 };
1473                                 return ret;
1474                         },
1475                         createAsyncOption: function( onOpen, visible, separateBefore, separateAfter, thisObject ){
1476                                 var data       = MenuPrivateDataClass.get( this ),
1477                                         before     = data.optionDataList[ data.optionDataList.length -1 ],
1478                                         ret        = new AsyncOptionClass( data, onOpen, visible, separateAfter, thisObject ),
1479                                         dataOption = PrivateOptionDataClass.get( ret );
1480                                 if( before ) before.border = separateBefore === true || before.separateAfter === true;
1481                                 data.optionDataList.push( dataOption );
1482                                 data.visible === true && dataOption.show();
1483                                 return ret;
1484                         },
1485                         remove : function( option ){
1486                                 var data       = MenuPrivateDataClass.get( this ),
1487                                         optionData = PrivateOptionDataClass.get( option ),
1488                                         i          = Util.getIndex( data.optionDataList, optionData );
1489                                 if( i === -1 ) return;
1490                                 
1491                                 data.optionDataList.splice( i, 1 );
1492                                 
1493                                 data.visible === true && elmBox.removeChild( optionData.elm ) && optionData.hide();
1494                                 optionData.remove();
1495                                 
1496                                 !( option instanceof AsyncOptionClass ) && data.optionDataList.length === 0 && this.hide();
1497                         }
1498                 };
1499                 
1500                 return {
1501                         id : 'MENU_BAR_CONTROL',
1502                         h  : 0,
1503                         init : function(){
1504                                 elmBar   = document.getElementById( 'menu-bar' );
1505                                 nodeBar  = eventRoot.createNode( elmBar, false, false, 'menu-bar-hover' );
1506                                 
1507                                 MENU_BAR_CONTROL.QUIT   = MENU_BAR_CONTROL.createItem( 'Quit' );
1508                                 MENU_BAR_CONTROL.EDIT   = MENU_BAR_CONTROL.createItem( 'Edit' );
1509                                 MENU_BAR_CONTROL.WINDOW = MENU_BAR_CONTROL.createItem( 'Window' );
1510                                 MENU_BAR_CONTROL.HELP   = MENU_BAR_CONTROL.createItem( 'Help' );
1511                                 
1512                                 var size = Util.getElementSize( MenuPrivateDataClass.list[ 0 ].elm );
1513                                 menuW    = size.width;
1514                                 barH     = MENU_BAR_CONTROL.h = size.height;
1515                                 
1516                                 elmBar.style.top = ( - barH ) + 'px';
1517                                 $( elmBar ).animate( { top: 0 } );                              
1518                                 
1519                                 delete MENU_BAR_CONTROL.init;
1520                         },
1521                         open : function(){
1522                                 for( var i = MenuPrivateDataClass.list.length; i; ) MenuPrivateDataClass.list[ --i ].open();
1523                                 delete MENU_BAR_CONTROL.open;
1524                         },
1525                         close : function(){
1526                                 var data;
1527                                 while( data = MenuPrivateDataClass.list.shift() ) data.close();
1528                                 nodeBox.remove();
1529                                 MenuPrivateDataClass.list = elmBar = layerBox = elmBox = null;
1530                                 MENU_BAR_CONTROL.kill = kill;
1531                                 MENU_BAR_CONTROL.kill();
1532                         },
1533                         createItem : function( title ){
1534                                 return new MenuClass( title );
1535                         },
1536                         busy : function( _busy ){
1537                                 return false;
1538                         },
1539                         onWindowResize: function( _windowW, _windowH ){
1540                                 
1541                         },
1542                         QUIT:   null,
1543                         EDIT:   null,
1544                         WINDOW: null,
1545                         HELP:   null
1546                 };
1547         })();
1548
1549
1550 /* ----------------------------------------
1551  * HISTORY_CONTROL
1552  *  - controler
1553  */
1554         var HISTORY_CONTROL = ( function() {
1555                 var     stackBack    = [],
1556                         stackForward = [],
1557                         menubarBack,
1558                         menubarForward;
1559
1560                 function back(){
1561                         /*
1562                          * currentを控えてstackForward.push(current)
1563                          * stackBack.pop()を実行してcurrentに
1564                          */
1565                         if( stackBack.length === 0 ) return;
1566
1567                         var s = stackBack.pop();
1568                         s.callback.apply( s.thisObj || {}, s.argBack );
1569                         menubarBack.visible( stackBack.length !== 0 );
1570                         SAVE_CONTROL.panelUpdated( stackBack.length !== 0 );
1571                         
1572                         stackForward.push( s );
1573                         menubarForward.visible( true );
1574                 };
1575                 function forward(){
1576                         if( stackForward.length === 0 ) return;
1577                         
1578                         var s = stackForward.pop();
1579                         s.callback.apply( s.thisObj || {}, s.argForword );
1580                         menubarForward.visible( stackForward.length !== 0 );
1581                         
1582                         stackBack.push( s );
1583                         menubarBack.visible( true );
1584                         SAVE_CONTROL.panelUpdated( true );
1585                 };
1586                 var RecordClass = function( callback, argBack, argForword, destroy, opt_thisObject ){
1587                         this.callback   = callback;
1588                         this.argBack    = argBack;
1589                         this.argForword = argForword;
1590                         this.destroy    = !!destroy;
1591                         this.thisObj    = opt_thisObject;
1592                 };
1593                 RecordClass.prototype.kill = function( _callDestroy ){
1594                         var     _argBack    = this.argBack,
1595                                 _argForword = this.argForword,
1596                                 v;
1597                         this._kill = kill;
1598                         this._kill();
1599                         
1600                         if( _callDestroy !== true ) return;
1601                         
1602                         if( Type.isArray( _argBack ) === true ){ // isArray
1603                                 while( v = _argBack.shift() ){
1604                                         _callDestroy === true && Type.isFunction( v.destroy ) === true && v.destroy();
1605                                 };
1606                         };
1607                         if( Type.isArray( _argForword ) === true ){
1608                                 while( v = _argForword.shift() ){
1609                                         _callDestroy === true && Type.isFunction( v.destroy ) === true && v.destroy();
1610                                 };
1611                         };
1612                 };
1613                 return {
1614                         init: function(){
1615                                 app.addKeyEventListener( 'keydown', back,    90, false, true ); // ctrl + Z
1616                                 app.addKeyEventListener( 'keydown', forward, 90, true,  true ); // ctrl + shift + Z
1617                                 app.addKeyEventListener( 'keydown', forward, 89, false, true ); // ctrl + Y
1618                                 
1619                                 delete HISTORY_CONTROL.init;
1620                         },
1621                         open: function(){
1622                                 menubarBack    = MENU_BAR_CONTROL.EDIT.createOption( 'back',    'ctrl + z', back, false );
1623                                 menubarForward = MENU_BAR_CONTROL.EDIT.createOption( 'forward', 'ctrl + y', forward, false, false, true );                              
1624                                 
1625                                 delete HISTORY_CONTROL.open;
1626                         },
1627                         close: function(){
1628                                 var s;
1629                         while( s = stackBack.shift() )    s.kill( true );
1630                         while( s = stackForward.shift() ) s.kill( true );
1631                         menubarBack = menubarForward = stackBack = stackForward = null;
1632                         },
1633                     saveState: function( _function, _argBack, _argForword, _onRecordDestroy, opt_thisObject ){
1634                         stackBack.push( new RecordClass( _function, _argBack, _argForword, _onRecordDestroy, opt_thisObject ));
1635                         menubarBack.visible( true );
1636                                 SAVE_CONTROL.panelUpdated( true );
1637                                 
1638                                 var s;
1639                         while( s = stackForward.shift() ) s.kill( s.destroy );
1640                                 menubarForward.visible( false );
1641                     }           
1642                 };
1643         })();
1644
1645 /* ----------------------------------------
1646  * SAVE_CONTROL
1647  *  - controler
1648  */
1649         var SAVE_CONTROL = ( function(){
1650                 var save, saveQuit, eXport, quit,
1651                         updated  = false;
1652                 
1653                 function quit(){
1654                         Editor.shutdown();
1655                 };
1656                 function onSave(){
1657                         PanelConsole.boot( Model.createPanel( {
1658                                 comicID           : comicID,
1659                                 panelID           : panelID,
1660                                 panelTimming      : panelTimming,
1661                                 panelW            : PANEL_CONTROL.w,
1662                                 panelH            : PANEL_CONTROL.h,
1663                                 borderSize        : 2,
1664                                 panelElementArray : PANEL_ELEMENT_ARRAY,
1665                                 publish           : true
1666                         } ) );
1667                 };
1668                 function onSaveQuit(){
1669                         // Editor.shutdown();
1670                         onSave();
1671                 };
1672                 function onExport(){
1673                         OutputConsole.boot(
1674                                 comicID, panelID, panelTimming,
1675                                 PANEL_CONTROL.w, PANEL_CONTROL.h,
1676                                 2, // border, BackgroundImage
1677                                 PANEL_ELEMENT_ARRAY
1678                         );
1679                 };
1680                 return {
1681                         init: function(){
1682                                 delete SAVE_CONTROL.init;
1683                         },
1684                         open: function(){
1685                                 save     = MENU_BAR_CONTROL.QUIT.createOption( 'save', 'ctrl + S', onSave, false );
1686                                 saveQuit = MENU_BAR_CONTROL.QUIT.createOption( 'save & quit', null, onSaveQuit, false, false, true );
1687                                 eXport   = MENU_BAR_CONTROL.QUIT.createOption( 'export', null, onExport, true, false, true );
1688                                 quit     = MENU_BAR_CONTROL.QUIT.createOption( 'quit', null, quit, true, true );                                
1689                                 
1690                                 delete SAVE_CONTROL.open;
1691                         },
1692                         close: function(){
1693                                 save = saveQuit = eXport = quit = null;
1694                                 SAVE_CONTROL.kill = kill;
1695                                 SAVE_CONTROL.kill();
1696                         },
1697                         quit: quit,
1698                         panelUpdated: function( _updated ){
1699                                 if( Type.isBoolean( _updated ) === true ){
1700                                         save.visible( _updated );
1701                                         saveQuit.visible( _updated );
1702                                         updated = _updated;
1703                                 }
1704                                 return updated;
1705                         },
1706                         save: function(){
1707                                 
1708                         }
1709                 };
1710         })();
1711
1712 /* ----------------------------------------
1713  * WINDOWS_CONTROL
1714  *  - contloler
1715  *  - mouseEventListener
1716  */     
1717         var WINDOWS_CONTROL = ( function(){
1718         /*
1719          *  表示上手前にあるwindowは、WINDOW_DATA_LISTの先頭にあり、htmlでは後ろにある。
1720          */
1721                 var DEFAULT_MIN_WINDOW_WIDTH  = 200,
1722                         DEFAULT_MIN_WINDOW_HEIGHT = 200,
1723                         WINDOW_DATA_LIST          = [],
1724                         WINDOW_BODY_BODER_SIZE    = 1,
1725                         currentWindowData,
1726                         elmRoot,
1727                         nodeContainer,
1728                         elmWindowOrigin,
1729                         closeButtonWidth;
1730         /**
1731          * WindowPrivateData
1732          */
1733                 var WindowPrivateData = function(){};
1734                 WindowPrivateData.prototype = {
1735                         window        : null,
1736                         menubarOption : null,
1737                         elm           : null,
1738                         elmHead       : null,
1739                         elmBody       : null,
1740                         nodeWindow    : null,
1741                         nodeHead      : null,
1742                         nodeBody      : null,
1743                         nodeFoot      : null,
1744                         nodeResize    : null,
1745                         visible       : false,
1746                         isDragging    : false,
1747                         isResizing    : false,
1748                         title         : null,
1749                         x             : 0,
1750                         y             : 0,
1751                         w             : 0,
1752                         h             : 0,
1753                         minWindowW    : 200,
1754                         minWindowH    : 200,
1755                         startX        : 0,
1756                         startY        : 0,
1757                         startW        : 0,
1758                         startH        : 0,
1759                         offsetX       : 0,
1760                         offsetY       : 0,
1761                         headerH       : 0,
1762                         bodyH         : 0,
1763                         footerH       : 0,
1764                         init : function( win, bodyTempleteID, title, x, y, w, h, visible, closeEnabled, resizeEnabled, minWindowW, minWindowH ){
1765                                 this.window         = win;
1766                                 this.bodyTempleteID = bodyTempleteID;
1767                                 this.title          = title;
1768                                 this.x              = x;
1769                                 this.y              = y;
1770                                 this.w              = w;
1771                                 this.h              = h;
1772                                 this.visible        = visible;
1773                                 this.closeEnabled   = closeEnabled;
1774                                 this.resizeEnabled  = resizeEnabled;
1775                                 this.minWindowW     = minWindowW;
1776                                 this.minWindowH     = minWindowH;
1777                                 
1778                                 WINDOW_DATA_LIST.push( this );
1779                         },
1780                         create : function(){
1781                                 var win = this.window;
1782                                 this.elm = win.elm = elmWindowOrigin.cloneNode( true );
1783                                 this.menubarOption = MENU_BAR_CONTROL.WINDOW.createOption( 
1784                                         ( this.visible !== true ? 'show ' : 'hide ' ) + this.title,
1785                                         null, this.onMenubarClick,
1786                                         true, false, false,
1787                                         this
1788                                 );
1789                                 if( win.onInit ){
1790                                         win.onInit();
1791                                         delete win.onInit;
1792                                 };
1793                         },
1794                         onMenubarClick : function(){
1795                                 this.window[ this.visible === true ? 'close' : 'open' ]();
1796                         },
1797                         update : function( x, y, w, h ){
1798                                 var win = this.window, bodyH;
1799                                         
1800                                 x = x !== undefined ? x : this.x;
1801                                 y = y !== undefined ? y : this.y;
1802                                 y = y > MENU_BAR_CONTROL.h ? y : MENU_BAR_CONTROL.h;
1803                                 w = w !== undefined ? w : this.w;
1804                                 h = h !== undefined ? h : this.h;
1805                                 
1806                                 this.nodeWindow.update( x, y, w, h );
1807                                 this.nodeHead && this.nodeHead.update( 0, 0, w, this.headerH );
1808                                 console.log( '************ hewader' + this.headerH )
1809                                 this.nodeBody.update( 0, this.headerH, w, this.bodyH = h - this.headerH - this.footerH );
1810                                 ( this.w !== w || this.h !== h ) && win.onResize && win.onResize( w, this.bodyH );
1811
1812                                 this.x = x;
1813                                 this.y = y;
1814                                 this.w = w;
1815                                 this.h = h;
1816                         },
1817                         firstOpen : function(){
1818                                 var win       = this.window,
1819                                         elmHead   = this.elmHead = Util.getElementsByClassName( this.elm, 'window-header' )[ 0 ];
1820                                         elmBody   = this.elmBody = Util.getElementsByClassName( this.elm, 'window-body' )[ 0 ],
1821                                         elmClose  = Util.getElementsByClassName( this.elm, 'window-close-button' )[ 0 ],
1822                                         elmFoot   = Util.getElementsByClassName( this.elm, 'window-footer' )[ 0 ],
1823                                         elmResize = Util.getElementsByClassName( this.elm, 'window-resize-button' )[ 0 ],
1824                                         replaceID = this.bodyTempleteID;
1825                                 
1826                                 this.nodeWindow = nodeContainer.createNode( this.elm, false, true, 'window-wrapper-hover' );
1827                                 this.nodeWindow.addEventListener( 'mousemove', this.mousemove, this );
1828                                 this.nodeWindow.addEventListener( 'mousedown', this.mousedown, this );
1829                                 this.nodeWindow.addEventListener( 'mouseup',   this.mouseup,   this );
1830                                 this.nodeWindow.addEventListener( 'mouseout',  this.mouseup,   this );
1831                                 
1832                                 // this.nodeHead   = this.nodeWindow.createNode( elmHead );
1833                                 win.title( this.title );
1834
1835                                 this.nodeBody   = this.nodeWindow.createNode( elmBody, false, true, null, '', true );
1836                                 replaceID && elmBody.appendChild( document.getElementById( replaceID ) );
1837                                 
1838                                 if( this.closeEnabled === true ){
1839                                         // this.nodeClose = this.nodeHead.createNode( elmClose );
1840                                         // this.nodeClose.addEventListener( 'mousedown', win.close, data );
1841                                 } else {
1842                                         elmClose.parentNode.removeChild( elmClose );
1843                                 };
1844                                 
1845                                 if( this.resizeEnabled === true ){
1846                                         // this.nodeFoot = this.nodeWindow.createNode( elmFoot );
1847                                         this.footerH  = Util.getElementSize( elmFoot ).height; // this.nodeFoot.height();
1848                                         
1849                                         // this.nodeResize = this.nodeFoot.createNode( elmResize );
1850                                         // this.nodeResize.addEventListener( 'mousedrag', this.resizeDrag, data );
1851                                 } else {
1852                                         elmFoot.parentNode.removeChild( elmFoot );
1853                                 };
1854                                 
1855                                 this.update( this.x, this.y, this.w, this.h );
1856                                 if( win.onFirstOpen ){
1857                                         win.onFirstOpen( this.w, this.bodyH, this.nodeBody );
1858                                         delete win.onFirtOpen;
1859                                 };
1860                                 
1861                                 this.firstOpen = null;
1862                         },
1863                         onFadeIn : function(){
1864                                 var data = WindowPrivateData.get( this ),
1865                                         win  = data.window;
1866                                 data.firstOpen && data.firstOpen();
1867                                 win.onOpen && win.onOpen( data.w, data.bodyH );
1868                                 data.nodeWindow.disabled( false );
1869                                 data.goFront();
1870                         },
1871                         onFadeOut : function(){
1872                                 var data = WindowPrivateData.get( this ),
1873                                         win  = data.window;
1874                                 elmRoot.removeChild( data.elm );
1875                                 win.onClose && app.addAsyncCall( win.onClose, null, win );
1876                         },
1877                         mousedown : function( e ){
1878                                 currentWindowData !== this && this.goFront();
1879                                 
1880                                 var x   = e.layerX,
1881                                         y   = e.layerY;
1882                                 if( this.resizeEnabled === true && this.w - 20 <= x && x < this.w && this.headerH + this.bodyH < y && y <= this.h ){
1883                                         this.isResizing = true;
1884                                         //this.startX     = this.x;
1885                                         //this.startY     = this.y;
1886                                         this.startW     = this.w;
1887                                         this.startH     = this.h;
1888                                         this.offsetX    = x;
1889                                         this.offsetY    = y;
1890                                         // app.updateCoursor( 'nw-resize' );
1891                                         this.nodeWindow.cursor( 'nw-resize' );
1892                                         return true;
1893                                 };
1894                                 
1895                                 // if( x < 0 || y < 0 || this.w < x || this.headerH < y ) return false;
1896                                 if( this.closeEnabled === true && this.w - closeButtonWidth < x && y < this.headerH ){
1897                                         this.window.close();
1898                                         return;
1899                                 };
1900                                 
1901                                 if( y < this.headerH ){
1902                                         this.isDragging = true;
1903                                         this.startX     = this.x;
1904                                         this.startY     = this.y;
1905                                         this.startW     = this.w;
1906                                         this.startH     = this.h;
1907                                         this.offsetX    = x;
1908                                         this.offsetY    = y;
1909                                         
1910                                         // app.updateCoursor( 'move' );
1911                                         this.nodeWindow.cursor( 'move' );
1912                                         return true;                                    
1913                                 }
1914                         },
1915                         mouseup : function( e ){
1916                                 if( this.isResizing === true || this.isDragging === true ){
1917                                         this.isDragging = this.isResizing = false;
1918                                         this.update();
1919                                 };
1920                                 this.nodeWindow.cursor( '' );
1921                         },
1922                         mousemove : function( e ){
1923                                 currentWindowData !== this && this.goFront();
1924                                 
1925                                 var x   = e.layerX,
1926                                         y   = e.layerY,
1927                                         w, h;
1928                                 if( this.isResizing === true ){
1929                                         w = this.startW + x - this.offsetX;
1930                                         h = this.startH + y - this.offsetY;
1931                                         this.w = w = w < this.minWindowW ? this.minWindowW : w;
1932                                         this.h = h = h < this.minWindowH ? this.minWindowH : h;
1933                                         this.elm.style.width  = w + 'px';
1934                                         this.elm.style.height = h + 'px';
1935                                         return true;                            
1936                                 } else
1937                                 if( this.isDragging === true ){
1938                                         this.x = x = this.startX + x - this.offsetX;
1939                                         this.y = y = this.startY + y - this.offsetY;
1940                                         this.elm.style.left = x + 'px';
1941                                         this.elm.style.top  = y + 'px';
1942                                         return true;
1943                                 };
1944                                 // if( e.hit === false || ( this.headerH < layerY && layerY < this.headerH + this.bodyH ) ) return false;
1945                                 this.nodeWindow.cursor( ( /*0 < layerX && layerX < this.w && 0 <= layerY &&*/ y <= this.headerH ) ? 'pointer' : '' );
1946                         },
1947                         goFront : function(){
1948                                 currentWindowData = this;
1949                                 var i = nodeContainer.numNode() - 1;
1950                                 // console.log( this.nodeWindow.nodeIndex() + ' , ' + this.nodeWindow.numNode() )
1951                                 if( this.nodeWindow.nodeIndex() !== i ){
1952                                         this.nodeWindow.nodeIndex( i );
1953                                         elmRoot.appendChild( this.elm );
1954                                 };
1955                         },
1956                         busy : function(){
1957                                 return this.isDragging === true || this.isResizing === true;
1958                         },
1959                         destroy : function(){
1960                                 
1961                         }
1962                 };
1963                 WindowPrivateData.get = function( windowOrElement ){
1964                         if( windowOrElement instanceof WindowPrivateData ) return windowOrElement;
1965                         var list = WINDOW_DATA_LIST,
1966                                 i    = list.length,
1967                                 data;
1968                         for( ; i; ){
1969                                 data = list[ --i ];
1970                                 if( data.window === windowOrElement || data.elm === windowOrElement ) return data;
1971                         };
1972                         return null;
1973                 };
1974                 
1975         /**
1976          * WindowClass
1977          */
1978                 var WindowClass = function( bodyTempleteID, title, x, y, w, h, visible, closeEnabled, resizeEnabled, minWindowW, minWindowH ){
1979                         ( new WindowPrivateData() ).init( this, bodyTempleteID, title, x, y, w, h, visible, closeEnabled, resizeEnabled, minWindowW, minWindowH );
1980                 };
1981                 WindowClass.prototype = {
1982                         elm        : null,
1983                         open : function(){
1984                                 var data = WindowPrivateData.get( this );
1985                                 if( data.visible === true ) return;
1986                                 
1987                                 data.visible = true;
1988                                 openWindow( data );
1989                                 data.menubarOption.title( 'hide ' + data.title );
1990                                 
1991                                 // WINDOW_DATA_LIST.splice( Util.getIndex( WINDOW_DATA_LIST, data ), 1 );
1992                                 // WINDOW_DATA_LIST.unshift( data );
1993                                 currentWindowData  = null;
1994                         },
1995                         close : function(){
1996                                 var data = WindowPrivateData.get( this );
1997                                 if( data.visible === false ) return;
1998                                 
1999                                 data.visible = false;
2000                                 $( data.elm ).fadeOut( data.onFadeOut );
2001                                 data.menubarOption.title( 'show ' + data.title );
2002                                 data.nodeWindow.disabled( true );
2003                         },
2004                         title : function( _title ){
2005                                 if( Type.isString( _title ) === true ){
2006                                         var data = WindowPrivateData.get( this );
2007                                         data.elmHead.firstChild.innerHTML = data.title = _title;
2008                                         // data.nodeHead.mesure();
2009                                         //data.headerH = data.nodeHead.height();
2010                                         data.headerH = Util.getElementSize( data.elmHead ).height;
2011                                 };
2012                                 return data.title;
2013                         },
2014                         createHeaderItem : function(){
2015                                 var data = WindowPrivateData.get( this ),
2016                                         elm  = document.createElement( 'div' ),
2017                                         node;
2018                                 if( !data.nodeHead ) data.nodeHead = data.nodeWindow.createNode( data.elmHead, true, false );
2019                                 data.elmHead.appendChild( elm );
2020                                 elm.className = 'header-item finder-path';
2021                                 node = data.nodeHead.createNode( elm, false, true, 'header-item-hover', '' )
2022                                 // data.nodeHead.mesure();
2023                                 // data.headerH = data.nodeHead.height();
2024                                 
2025                                 data.headerH = Util.getElementSize( data.elmHead ).height;
2026                                 data.update();
2027                                 
2028                                 return node;
2029                         }
2030                 };
2031                 
2032                 function openWindow( data ){
2033                         if( data.visible !== true ) return;
2034                         elmRoot.appendChild( data.elm );// appendした後に fadeIn() しないと ie で filterが適用されない.
2035                         $( data.elm ).fadeIn( data.onFadeIn );
2036                         return;
2037                 };
2038                 
2039                 return {
2040                         id   : 'WINDOWS_CONTROL',
2041                         init : function(){
2042                                 elmRoot          = document.getElementById( 'window-container' );
2043                                 nodeContainer    = eventRoot.createNode( elmRoot, true, false );
2044                                 elmWindowOrigin  = app.fetchHTMLElement( 'windowTemplete' );
2045                                 closeButtonWidth = Util.getElementSize( Util.getElementsByClassName( elmWindowOrigin, 'window-close-button' )[ 0 ] ).width;
2046                                 
2047                                 delete WINDOWS_CONTROL.init;
2048                         },
2049                         open : function(){
2050                                 for( var i = WINDOW_DATA_LIST.length, data; i; ){
2051                                         data = WINDOW_DATA_LIST[ --i ];
2052                                         data.create();
2053                                         openWindow( data );
2054                                 };
2055                                 delete WINDOWS_CONTROL.open;
2056                         },
2057                         close : function(){
2058                         },
2059                         onWindowResize : function( _windowW, _windowH ){
2060                                 /*
2061                                  * 画面外に出るwindowの移動
2062                                  */
2063                         },
2064                         createWindow : function( EXTENDS, bodyTempleteID, title, x, y, w, h, opt_visible, opt_closeEnabled, opt_resizeEnabled, opt_minWindowW, opt_minWindowH ){
2065                                 opt_visible       = opt_visible !== false;
2066                                 opt_closeEnabled  = opt_closeEnabled === true;
2067                                 opt_resizeEnabled = opt_resizeEnabled === true;
2068                                 opt_minWindowW    = opt_minWindowW || ( w < DEFAULT_MIN_WINDOW_WIDTH ) ? w : DEFAULT_MIN_WINDOW_WIDTH;
2069                                 opt_minWindowH    = opt_minWindowH || ( h < DEFAULT_MIN_WINDOW_HEIGHT ) ? h : DEFAULT_MIN_WINDOW_HEIGHT;
2070                                 
2071                                 var win = new WindowClass( bodyTempleteID, title, x, y, w, h, opt_visible, opt_closeEnabled, opt_resizeEnabled, opt_minWindowW, opt_minWindowH ),
2072                                         data;
2073                                 for( var key in EXTENDS ){
2074                                         win[ key ] = EXTENDS[ key ];
2075                                 };
2076                                 if( Type.isUndefined( WINDOWS_CONTROL.init ) === true ){
2077                                         data = WindowPrivateData.get( win );
2078                                         data.create();
2079                                         openWindow( data );
2080                                 };
2081                                 return win;
2082                         }
2083                 };
2084         })();
2085
2086 /* ----------------------------------------
2087  * TOOL_BOX_WINDOW
2088  * - window
2089  */
2090         var TOOL_BOX_WINDOW = ( function(){
2091                         
2092                 app.addKeyEventListener( 'keydown', addImage,   73, false, true );
2093                 app.addKeyEventListener( 'keydown', addText,    84, false, true );
2094                 app.addKeyEventListener( 'keydown', switchGrid, 71, false, true );
2095
2096                 function addImage(){
2097                         // IMAGE_EXPLORER_WINDOW.open();
2098                         app.addAsyncCall( IMAGE_EXPLORER_WINDOW.open, null, IMAGE_EXPLORER_WINDOW );
2099                         //TOOL_BOX_WINDOW.bodyBackOrForward( true );
2100                 };
2101                 function addText(){
2102                         app.addAsyncCall( PANEL_ELEMENT_CONTROL.createTextElement );
2103                 };
2104                 function switchGrid(){
2105                         app.addAsyncCall( GRID_CONTROL.update, null, GRID_CONTROL );
2106                 };
2107                 function popupHelp(){
2108                         //.bodyBackOrForward( true );
2109                         app.addAsyncCall( HELP_DOCUMENTS_WINDOW.open, null, HELP_DOCUMENTS_WINDOW );
2110                 };
2111                 function editBG( e ){
2112                         //TOOL_BOX_WINDOW.bodyBackOrForward( true );
2113                         app.addAsyncCall( INFOMATION_WINDOW.open, null, INFOMATION_WINDOW );
2114                 };
2115                 
2116                 return WINDOWS_CONTROL.createWindow(
2117                         {
2118                                 onInit: function(){
2119                                         MENU_BAR_CONTROL.EDIT.createOption( 'Add Image', 'ctrl + I', addImage, true, true, false );
2120                                         MENU_BAR_CONTROL.EDIT.createOption( 'Add Text',  'ctrl + T', addText, true, false, true );
2121                                         MENU_BAR_CONTROL.EDIT.createOption( 'show Grid', 'ctrl + G', switchGrid, true, true, true );
2122                                 },
2123                                 onFirstOpen: function( x, y, nodeBody ){
2124                                         nodeBody.createNode( document.getElementById( 'toolbox-add-image-button'  ), false, true, 'button-hover', 'pointer' ).addEventListener( 'click', addImage );
2125                                         nodeBody.createNode( document.getElementById( 'toolbox-add-text-button'   ), false, true, 'button-hover', 'pointer' ).addEventListener( 'click', addText );
2126                                         nodeBody.createNode( document.getElementById( 'toolbox-edit-bg-button'    ), false, true, 'button-hover', 'pointer' ).addEventListener( 'click', editBG );
2127                                         nodeBody.createNode( document.getElementById( 'toolbox-switch-grid'       ), false, true, 'button-hover', 'pointer' ).addEventListener( 'click', switchGrid );
2128                                         nodeBody.createNode( document.getElementById( 'toolbox-popup-help-button' ), false, true, 'button-hover', 'pointer' ).addEventListener( 'click', popupHelp );
2129                                         // nodeBody.createNode( document.getElementById( 'toolbox-add-text-button'   ), false, true, 'button-hover', 'pointer' ).addEventListener( 'click', addText );
2130                                 }
2131                         },
2132                         'toolbox-window', 'Tool box', 0, 215, 110, 290, true
2133                 );
2134         })();
2135         
2136         
2137 /* ----------------------------------------
2138  * IMAGE_EXPROLER
2139  *  - window
2140  */
2141         var IMAGE_EXPLORER_WINDOW = ( function(){
2142                 var tree, finder;
2143                 
2144                 function onFileSelect( _file ){
2145                         // 他の image ファイルも許可する?
2146                         if( Driver.isPettanrFileInstance( _file ) === true ){
2147                                 if( _file.getType() === FILE_TYPE.PICTURE ){
2148                                         PANEL_ELEMENT_CONTROL.onImageSelect( FileAPI.getFileData( _file ) );
2149                                 };
2150                         };
2151                 };
2152                 
2153                 return WINDOWS_CONTROL.createWindow(
2154                         {
2155                                 onInit: function(){},
2156                                 onFirstOpen: function( _w, _h, nodeBody ){
2157                                         tree = FileAPI.createTree( FILE_DATA_PICTURE_ROOT );
2158                                         var     _root  = tree.getRootFile(),
2159                                                 _myPic = _root.getChildFileByIndex( 0 ),
2160                                                 _pic   = _root.getChildFileByIndex( 1 );
2161                                         _myPic.getSeqentialFiles();
2162                                         _pic.getSeqentialFiles();
2163                                         _myPic.destroy();
2164                                         _pic.destroy(); 
2165                         
2166                                         finder = app.createFinder(
2167                                                 nodeBody,
2168                                                 tree,
2169                                                 onFileSelect,
2170                                                 PANEL_ELEMENT_CONTROL.onImageSelect
2171                                         );
2172                                         finder.createPath( IMAGE_EXPLORER_WINDOW.createHeaderItem() );
2173                                 },
2174                                 onOpen: function( _w, _h ){
2175                                         finder.resize( _w, _h );
2176                                 },
2177                                 onResize: function( _w, _h ){
2178                                         finder.resize( _w, _h );
2179                                 }
2180                         },
2181                         null, 'Album', 0, 215, 600, 350, false, true, true, 300, 300
2182                 );
2183         })();
2184         
2185         
2186 /* ----------------------------------------
2187  * INFOMATION_WINDOW
2188  *  - window
2189  */                     
2190         var INFOMATION_WINDOW = ( function(){
2191                 var backgroundInfomationElm;
2192
2193                 return WINDOWS_CONTROL.createWindow(
2194                         {
2195                                 onFirstOpen: function( _w, _h ){
2196                                         backgroundInfomationElm = $( '#panel-background-information');
2197                                 },
2198                                 onResize: function(  _w, _h ){
2199                                 },
2200                                 update: function( currentElement ){
2201
2202                                 }
2203                         },
2204                         'infomation-window', 'Infomation', 0, 30, 200, 180, true
2205                 );
2206         })();
2207
2208 /* ----------------------------------------
2209  * HELP_WINDOW
2210  *  - window
2211  */
2212         var HELP_DOCUMENTS_WINDOW = ( function(){
2213                 var visible          = true,
2214                         jqAjaxContents,
2215                         jqNaviItems,
2216                         jqPages,
2217                         currentPageIndex = 0,
2218                         numPage          = 0,
2219                         asyncOption      = null;
2220
2221                 function onAjaxStart( _pageIndex ){
2222                         delete asyncOption.callback;
2223                         
2224                         currentPageIndex = _pageIndex || currentPageIndex;
2225                         if( onHelpLoad !== null ){
2226                                 $.ajax({
2227                                         url:            'help/jp.xml',
2228                                         dataType:       'xml',
2229                                         success:        onHelpLoad
2230                                 });
2231                                 onHelpLoad = null;
2232                         }
2233                         onAjaxStart = null;
2234                 };
2235                 var onHelpLoad = function( _xml ){
2236                         var jqXML          = $( _xml ),
2237                                 helpTitle      = jqXML.find( 'pages' ).eq( 0 ).attr( 'title' ),
2238                                 elmRoot        = document.createElement( 'div' ),
2239                                 elmNavi        = document.createElement( 'div' ),
2240                                 elmItemOrigin  = document.createElement( 'a' ),
2241                                 elmPages       = document.createElement( 'div' ),
2242                                 elmPageOrigin  = document.createElement( 'div' ),
2243                                 elmTitleOrigin = document.createElement( 'h2' ),
2244                                 elmPage;
2245                         elmRoot.className       = 'multi-page-container clearfix';
2246                         elmNavi.className       = 'sidenavi';
2247                         elmItemOrigin.className = 'sidenavi-item';
2248                         elmItemOrigin.href      = '#';
2249                         elmPages.className      = 'page-contents';
2250                         elmPageOrigin.className = 'page-content main';
2251                         elmPageOrigin.appendChild( elmTitleOrigin);
2252                         
2253                         MENU_BAR_CONTROL.HELP.remove( asyncOption );
2254                         asyncOption = null;                     
2255                         
2256                         jqXML.find( 'page' ).each( function(){
2257                                 var xmlPage = $( this ),
2258                                         title = xmlPage.attr( 'title' ),
2259                                         content = xmlPage.text();
2260                                 
2261                                 elmItemOrigin.innerHTML = title;
2262                                 elmNavi.appendChild( elmItemOrigin.cloneNode( true ) );
2263                                 
2264                                 elmTitleOrigin.innerHTML = title;
2265                                 
2266                                 elmPage = elmPageOrigin.cloneNode( true );
2267                                 elmPage.innerHTML = content;
2268                                 
2269                                 Util.cleanElement( elmPage);
2270                                 
2271                                 if( elmPage.childNodes.length > 0 ){
2272                                         elmPage.insertBefore( elmTitleOrigin.cloneNode( true ), elmPage.childNodes[0]);
2273                                 } else {
2274                                         elmPage.appendChild( elmTitleOrigin.cloneNode( true ));
2275                                 }
2276                                 elmPages.appendChild( elmPage );
2277                                 
2278                                 MENU_BAR_CONTROL.HELP.createOption( title, null, onSelectionClick, true );
2279                                 ++numPage;
2280                         });
2281                         elmRoot.appendChild( elmNavi );
2282                         elmRoot.appendChild( elmPages );
2283                         jqAjaxContents.removeClass( 'loading' ).append( elmRoot );
2284                         
2285                         jqNaviItems = jqAjaxContents.find( 'a.' + elmItemOrigin.className ).click( onNaviClick );
2286                         jqPages     = jqAjaxContents.find( '.page-content' );
2287                         jqPages.find( 'a' ).click( onInnerLinkClick );
2288                         
2289                         app.addAsyncCall( jumpPage );
2290                 };
2291                 function onSelectionClick( _pageIndex ){
2292                         HELP_DOCUMENTS_WINDOW.open();
2293                         jumpPage( _pageIndex );
2294                 };
2295                 function jumpPage( _index ){
2296                         if( Type.isNumber( _index ) === true && 0 <= _index && _index < numPage && currentPageIndex !== _index ){
2297                                 currentPageIndex = _index;
2298                         };
2299                         jqNaviItems.removeClass( 'current' ).eq( currentPageIndex ).addClass( 'current' );
2300                         jqPages.hide().eq( currentPageIndex ).show();
2301                 };
2302                 function onNaviClick( e ){
2303                         // this は <a>
2304                         jumpPage( Util.getChildIndex( this.parentNode, this ) );
2305                         return false;
2306                 };
2307                 function onInnerLinkClick( e ){
2308                         var jump = ( this.href || '' ).split( '#jump' ),
2309                                 n = jump[ 1 ];
2310                         if( !n ) return;
2311                         jumpPage( '' + parseFloat( n ) === n ? parseFloat( n ) : -1 );
2312                         return false;                           
2313                 };
2314                 return WINDOWS_CONTROL.createWindow(
2315                         {
2316                                 onInit: function(){
2317                                         asyncOption    = MENU_BAR_CONTROL.HELP.createAsyncOption( onAjaxStart );
2318                                         jqAjaxContents = $( HELP_DOCUMENTS_WINDOW.elm ).find( '.window-body' ).addClass( 'loading' ).css( { overflow: 'auto' } );
2319                                 },
2320                                 onFirstOpen: function( _w, _h ){
2321                                         jqAjaxContents.css( { height: _h } );
2322                                         onAjaxStart !== null && onAjaxStart();
2323                                 },
2324                                 onResize: function( _w, _h ){
2325                                         jqAjaxContents && jqAjaxContents.css( { height: _h } );
2326                                 }
2327                         },
2328                         null, 'Help', 0, 215, 400, 350, false, true, true, 300, 300
2329                 );
2330         })();
2331
2332 /* ----------------------------------------
2333  * GRID_CONTROL
2334  *  - control
2335  *  - panelResizeListener
2336  */
2337         var GRID_CONTROL = ( function(){
2338                 var elmGrid,
2339                         urlBG   = "url('images/grid.gif')",
2340                         visible = false;
2341
2342                 return {
2343                         init: function(){
2344                                 elmGrid = document.getElementById( 'grid' );
2345                                 delete GRID_CONTROL.init;
2346                         },
2347                         open: function(){
2348                                 delete GRID_CONTROL.open;
2349                         },
2350                         close: function(){
2351                                 
2352                         },
2353                         onPanelResize: function( _panelX, _panelY ){
2354                                 elmGrid.style.backgroundPosition = [ _panelX % 10, 'px ', _panelY % 10, 'px' ].join( '' );
2355                                 elmGrid.style.height = windowH +'px';
2356                         },
2357                         enabled: function(){
2358                                 return visible;
2359                         },
2360                         update: function(){
2361                                 $( elmGrid ).stop().css( {
2362                                         opacity:        '',
2363                                         fliter:         ''
2364                                 })[ visible === true ? 'fadeOut' : 'fadeIn' ]();
2365                                 
2366                                 visible = !visible;
2367                                 
2368                                 if( visible === true && urlBG !== null ){
2369                                         elmGrid.style.backgroundImage = urlBG;
2370                                         urlBG = null;
2371                                 }
2372                                 return visible;
2373                         }
2374                 }
2375         })();
2376                 
2377 /* ----------------------------------------
2378  * WHITE_GLASS_CONTROL
2379  *  - panelResizeListener
2380  */
2381         var WHITE_GLASS_CONTROL = ( function(){
2382                 var styleTop, styleLeft, styleRight, styleBottom;
2383
2384                 return {
2385                         init: function(){
2386                                 styleTop    = document.getElementById( 'whiteGlass-top' ).style;
2387                                 styleLeft   = document.getElementById( 'whiteGlass-left' ).style;
2388                                 styleRight  = document.getElementById( 'whiteGlass-right' ).style;
2389                                 styleBottom = document.getElementById( 'whiteGlass-bottom' ).style;
2390                                 delete WHITE_GLASS_CONTROL.init;
2391                         },
2392                         onPanelResize: function( _panelX, _panelY, _panelW, _panelH ){
2393                                 var     _w             = _panelW,
2394                                         _h             = _panelH,
2395                                         marginTop      = _panelY,
2396                                         marginBottom   = windowH -_h -marginTop,
2397                                         marginX        = _panelX,
2398                                         rightWidth     = windowW -_w -marginX;
2399                                 
2400                                 styleTop.height    = ( marginTop < 0 ? 0 : marginTop ) + 'px';
2401                                 
2402                                 styleLeft.top      = marginTop + 'px';
2403                                 styleLeft.width    = ( marginX < 0 ? 0 : marginX ) + 'px';
2404                                 styleLeft.height   = ( _h + marginBottom ) + 'px';
2405                                 
2406                                 styleRight.top     = marginTop + 'px';
2407                                 styleRight.left    = _w +marginX + 'px';
2408                                 styleRight.width   = ( rightWidth < 0 ? 0 : rightWidth ) + 'px';
2409                                 styleRight.height  = ( _h + marginBottom ) + 'px';
2410                                 
2411                                 styleBottom.top    = ( _h +marginTop ) + 'px';
2412                                 styleBottom.left   = marginX + 'px';
2413                                 styleBottom.width  = _w + 'px';
2414                                 styleBottom.height = ( marginBottom < 0 ? 0 : marginBottom ) + 'px';
2415                         }
2416                 };
2417         })();
2418
2419
2420 /* --------------------------------------------------------------------------------------------
2421  * PanelResizerClass
2422  *  - mouseEventListener
2423  */
2424         var PanelResizerClass = function( id, isTop ){
2425                 this.id    = id;
2426                 this.isTop = isTop;
2427         };
2428         PanelResizerClass.BORDER_SIZE = 2;
2429         PanelResizerClass.HEIGHT      = 30;
2430         PanelResizerClass.prototype = {
2431                 id             : null,
2432                 node           : null,
2433                 style          : null,
2434                 isTop          : false,
2435                 x              : - PanelResizerClass.BORDER_SIZE / 2,
2436                 y              : 0,
2437                 w              : 0,
2438                 h              : PanelResizerClass.HEIGHT,
2439                 panelX         : 0,
2440                 panelY         : 0,
2441                 panelW         : 0,
2442                 panelH         : 0,
2443                 offsetY        : 0,
2444                 startY         : 0,
2445                 startH         : 0,
2446                 isDragging     : false,
2447                 init : function(){
2448                         this.node  = PANEL_CONTROL.node.createNode( document.getElementById( this.id ), false, false, 'panel-resizer-hover', 'pointer' );
2449                         this.node.addEventListener( 'mousedown', this.mousedown, this );
2450                         this.style = document.getElementById( this.id ).style;
2451                         this.y     = this.isTop === true ? ( -5 - PanelResizerClass.HEIGHT - PanelResizerClass.BORDER_SIZE ) : 0;
2452                 },      
2453                 mousedown : function( e ){
2454                         
2455                         var x = e.layerX, // - this.panelX,
2456                                 y = e.layerY; // - this.panelY;
2457                         this.offsetY    = y;
2458                         this.startY     = this.panelY;
2459                         this.startH     = this.panelH;
2460                         this.isDragging = true;
2461                         // app.updateCoursor( 'n-resize' );
2462                         this.node.addEventListener( 'mousemove', this.mousemove, this );
2463                         this.node.addEventListener( 'mouseup',   this.mouseup,   this );
2464                         this.node.cursor( 'n-resize' );
2465                         return true;
2466                 },
2467                 mousemove : function( e ){
2468                         var move = e.layerY - this.offsetY,
2469                                 h;
2470
2471                         if( this.isTop === true ){
2472                                 if( this.panelH - move < MIN_PANEL_HEIGHT ){
2473                                         move = this.panelH - MIN_PANEL_HEIGHT;
2474                                 };
2475                                 PANEL_CONTROL.resizeElement( true, this.panelX, this.panelY + move, this.panelW, this.panelH - move );
2476                         } else {
2477                                 h = this.startH + move;
2478                                 if( 0 < h && h < windowH - this.panelY - PanelResizerClass.HEIGHT - 5 - PanelResizerClass.BORDER_SIZE ){
2479                                         PANEL_CONTROL.resizeElement( false, this.panelX, this.panelY, this.panelW, h < MIN_PANEL_HEIGHT ? MIN_PANEL_HEIGHT : h );
2480                                 };
2481                         };
2482                         return true;
2483                 },
2484                 mouseup : function( e ){
2485                         if( this.isDragging !== true ) return;
2486                         ( this.startY !== this.panelY || this.startH !== this.panelH ) &&
2487                                 HISTORY_CONTROL.saveState(
2488                                         this.restoreState,
2489                                         [ undefined, this.startY, undefined, this.startH ],
2490                                         [ undefined, this.panelY, undefined, this.panelH ],
2491                                         null, this
2492                                 );
2493                         this.isDragging = false;
2494                         var move = e.layerY - this.offsetY,
2495                                 h;
2496
2497                         if( this.isTop === true ){
2498                                 if( this.panelH - move < MIN_PANEL_HEIGHT ){
2499                                         move = this.panelH - MIN_PANEL_HEIGHT;
2500                                 };
2501                                 PANEL_CONTROL.resize( true, this.panelX, this.panelY + move, this.panelW, this.panelH - move );
2502                         } else {
2503                                 h = this.startH + move;
2504                                 if( 0 < h && h < windowH - this.panelY - PanelResizerClass.HEIGHT - 5 - PanelResizerClass.BORDER_SIZE ){
2505                                         PANEL_CONTROL.resize( false, this.panelX, this.panelY, this.panelW, h < MIN_PANEL_HEIGHT ? MIN_PANEL_HEIGHT : h );
2506                                 };
2507                         };
2508                         // app.updateCoursor( '' );
2509                         this.node.removeEventListener( 'mousemove', this.mousemove );
2510                         this.node.removeEventListener( 'mouseup',   this.mouseup );
2511                         this.node.cursor( 'pointer' );
2512                 },
2513                 restoreState : function( x, y, w, h ){
2514                         PANEL_CONTROL.resize( this.isTop, x || this.panelX, y || this.panelY, w || this.panelW, h || this.panelH );
2515                 },
2516                 busy : function(){
2517                         return this.isDragging;
2518                 },
2519                 onPanelResize : function( x, y, w, h ){
2520                         this.panelX = x;
2521                         this.panelY = y;
2522                         if( this.panelW !== w ){
2523                                 this.style.width = ( w + 2 ) + 'px';
2524                                 this.panelW      = w;
2525                         };
2526                         this.panelH = h;
2527                         this.y = this.isTop === true ? this.y : ( this.panelH + 5 + PanelResizerClass.BORDER_SIZE );
2528                         this.w = this.panelW + 2;
2529                         this.node.update( undefined, undefined, this.w );
2530                 }       
2531         };
2532
2533 /* ----------------------------------------
2534  * PANEL_CONTROL
2535  *  - controler
2536  *  - mouseEventListener
2537  * 
2538  * panel-border の表示と onPanelResize の通知.
2539  * panel drag.
2540  * 
2541  */
2542         var PANEL_CONTROL = ( function(){
2543                 var elmPanel, stylePanel,
2544                         nodePanel            = null,
2545                         DEFAULT_PANEL_WIDTH  = 400,
2546                         DEFAULT_PANEL_HEIGHT = 300,
2547                         borderSize           = 2,
2548                         offsetX, offsetY, startX, startY,
2549                         isDragging           = false,
2550                         isDraggable          = false,
2551                         resizerTop           = new PanelResizerClass( 'panel-resizer-top',    true ),
2552                         resizerBottom        = new PanelResizerClass( 'panel-resizer-bottom', false );
2553                 
2554                 app.addKeyEventListener( 'keychange', onSpaceUpdate, 32, false, false );
2555                 
2556                 function onSpaceUpdate( e ){
2557                         if( e.type === 'keyup' ){
2558                                 app.isCurrentInteractiveEventListener( null ) === true && app.updateCoursor( '' );
2559                                 isDraggable = false;
2560                         } else {
2561                                 app.isCurrentInteractiveEventListener( null ) === true && app.updateCoursor( 'move' );
2562                                 isDraggable = true;
2563                         };
2564                         return false;
2565                 };
2566                 
2567                 return {
2568                         id   : 'PANEL_CONTROL',
2569                         x    : 0,
2570                         y    : 0,
2571                         w    : 0,
2572                         h    : 0,
2573                         elm  : null,
2574                         node : null,
2575                         init : function(){
2576                                 elmPanel      = this.elm  = document.getElementById( 'panel-tools-container' );
2577                                 nodePanel     = this.node = eventRoot.createNode( elmPanel, true, false );
2578                                 
2579                                 resizerTop.init();
2580                                 resizerBottom.init();
2581                                 
2582                                 stylePanel = elmPanel.style;
2583                                 delete PANEL_CONTROL.init;
2584                         },
2585                         open: function( _panelW, _panelH, _borderSize ){
2586                                 PANEL_CONTROL.w = Type.isFinite( _panelW ) === true ? _panelW : DEFAULT_PANEL_WIDTH;
2587                                 PANEL_CONTROL.h = Type.isFinite( _panelH ) === true ? _panelH : DEFAULT_PANEL_HEIGHT;
2588                                 borderSize      = Type.isFinite( _borderSize ) === true ? _borderSize : borderSize;
2589                                 
2590                                 delete PANEL_CONTROL.open;
2591                         },
2592                         close: function(){
2593                                 
2594                         },
2595                         resize: function( isResizerTopAction, x, y, w, h ){
2596                                 PANEL_CONTROL.x = x = x !== undefined ? x : PANEL_CONTROL.x;
2597                                 PANEL_CONTROL.y = y = y !== undefined ? y : PANEL_CONTROL.y;
2598                                 PANEL_CONTROL.w = w = w !== undefined ? w : PANEL_CONTROL.w;
2599                                 PANEL_CONTROL.h = h = h !== undefined ? h : PANEL_CONTROL.h;
2600                                 
2601                                 nodePanel.update( x - borderSize, y - borderSize, w, h );
2602
2603                                 
2604                                 resizerTop.onPanelResize( x, y, w, h );
2605                                 resizerBottom.onPanelResize( x, y, w, h );
2606                                 GRID_CONTROL.onPanelResize( x, y );
2607                                 WHITE_GLASS_CONTROL.onPanelResize( x, y, w, h );
2608                                 PANEL_ELEMENT_CONTROL.onPanelResize( x, y, w, h, isResizerTopAction === true );
2609                         },
2610                         resizeElement : function( isResizerTopAction, x, y, w, h ){
2611                                 stylePanel.cssText = [ 'left:',  ( x - borderSize ), 'px;',
2612                                                                          'top:',    ( y - borderSize ), 'px;',
2613                                                                          'width:',  w, 'px;',
2614                                                                          'height:', h, 'px' ].join( '' );
2615                                 // PANEL_RESIZER_TOP.onPanelResize( x, y, w, h );
2616                                 // PANEL_RESIZER_BOTTOM.onPanelResize( x, y, w, h );
2617                                 GRID_CONTROL.onPanelResize( x, y );
2618                                 WHITE_GLASS_CONTROL.onPanelResize( x, y, w, h );
2619                                 PANEL_ELEMENT_CONTROL.onPanelResize( x, y, w, h, isResizerTopAction );                  
2620                         },
2621                         onWindowResize: function( _windowW, _windowH ){
2622                                 PANEL_CONTROL.x = Math.floor( ( _windowW - PANEL_CONTROL.w ) / 2 );
2623                                 PANEL_CONTROL.y = Math.floor( ( _windowH - PANEL_CONTROL.h ) / 2 );
2624                                 PANEL_CONTROL.resize();
2625                         },
2626                         mousemove: function( _mouseX, _mouseY ){
2627                                 if( isDraggable === true && isDragging === true ){
2628                                         PANEL_CONTROL.resize( false, startX + _mouseX - offsetX, startY + _mouseY - offsetY );
2629                                 };
2630                         },
2631                         mouseup: function( _mouseX, _mouseY ){
2632                                 if( isDraggable === true ){
2633                                         isDragging = false;
2634                                         app.updateCoursor( '' );
2635                                 };
2636                         },
2637                         mousedown: function( _mouseX, _mouseY ){
2638                                 if( isDraggable === true ){
2639                                         offsetX    = _mouseX;
2640                                         offsetY    = _mouseY;
2641                                         startX     = PANEL_CONTROL.x;
2642                                         startY     = PANEL_CONTROL.y;
2643                                         isDragging = true;
2644                                         app.updateCoursor( 'move' );
2645                                         return true;
2646                                 };
2647                         },
2648                         busy: function(){
2649                                 return isDragging === true;
2650                         }                               
2651                 };
2652         })();
2653
2654 /* --------------------------------------------------------------------------------------------
2655  * TailOperator
2656  *  - panelElementOperator
2657  */
2658         var TailOperator = ( function(){
2659                 var     styleMover,
2660                         SIZE,
2661                         SIN          = Math.sin,
2662                         COS          = Math.cos,
2663                         ATAN         = Math.atan,
2664                         FLOOR        = Math.floor,
2665                         DEG_TO_RAD   = Math.PI / 180,
2666                         RAD_TO_DEG   = 1 / DEG_TO_RAD,
2667                         currentText  = null,
2668                         tailX, tailY,
2669                         w, h,
2670                         balloonW, balloonH, balloonA, radA,
2671                         visible = false,
2672                         startA;
2673                 
2674                 return {
2675                         init: function(){
2676                                 var elm    = document.getElementById( 'balloon-tail-mover' );
2677                                 SIZE       = Util.getElementSize( elm ).width;
2678                                 styleMover = elm.style;
2679                                 delete TailOperator.init;
2680                         },
2681                         update: function ( _w, _h, _a ){
2682                                 balloonW = _w !== undefined ? _w : balloonW;
2683                                 balloonH = _h !== undefined ? _h : balloonH;
2684                                 balloonA = _a !== undefined ? _a : balloonA;
2685                                 radA = ( balloonA - 90 ) * DEG_TO_RAD;
2686                                 tailX = FLOOR( ( ( COS( radA ) / 2 + 0.5 ) * ( balloonW + SIZE )) - SIZE / 2 );
2687                                 tailY = FLOOR( ( ( SIN( radA ) / 2 + 0.5 ) * ( balloonH + SIZE )) - SIZE / 2 );
2688                                 styleMover.left = tailX + 'px';
2689                                 styleMover.top  = tailY + 'px';
2690                         },
2691                         show: function( _currentText ){
2692                                 /**
2693                                  * visibilityのほうがいい, display:none だと ie で描画が狂う
2694                                  */
2695                                 styleMover.visibility = '';
2696                                 currentText = _currentText;
2697                                 TailOperator.update( _currentText.w, _currentText.h, _currentText.a );
2698                         },
2699                         hide: function(){
2700                                 styleMover.visibility = 'hidden';
2701                                 currentText = null;
2702                         },                      
2703                         hitTest: function( _mouseX, _mouseY ){
2704                                 var _x  = tailX -SIZE / 2,
2705                                         _y  = tailY -SIZE / 2,
2706                                         ret = _x <= _mouseX && _y <= _mouseY && _x + SIZE >= _mouseX && _y + SIZE >= _mouseY;
2707                                 ret === true && app.updateCoursor( 'move' );
2708                                 return ret;
2709                         },
2710                         onStart: function( _mouseX, _mouseY ){
2711                                 if( currentText.type !== PANEL_ELEMENT_TYPE_TEXT ) return false;
2712                                 if( TailOperator.hitTest( _mouseX, _mouseY ) === true ){
2713                                         w = currentText.w;
2714                                         h = currentText.h;
2715                                         startA = currentText.a;
2716                                         return true;
2717                                 };
2718                                 return false;
2719                         },
2720                         onDrag: function( _mouseX, _mouseY ){
2721                                 _mouseX = _mouseX - w / 2;
2722                                 _mouseY = _mouseY - h / 2; //Balloonの中心を0,0とする座標系に変換
2723                                 TailOperator.update( w, h,
2724                                         _mouseX !== 0 ?
2725                                                 ATAN( _mouseY / _mouseX ) * RAD_TO_DEG + ( _mouseX > 0 ? 90 : 270 ) :
2726                                                 _mouseY > 0 ? 180 : 0
2727                                 );
2728                                 currentText && currentText.angle( FLOOR( balloonA + 0.5 ) );
2729                                 CONSOLE_CONTROLER.update( currentText );
2730                         },
2731                         onFinish: function(){
2732                                 startA !== currentText.a && PanelElementOperatorManager.saveStatus( undefined, undefined, w, h, startA );
2733                                 startA !== currentText.a && PanelElementOperatorManager.resize( undefined, undefined, w, h, currentText.a );
2734                         },
2735                         onCancel: function(){
2736                                 currentText.angle( startA );
2737                                 PanelElementOperatorManager.resize( undefined, undefined, w, h, startA );
2738                         }
2739                 }
2740         })();
2741
2742 /* --------------------------------------------------------------------------------------------
2743  * ResizeOperator
2744  *  - panelElementOperator
2745  */
2746         var ResizeOperator = ( function(){
2747                 var     HIT_AREA        = MOUSE_HIT_AREA,
2748                         POSITION_ARRAY  = [],
2749                         FLOOR           = Math.floor,
2750                         CURSOR_AND_FLIP = [
2751                                 { cursor:       'n-resize',             v: 3 },
2752                                 { cursor:       'e-resize',             h: 2 },
2753                                 { cursor:       'e-resize',             h: 1 },
2754                                 { cursor:       'n-resize',             v: 0 },
2755                                 { cursor:       'nw-resize',    h: 5, v: 6, vh: 7 },
2756                                 { cursor:       'ne-resize',    h: 4, v: 7, vh: 6 },
2757                                 { cursor:       'ne-resize',    h: 7, v: 4, vh: 5 },
2758                                 { cursor:       'nw-resize',    h: 6, v: 5, vh: 4 }
2759                         ],
2760                         styleInner,
2761                         elmInner,
2762                         styleResizerTop,
2763                         styleResizerLeft,
2764                         styleResizerRight,
2765                         styleResizerBottom,
2766                         x, y, w, h,
2767                         currentIndex = -1,
2768                         currentElement,
2769                         isSpeach = false;
2770                 
2771                 var RESIZE_WORK_ARRAY = [
2772                                 { x:    0, w:    0, y:  1, h:   -1}, //top
2773                                 { x:    1, w:   -1, y:  0, h:    0}, //left
2774                                 { x:    0, w:    1, y:  0, h:    0}, //right
2775                                 { x:    0, w:    0, y:  0, h:    1}, //bottom
2776                                 { x:    1, w:   -1, y:  1, h:   -1}, //top-left
2777                                 { x:    0, w:    1, y:  1, h:   -1}, //top-right
2778                                 { x:    1, w:   -1, y:  0, h:    1}, //bottom-left
2779                                 { x:    0, w:    1, y:  0, h:    1}  //bottom-right
2780                         ],
2781                         startX, startY, startW, startH, startFilpV, startFilpH, startAspect,
2782                         baseX, baseY, baseW, baseH,
2783                         currentX, currentY, currentW, currentH,
2784                         offsetX, offsetY,
2785                         error = 0;
2786                 
2787                 function update( _x, _y, _w, _h ){
2788                         var __w, __h;
2789                         _x = _x !== undefined ? _x : currentX;
2790                         _y = _y !== undefined ? _y : currentY;
2791                         _w = _w !== undefined ? _w : currentW;
2792                         _h = _h !== undefined ? _h : currentH;
2793                         
2794                         if( isSpeach === false && currentIndex > 3 && app.shiftEnabled() === true ){
2795                                 if( startAspect >= 1 ){
2796                                         __w = _w;
2797                                         _w  = FLOOR( startAspect * _h );
2798                                         _x  = _x + ( currentIndex % 2 === 0 ? __w - _w : 0 );
2799                                 } else {
2800                                         __h = _h;
2801                                         _h  = FLOOR( _w / startAspect );
2802                                         _y  = _y + ( currentIndex <= 5 ? __h - _h : 0 );
2803                                 };
2804                         };
2805                         ResizeOperator.update( x = _x, y = _y, w = _w, h = _h );
2806                         currentElement.resize( _x, _y, _w, _h );
2807                         isSpeach === true && TailOperator.update( _w, _h );
2808                         CONSOLE_CONTROLER.show( currentElement, _w, _h );
2809                         // CONSOLE_CONTROLER.update( currentElement );
2810                 };
2811                 
2812                 function flip( _flipH, _flipV ){
2813                         var p = CURSOR_AND_FLIP[ currentIndex ];
2814                         currentIndex = _flipH === true || _flipV === true ? p[
2815                                         _flipH === true && _flipV === true ? 'vh' : ( _flipH === true ? 'h' : 'v' )
2816                                 ] : currentIndex;
2817                         app.updateCoursor( CURSOR_AND_FLIP[ currentIndex ].cursor );
2818                         elmInner.className = 'current-resizer-is-' + currentIndex;
2819                         currentElement.flip( _flipH, _flipV );
2820                 };
2821                 return {
2822                         init: function(){
2823                                 elmInner           = document.getElementById( 'comic-element-resizer-container-inner' );
2824                                 styleInner         = elmInner.style;
2825                                 
2826                                 styleResizerTop    = document.getElementById( 'comic-element-resizer-top' ).style;
2827                                 styleResizerLeft   = document.getElementById( 'comic-element-resizer-left' ).style;
2828                                 styleResizerRight  = document.getElementById( 'comic-element-resizer-right' ).style;
2829                                 styleResizerBottom = document.getElementById( 'comic-element-resizer-bottom' ).style;
2830                                 
2831                                 delete ResizeOperator.init;
2832                         },
2833                         update: function( _x, _y, _w, _h ){
2834                                 x = _x = _x !== undefined ? _x : x;
2835                                 y = _y = _y !== undefined ? _y : y;
2836                                 w = _w = _w !== undefined ? _w : w;
2837                                 h = _h = _h !== undefined ? _h : h;
2838                                 
2839                                 PanelElementOperatorManager.resizeElement( _x, _y, _w, _h );
2840                                 
2841                                 styleInner.width  = _w + 'px';
2842                                 styleInner.height = _h + 'px';
2843                                 
2844                                 styleResizerTop.left = styleResizerBottom.left = FLOOR( _w / 2 - 5 ) + 'px';
2845                                 styleResizerLeft.top = styleResizerRight.top   = FLOOR( _h / 2 - 5 ) + 'px';
2846         
2847                                 POSITION_ARRAY.length = 0;
2848                                 POSITION_ARRAY.push(
2849                                         {x:     5,                              y:      -HIT_AREA,              w:      _w - 5,                 h:      HIT_AREA * 2 }, // top
2850                                         {x: -HIT_AREA,          y:      5,                              w:      HIT_AREA * 2,   h:      _h - 5 },   // left
2851                                         {x: _w - 5,                     y:      HIT_AREA + 5,   w:      HIT_AREA * 2,   h:      _h - 5 },   // right
2852                                         {x:     5,                              y:      _h - 5,                 w:      _w - 5,                 h:      HIT_AREA * 2 }, // bottom
2853                                         {x:     -HIT_AREA,              y:      -HIT_AREA,              w:      HIT_AREA + 5,   h:      HIT_AREA + 5}, // top left
2854                                         {x: _w - 5,                     y:      -HIT_AREA,              w:      HIT_AREA + 5,   h:      HIT_AREA + 5}, // top right
2855                                         {x:     -HIT_AREA,              y:      _h - 5,                 w:      HIT_AREA + 5,   h:      HIT_AREA + 5}, // bottom left
2856                                         {x:     _w - 5,                 y:      _h - 5,                 w:      HIT_AREA + 5,   h:      HIT_AREA + 5}  // bottom right
2857                                 );
2858                         },
2859                         index: function( _mouseX, _mouseY ){
2860                                 var     p, i;
2861                                 for( i = 4; i < 8; ++i ){
2862                                         p = POSITION_ARRAY[ i ];
2863                                         if( p.x <= _mouseX && p.y <= _mouseY && p.x + p.w >= _mouseX && p.y + p.h >= _mouseY ){
2864                                                 app.updateCoursor( CURSOR_AND_FLIP[ i ].cursor );
2865                                                 elmInner.className  = 'current-resizer-is-' + i;
2866                                                 return currentIndex = i;
2867                                         };
2868                                 };
2869                                 for( i = 0; i < 4; ++i ){
2870                                         p = POSITION_ARRAY[ i ];
2871                                         if( p.x <= _mouseX && _mouseX <= p.x + p.w && p.y <= _mouseY && _mouseY <= p.y + p.h ){
2872                                                 app.updateCoursor( CURSOR_AND_FLIP[ i ].cursor );
2873                                                 elmInner.className  = 'current-resizer-is-' + i;
2874                                                 return currentIndex = i;
2875                                         };
2876                                 };
2877                                 app.updateCoursor( '' );
2878                                 elmInner.className = '';
2879                                 return -1;
2880                         },
2881                         show: function( _currentElement ){
2882                                 currentElement = _currentElement;
2883                                 isSpeach = _currentElement.type === PANEL_ELEMENT_TYPE_TEXT;
2884                                 ResizeOperator.update( _currentElement.x, _currentElement.y, _currentElement.w, _currentElement.h );
2885                         },
2886                         hide: function(){
2887                                 currentElement = null;
2888                         },
2889                         onStart: function( _mouseX, _mouseY ){
2890                                 isSpeach = currentElement.type === PANEL_ELEMENT_TYPE_TEXT;
2891                                 if( currentElement.keepSize === true ) return false;
2892                                 currentIndex = this.index( _mouseX, _mouseY );
2893                                 if( currentIndex === -1 ) return false;
2894                                 offsetX = _mouseX;
2895                                 offsetY = _mouseY;
2896                                 startX = baseX = currentElement.x;
2897                                 startY = baseY = currentElement.y;
2898                                 startW = baseW = currentElement.w;
2899                                 startH = baseH = currentElement.h;
2900                                 startFilpV = currentElement.flipV;
2901                                 startFilpH = currentElement.flipH;
2902                                 startAspect = startW / startH;
2903                                 return true;
2904                         },
2905                         onDrag: function( _mouseX, _mouseY ){
2906                                 var com      = RESIZE_WORK_ARRAY[ currentIndex ],
2907                                         moveX    = _mouseX - offsetX,
2908                                         moveY    = _mouseY - offsetY,
2909                                         _updated = moveX !== 0 || moveY !== 0,
2910                                         _x, _y, _w, _h,
2911                                         _thisError = 0;
2912                                         
2913                                 var _memoryX = 0,
2914                                         _memoryY = 0;
2915                                 /*
2916                                  * Opera 11+ often forget values, why ??
2917                                  */
2918                                 while( _x === undefined || _y === undefined || _w === undefined || _h === undefined ){
2919                                         _x = _x !== undefined ? _x : baseX + moveX * com.x;
2920                                         _y = _y !== undefined ? _y : baseY + moveY * com.y;
2921                                         _w = _w !== undefined ? _w : baseW + moveX * com.w;
2922                                         _h = _h !== undefined ? _h : baseH + moveY * com.h;
2923                                         error += _thisError === 0 ? 0 : 1;
2924                                         ++_thisError;
2925                                         if( _thisError > 9999 ){
2926                                                 ++error
2927                                                 //alert( 'opera error' +error);
2928                                                 this.onCancel();
2929                                                 return;
2930                                         };
2931                                 };
2932                                 
2933                                 if( _w >= MIN_ELEMENT_SIZE && _h >= MIN_ELEMENT_SIZE ){
2934                                         
2935                                 } else 
2936                                 if( _w >= -MIN_ELEMENT_SIZE && _h >= -MIN_ELEMENT_SIZE ){
2937                                         //return;
2938                                         if( _w < MIN_ELEMENT_SIZE){
2939                                                 //_x += Math.abs( MIN_ELEMENT_SIZE -_w);
2940                                                 _x = currentX;
2941                                                 _w = MIN_ELEMENT_SIZE;
2942                                         }
2943                                         if( _h < MIN_ELEMENT_SIZE){
2944                                                 //_y += Math.abs( MIN_ELEMENT_SIZE -_h);
2945                                                 _y = currentY;
2946                                                 _h = MIN_ELEMENT_SIZE;
2947                                         }
2948                                 } else 
2949                                 if( currentElement.type === PANEL_ELEMENT_TYPE_TEXT ){
2950                                         return;
2951                                 } else 
2952                                 if( _w < -MIN_ELEMENT_SIZE || _h < -MIN_ELEMENT_SIZE ){
2953
2954                                         if( _w < -MIN_ELEMENT_SIZE && _h > MIN_ELEMENT_SIZE ){
2955                                         // flipH
2956                                                 _memoryX = _x;
2957                                                 baseX    = _x = _x +_w;
2958                                                 baseY    = _y;
2959                                                 baseW    = _w = _memoryX -_x;
2960                                                 baseH    = _h;
2961                                                 flip( true, false );
2962                                                 flipV    = currentElement.flipV;
2963                                         } else 
2964                                         if( _w > MIN_ELEMENT_SIZE && _h < -MIN_ELEMENT_SIZE ){
2965                                         // flipV
2966                                                 _memoryY = _y;
2967                                                 baseX    = _x;
2968                                                 baseY    = _y = _y +_h;
2969                                                 baseW    = _w;
2970                                                 baseH    = _h = _memoryY -_y;
2971                                                 flip( false, true);
2972                                                 flipH    = currentElement.flipH;
2973                                         } else {
2974                                         // flipVH
2975                                                 _memoryX = _x;
2976                                                 _memoryY = _y;
2977                                                 baseX    = _x = _x + _w;
2978                                                 baseY    = _y = _y + _h;
2979                                                 baseW    = _w = _memoryX - _x;
2980                                                 baseH    = _h = _memoryY - _y;
2981                                                 flip( true, true );
2982                                                 flipV    = currentElement.flipV;
2983                                                 flipH    = currentElement.flipH;
2984                                         };
2985                                         _updated = true;
2986                                         offsetX  = _mouseX;
2987                                         offsetY  = _mouseY;     
2988                                 };
2989                                 currentX = _x;
2990                                 currentY = _y;
2991                                 currentW = _w;
2992                                 currentH = _h;
2993                                 _updated === true && update( _x, _y, _w, _h );
2994                                 /*
2995                                 log.html( [
2996                                                 'currentIndex:', currentIndex, 
2997                                                 'baseW', baseW, 'baseH', baseH,'<br>',
2998                                                 'mouse', _mouseX, _mouseY,'<br>',
2999                                                 'move', moveX, moveY,'<br>',
3000                                                 'xy', _x, _y, 'wh',_w, _h,'<br>',
3001                                                 'com.w', com.w, 'com.h', com.h,'<br>',
3002                                                 'current',currentW, currentH,'<br>',
3003                                                 'result', y, h,
3004                                                 'err', error
3005                                 ].join( ' , ')); */
3006                         },
3007                         onFinish: function(){
3008                                 app.updateCoursor( '' );
3009                                 if( w === startW && h === startH && x === startX && y === startY ) return;
3010                                 PanelElementOperatorManager.resize( x, y, w, h );
3011                                 currentElement.resize( x, y, w, h );
3012                                 PanelElementOperatorManager.saveStatus( startX, startY, startW, startH, undefined, startFilpV, startFilpH );
3013                         },
3014                         onCancel: function(){
3015                                 app.updateCoursor( '' );
3016                                 PanelElementOperatorManager.resize( startX, startY, startW, startH );
3017                                 currentElement.type === PANEL_ELEMENT_TYPE_IMAGE ?
3018                                         currentElement.animate( startX, startY, startW, startH, startFilpV, startFilpH ) :
3019                                         currentElement.animate( startX, startY, startW, startH, angle );
3020                         },
3021                         onShiftUpdate: update,
3022                         onCtrlUpdate: update
3023                 }
3024         })();
3025
3026 /* --------------------------------------------------------------------------------------------
3027  * PositionOperator
3028  *  - panelElementOperator
3029  */
3030         var PositionOperator = ( function(){
3031                 var HIT_AREA = MOUSE_HIT_AREA,
3032                         currentElement,
3033                         startX, startY,
3034                         x, y,
3035                         offsetX, offsetY,
3036                         isCopy = false;
3037                 function update( _x, _y ){
3038                         x = _x !== undefined ? _x : x;
3039                         y = _y !== undefined ? _y : y;
3040                         // ResizeOperator.update( x, y );
3041                         
3042                         PanelElementOperatorManager.resizeElement( x, y );
3043                         
3044                         currentElement.resize( x, y );
3045                         // CONSOLE_CONTROLER.update( currentElement );
3046                 };
3047                 return {
3048                         init: function(){
3049                                 delete PositionOperator.init;
3050                         },
3051                         show : function( _currentElement ){
3052                                 currentElement = _currentElement;
3053                         },
3054                         hide : function(){
3055                                 currentElement = null;
3056                         },
3057                         onStart: function( _mouseX, _mouseY ){
3058                                 offsetX = _mouseX;
3059                                 offsetY = _mouseY;
3060                                 startX  = x = currentElement.x;
3061                                 startY  = y = currentElement.y;
3062                                 app.updateCoursor( 'move' );
3063                         },
3064                         onDrag: function( _mouseX, _mouseY ){
3065                                 var moveX = _mouseX - offsetX,
3066                                         moveY = _mouseY - offsetY,
3067                                         _x    = startX + moveX,
3068                                         _y    = startY + moveY;
3069                                 if( GRID_CONTROL.enabled() === true ){
3070                                         _x = Math.floor( _x / 10 ) * 10;
3071                                         _y = Math.floor( _y / 10 ) * 10;
3072                                 };
3073                                 update( _x, _y );
3074                         },
3075                         onFinish: function(){
3076                                 app.updateCoursor( '' );
3077                                 if( x === startX && y === startY ) return;
3078                                 PanelElementOperatorManager.resize( x, y );
3079                                 currentElement.resize( x, y );
3080                                 PanelElementOperatorManager.saveStatus( startX, startY );
3081                         },
3082                         onCancel: function(){
3083                                 app.updateCoursor( '' );
3084                                 PanelElementOperatorManager.resize( startX, startY );
3085                                 currentElement.animate( startX, startY );
3086                         },
3087                         onShiftUpdate: update,
3088                         onCtrlUpdate: update
3089                 };
3090         })();
3091
3092
3093 /* --------------------------------------------------------------------------------------------
3094  * PanelElementOperatorManager
3095  */
3096         var PanelElementOperatorManager = ( function(){
3097                 var     HIT_AREA        = MOUSE_HIT_AREA,
3098                         isSpeach        = false,
3099                         currentOperator = null,
3100                         currentElement  = null,
3101                         node            = null,
3102                         styleContainer  = null,
3103                         currentX, currentY, currentW, currentH, angle, flipV, flipH;
3104                 
3105                 function mousedown( e ){
3106                         var x = e.layerX - HIT_AREA / 2 - 5,
3107                                 y = e.layerY - HIT_AREA / 2 - 5;
3108                         if( isSpeach === true && TailOperator.onStart( x, y ) === true ){
3109                                 currentOperator = TailOperator;
3110                         } else
3111                         if( ResizeOperator.onStart( x, y ) === true ){
3112                                 currentOperator = ResizeOperator;
3113                         } else {
3114                                 PositionOperator.onStart( x, y );
3115                                 currentOperator = PositionOperator;
3116                         };
3117                         // return true;
3118                 };
3119                 function mousemove( e ){
3120                         var x = e.layerX - HIT_AREA / 2 - 5,
3121                                 y = e.layerY - HIT_AREA / 2 - 5;
3122                         if( currentOperator !== null ){
3123                                 currentOperator.onDrag( x, y );
3124                                 return true;
3125                         } else
3126                         if( currentElement !== null ){
3127                                 ( isSpeach === false || TailOperator.hitTest( x, y ) === false ) && ResizeOperator.index( x, y );
3128                         };
3129                 };
3130                 function mouseup( e ){
3131                         currentOperator !== null && currentOperator.onFinish();
3132                         currentOperator = null;
3133                 };
3134                 
3135                 return {
3136                         elm  : null,
3137                         node : null,
3138                         init : function(){
3139                                 TailOperator.init();
3140                                 ResizeOperator.init();
3141                                 PositionOperator.init();
3142                                 CONSOLE_CONTROLER.init();
3143                                 
3144                                 app.addKeyEventListener( 'keychange', function( e ){
3145                                         currentOperator !== null && currentOperator.onShiftUpdate && currentOperator.onShiftUpdate();
3146                                         return false;
3147                                 }, 16 );
3148                                 app.addKeyEventListener( 'keychange', function( e ){
3149                                         currentOperator !== null && currentOperator.onCtrlUpdate && currentOperator.onCtrlUpdate();
3150                                         return false;
3151                                 }, 17 );
3152                                 app.addKeyEventListener( 'keydown', function( e ){
3153                                         currentOperator !== null && currentOperator.onCancel && currentOperator.onCancel();
3154                                         currentOperator = null;
3155                                         return false;
3156                                 }, 27, false, false );
3157                                 
3158                                 delete PanelElementOperatorManager.init;
3159                         },
3160                         open: function(){
3161                                 var elm = document.getElementById( 'comic-element-resizer-container' );
3162                                 PanelElementOperatorManager.elm  = elm;
3163                                 PanelElementOperatorManager.node = eventRoot.createNode( elm, false, false );
3164                                 PanelElementOperatorManager.node.addEventListener( 'mouseout', PanelElementOperatorManager.hide, PanelElementOperatorManager );
3165                                 styleContainer = elm.style;
3166                                 PanelElementOperatorManager.node.disabled( true );
3167                                 PanelElementOperatorManager.node.addEventListener( 'mousemove', mousemove );
3168                                 PanelElementOperatorManager.node.addEventListener( 'mousedown', mousedown );
3169                                 PanelElementOperatorManager.node.addEventListener( 'mouseup', mouseup );
3170                                 
3171                                 CONSOLE_CONTROLER.open();
3172                                 PanelElementOperatorManager.hide();
3173                                 
3174                                 delete PanelElementOperatorManager.open;
3175                         },
3176                         close: function(){
3177                                 
3178                         },
3179                         show : function( _currentElement ){
3180                                 if( currentElement === null ){
3181                                         styleContainer.display = '';
3182                                         PanelElementOperatorManager.node.disabled( false );
3183                                 };
3184                                 if( currentElement !== _currentElement ){
3185                                         currentElement = _currentElement;
3186                                         
3187                                         ResizeOperator.show( _currentElement );
3188                                         PositionOperator.show( _currentElement );
3189                                         
3190                                         isSpeach = ( _currentElement.type === PANEL_ELEMENT_TYPE_TEXT );
3191                                         isSpeach === true ? TailOperator.show( _currentElement ) : TailOperator.hide();
3192                                         
3193                                         flipV = _currentElement.flipV;
3194                                         flipH = _currentElement.flipH;
3195                                         
3196                                         PanelElementOperatorManager.resize(
3197                                                 _currentElement.x, _currentElement.y, _currentElement.w, _currentElement.h,
3198                                                 isSpeach === true ? _currentElement.a : 0
3199                                         );
3200                                 };
3201                         },
3202                         hide: function(){
3203                                 if( currentElement !== null ){
3204                                         styleContainer.display = 'none';
3205                                         PanelElementOperatorManager.node.disabled( true );
3206                                 };
3207                                 currentElement = null;
3208                                 app.updateCoursor( '' );
3209                                 TailOperator.hide();
3210                                 ResizeOperator.hide();
3211                                 PositionOperator.hide();
3212                                 CONSOLE_CONTROLER.hide();
3213                         },
3214                         resizeElement : function( _x, _y, _w, _h, _angle ){
3215                                 _x = _x !== undefined ? _x : currentX;
3216                                 _y = _y !== undefined ? _y : currentY;
3217                                 _w = _w !== undefined ? _w : currentW;
3218                                 _h = _h !== undefined ? _h : currentH;
3219                                 
3220                                 var style    = PanelElementOperatorManager.elm.style;
3221                                 style.left   = ( PANEL_CONTROL.x + _x - HIT_AREA / 2 - 5 ) + 'px';
3222                                 style.top    = ( PANEL_CONTROL.y + _y - HIT_AREA / 2 - 5 ) + 'px';
3223                                 style.width  = ( _w + HIT_AREA + 5 ) + 'px';
3224                                 style.heught = ( _h + HIT_AREA + 5 ) + 'px';
3225                                 
3226                                 // ResizeOperator.update( _x, _y, _w, _h );
3227                                 isSpeach === true && TailOperator.update( _w, _h, angle );
3228                                 CONSOLE_CONTROLER.show( currentElement, _w, _h );
3229                         },
3230                         resize: function( _x, _y, _w, _h, _angle ){
3231                                 currentX = _x = _x !== undefined ? _x : currentX;
3232                                 currentY = _y = _y !== undefined ? _y : currentY;
3233                                 currentW = _w = _w !== undefined ? _w : currentW;
3234                                 currentH = _h = _h !== undefined ? _h : currentH;
3235                                 angle = _angle = _angle !== undefined ? _angle : angle;
3236
3237                                 // ResizeOperator.update( _x, _y, _w, _h );
3238                                 isSpeach === true && TailOperator.update( _w, _h, angle );
3239                                 CONSOLE_CONTROLER.show( currentElement, _w, _h );
3240                                 //CONSOLE_CONTROLER.update( currentElement );
3241                                 PanelElementOperatorManager.node.update( PANEL_CONTROL.x + _x - HIT_AREA / 2 - 5, PANEL_CONTROL.y + _y - HIT_AREA / 2 - 5, _w + HIT_AREA + 5, _h + HIT_AREA + 5 );
3242                         },
3243                         /* history */
3244                         restoreState: function( _currentElement, _x, _y, _w, _h, _a, _flipV, _flipH ){
3245                                 if( arguments.length !== 8 ) return;
3246                                 if( !_currentElement && !currentOperator ) return;
3247                                 _currentElement.type === PANEL_ELEMENT_TYPE_IMAGE ?
3248                                         _currentElement.animate( _x, _y, _w, _h, _flipV, _flipH ) :
3249                                         _currentElement.animate( _x, _y, _w, _h, _a );
3250                                 currentOperator !== null && currentOperator.onCancel && currentOperator.onCancel();
3251                                 currentOperator = null;
3252                                 currentElement === _currentElement ? PanelElementOperatorManager.resize( _x, _y, _w, _h, _a ) : PanelElementOperatorManager.show( _currentElement );
3253                         },
3254                         saveStatus: function( startX, startY, startW, startH, startA, startFilpV, startFilpH ){
3255                                 startX = startX !== undefined ? startX : currentX;
3256                                 startY = startY !== undefined ? startY : currentY;
3257                                 startW = startW !== undefined ? startW : currentW;
3258                                 startH = startH !== undefined ? startH : currentH;
3259                                 startA = startA !== undefined ? startA : angle;
3260                                 startFilpV = startFilpV !== undefined ? startFilpV : flipV;
3261                                 startFilpH = startFilpH !== undefined ? startFilpH : flipH;
3262                                 currentElement && HISTORY_CONTROL.saveState( PanelElementOperatorManager.restoreState,
3263                                         [ currentElement, startX, startY, startW, startH, startA, startFilpV, startFilpH ],
3264                                         [ currentElement, currentX, currentY, currentW, currentH, angle, flipV, flipH ]
3265                                 );
3266                         }
3267                 };
3268         })();
3269         /*
3270          *  // PanelElementOperatorManager
3271          */
3272
3273 /* --------------------------------------------------------------------------------------------
3274  * CONSOLE_CONTROLER
3275  */
3276         var CONSOLE_CONTROLER = ( function(){
3277                 var LAYER_BACK_BUTTON, LAYER_FORWARD_BUTTON, DELETE_BUTTON, EDIT_BUTTON, CHANGE_BUTTON,
3278                         PUSH_OUT_RATIO  = 0.5,
3279                         tailSize        = 10,
3280                         elmRoot, elmContainer, elmPushout, elmTail,
3281                         pushoutW        = 0,
3282                         pushoutH        = 0,
3283                         pushout         = false,
3284                         currentType     = -1,
3285                         visible         = false,
3286                         node            = null,
3287                         ui, inputX, inputY, inputZ, inputA, inputW, inputH, inputAspectRatio,
3288                         buttonBack, buttonForward, buttonDel, buttonEdit, butonChange;
3289                         
3290                 function layerBack(){
3291                         if( currentElement === null ) return;
3292                         if( PANEL_ELEMENT_CONTROL.replace( currentElement, false ) === false ) return;
3293                         CONSOLE_CONTROLER.update( currentElement );
3294                         HISTORY_CONTROL.saveState( PANEL_ELEMENT_CONTROL.replace, [ currentElement, true ], [ currentElement, false ] );
3295                         var _z = currentElement.z;
3296                         LAYER_BACK_BUTTON.visible( _z > 0 );
3297                         LAYER_FORWARD_BUTTON.visible( _z < PANEL_ELEMENT_ARRAY.length - 1 );
3298                 };
3299                 function layerForward(){
3300                         if( currentElement === null ) return;
3301                         if( PANEL_ELEMENT_CONTROL.replace( currentElement, true ) === false ) return;
3302                         CONSOLE_CONTROLER.update( currentElement );
3303                         HISTORY_CONTROL.saveState( PANEL_ELEMENT_CONTROL.replace, [ currentElement, false ], [ currentElement, true ] );
3304                         var _z = currentElement.z;
3305                         LAYER_BACK_BUTTON.visible( _z > 0 );
3306                         LAYER_FORWARD_BUTTON.visible( _z < PANEL_ELEMENT_ARRAY.length - 1 );
3307                 };
3308                 function del(){
3309                         if( currentElement === null ) return;
3310                         HISTORY_CONTROL.saveState( PANEL_ELEMENT_CONTROL.restore, [ true, currentElement ], [ false, currentElement ], false ); // true
3311                         PANEL_ELEMENT_CONTROL.remove( currentElement );
3312                         PanelElementOperatorManager.hide();
3313                 };
3314                 function edit(){
3315                         if( currentElement === null || currentElement.type !== PANEL_ELEMENT_TYPE_TEXT ) return;
3316                         TextEditor.boot( PANEL_CONTROL.x, PANEL_CONTROL.y, currentElement );
3317                 };
3318                 function change(){
3319                         if( currentElement === null ) return;
3320                         PremiumSatge.boot( currentElement.artistID, currentElement.resourcePicture, currentElement );
3321                 };
3322
3323                 return {
3324                         x: 0,
3325                         y: 0,
3326                         w: 0,
3327                         h: 0,
3328                         init: function(){
3329                                 app.addKeyEventListener( 'keydown', layerBack, 66, false, true );
3330                                 app.addKeyEventListener( 'keydown', layerForward, 70, false, true );
3331                                 app.addKeyEventListener( 'keydown', del, 68, false, true );
3332                                 app.addKeyEventListener( 'keydown', edit, 69, false, true );
3333                                 app.addKeyEventListener( 'keydown', change, 85, false, true );
3334                                 
3335                                 elmContainer = document.getElementById( 'comic-element-consol-container' );
3336                                 elmRoot      = elmContainer.parentNode;
3337                                 elmPushout   = document.getElementById( 'comic-element-consol-pushout-wrapper' );
3338                                 elmTail      = document.getElementById( 'comic-element-consol-pushout-tail' );
3339                                 delete CONSOLE_CONTROLER.init;
3340                         },
3341                         open: function(){
3342                                 LAYER_BACK_BUTTON    = MENU_BAR_CONTROL.EDIT.createOption( 'layer back', 'ctrl + B', layerBack, false, true, false );
3343                                 LAYER_FORWARD_BUTTON = MENU_BAR_CONTROL.EDIT.createOption( 'layer forward', 'ctrl + F', layerForward, false, false, false );
3344                                 DELETE_BUTTON        = MENU_BAR_CONTROL.EDIT.createOption( 'delete', 'ctrl + D', del, false, true, true );
3345                                 EDIT_BUTTON          = MENU_BAR_CONTROL.EDIT.createOption( 'Edit Text', 'ctrl + E', edit, false, true, false );
3346                                 CHANGE_BUTTON        = MENU_BAR_CONTROL.EDIT.createOption( 'change', 'ctrl + U', change, false, false, true );
3347                                 // inputAspectRatio = $( '#comic-element-keep-aspect' );
3348                                 
3349                                 delete CONSOLE_CONTROLER.open;
3350                         },
3351                         onMouseover : function( e ){
3352                                 node.mesureChildren();
3353                                 //node.mesure();
3354                         },
3355                         show: function( _currentElement, w, h ){
3356                                 if( node === null ){
3357                                         node = CONSOLE_CONTROLER.node = PanelElementOperatorManager.node.createNode( elmContainer, false, false, 'comic-element-consol-container-hover' );
3358                                         node.addEventListener( 'mouseover', CONSOLE_CONTROLER.onMouseover, CONSOLE_CONTROLER );
3359                                         ui            = app.createUIGroup( node );
3360                                         inputX        = ui.createInputText( document.getElementById( 'comic-element-x' ), null );
3361                                         inputY        = ui.createInputText( document.getElementById( 'comic-element-y' ), null );
3362                                         inputZ        = ui.createInputText( document.getElementById( 'comic-element-z' ), null );
3363                                         inputA        = ui.createInputText( document.getElementById( 'comic-element-a' ), null );
3364                                         inputW        = ui.createInputText( document.getElementById( 'comic-element-w' ), null );
3365                                         inputH        = ui.createInputText( document.getElementById( 'comic-element-h' ), null );
3366                                         inputPercentW = ui.createInputText( document.getElementById( 'comic-element-w-percent' ), null );
3367                                         inputPercentH = ui.createInputText( document.getElementById( 'comic-element-h-percent' ), null );
3368                                         butonChange   = ui.createButton( document.getElementById( 'change-image-button' ), change ),
3369                                         buttonBack    = ui.createButton( document.getElementById( 'layer-back-button' ), layerBack ),
3370                                         buttonDel     = ui.createButton( document.getElementById( 'delete-button' ), del ),
3371                                         buttonForward = ui.createButton( document.getElementById( 'layer-forward-button' ), layerForward ),
3372                                         buttonEdit    = ui.createButton( document.getElementById( 'edit-text-button' ), edit );
3373                                 };
3374                                 
3375                                 currentElement = _currentElement;
3376                                 
3377                                 var type = currentElement.type,
3378                                         z    = currentElement.z;
3379                                 
3380                                 LAYER_BACK_BUTTON.visible( z > 0 );
3381                                 buttonBack.enabled( z > 0 );
3382                                 LAYER_FORWARD_BUTTON.visible( z < PANEL_ELEMENT_ARRAY.length - 1 );
3383                                 buttonForward.enabled( z < PANEL_ELEMENT_ARRAY.length - 1 )
3384                                 DELETE_BUTTON.visible( true );
3385                                 EDIT_BUTTON.visible( type === PANEL_ELEMENT_TYPE_TEXT );
3386                                 CHANGE_BUTTON.visible( false );
3387                                 
3388                                 //CONSOLE_CONTROLER.x = Math.floor( ( _w - CONSOLE_CONTROLER.w ) / 2 );
3389                                 CONSOLE_CONTROLER.w = w;
3390                                 CONSOLE_CONTROLER.h = elmContainer.offsetHeight;
3391                                 CONSOLE_CONTROLER.y = h - CONSOLE_CONTROLER.h;
3392                                 
3393                                 if( h * PUSH_OUT_RATIO < CONSOLE_CONTROLER.h ){
3394                                         if( pushout === false ){
3395                                                 pushout = true;
3396                                                 elmPushout.lastChild.appendChild( elmContainer );
3397                                                 elmPushout.style.display = 'block';
3398                                                 pushoutW = elmPushout.offsetWidth;
3399                                                 pushoutH = elmPushout.offsetHeight;
3400                                                 elmTail.style.top = ( pushoutH / 2 - tailSize / 2 ) + 'px';
3401                                         };
3402                                         elmPushout.style.left = ( -pushoutW ) + 'px';
3403                                         elmPushout.style.top  = ( h / 2 - pushoutH / 2 ) + 'px';
3404                                         elmPushout.className  = 'satellite-left';
3405                                 } else
3406                                 if( pushout === true ){
3407                                         pushout = false;
3408                                         elmRoot.insertBefore( elmContainer, elmRoot.firstChild );
3409                                         elmPushout.style.cssText = '';
3410                                 };
3411                                 
3412                                 CONSOLE_CONTROLER.update( currentElement );
3413                                 ui.visible( true );
3414                                 node.mesure();
3415                         },
3416                         update : function( _currentElement ){
3417                                 if( _currentElement === null ){
3418                                         visible = false;
3419                                         return;
3420                                 };
3421                                 currentElement = _currentElement;
3422                                 var type = currentElement.type,
3423                                         x    = currentElement.x,
3424                                         y    = currentElement.y,
3425                                         z    = currentElement.z,
3426                                         a    = type === PANEL_ELEMENT_TYPE_TEXT ? Math.floor( currentElement.a ) : 0,
3427                                         w    = currentElement.w,
3428                                         h    = currentElement.h,
3429                                         actualW    = type === PANEL_ELEMENT_TYPE_IMAGE ? currentElement.actualW : 1,
3430                                         actualH    = type === PANEL_ELEMENT_TYPE_IMAGE ? currentElement.actualH : 1,
3431                                         wPercent   = type === PANEL_ELEMENT_TYPE_IMAGE ? Math.floor( w / actualW * 100 ) : 0,
3432                                         hPercent   = type === PANEL_ELEMENT_TYPE_IMAGE ? Math.floor( h / actualH * 100 ) : 0,
3433                                         keepAspect = currentElement.keepAspect;
3434                                         
3435                                 if( currentType !== type ){
3436                                         if( type === PANEL_ELEMENT_TYPE_TEXT ){
3437                                                 inputA.visible( true );
3438                                                 inputPercentW.visible( false );
3439                                                 inputPercentH.visible( false );
3440                                                 buttonEdit.enabled( true );
3441                                                 //inputAspectRatio.hide();
3442                                         } else {
3443                                                 inputA.visible( false );
3444                                                 inputPercentW.visible( true );
3445                                                 inputPercentH.visible( true );
3446                                                 buttonEdit.enabled( false );
3447                                                 //inputAspectRatio.show();
3448                                         };
3449                                         currentType = type;
3450                                 };
3451
3452                                 inputX.value( x );
3453                                 inputY.value( y );
3454                                 inputZ.value( z );
3455                                 type === 1 && inputA.value( a );
3456                                 inputW.value( w );
3457                                 inputH.value( h );
3458                                 type === 0 && inputPercentW.value( wPercent );
3459                                 type === 0 && inputPercentH.value( hPercent );
3460                         },
3461                         hide: function(){
3462                                 // if( visible === true ) styleConsoleWrapper.display = 'none';
3463                                 ui && ui.visible( false );
3464                                 visible = false;
3465                                 currentElement = null;
3466                                 LAYER_BACK_BUTTON.visible( false );
3467                                 LAYER_FORWARD_BUTTON.visible( false );
3468                                 DELETE_BUTTON.visible( false );
3469                                 EDIT_BUTTON.visible( false );
3470                                 CHANGE_BUTTON.visible( false );
3471                         }
3472                 };
3473         })();
3474
3475
3476
3477         var AbstractPanelElement = function( COMIC_ELM_TYPE ){
3478                 this.type = COMIC_ELM_TYPE;
3479         };
3480         AbstractPanelElement.prototype = {
3481                 $       : null,
3482                 data    : null,
3483                 node    : null,
3484                 x       : 0,
3485                 y       : 0,
3486                 w       : 0,
3487                 h       : 0,
3488                 z       : 0,
3489                 timing  : 0,
3490                 actualW : 0,
3491                 actualH : 0,
3492                 flipV   : 0,
3493                 flipH   : 0,
3494                 shift : function( shiftX, shiftY ){
3495                         this.resize( this.x + shiftX, this.y + shiftY );
3496                 },
3497                 mouseover : function( e ){
3498                         PanelElementOperatorManager.show( this );
3499                 }
3500         };
3501
3502 /* --------------------------------------------------------------------------------------------
3503  * ImageElementClass
3504  */
3505         var     jqImageElementOrigin;
3506         var ImageElementClass = function( data ){
3507                 jqImageElementOrigin = jqImageElementOrigin || $( app.fetchHTMLElement( 'imgElementTemplete' ) );
3508                 
3509                 this.$        = jqImageElementOrigin.clone( true );
3510                 this.data     = data;
3511                 this.z        = data.z;
3512                 this.timing   = data.t || PANEL_ELEMENT_ARRAY.length + 1;
3513                 this.keepSize = false;
3514                 this.flipV    = data.height < 0 ? -1 : 1;
3515                 this.flipH    = data.width  < 0 ? -1 : 1;
3516                 this.rPicture = data.resource_picture;
3517                 this.artistID = this.rPicture.artist_id || this.rPicture.artist.id || -1;
3518                 
3519                 var self = this;
3520                 function animeComplete(){
3521                         self.node.mesure();
3522                         self._flipReversibleImage();
3523                 };
3524                 this.animeComplete = animeComplete;
3525         };
3526         ImageElementClass.prototype = Util.extend(
3527                 new AbstractPanelElement( PANEL_ELEMENT_TYPE_IMAGE ),
3528                 {
3529                         reversibleImage : null,
3530                         init : function(){
3531                                 this._updateResourcePicture( this.rPicture );
3532                                 this.node = PANEL_ELEMENT_CONTROL.node.createNode( this.$.get( 0 ), false, true );
3533                                 this.node.addEventListener( 'mouseover', this.mouseover, this );                                
3534                                 this.resize( this.data.x, this.data.y, Math.abs( this.data.width ), Math.abs( this.data.height ) );
3535                                 this.init = null;
3536                         },
3537                         flip : function( updateH, updateV ){
3538                                 if( updateH !== true && updateV !== true ) return;
3539                                 this.flipH = updateH === true ? -this.flipH : this.flipH;
3540                                 this.flipV = updateV === true ? -this.flipV : this.flipV;
3541                                 this.reversibleImage.resize( this.flipH * this.w, this.flipV * this.h );
3542                         },
3543                         resourcePicture : function( _rPicture ){
3544                                 if( _rPicture && _rPicture !== this.rPicture ){
3545                                         HISTORY_CONTROL.saveState( this._updateResourcePicture, this.rPicture, _rPicture, this );
3546                                         this._updateResourcePicture( _rPicture );
3547                                 };
3548                                 return this.rPicture;
3549                         },
3550                         resize : function( x, y, w, h, animate ){
3551                                 this.x = x = Type.isFinite( x ) === true ? x : this.x;
3552                                 this.y = y = Type.isFinite( y ) === true ? y : this.y;
3553                                 this.w = w = Type.isFinite( w ) === true ? w : this.w;
3554                                 this.h = h = Type.isFinite( h ) === true ? h : this.h;
3555                                 if( animate === true ){
3556                                         this.$.animate( { 
3557                                                 left:   x,
3558                                                 top:    y,
3559                                                 width:  w,
3560                                                 height: h
3561                                         }, 250 , this.animeComplete );                                  
3562                                 } else {
3563                                         this.node.update( x, y, w, h );
3564                                         this._flipReversibleImage();
3565                                 };
3566                         },
3567                         animate : function ( x, y, w, h, flipH, flipV ){
3568                                 this.flipH = flipH !== undefined ? flipH : this.flipH;
3569                                 this.flipV = flipV !== undefined ? flipV : this.flipV;
3570                                 this.resize( x, y, w, h, true );
3571                         },
3572                         destroy : function(){
3573                                 this.reversibleImage.destroy();
3574                                 this.$.stop().remove();
3575                                 this.node.remove();
3576                                 
3577                                 this.destroy = null;
3578                         },
3579                         _updateResourcePicture : function( _rPicture ){
3580                                 this.rPicture = _rPicture;
3581                                 
3582                                 this.actualW = _rPicture.width;
3583                                 this.actualH = _rPicture.height;
3584                                 
3585                                 var _reversibleImage = pettanr.image.createReversibleImage( 
3586                                                 [ pettanr.CONST.RESOURCE_PICTURE_PATH, _rPicture.id, '.', _rPicture.ext ].join(''),
3587                                                 this.flipH * this.w, this.flipV * this.h
3588                                         );
3589                                 if( this.reversibleImage !== null ){
3590                                         this.$.children( this.reversibleImage.elm ).replaceWith( _reversibleImage.elm );
3591                                         this.reversibleImage.destroy();
3592                                 } else {
3593                                         this.$.append( _reversibleImage.elm );
3594                                 };
3595                                 this.reversibleImage = _reversibleImage;
3596                         },
3597                         _flipReversibleImage : function(){
3598                                 this.reversibleImage && this.reversibleImage.resize( this.flipH * this.w, this.flipV * this.h );
3599                         }
3600                 }
3601         );
3602
3603         
3604 /*
3605  * / ImageElementClass
3606  * --------------------------------------------------------------------------------------------
3607  */
3608
3609
3610 /* --------------------------------------------------------------------------------------------
3611  * TextElementClass
3612  * 
3613  * type
3614  * 0.none
3615  * 1.speach balloon
3616  * 2.think
3617  * 3.bom
3618  * 4.black-box( dq style)
3619  * 5.blue-box( ff style)
3620  * 
3621  */
3622         var jqTextElementOrigin;
3623         var TextElementClass = function( data ){
3624                 jqTextElementOrigin = jqTextElementOrigin || ( function(){
3625                         var _OLD_IE = $( app.fetchHTMLElement( 'textElementTempleteForOldIE' ) ),
3626                                 _MODERN = $( app.fetchHTMLElement( 'textElementTemplete' ) );
3627                         return UA.isIE === true && UA.ieRenderingVersion < 8 ? _OLD_IE : _MODERN;
3628                 })();
3629                 
3630                 this.$       = jqTextElementOrigin.clone( true );
3631                 this.data    = data;
3632                 this.elmText = this.$.find( 'td,.speach-inner' ).get( 0 );
3633                 this.type    = data.balloon_template_id;
3634                 this.content = ( function(){
3635                         var _speachs = data.speeches_attributes;
3636                         for( var k in _speachs ){
3637                                 return _speachs[ k ].content || '';
3638                         }
3639                         return '';
3640                 })();
3641                 this.balloon = pettanr.balloon.createBalloon( data.width, data.height, data.tail, this.type );
3642                 this.z       = data.z;
3643                 this.timing  = data.t || PANEL_ELEMENT_ARRAY.length + 1;
3644                 
3645                 this.$.find( 'img' ).eq( 0 ).replaceWith( this.balloon.elm );
3646                 
3647                 var self = this;
3648                 function animeComplete(){
3649                         self.node.mesure();
3650                         self._resizeBalloon();
3651                 };
3652                 this.animeComplete = animeComplete; 
3653         };
3654         TextElementClass.prototype = Util.extend(
3655                 new AbstractPanelElement( PANEL_ELEMENT_TYPE_TEXT ),
3656                 {
3657                         init : function(){
3658                                 this._updateText();
3659                                 this.node = PANEL_ELEMENT_CONTROL.node.createNode( this.$.get( 0 ), false, true );
3660                                 this.node.addEventListener( 'mouseover', this.mouseover, this );                                
3661                                 this.resize( this.data.x, this.data.y, this.data.width, this.data.height, this.data.tail );
3662                                 this.init = null;
3663                         },
3664                         _updateType : function( _type ){
3665                                 if( this.type !== _type ){
3666                                         this.type = _type || this.type;
3667                                         this.balloon.type( this.type );
3668                                 };
3669                         },
3670                         _updateAngle : function( _a ){
3671                                 if( _a !== undefined && a !== _a ){
3672                                         this.a = _a !== undefined ? _a : this.a;
3673                                         this.balloon.angle( this.a );
3674                                 };
3675                         },
3676                         /* history */
3677                         _updateText : function( _text ){
3678                                 this.content = _text || this.content || '';
3679                                 this.elmText.firstChild.data = this.content;
3680                         },
3681                         _resizeBalloon : function(){
3682                                 this.balloon && this.balloon.resize( this.a, this.w, this.h );
3683                         },
3684                         angle : function( _a ){
3685                                 _a !== undefined && this.resize( this.x, this.y, this.w, this.h, _a );
3686                                 return this.a;
3687                         },
3688                         text : function( _text ){
3689                                 if( _text && this.content !== _text ){
3690                                         HISTORY_CONTROL.saveState( this._updateText, this.content || '', _text, this );
3691                                         this._updateText( _text );
3692                                 };
3693                                 return this.content;
3694                         },
3695                         resize : function( x, y, w, h, a, animate ){
3696                                 this.x = x = x !== undefined ? x : this.x;
3697                                 this.y = y = y !== undefined ? y : this.y;
3698                                 this.w = w = w !== undefined ? w : this.w;
3699                                 this.h = h = h !== undefined ? h : this.h;
3700                                 this.a = a !== undefined ? a : this.a;
3701                                 
3702                                 if( animate === true ){
3703                                         this.$.animate( { 
3704                                                 left:   x,
3705                                                 top:    y,
3706                                                 width:  w,
3707                                                 height: h
3708                                         }, 250 , this.animeComplete );                                  
3709                                 } else {
3710                                         this.node.update( x, y, w, h );
3711                                         this._resizeBalloon();
3712                                 };
3713                         },
3714                         animate : function ( _x, _y, _w, _h, _a ){
3715                                 this.resize( _x, _y, _w, _h, _a, true );
3716                         },
3717                         destroy : function(){
3718                                 this.$.stop().remove();
3719                                 this.balloon.destroy();
3720                                 this.node.remove();
3721                                 
3722                                 this.destroy = null;
3723                         }
3724                 }
3725         );
3726
3727 /* --------------------------------------------------------------------------------------------
3728  * PANEL_ELEMENT_CONTROL
3729  *  - mouseEventListener
3730  */
3731         var PANEL_ELEMENT_CONTROL = ( function(){
3732                 var     elmContainer   = null,
3733                         currentElement = null,
3734                         node           = null,
3735                         panelX, panelY, panelW, panelH,
3736                         startX, startY;
3737         /*
3738          * append, remove, replace
3739          * 
3740          * panelElement には、z-position と dom-index がある。
3741          *   z-position は 表示上の位置。大きいほど前に表示される( z-index)
3742          *   dom-index は 意味上の順番。htmlタグの登場順で、検索結果や音声読み上げブラウザで正しく意味が取れる順番。
3743          * 
3744          * editerでは、実際には z-index は使わず、htmlの順序で前後を表現する。
3745          * dom-index は、数値のみ保持して、投稿時にpanelElementを適宜に並び替える。
3746          * 
3747          * append panelElement
3748          * 1. 新しい panelElement の z-position を得る
3749          * 2. z の同じ panelElementを見つけ、その前に加える。または一番先頭へ。(PANEL_ELEMENT_ARRAY)
3750          *    zが大きいほど、PANEL_ELEMENT_ARRAYの先頭へ。
3751          * 3. dom位置は、PANEL_ELEMENT_ARRAY とは反対に、前のものほど後ろへ。
3752          * 
3753          * 
3754          * remove panelElement
3755          * 1. remove
3756          * 2. renumber z
3757          */
3758                 function onFadeOut(){
3759                         this.parentNode.removeChild( this );
3760                 };
3761                 /*
3762                  * PANEL_ELEMENT_ARRAY の順番を基準に、zの再計算
3763                  * jqElmの並び替え。
3764                  */
3765                 function renumber(){
3766                         var _panelElement, jqElm, jqAfter,
3767                                 i = PANEL_ELEMENT_ARRAY.length;
3768                         for( ; i; ){
3769                                 _panelElement = PANEL_ELEMENT_ARRAY[ --i ];
3770                                 jqElm = _panelElement.$;
3771                                 !jqAfter && elmContainer.appendChild( jqElm.get( 0 ) );
3772                                 jqAfter  && jqAfter.before( jqElm );
3773                                 if( phase === 1 ){
3774                                         _panelElement.z = i;
3775                                         _panelElement.node.nodeIndex( i );
3776                                 };
3777                                 jqAfter = jqElm;
3778                         };
3779                 };
3780                 function onTextInput( _panelElement ){
3781                         PANEL_ELEMENT_CONTROL.add( _panelElement );
3782                         _panelElement.animate( undefined, undefined, _panelElement.w, _panelElement.h );
3783                         HISTORY_CONTROL.saveState( PANEL_ELEMENT_CONTROL.restore, [ false, _panelElement ], [ true, _panelElement ], true, PANEL_ELEMENT_CONTROL );
3784                 };
3785                 
3786                 return {
3787                         id : 'PANEL_ELEMENT_CONTROL',
3788                         node : null,
3789                         init : function(){
3790                                 elmContainer = document.getElementById( 'comic-element-container' );
3791                                 node         = PANEL_ELEMENT_CONTROL.node = eventRoot.createNode( elmContainer, true, false );
3792                                 node.nodeIndex( 0 );
3793                                 delete PANEL_ELEMENT_CONTROL.init;
3794                         },
3795                         open : function(){
3796                                 delete PANEL_ELEMENT_CONTROL.open;
3797                         },
3798                         close : function(){
3799                                 var panelElm;
3800                                 while( panelElm = PANEL_ELEMENT_ARRAY.shift() ){
3801                                         panelElm.destroy && panelElm.destroy();
3802                                 };
3803                         },
3804                         add : function( _panelElement ){
3805                                 var z = Type.isFinite( _panelElement.z ) === true ? _panelElement.z : -1,
3806                                         i = PANEL_ELEMENT_ARRAY.length,
3807                                         _jqElm = _panelElement.$.stop().css( {
3808                                                 filter:         '',
3809                                                 opacity:        ''
3810                                         }),
3811                                         j;
3812                                 if( z < 0 ){
3813                                         PANEL_ELEMENT_ARRAY.push( _panelElement );
3814                                 } else {
3815                                         for( ; i; ){
3816                                                 if( PANEL_ELEMENT_ARRAY[ --i ].z < z ){
3817                                                         j = i;
3818                                                         break;
3819                                                 };
3820                                         };
3821                                         if( j !== i ){
3822                                                 PANEL_ELEMENT_ARRAY.unshift( _panelElement );
3823                                         } else {
3824                                                 PANEL_ELEMENT_ARRAY.splice( i + 1, 0, _panelElement );
3825                                         };
3826                                 };
3827                                 renumber();
3828                                 _jqElm.fadeIn();
3829                                 _panelElement.node.disabled( false );   
3830                         },
3831                         remove : function( _panelElement ){
3832                                 for( var i = PANEL_ELEMENT_ARRAY.length; i; ){
3833                                         if( PANEL_ELEMENT_ARRAY[ --i ] === _panelElement ){
3834                                                 console.log( PANEL_ELEMENT_ARRAY.length );
3835                                                 PANEL_ELEMENT_ARRAY.splice( i, 1 );
3836                                                 _panelElement.node.disabled( true );
3837                                                 _panelElement.node.nodeIndex( PANEL_ELEMENT_ARRAY.length );
3838                                                 renumber();
3839                                                 _panelElement.$.stop().css( {
3840                                                         filter:         '',
3841                                                         opacity:        ''
3842                                                 }).fadeOut( onFadeOut );
3843                                                 if( currentElement === _panelElement ) currentElement = null;
3844                                                 return;
3845                                         };
3846                                 };
3847                         },
3848                         /* history */
3849                         restore : function( isAppend, panelElement ){
3850                                 PANEL_ELEMENT_CONTROL[ isAppend === true ? 'add' : 'remove' ]( panelElement );
3851                         },
3852                         replace: function( _panelElement, goForward ){
3853                                 // PANEL_ELEMENT_ARRAYの再構築
3854                                 var l = PANEL_ELEMENT_ARRAY.length,
3855                                         i,
3856                                         j = l;
3857                                 for( ; j; ){
3858                                         if( PANEL_ELEMENT_ARRAY[ --j ] === _panelElement ){
3859                                                 i = j;
3860                                                 break;
3861                                         };
3862                                 };
3863                                 if( i !== j ) return false;
3864                                 if( goForward === true ){
3865                                         if( i === 0 ) return false;
3866                                         PANEL_ELEMENT_ARRAY.splice( i, 1 );
3867                                         PANEL_ELEMENT_ARRAY.splice( i + 1, 0, _panelElement );
3868                                 } else {
3869                                         if( i === l - 1 ) return false;
3870                                         PANEL_ELEMENT_ARRAY.splice( i, 1 );
3871                                         PANEL_ELEMENT_ARRAY.splice( i - 1, 0, _panelElement );
3872                                 };
3873                                 renumber( true );
3874                                 return true;
3875                         },
3876                         onPanelResize : function ( _panelX, _panelY, _panelW, _panelH, isResizerTopAction ){
3877                         /*
3878                          * リサイズが、ResizerTopによって行われた場合、panelElementのyを動かして見かけ上動かないようにする。
3879                          */                                     
3880                                 if( isResizerTopAction === true ){
3881                                         var     _shiftX = _panelW - panelW,
3882                                                 _shiftY = _panelH - panelH;
3883                                         for( var i = PANEL_ELEMENT_ARRAY.length; i; ){
3884                                                 PANEL_ELEMENT_ARRAY[ --i ].shift( _shiftX, _shiftY );
3885                                         };
3886                                 };
3887                                 node.update( panelX = _panelX, panelY = _panelY, panelW = _panelW, panelH = _panelH );
3888                         },
3889                         createImageElement: function( data ){
3890                                 if( Type.isObject( data ) === false ){
3891                                         PremiumSatge.boot( 1, PANEL_ELEMENT_CONTROL.onImageSelect );
3892                                 } else {
3893                                         PANEL_ELEMENT_CONTROL.onImageSelect( data, true );
3894                                 };
3895                         },
3896                         onImageSelect: function( data, isPanelPictureData ){
3897                                 var _panelElement;
3898                                 if( isPanelPictureData !== true ){
3899                                         _panelElement = new ImageElementClass( {
3900                                                 resource_picture:data,
3901                                                 x:               Math.floor( panelW / 2 - data.width / 2 ),
3902                                                 y:               Math.floor( panelH / 2 - data.height / 2 ),
3903                                                 z:               -1,
3904                                                 t:               PANEL_ELEMENT_ARRAY.length + 1,
3905                                                 width:           1,
3906                                                 height:          1
3907                                         });
3908                                         _panelElement.init();
3909                                         PANEL_ELEMENT_CONTROL.add( _panelElement );
3910                                         _panelElement.animate( undefined, undefined, Math.abs( data.width ), Math.abs( data.height ) );
3911                                 } else {
3912                                         _panelElement = new ImageElementClass( data );
3913                                         _panelElement.init();
3914                                         PANEL_ELEMENT_CONTROL.add( _panelElement );
3915                                 };
3916                                 HISTORY_CONTROL.saveState( PANEL_ELEMENT_CONTROL.restore, [ false, _panelElement ], [ true, _panelElement ], true );
3917                         },
3918                         createTextElement: function( data ){
3919                                 var _panelElement;
3920                                 if( Type.isObject( data ) === false ){
3921                                         data = {
3922                                                 balloon_template_id:1,
3923                                                 size:               1,
3924                                                 tail:               90,
3925                                                 x:                                      Math.floor( panelW / 2 - 100 + Math.random() * 10 ),
3926                                                 y:                  Math.floor( panelH / 2 - 100 + Math.random() * 10 ),
3927                                                 z:                  -1,
3928                                                 t:                  PANEL_ELEMENT_ARRAY.length + 1,
3929                                                 width:              200,
3930                                                 height:             200,
3931                                                 speeches_attributes: {
3932                                                         text1: {
3933                                                                 content:    'Hello'
3934                                                         }
3935                                                 }
3936                                         };
3937                                         _panelElement = new TextElementClass( data );
3938                                         _panelElement.init();
3939                                         TextEditor.boot( PANEL_CONTROL.x, PANEL_CONTROL.y, _panelElement, onTextInput );
3940                                 } else {
3941                                         _panelElement = new TextElementClass( data );
3942                                         _panelElement.init();
3943                                         onTextInput( _panelElement );
3944                                 };
3945                         }
3946                 };
3947         })();
3948
3949         /*
3950          * end of PANEL_ELEMENT_CONTROL
3951          */
3952
3953         function centering(){
3954                 app.onPaneResize( windowW, windowH );
3955         };
3956
3957         /* grobal method */
3958         
3959         this.MIN_WIDTH   = 320;
3960         this.MIN_HEIGHT  = 320;
3961         this.onInit = function(){
3962                 app.rootElement.id = 'editor';
3963                 app.rootElement.innerHTML = [
3964                         '<div id="grid" style="display:none;"></div>',
3965                         '<div id="comic-element-container"></div>',
3966                         '<div id="whiteGlass-container">',
3967                                 '<div id="whiteGlass-top"></div>',
3968                                 '<div id="whiteGlass-left"></div>',
3969                                 '<div id="whiteGlass-right"></div>',
3970                                 '<div id="whiteGlass-bottom"></div>',
3971                         '</div>',
3972                         '<div id="panel-tools-container">',
3973                                 '<div id="panel-resizer-top">▲</div>',
3974                                 '<div id="panel-resizer-bottom">▼</div>',
3975                         '</div>',
3976                         '<div id="window-container"></div>',
3977                         '<div id="comic-element-resizer-container">',
3978                                 '<div id="comic-element-resizer-container-inner">',
3979                                         '<div id="comic-element-consol-wrapper">',
3980                                                 '<div id="comic-element-consol-container" class="clearfix">',
3981                                                         '<div class="comic-element-consol-item">',
3982                                                                 '<div id="comic-element-x">',
3983                                                                         '<span class="comic-element-attribute-label">x:</span>',
3984                                                                         '<span id="comic-element-x-value" class="comic-element-attribute-value editable-value">0</span>',
3985                                                                 '</div>',
3986                                                                 '<div id="comic-element-y">',
3987                                                                         '<span class="comic-element-attribute-label">y:</span>',
3988                                                                         '<span id="comic-element-y-value" class="comic-element-attribute-value editable-value">0</span>',
3989                                                                 '</div>',
3990                                                         '</div>',
3991                                                         '<div class="comic-element-consol-item">',                                              
3992                                                                 '<div id="comic-element-z">',
3993                                                                         '<span class="comic-element-attribute-label">z:</span>',
3994                                                                         '<span id="comic-element-z-value" class="comic-element-attribute-value editable-value">0</span>',
3995                                                                 '</div>',
3996                                                                 '<div id="comic-element-a">',
3997                                                                         '<span id="comic-element-a-value" class="comic-element-attribute-value editable-value">0</span>',
3998                                                                         '<span class="comic-element-attribute-label">°</span>',
3999                                                                 '</div>',
4000                                                         '</div>',
4001                                                         '<div class="comic-element-consol-item">',
4002                                                                 '<div id="comic-element-w">',
4003                                                                         '<span class="comic-element-attribute-label">w:</span>',
4004                                                                         '<span id="comic-element-w-value" class="comic-element-attribute-value editable-value">0</span>',
4005                                                                 '</div>',
4006                                                                 '<div id="comic-element-h">',
4007                                                                         '<span class="comic-element-attribute-label">h:</span>',
4008                                                                         '<span id="comic-element-h-value" class="comic-element-attribute-value editable-value">0</span>',
4009                                                                 '</div>',
4010                                                         '</div>',
4011                                                         '<div class="comic-element-consol-item">',
4012                                                                 '<!-- <div id="comic-element-keep-aspect"></div> -->',
4013                                                                 '<div id="comic-element-w-percent">',
4014                                                                         '<span id="comic-element-w-percent-value" class="comic-element-attribute-value editable-value">0</span>',
4015                                                                         '<span class="comic-element-attribute-label">%</span>',
4016                                                                 '</div>',
4017                                                                 '<div class="comic-element-consol-item" id="comic-element-h-percent">',
4018                                                                         '<span id="comic-element-h-percent-value" class="comic-element-attribute-value editable-value">0</span>',
4019                                                                         '<span class="comic-element-attribute-label">%</span>',
4020                                                                 '</div>',
4021                                                         '</div>',
4022                                         '<div class="comic-element-consol-item-small">',
4023                                                                 '<div class="button" id="change-image-button">change</div>',
4024                                                         '</div>',
4025                                                         '<div class="comic-element-consol-item-small">',
4026                                                                 '<div class="button" id="layer-back-button">back</div>',
4027                                                         '</div>',
4028                                                         '<div class="comic-element-consol-item-small">',
4029                                                                 '<div class="button" id="delete-button">delete</div>',
4030                                                         '</div>',
4031                                                         '<div class="comic-element-consol-item-small">',
4032                                                                 '<div class="button" id="layer-forward-button">forward</div>',
4033                                                         '</div>',
4034                                                         '<div class="comic-element-consol-item-small">',
4035                                                                 '<div class="button" id="edit-text-button">edit</div>',
4036                                                         '</div>',
4037                                                         '<!-- <div class="comic-element-consol-item-small">',
4038                                                                 '<div class="button" id="hide-text-tail-button">tail</div>',
4039                                                         '</div> -->',
4040                                                 '</div>',
4041                                         '</div>',                               
4042                                         '<div class="comic-element-resizer" id="comic-element-resizer-top"></div>',
4043                                         '<div class="comic-element-resizer" id="comic-element-resizer-left"></div>',
4044                                         '<div class="comic-element-resizer" id="comic-element-resizer-right"></div>',
4045                                         '<div class="comic-element-resizer" id="comic-element-resizer-bottom"></div>',
4046                                         '<div class="comic-element-resizer" id="comic-element-resizer-top-left"></div>',
4047                                         '<div class="comic-element-resizer" id="comic-element-resizer-top-right"></div>',
4048                                         '<div class="comic-element-resizer" id="comic-element-resizer-bottom-left"></div>',
4049                                         '<div class="comic-element-resizer" id="comic-element-resizer-bottom-right"></div>',
4050                                         '<div id="comic-element-consol-pushout-wrapper">',
4051                                                 '<div id="comic-element-consol-pushout-tail"></div>',
4052                                                 '<div id="comic-element-consol-pushout-inner"></div>',
4053                                         '</div>',
4054                                         '<div id="balloon-tail-mover"></div>',
4055                                 '</div>',       
4056                         '</div>',
4057                         '<div id="menu-bar"></div>',
4058                         
4059                         '<div id="templete-container" style="display: none;">',
4060                                 '<div id="imgElementTemplete" class="comic-element-wrapper image-element"></div>',
4061                                 
4062                                 '<div id="textElementTemplete" class="comic-element-wrapper text-element">',
4063                                         '<img>',
4064                                         '<div class="speach">',
4065                                                 '<div class="speach-inner">&nbsp;</div>',
4066                                         '</div>',
4067                                 '</div>',
4068                                 
4069                                 '<div id="textElementTempleteForOldIE" class="comic-element-wrapper text-element">',
4070                                         '<img>',
4071                                         '<div class="speach">',
4072                                                 '<table><tr><td>&nbsp;</td></tr></table>',
4073                                         '</div>',
4074                                 '</div>',
4075                                 
4076                                 '<div id="imageGroupItemTemplete" class="image-group-item">',
4077                                         '<div class="image-group-item-title">img-title</div>',
4078                                 '</div>',
4079                                 
4080                                 '<div id="windowTemplete" class="window-wrapper">',
4081                                         '<div class="window-header">',
4082                                                 '<div class="window-header-title">window title</div>',
4083                                                 '<div class="window-close-button">x</div>',
4084                                         '</div>',
4085                                         '<div class="window-body"></div>',
4086                                         '<div class="window-footer">',
4087                                                 '<div class="window-resize-button"></div>',
4088                                         '</div>',
4089                                 '</div>',
4090                                 
4091                                 '<div id="infomation-window">',
4092                                         '<div id="panel-background-information">',
4093                                                 '<div id="bg-pattern"></div>',
4094                                                 '<div id="select-bg-pattern-button">pattern</div>',
4095                                                 '<div id="reset-bg-pattern-button">x</div>',
4096                                                 '<div id="bg-color"></div>',
4097                                                 '<div id="select-bg-color-button">color</div>',
4098                                                 '<div id="reset-bg-color-button">x</div>',
4099                                                 '<!-- <div id="bg-pattern-x"></div>',
4100                                                 '<div id="bg-pattern-y"></div>',
4101                                                 '<div id="bg-pattern-repeat-x"></div>',
4102                                                 '<div id="bg-pattern-repeat-y"></div> -->',
4103                                         '</div>',
4104                                 '</div>',
4105                                 
4106                                 '<div id="toolbox-window">',
4107                                         '<div id="toolbox-add-image-button" class="button">add image</div>',
4108                                         '<div id="toolbox-add-text-button" class="button">add text</div>',
4109                                         '<div id="toolbox-edit-bg-button" class="button">edit bg</div>',
4110                                         '<div id="toolbox-switch-grid" class="button">grid</div>',
4111                                         '<div id="toolbox-popup-help-button" class="button">?</div>',
4112                                         '<div id="toolbox-post-button" class="button">post</div>',
4113                                 '</div>',
4114                                                         
4115                         '</div>'
4116                 ].join( '' );
4117                 
4118                 app.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
4119                 eventRoot = app.getPointingDeviceEventTreeRoot();
4120                 
4121                 delete app.onInit;
4122         };
4123         this.onOpen = function( _w, _h, _file ){
4124                 // 表示奥の物から順に init() していく
4125                 PANEL_ELEMENT_CONTROL.init();
4126                 PANEL_CONTROL.init();
4127                 // PANEL_RESIZER_BOTTOM.init();
4128                 // PANEL_RESIZER_TOP.init();
4129                 WINDOWS_CONTROL.init();
4130                 MENU_BAR_CONTROL.init();
4131                 
4132                 // レイヤーにあまり関係ないモジュール  
4133                 HISTORY_CONTROL.init();
4134                 SAVE_CONTROL.init();
4135                 GRID_CONTROL.init();
4136                 WHITE_GLASS_CONTROL.init();
4137                 PanelElementOperatorManager.init();
4138                 
4139                 comicID      = -1;
4140                 panelID      = -1;
4141                 panelTimming = -1;
4142                 phase        = 0;
4143                 
4144                 var panelW, panelH,
4145                         borderSize,
4146                         fileData, panelElements, panelElm;
4147
4148                 if( FileAPI.isFileInstance( _file ) === true ){
4149                         if( Driver.isPettanrFileInstance( _file ) === true ){
4150                                 if( _file.getType() === FILE_TYPE.COMIC ){
4151                                         fileData = _file.read();
4152                                         panelW   = fileData.width;
4153                                         panelH   = fileData.height;
4154                                         comicID  = fileData.id || -1;
4155                                 } else
4156                                 if( _file.getType() === FILE_TYPE.PANEL ){
4157                                         fileData      = _file.read();
4158                                         panelW        = fileData.width;
4159                                         panelH        = fileData.height;
4160                                         borderSize    = fileData.border;
4161                                         panelElements = fileData.panel_elements;
4162                                         comicID       = fileData.comic ? fileData.comic.id || -1 : -1;
4163                                         panelID       = fileData.id || -1;
4164                                         panelTimming  = fileData.t  || -1;
4165                                 };
4166                         } else {
4167                         };
4168                 } else {
4169                 };
4170                 
4171                 // open() は各モジュールの init() 後に実施可能になる. 
4172                 HISTORY_CONTROL.open();
4173                 SAVE_CONTROL.open();
4174                 WINDOWS_CONTROL.open();
4175                 
4176                 GRID_CONTROL.open();
4177                 PANEL_CONTROL.open( panelW, panelH, borderSize );
4178                 PanelElementOperatorManager.open();
4179                 PANEL_ELEMENT_CONTROL.open();
4180                 
4181                 // last
4182                 MENU_BAR_CONTROL.open();
4183                 
4184                 windowW = _w;
4185                 windowH = _h;
4186                 app.onPaneResize( _w, _h );
4187                 
4188
4189                 if( Type.isArray( panelElements ) === true ){
4190                         for( var i=0; i<panelElements.length; ++i ){
4191                                 panelElm = panelElements[ i ];
4192                                 if( panelElm.resource_picture ){
4193                                         PANEL_ELEMENT_CONTROL.createImageElement( panelElm );
4194                                 } else
4195                                 if( panelElm.balloon_template_id ){
4196                                         PANEL_ELEMENT_CONTROL.createTextElement( panelElm );
4197                                 };
4198                         };
4199                 };
4200                 
4201         /*
4202          * centering
4203          */
4204                 app.addKeyEventListener( 'keydown', centering, 96, false, true );       // ctrl + 0
4205                 app.addKeyEventListener( 'krydown', centering, 48, false, true );       // ctrl + 0
4206                 MENU_BAR_CONTROL.EDIT.createOption( 'centering', 'ctrl + 0', centering, true, true, true);
4207                 
4208                 phase   = 1;
4209
4210                 delete app.onOpen;
4211         };
4212         this.onClose = function(){
4213                 phase   = 2;
4214                 HISTORY_CONTROL.close();
4215                 
4216                 WINDOWS_CONTROL.close();
4217                 
4218                 GRID_CONTROL.close();
4219                 PANEL_CONTROL.close();
4220                 
4221                 PanelElementOperatorManager.close();
4222                 PANEL_ELEMENT_CONTROL.close();
4223                 
4224                 // last
4225                 MENU_BAR_CONTROL.close();
4226                 
4227                 phase = -1;
4228         };
4229         this.onPaneResize = function( _windowW, _windowH ){
4230                 windowW = _windowW || windowW;
4231                 windowH = _windowH || windowH;
4232
4233                 app.rootElement.style.height = _windowH + 'px';
4234                 
4235                 WINDOWS_CONTROL.onWindowResize( _windowW, _windowH );
4236                 MENU_BAR_CONTROL.onWindowResize( _windowW, _windowH );
4237                 PANEL_CONTROL.onWindowResize( _windowW, _windowH );
4238         };
4239 }, false, true, 'Panel Editor', 'paneleditor', null, '#2D89F0' );
4240
4241 var FormApplicationHelper = function( app ){
4242         app.isUploading = false;
4243         app.elmProgress = null;
4244         app.elmUploader = null;
4245         app.elmScript   = null;
4246         app.elmIframe   = null;
4247         app.elmForm     = null;
4248         app.fetchScript = function(){
4249                 app.elmProgress = document.getElementById( app.elmProgressID );
4250                 
4251                 if( !( app.elmUploader = document.getElementById( app.elmUploaderID ) ) ){
4252                         app.elmUploader    = document.createElement( 'div' );
4253                         app.rootElement.appendChild( app.elmUploader );
4254                         app.elmUploader.id = app.elmUploaderID;
4255                         if( app.hideUploader === true ){
4256                                 app.elmUploader.style.cssText = 'height:1px;line-height:1px;visibility:hidden;overflow:hidden;';
4257                         };                      
4258                 };
4259                 
4260                 app.elmScript        = document.createElement( 'script' );
4261                 document.body.appendChild( app.elmScript );
4262                 app.elmScript.type   = 'text\/javascript';
4263                 app.elmScript.src    = app.scriptSrc;
4264                 
4265                 app.elmProgress.innerHTML = 'loading form.';
4266                 
4267                 app.addTimer( app.detectForm, 250 );
4268                 
4269                 delete app.fetchScript;
4270         };
4271         app.detectForm = function(){
4272                 app.elmForm = app.elmUploader.getElementsByTagName( 'form' )[ 0 ];
4273                 if( !app.elmForm ) return;
4274                 
4275                 app.removeTimer( app.detectForm );
4276                 Util.createIframe( 'targetFrame', app.onCreateIframe );
4277                 app.elmProgress.innerHTML = 'create iframe';
4278                 
4279                 delete app.detectForm;
4280         };
4281         app.onCreateIframe = function( _iframe ){
4282                 app.elmUploader.appendChild( _iframe );
4283                 app.elmIframe             = _iframe;
4284                 app.elmForm.target        = _iframe.name;
4285                 app.elmProgress.innerHTML = '';
4286                 app.onFormReady && app.onFormReady();
4287                 
4288                 delete app.onCreateIframe;
4289         };
4290         app.submit = function(){
4291                 app.elmProgress.innerHTML = 'submit!';
4292                 try {
4293                         app.elmForm.submit();
4294                         app.isUploading = true;
4295                 } catch( e ){
4296                         app.elmProgress.innerHTML = 'submit err..';
4297                         app.submitError && app.submitError();
4298                         return;
4299                 };
4300                 if( app.detectIframe ){
4301                         app.elmIframe.onreadystatechange = app.detectIframe;
4302                 } else {
4303                         app.elmIframe.onload = app.onIframeUpdate;
4304                 };
4305                 app.elmProgress.innerHTML = 'uploading..';
4306                 
4307                 delete app.submit;
4308         };
4309         if( UA.isIE ){
4310                 app.detectIframe = function(){
4311                 if ( this.readyState !== 'complete' ) return;
4312                 this.onreadystatechange = new Function();
4313                 this.onreadystatechange = null;
4314                 app.onIframeUpdate();
4315                 delete app.detectIframe;
4316                 };
4317         };
4318         app.onIframeUpdate = function(){
4319                 app.elmIframe.onload = null;
4320                 try {
4321                         console.log( ( app.elmIframe.contentWindow || app.elmIframe.contentDocument.parentWindow ).document.body.innerHTML );
4322                         console.log( ( app.elmIframe.contentWindow || app.elmIframe.contentDocument.parentWindow )[ 'current_author' ] );
4323                 } catch(e){
4324                         
4325                 };
4326                 ( app.elmIframe.contentWindow || app.elmIframe.contentDocument.parentWindow ).close();
4327                 app.elmIframe = null;
4328                 app.elmProgress.innerHTML = 'success!';
4329                 app.isUploading = false;
4330                 app.submitSuccess && app.submitSuccess();
4331                 delete app.onIframeUpdate;
4332         };
4333         app.destroyHelper = function(){
4334                 app = null;
4335         };
4336 };
4337
4338 var ComicConsole = gOS.registerApplication( function(){
4339         var elmHeader, elmProgress,
4340                 windowW, windowH,
4341                 inputTitle, inputW, inputH,
4342                 eventRoot, node,
4343                 comboboxVisible, // comboboxEditable,
4344                 buttonSubmit, buttonCancel,
4345                 app         = this;
4346
4347         function clickOK(){
4348                 if( !app.elmForm || !app.elmIframe || app.isUploading === true ) return false;
4349                 // validate
4350
4351                 var _inputList = app.elmForm.getElementsByTagName( 'input' ),
4352                         _input, _name;
4353                 for( var i = _inputList.length; i; ){
4354                         _input = _inputList[ --i ];
4355                         _name = _input.name;
4356                         if( _name === 'comic[title]' ){
4357                                 _input.value = inputTitle.value();
4358                         } else
4359                         if( _name === 'comic[width]' ){
4360                                 _input.value = inputW.value();
4361                         } else
4362                         if( _name === 'comic[height]' ){
4363                                 _input.value = inputH.value();
4364                         };
4365                 };
4366                 var _selectList = app.elmForm.getElementsByTagName( 'select' ),
4367                         _select, _optionList;
4368                 for( i = _selectList.length; i; ){
4369                         _select = _selectList[ --i ];
4370                         _name = _select.name;
4371                         _optionList = _select.getElementsByTagName( 'option' )
4372                         if( _name === 'comic[visible]' ){
4373                                 _select.selectedIndex = comboboxVisible.selectIndex();
4374                         }/* else
4375                         if( _name === 'comic[editable]' ){
4376                                 _select.selectedIndex = comboboxEditable.selectIndex();
4377                         } */;
4378                 };
4379                 app.submit();
4380         };
4381         function clickCancel(){
4382                 if( app.isUploading === true ) return false;
4383                 ComicConsole.shutdown();
4384         };
4385
4386         /* grobal method */
4387         this.MIN_WIDTH   = 320;
4388         this.MIN_HEIGHT  = 320;
4389         this.onInit = function(){
4390                 app.rootElement.id = 'comic-console-wrapper';
4391                 app.rootElement.className = 'console-wrapper';
4392                 app.rootElement.innerHTML = [
4393                         '<div id="comic-console-header" class="console-header">Create New Comic</div>',
4394                         '<div id="comic-console" class="console-inner">',
4395                                 '<div id="comic-console-title" class="field">',
4396                                         '<span class="field-label">Title:</span>',
4397                                         '<span id="comic-console-title-value" class="comic-console-value editable-value">No Title</span>',
4398                                 '</div>',
4399                                 '<div id="comic-console-width" class="field">',
4400                                         '<span class="field-label">Default Width:</span>',
4401                                         '<span id="comic-console-width-value" class="comic-console-value editable-value">300</span>',
4402                                 '</div>',
4403                                 '<div id="comic-console-height" class="field">',
4404                                         '<span class="field-label">Default Height:</span>',
4405                                         '<span id="comic-console-height-value" class="comic-console-value editable-value">200</span>',
4406                                 '</div>',
4407                                 '<div id="comic-console-visible" class="field">',
4408                                         '<span class="field-label">Visible:</span>',
4409                                         '<span id="comic-console-visible-value" class="comic-console-value combobox"></span>',
4410                                 '</div>',
4411                                 //'<div id="comic-console-editable" class="field">',
4412                                 //      '<span class="field-label">Editable:</span>',
4413                                 //      '<span id="comic-console-editable-value" class="comic-console-value combobox"></span>',
4414                                 //'</div>',
4415                                 '<div class="console-button-container">',
4416                                         '<div id="comic-console-post-button" class="button console-submit-button">create</div>',
4417                                         '<div id="comic-console-cancel-button" class="button console-cancel-button">cancel</div>',
4418                                 '</div>',
4419                                 '<div id="comic-console-progress" class="console-progress">&nbsp;</div>',
4420                         '</div>'
4421                 ].join( '' );
4422                 
4423                 app.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
4424                 eventRoot = app.getPointingDeviceEventTreeRoot();
4425                 
4426                 delete app.onInit;
4427         };
4428         this.elmProgressID   = 'comic-console-progress';
4429         this.elmUploaderID   = 'newcomic';
4430         this.elmIframeName   = 'targetFrameCreateComic'
4431         this.scriptSrc       = pettanr.CONST.CREATE_COMIC_JS;
4432         this.hideUploader    = true;
4433         
4434         FormApplicationHelper( this );
4435         
4436         this.onOpen = function( w, h ){
4437                 node             = eventRoot.createNode( app.rootElement, true, true );
4438                 
4439                 var ui           = app.createUIGroup( node );
4440                 
4441                 inputTitle       = ui.createInputText( document.getElementById( 'comic-console-title') );
4442                 inputW           = ui.createInputText( document.getElementById( 'comic-console-width') );
4443                 inputH           = ui.createInputText( document.getElementById( 'comic-console-height') );
4444                 comboboxVisible  = ui.createCombobox( document.getElementById( 'comic-console-visible') );
4445                 // comboboxEditable = ui.createCombobox( document.getElementById( 'comic-console-editable') );
4446                 buttonSubmit     = ui.createButton( document.getElementById( 'comic-console-post-button'), clickOK );
4447                 buttonCancel     = ui.createButton( document.getElementById( 'comic-console-cancel-button'), clickCancel );
4448                 
4449                 app.onPaneResize( w, h );
4450                 app.fetchScript();
4451                 delete app.onOpen;
4452         };
4453         this.onPaneResize = function( w, h ){
4454                 windowW = w;
4455                 windowH = h;
4456                 //app.rootElement.style.cssText = [
4457                 //      'left:', Math.floor( ( _w - app.rootElement.offsetWidth  ) /2 ), 'px;',
4458                 //      'top:',  Math.floor( ( _h- app.rootElement.offsetHeight ) /2 ), 'px;'
4459                 //].join( '' );
4460                 node.update( w / 2 - node.width() / 2, h / 2 - node.height() / 2 );
4461         };
4462         this.onClose = function(){
4463                 app.destroyHelper();
4464                 app = inputTitle = inputW = inputH = comboboxVisible = buttonSubmit = buttonCancel = null;
4465         };
4466         this.onFormReady     = function(){
4467                 var selectList = app.elmForm.getElementsByTagName( 'select' ),
4468                         select,
4469                         j, m,
4470                         optionList, option;
4471                 for( var i=0, l=selectList.length; i<l; ++i ){
4472                         select = selectList[ i ];
4473                         optionList = select.getElementsByTagName( 'option' );
4474                         for( j=0, m=optionList.length; j<m; ++j ){
4475                                 option = optionList[ j ];
4476                                 if( select.name === 'comic[visible]' ){
4477                                         comboboxVisible.createOption( option.innerHTML, option.value, option.selected );
4478                                 }/* else
4479                                 if( select.name === 'comic[editable]' ){
4480                                         comboboxEditable.createOption( option.innerHTML, option.value, option.selected );
4481                                 }*/;
4482                         };
4483                 };
4484                 inputTitle.focus();
4485                 
4486                 //node.mesure();
4487                 app.onPaneResize( windowW, windowH );
4488                 
4489                 delete app.onFormReady;
4490         };
4491         this.submitError = function(){
4492                 app.addTimer( clickCancel , 5000, true );
4493         };
4494         this.submitSuccess = function(){
4495                 app.addTimer( clickCancel , 5000, true );
4496         };
4497 }, true, true, 'Comic Console', 'comicConsole', null, '#D44A26' );
4498
4499 var UploadConsole = gOS.registerApplication( function(){
4500         var windowW, windowH,
4501                 eventRoot, node, nodeForm,
4502                 buttonSubmit, buttonCancel,
4503                 elmFile,
4504                 app = this;
4505
4506         function clickOK(){
4507                 if( !app.elmForm || !app.elmIframe || app.isUploading === true ) return false;
4508                 if( elmFile.value.length === 0 ) return false;
4509                 app.submit();
4510                 return false;
4511         };
4512         function clickCancel(){
4513                 if( app.isUploading === true ) return false;
4514                 UploadConsole.shutdown();
4515                 return false;
4516         };
4517
4518         /* grobal method */
4519         this.MIN_WIDTH   = 320;
4520         this.MIN_HEIGHT  = 320;
4521         this.onInit = function(){
4522                 app.rootElement.id = 'upload-console-wrapper';
4523                 app.rootElement.className = 'console-wrapper';
4524                 app.rootElement.innerHTML = [
4525                         '<div id="upload-console-header" class="console-header">Upload Picture</div>',
4526                         '<div id="upload-console" class="console-inner">',
4527                                 '<div id="upload-console-uiform"></div>',
4528                                 '<div class="console-button-container">',
4529                                         '<div id="upload-console-post-button" class="button console-submit-button">upload</div>',
4530                                         '<div id="upload-console-cancel-button" class="button console-cancel-button">cancel</div>',
4531                                 '</div>',
4532                                 '<div id="upload-console-progress" class="console-progress">&nbsp;</div>',
4533                         '</div>'
4534                 ].join( '' );
4535                 
4536                 app.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
4537                 
4538                 eventRoot = app.getPointingDeviceEventTreeRoot();
4539                 document.body.appendChild( Util.pullHtmlAsTemplete( '<div id="uploader"></div>' ) );
4540                 delete app.onInit;
4541         };
4542         this.elmProgressID   = 'upload-console-progress';
4543         this.elmUploaderID   = 'uploader';
4544         this.elmIframeName   = 'targetFrameUpload';
4545         this.scriptSrc       = pettanr.CONST.UPLOAD_PICTURE_JS;
4546         this.hideUploader    = false;
4547         FormApplicationHelper( this );
4548         this.onOpen = function( w, h ){
4549                 node             = eventRoot.createNode( app.rootElement, true, true );
4550                 nodeForm         = node.createNode( document.getElementById( 'upload-console-uiform' ), false, true );
4551                 var ui           = app.createUIGroup( node );
4552                 
4553                 buttonSubmit     = ui.createButton( document.getElementById( 'upload-console-post-button' ), clickOK );
4554                 buttonCancel     = ui.createButton( document.getElementById( 'upload-console-cancel-button' ), clickCancel );
4555
4556                 app.onPaneResize( w, h );
4557                 app.fetchScript();
4558         };
4559         this.onPaneResize = function( w, h ){
4560                 windowW = w;
4561                 windowH = h;
4562                 node.update( w / 2 - node.width() / 2, h / 2 - node.height() / 2 );
4563         };
4564         this.onClose = function(){
4565                 app.destroyHelper();
4566                 app = elmFile = buttonSubmit = buttonCancel = null;
4567         };
4568         this.onFormReady = function(){
4569                 var elmForm    = app.elmForm,
4570                         _inputList = elmForm.getElementsByTagName( 'input' ),
4571                         _input;
4572                 for( var i = _inputList.length; i; ){
4573                         _input = _inputList[ --i ];
4574                         if( _input.type === 'file' ){
4575                                 elmFile = _input;
4576                         } else
4577                         if( _input.type === 'submit' ){
4578                                 _input.style.display = 'none';
4579                         };
4580                 };
4581                 app.onPaneResize( windowW, windowH );
4582                 app.createUIForm( nodeForm, elmForm );
4583                 node.mesure();
4584                 node.mesureChildren();
4585                 delete app.onFormReady;
4586         };
4587         this.submitError = function(){
4588                 app.addTimer( clickCancel , 5000, true );
4589         };
4590         this.submitSuccess = function(){
4591                 app.addTimer( clickCancel , 5000, true );
4592         };
4593 }, true, true, 'Upload Console', 'uploadConsole', null, '#01A31C' );
4594
4595 var ArtistConsole = gOS.registerApplication( function(){
4596         var windowW, windowH,
4597                 eventRoot, node,
4598                 elmName, elmLicense,
4599                 inputName, inputLicense,
4600                 buttonSubmit, buttonCancel,
4601                 app = this;
4602
4603         function clickOK(){
4604                 if( !app.elmForm || !app.elmIframe || app.isUploading === true ) return false;
4605                 inputUpdate();
4606                 app.submit();
4607                 return false;
4608         };
4609         function clickCancel(){
4610                 if( app.isUploading === true) return false;
4611                 ArtistConsole.shutdown();
4612                 return false;
4613         };
4614         function inputUpdate( v ){
4615                 elmName.value    = inputName.value();
4616                 elmLicense.value = inputLicense.value();
4617         };
4618
4619         /* grobal method */
4620         this.MIN_WIDTH   = 320;
4621         this.MIN_HEIGHT  = 320;
4622         this.onInit = function(){
4623                 app.rootElement.id = 'artist-console-wrapper';
4624                 app.rootElement.className = 'console-wrapper';
4625                 app.rootElement.innerHTML = [
4626                         '<div id="artist-console-header" class="console-header">Register Artist</div>',
4627                         '<div id="artist-console" class="console-inner">',
4628                                 '<div id="artist-console-name" class="field">',
4629                                         '<span class="field-label">Name:</span>',
4630                                         '<span id="artist-console-name-value" class="comic-console-value editable-value">artist name here.</span>',
4631                                 '</div>',
4632                                 '<div id="artist-console-license" class="field">',
4633                                         '<span class="field-label">License:</span>',
4634                                         '<span id="artist-console-license-value" class="comic-console-value editable-value">license here.</span>',
4635                                 '</div>',
4636                                 '<div class="console-button-container">',
4637                                         '<div id="artist-console-post-button" class="button console-submit-button">register</div>',
4638                                         '<div id="artist-console-cancel-button" class="button console-cancel-button">cancel</div>',
4639                                 '</div>',
4640                                 '<div id="artist-console-progress" class="console-progress">&nbsp;</div>',
4641                                 '<div id="register" style="display:none;"></div>',
4642                         '</div>'
4643                 ].join( '' );
4644                 
4645                 app.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
4646                 eventRoot = app.getPointingDeviceEventTreeRoot();
4647                 
4648                 delete app.onInit;
4649         };
4650         this.elmProgressID   = 'artist-console-progress';
4651         this.elmUploaderID   = 'register';
4652         this.elmIframeName   = 'targetFrameArtistRegister'
4653         this.scriptSrc       = pettanr.CONST.REGISTER_ARTIST_JS;
4654         this.hideUploader    = false;
4655         FormApplicationHelper( this );
4656         this.onFormReady     = function(){
4657                 var _inputList = app.elmForm.getElementsByTagName( 'input' ),
4658                         _input;
4659                 for( var i = _inputList.length; i; ){
4660                         _input = _inputList[ --i ];
4661                         if( _input.type === 'submit' ){
4662                                 _input.style.display = 'none';
4663                         };
4664                         if( _input.name === 'artist[name]' ){
4665                                 elmName = _input;
4666                         };
4667                         if( _input.name === 'artist[default_license_id]' ){
4668                                 elmLicense = _input;
4669                         };
4670                 };
4671                 
4672                 node.mesure();
4673                 app.onPaneResize( windowW, windowH );
4674                 node.mesureChildren();
4675                 
4676                 delete app.onFormReady;
4677         };
4678         this.submitError = function(){
4679                 app.addTimer( clickCancel , 5000, true );
4680         };
4681         this.submitSuccess = function(){
4682                 app.addTimer( clickCancel , 5000, true );
4683         };
4684         this.onOpen = function( w, h ){
4685                 node             = eventRoot.createNode( app.rootElement, true, true );
4686                 var ui           = app.createUIGroup( node );
4687                 
4688                 inputName        = ui.createInputText( document.getElementById( 'artist-console-name' ), inputUpdate );
4689                 inputLicense     = ui.createInputText( document.getElementById( 'artist-console-license' ), inputUpdate );
4690                 buttonSubmit     = ui.createButton( document.getElementById( 'artist-console-post-button' ), clickOK );
4691                 buttonCancel     = ui.createButton( document.getElementById( 'artist-console-cancel-button' ), clickCancel );
4692
4693                 app.onPaneResize( w, h );
4694                 app.fetchScript();
4695         };
4696         this.onPaneResize = function( w, h ){
4697                 windowW = w;
4698                 windowH = h;
4699                 //app.rootElement.style.cssText = [
4700                 //      'left:', Math.floor( ( _w - app.rootElement.offsetWidth  ) /2 ), 'px;',
4701                 //      'top:',  Math.floor( ( _h- app.rootElement.offsetHeight ) /2 ), 'px;'
4702                 //].join( '' );
4703                 node.update( w / 2 - node.width() / 2, h / 2 - node.height() / 2 );
4704         };
4705         this.onClose = function(){
4706                 app.destroyHelper();
4707                 app = buttonSubmit = buttonCancel = null;
4708         };
4709 }, true, true, 'Artist Console', 'artistConsole', null, '#FFC40D' );
4710
4711 var PanelConsole = gOS.registerApplication( function(){
4712         var windowW, windowH,
4713                 eventRoot, node, inputData,
4714                 comboboxPublish, buttonPost, buttonClose,
4715                 elmInput,
4716                 app         = this,
4717                 model       = null;
4718                 
4719         /*
4720          * upload ボタンが押されたらまず iframe をつくる.
4721          */
4722         function clickOK(){
4723                 if( !app.elmForm || !app.elmIframe || app.isUploading === true ) return false;
4724                 inputData.value();
4725                 app.submit();
4726                 return false;
4727         }
4728
4729         function clickCancel(){
4730                 if( app.isUploading === true ) return false;
4731                 PanelConsole.shutdown();
4732                 return false;
4733         };
4734         function publishUpdate(){
4735                 if( model ){
4736                         model.publish( comboboxPublish.selectIndex() === 1 );
4737                         elmInput.value = model.getJsonPostString().replace( /\n/g, '' );
4738                         inputData.value( elmInput.value );              
4739                 } else {
4740                         elmInput.value = inputData.value();
4741                 };
4742         };
4743
4744         /* grobal method */
4745         this.MIN_WIDTH   = 320;
4746         this.MIN_HEIGHT  = 320;
4747         this.onInit = function(){
4748                 app.rootElement.id = 'panel-console-wrapper';
4749                 app.rootElement.className = 'console-wrapper';
4750                 app.rootElement.innerHTML = [
4751                         '<div id="panel-console-header" class="console-header">Create New Panel (dev)</div>',
4752                         '<div id="panel-console" class="console-inner">',
4753                                 '<div id="panel-console-data" class="field">',
4754                                         '<span class="field-label">POST DATA:</span>',
4755                                         '<span id="panel-console-data-value" class="comic-console-value editable-value">panel json here.</span>',
4756                                 '</div>',
4757                                 '<div id="panel-console-publish" class="field">',
4758                                         '<span class="field-label">Publish:</span>',
4759                                         '<span id="panel-console-publish-value" class="combobox"></span>',
4760                                 '</div>',
4761                                 '<div class="console-button-container">',
4762                                         '<div id="panel-console-post-button" class="button console-submit-button">post</div>',
4763                                         '<div id="panel-console-cancel-button" class="button console-cancel-button">cancel</div>',
4764                                 '</div>',
4765                                 '<div id="panel-console-progress" class="console-progress">&nbsp;</div>',
4766                                 '<div id="newpanel" style="display:none;"></div>',
4767                         '</div>'
4768                 ].join( '' );
4769
4770                 app.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
4771                 eventRoot = app.getPointingDeviceEventTreeRoot();
4772
4773                 delete app.onInit;
4774         };
4775         this.elmProgressID   = 'panel-console-progress';
4776         this.elmUploaderID   = 'newpanel';
4777         this.elmIframeName   = 'targetFrameNewPanel'
4778         this.scriptSrc       = pettanr.CONST.CREATE_PANEL_JS;
4779         this.hideUploader    = false;
4780         FormApplicationHelper( this );
4781         this.onFormReady     = function(){
4782                 var _inputList = app.elmForm.getElementsByTagName( 'input' ),
4783                         _input;
4784                 for( var i = _inputList.length; i; ){
4785                         _input = _inputList[ --i ];
4786                         if( _input.type === 'submit' ){
4787                                 _input.style.display = 'none';
4788                         };
4789                         if( _input.name === 'json' ){
4790                                 elmInput     = _input;
4791                                 publishUpdate();
4792                         };
4793                 };
4794                 
4795                 node.mesure();
4796                 app.onPaneResize( windowW, windowH );
4797                 node.mesureChildren();
4798                 
4799                 delete app.onFormReady;
4800         };
4801         this.submitError = function(){
4802                 app.addTimer( clickCancel , 5000, true );
4803         };
4804         this.submitSuccess = function(){
4805                 app.addTimer( clickCancel , 5000, true );
4806         };
4807         this.onOpen = function( w, h, _model ){
4808                 node    = eventRoot.createNode( app.rootElement, true, true );
4809                 var ui  = app.createUIGroup( node ),
4810                         elm = document.getElementById( 'panel-console-publish' );
4811                         
4812                 inputData        = ui.createInputText( document.getElementById( 'panel-console-data' ), publishUpdate );
4813                 
4814                 if( _model ){
4815                         comboboxPublish = ui.createCombobox( elm, publishUpdate );
4816                         comboboxPublish.createOption( 'only me', '0', _model.publish() === false );
4817                         comboboxPublish.createOption( 'publish', '1', _model.publish() === true );
4818                         model = _model;
4819                 } else {
4820                         elm.parentNode.removeChild( elm );
4821                 };
4822                 
4823                 buttonPost       = ui.createButton( document.getElementById( 'panel-console-post-button' ), clickOK );
4824                 buttonClose      = ui.createButton( document.getElementById( 'panel-console-cancel-button' ), clickCancel );
4825                 
4826                 app.onPaneResize( w, h );
4827                 app.fetchScript();
4828         };
4829         this.onPaneResize = function( w, h ){
4830                 windowW = w;
4831                 windowH = h;
4832                 //app.rootElement.style.cssText = [
4833                 //      'left:', Math.floor( ( _w - app.rootElement.offsetWidth  ) /2 ), 'px;',
4834                 //      'top:',  Math.floor( ( _h- app.rootElement.offsetHeight ) /2 ), 'px;'
4835                 //].join( '' );
4836                 node.update( w / 2 - node.width() / 2, h / 2 - node.height() / 2 );
4837         };
4838         this.onClose = function(){
4839                 app.destroyHelper();
4840                 model && model.destroy();
4841                 app = model = comboboxPublish = buttonPost = buttonClose = elmInput = null;
4842         };
4843 }, true, true, 'Panel Console', 'panelConsole', null, '#603CBA' );
4844
4845 var Model = ( function(){
4846         
4847         var PanelModelClass = function( panel ){
4848                 var comicID           = panel.comicID || -1,
4849                         panelID           = panel.panelID || -1,
4850                         panelTimming      = panel.panelTimming || -1,
4851                         panelW            = panel.panelW,
4852                         panelH            = panel.panelH,
4853                         borderSize        = panel.borderSize,
4854                         panelElementArray = panel.panelElementArray,
4855                         publish           = panel.publish,
4856                         timing            = 0;
4857                         
4858                 function getPanelElementByTiming(){
4859                         var i, l = panelElementArray.length;
4860                         while( timing < l * 2 ){
4861                                 for( i=0; i<l; ++i ){
4862                                         if( timing === panelElementArray[ i ].timing ){
4863                                                 // console.log( timing + ' , ' + panelElementArray[ i ].timing );
4864                                                 ++timing;
4865                                                 return panelElementArray[ i ];
4866                                         };
4867                                 };
4868                                 ++timing;
4869                         };
4870                         return null;
4871                 };
4872                 function panelElementToHtml( _panelElement, isAbsoluteUrl, isXHTML ){
4873                         var url;
4874                         if( _panelElement.type === 0 ){
4875                                 url = [ pettanr.CONST.RESOURCE_PICTURE_PATH, _panelElement.resourcePicture().id, '.', _panelElement.resourcePicture().ext ].join( '' );
4876                                 return [
4877                                         '<img ',
4878                                                 'src="',        isAbsoluteUrl !== true ? url : Util.getAbsolutePath( url ), '" ',
4879                                                 'width="',      _panelElement.w, '" ',
4880                                                 'height="',     _panelElement.h, '" ',
4881                                                 'style="',
4882                                                         'left:',    _panelElement.x, 'px;',
4883                                                         'top:',     _panelElement.y, 'px;',
4884                                                         'z-index:', _panelElement.z, ';',
4885                                                 '"',
4886                                         isXHTML !== true ? '>' : ' \/>'
4887                                 ].join( '');                            
4888                         } else {
4889                                 url = pettanr.balloon.getBalloonUrl( _panelElement.w, _panelElement.h, _panelElement.a );
4890                                 return [
4891                                         '<img ',
4892                                                 'src="',        isAbsoluteUrl !== true ? url : Util.getAbsolutePath( url ), '" ',
4893                                                 'width="',      _panelElement.w, '" ',
4894                                                 'height="',     _panelElement.h, '" ',
4895                                                 'style="',                                                                      
4896                                                         'left:',    _panelElement.x, 'px;',
4897                                                         'top:',     _panelElement.y, 'px;',
4898                                                         'z-index:', _panelElement.z, ';',
4899                                                 '"',
4900                                         isXHTML !== true ? '>' : ' \/>',
4901                                         pettanr.LINE_FEED_CODE_TEXTAREA,
4902                                         '<div class="balloon" style="',
4903                                                 'left:',        _panelElement.x, 'px;',
4904                                                 'top:',         _panelElement.y, 'px;',
4905                                                 'width:',       _panelElement.w, 'px;',
4906                                                 'height:',      _panelElement.h, 'px;',
4907                                                 'z-index:',     _panelElement.z,
4908                                         '"><span>', _panelElement.content, '<\/span>', '<\/div>'
4909                                                 
4910                                 ].join( '');                            
4911                         };
4912                 };
4913                 function getImageJsonGET( _imageElement ){
4914                         var cr = pettanr.LINE_FEED_CODE_TEXTAREA;
4915                         return [
4916                                 '{', cr,
4917                                         '"resource_picture": {', cr,
4918                                                 '"id": ',              _imageElement.resourcePicture().id, ',', cr,
4919                                                 '"ext": ',             '"',_imageElement.resourcePicture().ext, '"', cr,
4920                                         '},', cr,
4921                                         '"x": ',                   _imageElement.x, ',', cr,
4922                                         '"y": ',                   _imageElement.y, ',', cr,
4923                                         '"z": ',                   _imageElement.z, ',', cr,
4924                                         '"width": ',               _imageElement.flipH * _imageElement.w, ',', cr,
4925                                         '"height": ',              _imageElement.flipV * _imageElement.h, ',', cr,
4926                                         '"t": ',                   timing, cr,
4927                                 '}'
4928                         ].join( '');
4929                 };
4930                 function imageToJson( _imageElement, _timing ){
4931                         var cr = pettanr.LINE_FEED_CODE_TEXTAREA;
4932                         return [
4933                                 '{', cr,
4934                                         '"picture_id": ', _imageElement.resourcePicture().id, ',', cr,
4935                                         '"x": ',          _imageElement.x, ',', cr,
4936                                         '"y": ',          _imageElement.y, ',', cr,
4937                                         '"z": ',          _imageElement.z + 1, ',', cr,
4938                                         '"t": ',          _timing, ',', cr,
4939                                         '"width": ',      _imageElement.flipH * _imageElement.w, ',', cr,
4940                                         '"height": ',     _imageElement.flipV * _imageElement.h, cr,
4941                                 '}'
4942                         ].join( '');
4943                 };
4944
4945                 function balloonToJson( _textElement, _timing ){
4946                         var cr = pettanr.LINE_FEED_CODE_TEXTAREA;
4947                         return [
4948                                 '{', cr,
4949                                         '"balloon_template_id": ', 1, ',', cr,
4950                                         '"system_picture_id": ',   1, ',', cr,
4951                                         '"size": ',                1, ',', cr,
4952                                         '"tail": ',                _textElement.a, ',', cr,
4953                                         '"x": ',                   _textElement.x, ',', cr,
4954                                         '"y": ',                   _textElement.y, ',', cr,
4955                                         '"z": ',                   _textElement.z + 1, ',', cr,
4956                                         '"t": ',                   timing, ',', cr,
4957                                         '"width": ',               _textElement.w, ',', cr,
4958                                         '"height": ',              _textElement.h, ',', cr,
4959                                         '"speeches_attributes": {', cr,
4960                                                 '"newf', timing, '": {', cr,
4961                                                 '"content": "', _textElement.content, '",', cr,
4962                                                         '"x": ',        _textElement.x, ',', cr,
4963                                                         '"y": ',        _textElement.y, ',', cr,
4964                                                         '"t": ',        timing, ',', cr,
4965                                                         '"width": ',    _textElement.w, ',', cr,
4966                                                         '"height": ',   _textElement.h, cr,
4967                                                 '}', cr,
4968                                         '}', cr,
4969                                 '}'
4970                         ].join( '');
4971                 };
4972                         
4973                 this.getJsonPostString = function(){
4974                         timing = 0;
4975                         
4976                         var JSON_STRING_ARRAY = [],
4977                                 IMAGE_ARRAY       = [],
4978                                 BALLOON_ARRAY     = [],
4979                                 l = panelElementArray.length,
4980                                 _panelElement, n,
4981                                 cr = pettanr.LINE_FEED_CODE_TEXTAREA;
4982         
4983                         while( IMAGE_ARRAY.length + BALLOON_ARRAY.length <= l ){
4984                                 _panelElement = getPanelElementByTiming();
4985                                 if( _panelElement === null) break;
4986                                 n = IMAGE_ARRAY.length + BALLOON_ARRAY.length;
4987                                 _panelElement.type === 0 ? 
4988                                         IMAGE_ARRAY.push( [ '"new', n, '": ', imageToJson( _panelElement, n ) ].join( '' ) ) :
4989                                         BALLOON_ARRAY.push( [ '"new', n, '": ', balloonToJson( _panelElement, n ) ].join( '' ) );
4990                         };
4991                         return [
4992                                 '{', cr,
4993                                         panelID !== -1 ? ( '"id": ' + panelID + ',' + cr ) : '',
4994                                         comicID !== -1 ? ( '"comic_id": ' + comicID + ',' + cr ) : '',
4995                                     '"width": ',            panelW, ',', cr,
4996                                     '"height": ',           panelH, ',', cr,
4997                                     '"border": ',           borderSize, ',', cr,
4998                                     
4999                                     // '"picture_id": 1,', cr,
5000                                         '"x": ',                0, ',', cr,
5001                                         '"y": ',                0, ',', cr,
5002                                         '"z": ',                1, ',', cr,
5003                                         panelTimming !== -1 ? ( '"t": ' + panelTimming + ',' + cr ) : '',
5004                                     '"panel_pictures_attributes": {', cr,
5005                                         IMAGE_ARRAY.join( ',' + cr ), cr,
5006                                     '},', cr,
5007                                     '"speech_balloons_attributes": {', cr,
5008                                         BALLOON_ARRAY.join( ',' + cr ), cr,
5009                                     '}', ',', cr,
5010                                     '"publish": ',           ( publish === true ? 1 : 0 ), cr,
5011                                 '}'
5012                         ].join( '' );
5013                 };
5014                 this.getJsonGetString = function(){
5015                         timing = 0;
5016                         
5017                         var JSON_STRING_ARRAY = [],
5018                                 ELEMENT_ARRAY     = [],
5019                                 l                 = panelElementArray.length,
5020                                 cr                = pettanr.LINE_FEED_CODE_TEXTAREA,
5021                                 _panelElement;
5022         
5023                         while( ELEMENT_ARRAY.length <= l){
5024                                 _panelElement = getPanelElementByTiming();
5025                                 if( _panelElement === null ) break;
5026                                  
5027                                 ELEMENT_ARRAY.push( _panelElement.type === 0 ? getImageJsonGET( _panelElement ) : balloonToJson( _panelElement ));
5028                         };
5029                         return [
5030                                 '{', cr,
5031                                         //'"panel": {', cr,
5032                                                 //'"id": ',               panelID, ',', cr,
5033                                             '"border": ',           borderSize, ',', cr,
5034                                             // '"comic_id": ',         comicID, ',', cr,
5035                                             // '"picture_id": 1,', cr,
5036                                                 '"x": ',                0, ',', cr,
5037                                                 '"y": ',                0, ',', cr,
5038                                                 '"z": ',                1, ',', cr,
5039                                                 // panelTimming !== -1 ? ( '"t": ' + panelTimming + ',' + cr ) : '',
5040                                             '"width": ',            panelW, ',', cr,
5041                                             '"height": ',           panelH, ',', cr,
5042                                             '"panel_elements": [', cr,
5043                                                 ELEMENT_ARRAY.join( ',' + cr ), cr,
5044                                             ']', cr,
5045                                         //'}', cr,
5046                                 '}'
5047                         ].join( '' );
5048                 };
5049                 this.getAsHtmlString = function( isAbsoluteUrl, isXHTML ){
5050                         timing = 0;
5051                         
5052                         var HTML_ARRAY = [],
5053                                 l = panelElementArray.length,
5054                                 _panelElement;
5055         
5056                         while( HTML_ARRAY.length < l ){
5057                                 _panelElement = getPanelElementByTiming();
5058                                 if( _panelElement === null) break;
5059                                 HTML_ARRAY.push( panelElementToHtml( _panelElement, isAbsoluteUrl, isXHTML ));
5060                         };
5061         
5062                         HTML_ARRAY.unshift(
5063                                 [
5064                                         '<div class="panel" ',
5065                                                 'style="',
5066                                                         'height:', panelH, 'px;',
5067                                                         'background-color:', ';',
5068                                                 '"',
5069                                         '>'
5070                                 ].join( '')
5071                         );              
5072                         HTML_ARRAY.push( '</div>');
5073                         
5074                         return HTML_ARRAY.join( pettanr.LINE_FEED_CODE_TEXTAREA );
5075                 };
5076                 this.publish = function( v ){
5077                         return publish = Type.isBoolean( v ) === true ? v : publish;
5078                 };
5079                 this.destroy = function(){
5080                         panel = panelElementArray = null;
5081                 };
5082         };
5083         
5084         return {
5085                 createPanel: function( panelData ){
5086                         return new PanelModelClass( panelData );
5087                 }
5088         };
5089 } )();
5090
5091
5092 var OutputConsole = gOS.registerApplication( function(){
5093         var FORMAT_LIST = [ 'json[POST]', 'json[GET]', 'XML', 'HTML', 'XHTML', 'MT export', 'Blogger ATOM' ];
5094         var elmOutputArea,
5095                 eventRoot, node,
5096                 comboboxFormat, inputOption,
5097                 buttonPost, buttonClose,
5098                 windowW, windowH,
5099                 timing   = 0,
5100                 comicID, panelID, panelTimming, panelW, panelH, borderSize, panelElementArray,
5101                 app      = this,
5102                 model    = null;
5103         
5104         function clickOK(){
5105                 OutputConsole.shutdown();
5106         };
5107
5108         function formatUpdate(){
5109                 var i = comboboxFormat.selectIndex(),
5110                         text = 'sorry...';
5111                 // buttonPost.enabled( false );
5112                 if( i === 0 ){
5113                         text = model.getJsonPostString();
5114                         // buttonPost.enabled( true );
5115                 } else
5116                 if( i === 1 ){
5117                         text = model.getJsonGetString();
5118                 } else
5119                 if( i === 3 ){
5120                         text = model.getAsHtmlString( false, false );
5121                 } else {
5122                         
5123                 };
5124                 elmOutputArea.value = text;
5125         };
5126         function clickClose(){
5127                 OutputConsole.shutdown();
5128                 return false;
5129         };
5130         
5131         function clickPost(){
5132                 // PanelConsole.boot( elmOutputArea.value );
5133                 return false;
5134         }
5135         
5136         /* grobal method */
5137         this.MIN_WIDTH   = 320;
5138         this.MIN_HEIGHT  = 320;
5139         this.onInit = function(){
5140                 app.rootElement.id = 'output-console-wrapper';
5141                 app.rootElement.className = 'console-wrapper';
5142                 app.rootElement.innerHTML = [
5143                         '<div id="output-console-header" class="console-header">Output Console</div>',
5144                         '<div id="output-console" class="console-inner">',
5145                                 '<div id="output-console-format" class="field">',
5146                                         '<span class="field-label">Format:</span>',
5147                                         '<span id="output-console-format-value" class="output-console-value combobox"></span>',
5148                                 '</div>',
5149                                 '<div id="output-console-option" class="field">',
5150                                         '<span class="field-label">Options:</span>',
5151                                         '<span id="output-console-option-value" class="output-console-value editable-value">absolute-path</span>',
5152                                 '</div>',
5153                                 '<textarea id="output-area" readonly></textarea>',
5154                                 '<div id="output-console-close-button" class="button">close</div>',
5155                         '</div>'
5156                 ].join( '' );
5157
5158                 app.fetchCSS( pettanr.CONST.URL_PETA_APPS_CSS );
5159                 eventRoot = app.getPointingDeviceEventTreeRoot();
5160
5161                 delete app.onInit;
5162         };
5163         this.onOpen = function( _w, _h, _comicID, _panelID, _panelTimming, _panelW, _panelH, _borderSize, _panelElementArray ){
5164                 elmOutputArea = document.getElementById( 'output-area' );
5165                 
5166                 node   = eventRoot.createNode( app.rootElement, true, true );
5167                 var ui = app.createUIGroup( node );
5168                 comboboxFormat = ui.createCombobox( document.getElementById( 'output-console-format' ), formatUpdate );
5169                 
5170                 for( var i=0; FORMAT_LIST[ 0 ]; ++i ){
5171                         comboboxFormat.createOption( FORMAT_LIST.shift(), null, i === 0 );
5172                 };
5173                 inputOption    = ui.createInputText( document.getElementById( 'output-console-option' ), null );
5174                 // buttonPost     = ui.createButton( document.getElementById( 'output-console-post-button' ), clickPost );
5175                 buttonClose    = ui.createButton( document.getElementById( 'output-console-close-button' ), clickClose );
5176                 
5177                 app.onPaneResize( _w, _h );
5178                 
5179                 comboboxFormat.focus( true );
5180                 
5181                 model = Model.createPanel( {
5182                         comicID           : _comicID,
5183                         panelID           : _panelID,
5184                         panelTimming      : _panelTimming,
5185                         panelW            : _panelW,
5186                         panelH            : _panelH,
5187                         borderSize        : _borderSize,
5188                         panelElementArray : _panelElementArray,
5189                         publish           : true
5190                 } );
5191                 
5192                 formatUpdate();
5193         };
5194         this.onPaneResize = function( w, h ){
5195                 windowW = w;
5196                 windowH = h;
5197                 //app.rootElement.style.cssText = [
5198                 //      'left:', Math.floor( ( _windowW - app.rootElement.offsetWidth  ) /2 ), 'px;',
5199                 //      'top:',  Math.floor( ( _windowH - app.rootElement.offsetHeight ) /2 ), 'px;'
5200                 //].join( '' );
5201                 node.update( w / 2 - node.width() / 2, h / 2 - node.height() / 2 );
5202         };
5203         this.onClose = function(){
5204                 elmOutputArea.value = '';
5205                 model.destroy();
5206                 elmOutputArea = comboboxFormat = inputOption = buttonPost = buttonClose = panelElementArray = instance = model = null;
5207         };
5208 }, true, false, 'Output Console', 'outputConsole', null, '#2D89F0' );
5209
5210 })( pettanr, gOS, window, document );