Ext.onReady(function(){
	Ext.QuickTips.init();
	//Horizontal menu. Associated with menu.css
	new Ext.ux.Menu('simple-horizontal-menu', {
		transitionType	: 'slide',
		direction		: 'horizontal', // default
		delay			: 0.2,          // default
		autoWidth		: true,         // default
		transitionDuration: 0.3, 		// default
		animate			: true,         // default
		currentClass	: 'current'     // default
	});
	
	//Main panel for loading the different screens
    var mainPanel = {
    	region			: 'center',
    	xtype			: 'container',
    	title			: 'HOME',
		layout			: 'fit',
		border			: false,
    	id				: 'mainPanel',
        margins			: '0 5 5 0',
    	//autoHeight		: true,
    	autoScroll		: true,
    	defaults:{ 
    		autoScroll	: true 
    	},
    	style:{ 
    		//background 	: '#DFE8F6',
    		background 	: '#EDEDED',
    		height 		: '100%'
    	},
    	title			: 'section',
    	closable		: false
    };

	//Viewport to force the application to use all screen available
    var viewport = new Ext.Viewport({
        layout			: 'border',
renderTo: Ext.getBody(),        
//        forceFit		: true,
//        hideMode		: "offsets",
        defaults: {
        	autoScroll	: true
        }, 
        items: [{
            	xtype		: 'box',
            	region		: 'north',
            	applyTo		: 'header',
            	height		: 128,
            	split		: false
        	}, 
        	mainPanel
        ]
    });

	//Initialize the main panel content
	loadMainPanel('caps_page');
	
	Ext.ux.FloatClear = Ext.extend(Ext.Component, {
		cls: 'x-clear'
	});
	Ext.reg('float-clear', Ext.ux.FloatClear);
	
	Ext.ux.FloatingFormLayout = Ext.extend(Ext.layout.FormLayout, {
		getLabelStyle: function(s, field) {
			var labelStyle = this.labelStyle;
			if (this.labelAlign !== 'top') {
				if (field.labelWidth) {
					labelStyle = 'width:' + field.labelWidth + 'px;';
				}
			}
			var ls = '', items = [labelStyle, s];
			for (var i = 0, len = items.length; i < len; ++i) {
				if (items[i]) {
					ls += items[i];
					if (ls.substr(-1, 1) != ';') {
						ls += ';';
					}
				}
			}
			return ls;
		},
	
		getElementStyle: function(field) {
			if (this.labelAlign === 'top' || !field.labelWidth) {
				return this.elementStyle;
			} else {
				var pad = Ext.isNumber(this.labelPad) ? this.labelPad : 5;
				return 'padding-left:' + (field.labelWidth + pad) + 'px';
			}
		},
	
		getTemplateArgs: function(field) {
			var noLabelSep = !field.fieldLabel || field.hideLabel;
	
			return {
				id: field.id,
				label: field.fieldLabel,
				itemCls: (field.itemCls || this.container.itemCls || '') + (field.hideLabel ? ' x-hide-label' : ''),
				clearCls: field.clearCls || 'x-form-clear-left',
				labelStyle: this.getLabelStyle(field.labelStyle, field),
				elementStyle: this.getElementStyle(field) || '',
				labelSeparator: noLabelSep ? '' : (Ext.isDefined(field.labelSeparator) ? field.labelSeparator : this.labelSeparator)
			};
		}
	});
	Ext.Container.LAYOUTS['floating-form'] = Ext.ux.FloatingFormLayout;
	
	Ext.ux.FloatingFormPanel = Ext.extend(Ext.form.FormPanel, {
		cls: 'floating-form',
		layout: 'floating-form',
		lookupComponent: function(comp) {
			if (Ext.isString(comp)) {
				switch (comp) {
					case "|":
						comp = new Ext.ux.FloatClear();
						break;
				}
			}
			return Ext.ux.FloatingFormPanel.superclass.lookupComponent.call(this, comp);
		}
	});
	Ext.reg('floating-form', Ext.ux.FloatingFormPanel);
	
	Ext.form.ImageField = Ext.extend(Ext.form.Field, {
		autoCreate: {tag: 'img'}
		,setValue: function(new_value){
			if (new_value == undefined || new_value == null || new_value == 0) {
				this.el.dom.src = '/images/users/defuser1.jpg';
			} else {
				this.el.dom.src = 'phplib/users/user_image.php?ID=' + new_value;
				this.el.dom.onerror = 'this.src=\'images/users/defuser1.jpg\'';
			}
		}
		,initValue : function(){
			  this.setValue(this.value);
		}
	
		,initComponent: function() {
			Ext.apply(this, {
	
			});
	
			Ext.form.ImageField.superclass.initComponent.apply(this);
		}
	});
	Ext.reg('image-field', Ext.form.ImageField);
	
	
	Ext.form.CapImageField = Ext.extend(Ext.form.Field, {
		defaultAutoCreate: {tag: 'img', height: '150', width: '150'},
							id				 : 'cap_own_image',
							fieldLabel		 : '',
							name			 : 'cap_own_image'
		,setValue: function(new_value){
			if (new_value == undefined || new_value == null || new_value == 0) {
				this.el.dom.src = 'images/chapas/defcap.jpg';
			} else {
				this.el.dom.src = 'images/chapas/' + new_value + '.jpg';
				this.el.dom.onerror = 'this.src=\'images/chapas/defcap.jpg\'';
			}
		}
		,initValue : function(){
			  this.setValue(this.value);
		}
	
		,initComponent: function() {
			Ext.apply(this, {
	
			});
	
			Ext.form.ImageField.superclass.initComponent.apply(this);
		}
	});
	Ext.reg('cap-image-field', Ext.form.CapImageField);
	
});

function loadMainPanel( url, params ) {
	Ext.get('mainPanel').load({
		url: 'phplib/' + url + '.php?' + params,
		scripts: true,
		text: lang.message_loading + '...'
	});
}


function regenerate(){
	var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"Please wait..."});
	myMask.show();
	Ext.Ajax.request({
		url : 'phplib/printer/pdf_functions.php?action=regenerate',
	  	method: 'POST',
		success: function ( result, request ) {
			try{   
				r = Ext.decode(result.responseText);
				if( r.success ) {
					myMask.hide();
				} else {
					myMask.hide();
					Ext.Msg.alert('Failure', "Se ha producido un error");
				}
			} catch(e) {   
				myMask.hide();
				Ext.Msg.alert('Failure', "Se ha producido un error");
			}                 
		},
		failure: function ( result, request ) {
			myMask.hide();
			Ext.Msg.alert('Failure', "Se ha producido un error");
		}
	});
}

function exportLabelForJS(){
	var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"Please wait..."});
	myMask.show();
	Ext.Ajax.request({
		url : 'phplib/labels/label_functions.php?action=export',
	  	method: 'POST',
		success: function ( result, request ) {
			try{   
				r = Ext.decode(result.responseText);
				if( r.success ) {
					myMask.hide();
				} else {
					myMask.hide();
					Ext.Msg.alert('Failure', "Se ha producido un error");
				}
			} catch(e) {   
				myMask.hide();
				Ext.Msg.alert('Failure', "Se ha producido un error");
			}                 
		},
		failure: function ( result, request ) {
			myMask.hide();
			Ext.Msg.alert('Failure', "Se ha producido un error");
		}
	});
}

