MyCombo = function(config) { 
    MyCombo.superclass.constructor.call(this, config);
};

Ext.extend(MyCombo, Ext.form.ComboBox, { 
	listAlignOffsets: [0, 0],

	restrictHeight : function(){ 
	    this.innerList.dom.style.height = '';
	    var inner = this.innerList.dom;
	    var pad = this.list.getFrameWidth('tb')+(this.resizable?this.handleHeight:0)+this.assetHeight;
	    var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
	    var ha = this.getPosition()[1]-Ext.getBody().getScroll().top;
	    var hb = Ext.lib.Dom.getViewHeight()-ha-this.getSize().height;
	    var space = Math.max(ha, hb, this.minHeight || 0)-this.list.shadowOffset-pad-5;
	    h = Math.min(h, space, this.maxHeight);
	
	    this.innerList.setHeight(h);
	    this.list.beginUpdate();
	    this.list.setHeight(h+pad);
	    this.list.alignTo(this.wrap, this.listAlign, this.listAlignOffsets);
	    this.list.endUpdate();
	},
	
	expand : function(){ 
	    if(this.isExpanded() || !this.hasFocus){ 
		return;
	    }
	    this.list.alignTo(this.wrap, this.listAlign, this.listAlignOffsets);
	    this.list.show();
	    this.innerList.setOverflow('auto');         Ext.getDoc().on('mousewheel', this.collapseIf, this);
	    Ext.getDoc().on('mousedown', this.collapseIf, this);
	    this.fireEvent('expand', this);
	}
    });

LoginBox={ 
    show:function(){ 
	if(!LoginBox.loginwin){ 
	    var loginform=new Ext.FormPanel({ 
		    labelAlign: 'left',
		    labelWidth: 90,
		    baseCls: 'x-plain',
		    bodyBorder: false,
		    border: false,
		    autoHeight:true,
		    items: [{ 
			    fieldLabel: 'Eメール',
			    xtype:'textfield',
			    name: 'ulogin',			  
			    width: 160,
			    id: 'ulogin',
			    allowBlank:false
			},{ 
			    fieldLabel: 'パスワード',
			    xtype:'textfield',
			    id: 'upassword',
			    width: 160,
			    inputType: 'password',
			    name: 'upassword',
			    allowBlank:false
			},{ 
				fieldLabel: 'ログイン情報を記憶する',
				xtype: 'checkbox',
				id: 'urememberme',
				name: 'urememberme'
			},{ 			
			    xtype:'label',
			    html:'ジェネティックスホームページのアカウントを持っていない方はサインアップが必要です。'
			}],
		    buttons: [{ 
			    text     : 'ログイン',
			    handler: function(){ 
				loginform.getForm().submit({ 
					url:'?pageaction=login&lang=2', 
					waitMsg:'ログイン中',
					failure: function(form,action) { 
					    var errorMessage = "ログインエラーです。もう一度ログインを行ってください。";
					    if(action.result!=undefined && action.result.message!=undefined){ 
						errorMessage = action.result.message;
					    }
					    var sb = Ext.getCmp('loginStatusBar');
			                    sb.setStatus({ 
						    text:errorMessage,
							iconCls:''
							});
					},
					success: function(form,action) { 
					    LoginBox.loginwin.hide();
					    window.location.href=action.result.url;
					}});
			    }
			},{ 
			    text     : 'Close',
			    handler  : function(){ 
				LoginBox.loginwin.hide();
			    }
			}]
		});
	    LoginBox.loginwin = new Ext.Window({ 
		    layout      : 'fit',
		    closeAction :'hide',
		    plain       : true,
		    title       : "ログイン",
		    modal       : true,
		     bodyBorder: false,
		    border: false,
		    width       : 300,
		    height      : 237,
		    items	    : loginform,
		    resizable : false,
		    bbar: new Ext.StatusBar({ 
		            id: 'loginStatusBar',
		             bodyBorder: false,
		   			 border: false,
		            defaultText: '',
			    cls:"loginStatusBarErrorText"
			})
		});
	    LoginBox.loginwin.render(Ext.getBody());
	}
	LoginBox.loginwin.show();
	LoginBox.loginwin.center();
        
	return false;
    },
    loginwin: null
};


Ext.onReady(function(){ 

	var button;
	if(button = Ext.get('pcms_login')) { 
	    button.on('click', LoginBox.show ,button,{ stopEvent: true});
	}

	var languagewin;
	var langbutton;
	if(langbutton = Ext.get('pcms_language')) { 
	    langbutton.on('click', function(){ 
		    if(!languagewin){ 
		    	var serverArray = window.location.href.split("://");
		    	var langArray;
		    	var selectedLang = "en";
		    	if(serverArray.length){ 
			    langArray = serverArray[1].split("/");
			    if(langArray && langArray.length){ 
				selectedLang = langArray[1];
			    }
		    	}
		    	
			var langform=new Ext.FormPanel({ 
				labelAlign: 'right',
				baseCls: 'x-plain',
				defaultType: 'radio',
				id: 'langForm',
				 bodyBorder: false,
		    border: false,
				labelWidth: 60,
				items: [{ 
					fieldLabel: '英語',
					checked : selectedLang=='en' ? true : false,
					inputValue : 'en',
					name:"selectLang"
				    },{ 
					fieldLabel: '日本語',
					inputValue : 'jp',
					checked : selectedLang=='jp' ? true : false,
					name:"selectLang"
				    }],
				buttons: [{ 
					text     : 'Save',
					handler: function(){ 
					    langform.getForm().submit({ 
						    url:'?pageaction=language&lang=2', 
						    waitMsg:'言語を選択してください',
						    failure: function(form,action) { 
							languagewin.hide();
							window.location.href=action.result.url;
						    },
						    success: function(form,action) { 
							languagewin.hide();
							window.location.href=action.result.url;
						    }});
					}
				    },{ 
					text     : 'Close',
					handler  : function(){ 
					    languagewin.hide();
					}
				    }]
			    });
			languagewin = new Ext.Window({ 
				layout      : 'fit',
				closeAction :'hide',
				plain       : true,
				title       : "言語",
				 bodyBorder: false,
		    	border: false,
				resizable   : false,
				modal       : true,
				width       : 250,
				height      : 150,
				items	    : langform
			    });
			languagewin.render(Ext.getBody());
		    }
		    languagewin.show();
	    
		    return false;
		},langbutton,{ stopEvent: true});
	}
	
	var search;
	if(searchbox = Ext.get('searchinput')) { 
	    var ds = new Ext.data.Store({ 
		    proxy: new Ext.data.ScriptTagProxy({ 
			    url: '/jp/search/index.html?json=1'
			}),
		   
		    reader: new Ext.data.JsonReader({ 
			    root: 'items',
			    totalProperty: 'totalCount',
			    id: 'url'
			}, [
            { name: 'title', mapping: 'title'},
            { name: 'description', mapping: 'description'}
			    ])
		});
	    
	    // Custom rendering Template
	    var resultTpl = new Ext.XTemplate(
					      '<tpl for="."><div class="search-item-container">',
					      '<div class="search-item"><div class="search-cb-title">{title}</div>',
					      '<div>{description}</div>',
					      '</div>',
					      '</div></tpl>'
					      );

	    var search = new MyCombo({ 
		    store: ds,
		    displayField:'title',
		    typeAhead: false,
		    loadingText: '検索中',
		    pageSize:8,
		    hideTrigger:true,
		    tpl: resultTpl,
		    applyTo: 'searchinput',
		    itemSelector: 'div.search-item-container',
		    listAlign: 'tr-br',
		    listAlignOffsets:[27,2],
		    listWidth: 350,
		    onSelect: function(record){ // override default onSelect to do redirect
			window.location = String.format('{0}',record.id);
		    }
		});
		
	}

    });