// $Id: $

/**
 * class itosan 
 */
itosan = Ext.extend(webapp,{
    init: function(mode) {
        for(var i=0;i<objlist.length;i++) {
            if(objlist[i]!="webappobj") {
                eval("Ext.apply("+objlist[i]+",webappobj);");
            }
        }

        this.novellist = [["1","第一小説「我々の恋愛」"],
                          ["2","第二小説「すっぽん」"],
                          ["3","「思い出すままに 伊藤郁男」"]];

        this.createCutupForm();

        var tabs = new Ext.TabPanel({
            renderTo: 'tabs',
            activeTab: 0,
            defaults:{autoHeight: true},
            plain:true,
            items:[
               {contentEl:'daiichi-box', title:'第一小説「我々の恋愛」'},
               {contentEl:'daini-box', title:'第二小説「すっぽん」'},
               {contentEl:'daddy-box', title:'思い出すままに 伊藤郁男'}
            ]
        });
        
        //this.closeAll();
    },

    createCutupForm: function() {
        if(Ext.get("cutup-form")==undefined) {
            return;
        }
        Ext.QuickTips.init();

        // prepare for status combobox
        var store = new Ext.data.SimpleStore({
                fields: ['value', 'status'],
                data : this.novellist
            });
        var combo = new Ext.form.ComboBox({
            fieldLabel: "小説",
            labelStyle: 'font-weight:bold; text-align: right;',
            name: "novel",
            store: store,
            valueField: 'value',
            displayField:'status',
            editable: false,
            typeAhead: true,
            mode: 'local',
            triggerAction: 'all',
            selectOnFocus: false,
            emptyText: "選んでください",
            listClass: 'x-combo-list-small',
            msgTarget: "side",
            allowBlank: false
        });

        // create form items
        var items = new Array();
        items.push(combo);
        items.push({
            xtype: 'radiogroup',
            fieldLabel: 'セクションを維持',
            labelStyle: 'font-weight:bold; text-align: right;',
            items: [
                {boxLabel: 'する', name: 'section', inputValue: 1, checked: true},
                {boxLabel: 'しない', name: 'section', inputValue: 2}
            ]
        });
        items.push({
            xtype: "box",
            autoEl: {
                tag: "div",
                style: "margin-top: 5px; margin-left: 110px;",
                html: "<p style='font-size: 10px;'>「セクション」とは「<a href='http://seikonovel.exblog.jp/' target='_blank'>連載小説空間</a>」における記事をさします。<br />「セクションを維持」で「しない」にすると記事を越えたセンテンスの組み合わせが行われます。</p>"
            }
        });

        // create form
        this.form = new Ext.FormPanel({
            el: "cutup-form",
            frame:true,
            bodyStyle:'padding: 10px;',
            labelWidth: 110,
            defaults: {width: 250,height: 300},
            items: items,
            buttonAlign: "right",
            buttons: [{
                text: "カットアップ",
                handler: function() {
                    if(itosanobj.form.getForm().isValid()) {
                        itosanobj.getCutup();
                    }
                }
            }]
        });        
        this.form.render();
    },

    getCutup: function() {
        var ownobj = itosanobj;
        var values = ownobj.form.getForm().getValues();
        var title = "";
        for(var i=0;i<this.novellist.length;i++) {
            if(this.novellist[i][1]==values.novel) {
                title = this.novellist[i][1];
                values.novel = this.novellist[i][0];
                break;
            }
        }
        var params = Ext.apply(values,{classname: "ItosanAjax",method: "getCutup",token: ownobj.token});
        ownobj.startLoading();
        Ext.Ajax.request({
            url: ownobj.wwwpath+ownobj.ajaxpath+"/",
            params: params,
            method: "POST",
            success: function(response) {
                ownobj.finishLoading();
                var data = eval("("+response.responseText+")");
                ownobj.setToken(data.token);
                if(data.success) {
                    ownobj.displayDialog({
                        title: title+" カットアップ版",
                        iconCls: "",
                        closable: true,
                        constrain: true,
                        stateful: false,
                        width: 600,
                        border: true,
                        height: 400,
                        modal: true,
                        layout: "fit",
                        autoScroll: true,
                        bodyStyle: "padding: 5px; font-size: 12px;",
                        plain: true
                    });
                    ownobj.setDialogContents(data.contents);
                } else {
                    ownobj.displayAlert(data.errormsg);
                }
            },
            failure: function(response) {
                ownobj.finishLoading();
                ownobj.displayAlert("ページのリロードをしてからやり直してください");
            }
        });        
    },
    
    closeAll: function() {
        var contents = Ext.query(".itosan-contents");
        Ext.each(contents,function(item) {
                Ext.get(item.id).enableDisplayMode();
                Ext.get(item.id).hide(false);
        });
    },
    
    openContents: function(target,id) {
        if(target==1)
            var idname = "daiichi";
        else if(target==2)
            var idname = "daini";
        else if(target==3)
            var idname = "daddy";
            
        var item = Ext.get(idname+"-contents"+id);
        this.slideToggle(item);
    },

    changeFontSize: function(size,target,id) {
        if(target==1)
            var idname = "daiichi";
        else if(target==2)
            var idname = "daini";
        else if(target==3)
            var idname = "daddy";
        var item = Ext.get(idname+"-body"+id);

        switch(size) {
        case 0:
            var font = 13;
            break;
        case 1:
            var font = 18;
            break;
        case 2:
            var font = 24;
            break;
        }
        item.setStyle("font-size",font+"px");
        item.setStyle("line-height","1.5em");

        var html = "";
        if(size!=0) {
            html += "<a href='javascript:itosanobj.changeFontSize(0,"+target+","+id+");' title='フォントサイズを普通サイズに変更'>普通</a>：";
        } else {
            html += "普通：";
        }
        if(size!=1) {
            html += "<a href='javascript:itosanobj.changeFontSize(1,"+target+","+id+");' title='フォントサイズを大きいサイズに変更'>大</a>：";
        } else {
            html += "大：";
        }
        if(size!=2) {
            html += "<a href='javascript:itosanobj.changeFontSize(2,"+target+","+id+");' title='フォントサイズを特大サイズに変更'>特大</a>";
        } else {
            html += "特大";
        }
        Ext.get(idname+"-fontsize-selector"+id).dom.innerHTML = html;
    },

    slideToggle: function(e)
	{
		if(e.isDisplayed() )
		{
			//Es visible.
			e.slideOut('t',{
				useDisplay:true
			});
		}
		else
		{
			//No es Visible.
			e.slideIn('t',{
				useDisplay:true
			});
		}
	},
    
    setToken: function(token) {
        for(var i=0;i<objlist.length;i++) {
            eval(objlist[i]+".token = token;");
        }
    }
});
var itosanobj = new itosan();
objlist.push("itosanobj");

