index = function(){
	
	Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
		path: "/",
		domain: "levantateyresplandece.imech.cl"
	}));
	
	if(Ext.state.Manager.get("PER_ID", "1") == "1"){
		Ext.state.Manager.set("PER_ID", "1")
	};
	
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = "side";
	Ext.data.Connection.prototype.disableCaching = false;
	Ext.data.Connection.prototype.method = "GET";
	
	var elLayout = Ext.DomHelper.append(document.body, {tag: "div"}, true);
	
	var layout = new Ext.BorderLayout(elLayout, {
		//monitorWindowResize: true,
		north: { initialSize: 87 },
		center: { initialSize:800}//, split: true }
	});	
	
	var panelBanner = layout.add("north", new Ext.ContentPanel({
		autoCreate: true
	}));
	
	var templateBanner = new Ext.Template(
		'<table cellpadding="0" cellspacing="0" style="width:100%; height:100%; background:#EDF6FF;">',
			'<tr>',
				'<td align="center">',
					'<img src="images/bannerblue10.jpg" title="Levántate y Resplandece">',
				'</td>',
			'</tr>',
		'</table>'
	);
	
	templateBanner.overwrite(panelBanner.getEl());
	
	var elInnerLayout = Ext.DomHelper.append(document.body, {tag: "div"}, true);
	 
	var innerLayout = new Ext.BorderLayout(elInnerLayout, {
		monitorWindowResize: true,
		north: { initialSize: 28 },
		center: {
			split: true,
			tabPosition: "top",
			closeOnTab: true,
			autoScroll: true,
			alwaysShowTabs: true
		}
	});
	
	var panelMenu = innerLayout.add("north", new Ext.ContentPanel({ autoCreate: true }));
	var elMenu = Ext.DomHelper.append(panelMenu.getEl(), {tag: "div"}, true);	
	var menu = new botonera({
		el: elMenu,
		ambito: this,
		parametros: { APL_PADRE_ID: "32" }
	});
	
	menu.on("crear", function(m){
	
		m.add("-", {
			text: "Videos",
			iconCls: "videos",
			handler: this.videos,
			scope: this
		});
	
		if(m.items.length > 0){
			
			m.add("-");
			
		};

		m.addDom({
			tag: "iframe",
			frameborder: 0,
			height: 16,
			width: 301,
			scrolling: "no",
			src: "reproductor/reproductor.html"
		}, false);
		
		m.add("-");
		
/*		m.add("-", {
			text: "Videos",
			iconCls: "videos",
			handler: this.videos,
			scope: this
		});*/

	}, this);
	
	innerLayout.add("center", new portada());
	layout.add("center", new Ext.NestedLayoutPanel(innerLayout, { autoCreate: true }));	
	this.innerLayout = innerLayout;
};

index.prototype = {
	
	ventanas: [],

	salir: function(){
		Ext.state.Manager.clear("USU_ID");
		Ext.state.Manager.clear("PER_ID");
		window.location.reload();
	},
	
	entrar: function(boton){
		if(!this.ventanas["entrar"]){
			var el = Ext.DomHelper.append(document.body, {tag: "div"}, true);
			var ventana = new Ext.LayoutDialog(el, {
				modal: true,
				width: 246,
				height: 215,
				shadow: true,
				resizable: false,
				collapsible: false,
				title: "Entrar",
				center: { autoCreate: true }
			});
			
			var layout = ventana.getLayout();
			var panel = layout.add("center", new entrar());
			ventana.addButton("Cerrar", ventana.hide, ventana);
			ventana.addButton("Aceptar", panel.enviar, panel);
			ventana.addKeyListener(13, panel.enviar, panel);
			ventana.on("show", panel.limpiar, panel);
			this.ventanas["entrar"] = ventana;
		};
		this.ventanas["entrar"].show(boton.id);
	},
	
	usuarios: function(boton){
		
		var panel = this.innerLayout.findPanel("usuarios");
		if(!panel){
			panel = this.innerLayout.add("center", new usuarios({
				APL_ID: boton.APP_ID
			}));
		}else{
			this.innerLayout.showPanel(panel);	
		};
	},
	
	noticias: function(boton){
		
		var panel = this.innerLayout.findPanel("noticias");
		if(!panel){
			panel = this.innerLayout.add("center", new noticias({
				APL_ID: boton.APP_ID
			}));
		}else{
			this.innerLayout.showPanel(panel);
		};
	},
	
	fotos: function(boton){
		
		var panel = this.innerLayout.findPanel("fotos");
		if(!panel){
			panel = this.innerLayout.add("center", new fotos({
				APL_ID: boton.APP_ID
			}));
		}else{
			this.innerLayout.showPanel(panel);
		};
	},
	
	ayuda: function(boton){
		
		var panel = this.innerLayout.findPanel("ayuda");
		if(!panel){
			panel = this.innerLayout.add("center", new ayuda({
				APL_ID: boton.APP_ID
			}));
		}else{
			this.innerLayout.showPanel(panel);
		};
	},
	
	galerias: function(boton){
		
		var panel = this.innerLayout.findPanel("galerias");
		if(!panel){
			panel = this.innerLayout.add("center", new galerias({
				APL_ID: boton.APP_ID
			}));
		}else{
			this.innerLayout.showPanel(panel);
		};
	},
	
	chat: function(boton){
		
		if(!this.ventanas["chat"]){	
			var el = Ext.DomHelper.append(document.body, {tag: "div"}, true);
			var ventana = new Ext.LayoutDialog(el, {
				width: 500,
				height: 400,
				shadow: true,
				resizable: false,
				title: "Chat",
				center: { autoCreate: true }
			});
			
			var layout = ventana.getLayout();
			layout.add("center", new chat());
			ventana.addButton("Cerrar", ventana.hide, ventana);
			this.ventanas["chat"] = ventana;
		};
		this.ventanas["chat"].show(boton.id);
	},
	
	/*registrar: function(e, target){
		
		if(!this.ventanas["registrar"]){
			var el = Ext.DomHelper.append(document.body, {tag: "div"}, true);
			var ventana = new Ext.LayoutDialog(el, {
				modal: true,
				width: 300,
				height: 320,
				shadow: true,
				collapsible: false,
				draggable: false,
				autoTabs: true,
				resizable: false,
				autoScroll: true,
				title: "AVISO",
				center: { autoCreate: true }
			});
			var layout = ventana.getLayout();
			var panelCentro = layout.add("center", new Ext.ContentPanel({ autoCreate: true }));
			
			var templatePanelCentro = new Ext.Template(										   
				'<table border="0" cellpadding="5" cellspacing="2" style="width:100%; height:100%; background:#EDF6FF; background-image:url(images/background.png); color:000066;">',
					'<tbody>',
						'<tr>',
							'<td style="padding:20px 15px 20px 15px;">',
								'<font size=2><font color="000066">',
								'<b>Inscripción temporalmente deshabilitada</b>',
								'<hr size=1 color="#C3DAF9"></hr>',
								'Discúlpanos, por el momento no es posible realizar el registro debido a un problema técnico.<br>Estamos trabajando para solucionarlo<br><br>Te invitamos a volver pronto e intentar otra vez! :)',
								'</font></font>',
							'</td></tr></tbody></table>'
			);
			templatePanelCentro.overwrite(panelCentro.getEl());
			ventana.addButton("Cerrar", ventana.hide, ventana);
			this.ventanas["registrar"] = ventana;
		};
		this.ventanas["registrar"].show(target.id);
	},*/
	//FUNCIÓN REGISTRAR ORIGINAL, SÓLO QUITAR QUOTES
	registrar: function(boton){
	
		if(!this.ventanas["registrar"]){
			var el = Ext.DomHelper.append(document.body, {tag: "div"}, true);
			var ventana = new Ext.LayoutDialog(el, {
				modal: true,
				width: 296,
				height: 502,
				shadow: true,
				collapsible: false,
				autoTabs: true,
				resizable: false,
				title: "Crear cuenta",
				center: { autoCreate: true, autoScroll: true }
			});
			
			var layout = ventana.getLayout();
			var panelCentro = layout.add("center", new Ext.ContentPanel({
				autoCreate: true										  
			}));
			var formulario = new Ext.form.Form({
				style: "padding:10px",
				labelWidth: "auto",
				labelAlign: "top",
				method: "GET",
				waitTitle: "Espere por favor",
				baseParams: {
					USU_PER_ID: "3"
				},
				errorReader: new Ext.data.JsonReader({
					root: "resultado", 
					successProperty: "exito"
				}, ["id", "msg"])
			});
			
			formulario.fieldset({legend:"Datos requeridos"},
					new Ext.form.NumberField({
						fieldLabel: "Rut/RG (ej: 113334442 ; k=0)",
						name: "USU_RUT",
						allowBlank: false,
						allowDecimals: false,
						allowNegative: false,
						selectOnFocus: true,
						minLength: 8,
						maxLength: 20,
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "Nombres",
						name: "USU_NOMBRES",
						selectOnFocus: true,
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "Apellidos",
						name: "USU_APELLIDOS",
						selectOnFocus: true,
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "Contraseña (4-10 caracteres)",
						name: "USU_CONTRASENA",
						selectOnFocus: true,
						inputType: "password",
						minLength: 4,
						maxLength: 10,
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "País",
						name: "USU_PAIS",
						selectOnFocus: true,
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "E-mail",
						name: "USU_EMAIL",
						selectOnFocus: true,
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "Teléfono",
						name: "USU_FONOFIJO",
						selectOnFocus: true,
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "Celular",
						name: "USU_CELULAR",
						selectOnFocus: true,
						width: 210
					})
			);
			
			formulario.fieldset({legend:"Datos requeridos (sólo Chile)"},
														   
				new Ext.form.ComboBox({
					fieldLabel: "Iglesia",
					hiddenName: "USU_IGL_ID",
					editable : false,
					store: new Ext.data.Store({
						proxy: new Ext.data.HttpProxy({
							url: "modulos/iglesia/listar.php"
						}),
						reader: new Ext.data.JsonReader({  
							root: "resultado", 
							successProperty: "exito",
							id: "IGL_ID" 
						}, ["IGL_ID", "IGL_NOMBRE"])
					}),
					valueField: "IGL_ID",
					displayField: "IGL_NOMBRE",
					mode: "remote",
					triggerAction: "all",
					selectOnFocus: true,
					//value: "SIN IGLESIA",
					allowBlank: false,
					width: 210
				}),
				new Ext.form.ComboBox({
					fieldLabel: "Colegio",
					hiddenName: "USU_ESC_ID",
					editable : false,
					store: new Ext.data.Store({
						proxy: new Ext.data.HttpProxy({
							url: "modulos/escuela/listar.php"
						}),
						reader: new Ext.data.JsonReader({  
							root: "resultado", 
							successProperty: "exito",
							id: "ESC_ID" 
						}, ["ESC_ID", "ESC_NOMBRE"])
					}),
					valueField: "ESC_ID",
					displayField: "ESC_NOMBRE",
					mode: "remote",
					triggerAction: "all",
					selectOnFocus: true,
					//value: "SIN ESCUELA",
					allowBlank: false,
					width: 210
				})
			);
			formulario.fieldset({legend:"Datos opcionales"},
					new Ext.form.TextField({
						fieldLabel: "Ciudad",
						name: "USU_CIUDAD",
						selectOnFocus: true,
						width: 210
					}),
					new Ext.form.NumberField({
						fieldLabel: "Edad",
						name: "USU_EDAD",
						selectOnFocus: true,
						allowDecimals: false,
						maxLength: 2,
						width: 210
					}),
/*					new Ext.form.DateField({
						fieldLabel: "Cumpleaños",
						name: "USU_CUMPLE",
						selectOnFocus: true,
						format: "d-m-Y",
						width: 210
					}),*/ 
					new Ext.form.TextField({
						fieldLabel: "Cumpleaños",
						name: "USU_CUMPLE",
						selectOnFocus: true,
						//emptyText: "Día-Mes-Año",
						//format: "d-m-Y",
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "Actividad",
						name: "USU_ACTIVIDAD",
						//emptyText: "Estudias, trabajas...",
						selectOnFocus: true,
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "Detalle de actividad",
						name: "USU_QUEACTIVIDAD",
						//emptyText: "Carrera, profesión...",
						selectOnFocus: true,
						width: 210
					}),
					new Ext.form.TextField({
						fieldLabel: "Talento especial",
						name: "USU_TALENTO",
						//emptyText: "Cantas, bailas, te ríes...",
						selectOnFocus: true,
						width: 210
					}),
					new Ext.form.TextArea({
						fieldLabel: "Pasatiempos, hobbies",
						name: "USU_INFO",
						grow: false,
						maxLength: 300,
						preventScrollbars: true,
						//emptyText: "Cuéntanos qué te gusta hacer... :)",
						height: 100,
						width: 210
					}),
					new Ext.form.TextArea({
						fieldLabel: "Otros",
						name: "USU_MASINFO",
						grow: false,
						maxLength: 300,
						preventScrollbars: true,
						//emptyText: "Tus deseos, anhelos y expectativas para este Encuentro ;)",
						height: 100,
						width: 210
					})
			);
			
		    formulario.render(panelCentro.getEl());
			formulario.on("actioncomplete", function(form, action){
				if(action.type == "submit"){
					ventana.hide();
				};	
			});
			
			ventana.addButton("Cerrar", ventana.hide, ventana);
			ventana.addButton("Aceptar", function(){
				formulario.submit({
					url: "modulos/usuario/insertar.php",
					waitMsg: "Grabando..."
				});									 
			});
			ventana.addKeyListener(13, function(){
				formulario.submit({
					url: "modulos/usuario/insertar.php",
					waitMsg: "Grabando..."
				});	
			});
			
			ventana.on("show", formulario.reset, formulario);
			this.ventanas["registrar"] = ventana;
		};
		this.ventanas["registrar"].show(boton.id);
	},
	
	videos: function(){
		
		var panel = this.innerLayout.findPanel("videos");
		
		if(!panel){
			panel = this.innerLayout.add("center", new Videos());
		}else{
			this.innerLayout.showPanel(panel);
		};
		
		panel.pintar();
		
	}
	
};

Ext.onReady(function(){
	new index();
});