<!DOCTYPE html>
<html>
	<head>
		<%= htmlparts["headtag"] %>
		<%= htmlparts['headjs_switchpost'] %>
		<% unless cgi.user_agent =~ /(iPod|iPhone|iPad|Android)/ %>
        <link rel="stylesheet" type="text/css" href="./yui/build/menu/assets/skins/sam/menu.css" />
        <link rel="stylesheet" type="text/css" href="./yui/build/button/assets/skins/sam/button.css" />
        <link rel="stylesheet" type="text/css" href="./yui/build/fonts/fonts-min.css" />
        <link rel="stylesheet" type="text/css" href="./yui/build/container/assets/skins/sam/container.css" />
        <link rel="stylesheet" type="text/css" href="./yui/build/editor/assets/skins/sam/editor.css" />
        <script type="text/javascript" src="./yui/build/yahoo-dom-event/yahoo-dom-event.js">
        </script>
        <script type="text/javascript" src="./yui/build/animation/animation-min.js">
        </script>
        <script type="text/javascript" src="./yui/build/element/element-min.js">
        </script>
        <script type="text/javascript" src="./yui/build/container/container-min.js">
        </script>
        <script type="text/javascript" src="./yui/build/menu/menu-min.js">
        </script>
        <script type="text/javascript" src="./yui/build/button/button-min.js">
        </script>
        <script type="text/javascript" src="./yui/build/editor/editor-min.js">
        </script>
        <script>
            (function(){
                var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event;
                
                var myConfig = {
                    height: '350px',
                    width: '100%',
                    animate: true,
                    dompath: true,
                    focusAtStart: true,
                    handleSubmit: true
                };
                
                var state = 'off';
                YAHOO.log('Set state to off..', 'info', 'example');
                
                YAHOO.log('Create the Editor..', 'info', 'example');
                var myEditor = new YAHOO.widget.Editor('contenteditor', myConfig);
                myEditor.on('toolbarLoaded', function(){
                    var codeConfig = {
                        type: 'push',
                        label: 'Edit HTML Code',
                        value: 'editcode'
                    };
                    YAHOO.log('Create the (editcode) Button', 'info', 'example');
                    this.toolbar.addButtonToGroup(codeConfig, 'insertitem');
                    
                    this.toolbar.on('editcodeClick', function(){
                        var ta = this.get('element'), iframe = this.get('iframe').get('element');
                        
                        if (state == 'on') {
                            state = 'off';
                            this.toolbar.set('disabled', false);
                            YAHOO.log('Show the Editor', 'info', 'example');
                            YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example');
                            this.setEditorHTML(ta.value);
                            if (!this.browser.ie) {
                                this._setDesignMode('on');
                            }
                            
                            Dom.removeClass(iframe, 'editor-hidden');
                            Dom.addClass(ta, 'editor-hidden');
                            this.show();
                            this._focusWindow();
                        }
                        else {
                            state = 'on';
                            YAHOO.log('Show the Code Editor', 'info', 'example');
                            this.cleanHTML();
                            YAHOO.log('Save the Editors HTML', 'info', 'example');
                            Dom.addClass(iframe, 'editor-hidden');
                            Dom.removeClass(ta, 'editor-hidden');
                            this.toolbar.set('disabled', true);
                            this.toolbar.getButtonByValue('editcode').set('disabled', false);
                            this.toolbar.selectButton('editcode');
                            this.dompath.innerHTML = 'Editing HTML Code';
                            this.hide();
                        }
                        return false;
                    }, this, true);
                    
                    this.on('cleanHTML', function(ev){
                        YAHOO.log('cleanHTML callback fired..', 'info', 'example');
                        this.get('element').value = ev.html;
                    }, this, true);
                    
                    this.on('afterRender', function(){
                        var wrapper = this.get('editor_wrapper');
                        wrapper.appendChild(this.get('element'));
                        this.setStyle('width', '100%');
                        this.setStyle('height', '100%');
                        this.setStyle('visibility', '');
                        this.setStyle('top', '');
                        this.setStyle('left', '');
                        this.setStyle('position', '');
                        
                        this.addClass('editor-hidden');
                    }, this, true);
                }, myEditor, true);
                myEditor.render();
                
            })();
            
            function switchsubmit(form, action){
                document.getElementsByName("action").item(0).value = action;
                form.submit();
            }
        </script>
        <style>
            .yui-skin-sam .yui-toolbar-container .yui-toolbar-editcode span.yui-toolbar-icon {
                background-image: url( ./yui/assets/html_editor.gif );
                background-position: 0 1px;
                left: 5px;
            }
            
            .yui-skin-sam .yui-toolbar-container .yui-button-editcode-selected span.yui-toolbar-icon {
                background-image: url( ./yui/assets/html_editor.gif );
                background-position: 0 1px;
                left: 5px;
            }
            
            .editor-hidden {
                visibility: hidden;
                top: -9999px;
                left: -9999px;
                position: absolute;
            }
            
            textarea {
                border: 0;
                margin: 0;
                padding: 0;
            }
            
            body {
            	font-family: 'ヒラギノ角ゴ Pro W3', 'Hiragino Kaku Gothic Pro', 'メイリオ', Meiryo, 'ＭＳ Ｐゴシック', sans-serif;
            	font-size: 12px;
            	line-height: 18px;
            }
        </style>
        <% end %>
        <%= htmlparts['headjs_tag'] %>
	</head>
	<body class="yui-skin-sam">
		<form action="<%= cgi.script_name %>" method="POST">
			<div class="menu_wrapper">
				<%= htmlparts["menu"] %>
			</div>
			<%= htmlparts["selectfile"] %>
			<%= htmlparts["infoarea"] %>
			<div class="contents">
				<h1>記事作成</h1>
				<input type="hidden" name="target_filepath" value="<%= session["target_filepath"] %>">
				<% case params["action"]
                when "confirm" %>
                <table>
                    <tbody>
                        <% db.transaction do
                        entry = db["newentry"]
                        entry.paramlist.each do |val| %>
                        <tr style="display: <%= entry.display[val] %>;">
                            <th class="small middle padding_normal">
                                <%= entry.name[val] %>
                            </th>
                            <td class="large middle padding_normal">
                                <input type="hidden" name="<%= val %>" value="<%= entry.send(val) %>">
                                <% if val == "category" %>
                            	<div id="entry_category"></div>
                                <% end %>
                                <% if val != "content" %>
                                <%= entry.send(val) %>
                                <% else %>
                                <%= entry.content_for_view %><% end %>
                            </td>
                        </tr>
                        <tr style="display: <%= entry.display[val] %>;">
                            <td class="hr"></td>
                            <td class="hr"></td>
                        </tr>
                        <% end %>
                        <% end %>
                    </tbody>
                </table>
                <br>
                <div class="center">
                	<input type="hidden" name="mode" value="newentry"><input type="hidden" name="action" value=""><input type="button" value="戻る" onclick="switchsubmit(this.form, 'back')">　<input type="button" value="確定" onclick="switchsubmit(this.form, 'exec')">
                </div>
                <br>
                <% when "back" %>
                <table>
                    <tbody>
                        <% db.transaction do
                        if cgi["action"] == "back"
                        entry = db["newentry"]
                        else
                        entry = Entry.new({})
                        end
                        entry.paramlist.each do |val| %>
                        <tr style="display: <%= entry.display[val] %>;">
                            <th class="small middle padding_normal">
                                <%= entry.name[val] %>
                            </th>
                            <td class="large left padding_normal">
                            	<% if val == "category" %>
                            	<input type="text" name="<%= val %>" value="<%= entry.send(val) %>" class="max_width" style="display: none;">
                                <div id="entry_category"></div>
                                <input type="button" value="タグ追加" onclick="javascript: addTagSelectBox();">
                                <% elsif val != "content" %>
                                <input type="text" name="<%= val %>" value="<%= entry.send(val) %>" class="max_width"><% else %>
                                <textarea name="<%= val %>" rows="10" class="max_width" id="contenteditor"><%= entry.content_for_generator %></textarea>
                                <% end %>
                            </td>
                        </tr>
                        <tr style="display: <%= entry.display[val] %>;">
                            <td class="hr"></td>
                            <td class="hr"></td>
                        </tr>
                        <% end %>
                        <% end %>
                    </tbody>
                </table>
                <br>
                <div class="center">
                	<input type="hidden" name="mode" value="newentry"><input type="hidden" name="action" value="confirm"><input type="submit" value="確認" onclick="javascript: unionCategory();">
                </div>
                <br>
                <% else %>
                <table>
                    <tbody>
                        <% db.transaction do
                        if cgi["action"] == "back"
                        entry = db["newentry"]
                        else
                        entry = Entry.new({})
                        end
                        entry.paramlist.each do |val| %>
                        <tr style="display: <%= entry.display[val] %>;">
                            <th class="small middle padding_normal">
                                <%= entry.name[val] %>
                            </th>
                            <td class="large left padding_normal">
                                <% date = Time.now.iso8601 %>
                                <% if val == "content" %>
                                <textarea name="<%= val %>" rows="10" class="max_width" id="contenteditor"><% if entry.content.empty? %><p><br></p><% else %><%= entry.content_for_generator %><% end %></textarea>
                                <% elsif val == "category" %>
                                <input type="text" name="<%= val %>" value="<%= entry.send(val) %>" class="max_width" style="display: none;">
                                <div id="entry_category"></div>
                                <input type="button" value="タグ追加" onclick="javascript: addTagSelectBox();">
                                <% elsif val == "entryid" %>
                                <input type="text" name="<%= val %>" value="<%= db["feed"].feedid %>?<%= date %>" class="max_width"><% elsif val == "updated" || val == "published" %>
                                <input type="text" name="<%= val %>" value="<%= date %>" class="max_width"><% elsif val == "url" %>
                                <input type="text" name="<%= val %>" value="<%= db["feed"].url %>#<%= db["feed"].feedid %>?<%= date %>" class="max_width"><% else %>
                                <input type="text" name="<%= val %>" value="<%= entry.send(val) %>" class="max_width"><% end %>
                            </td>
                        </tr>
                        <tr style="display: <%= entry.display[val] %>;">
                            <td class="hr"></td>
                            <td class="hr"></td>
                        </tr>
                        <% end %>
                        <% end %>
                    </tbody>
                </table>
                <br>
                <div class="center">
                	<input type="hidden" name="mode" value="newentry"><input type="hidden" name="action" value="confirm"><input type="submit" class="wymupdate" value="確認" onclick="javascript: unionCategory();">
                </div>
                <br>
                <% end %>
            </div>
			<div class="bottom_area">
				<%= APPVERSION %>
			</div>
		</form>
	</body>
</html>
