/**
* -------------------------------------------------------------------------------
* @class CarrinhoCompras
* 	Gerencia o carrinho de compras
* -------------------------------------------------------------------------------
*/
var oCarrinhoCompras = {
	
	hElements: $H({
		dialog: 'carrinho-compras',
		msg: 'msg-carrinho'
	}),
	
	hTimeout: $H({
		id: null,
		time: 8000
	}),
	
	//---------------------------------------------------------------------------
    
	/**
	* Atalho para oCore.Dialog.getCmp
	*/
	getCmp: function(sCmp){
		return oCore.Dialog.getCmp(this.hElements.get('dialog'), sCmp);
	},
	
	/**
	* Cria a janela do carrinho de compras.
	* Uma vez criada, o objeto recebe o event 'carrinho:updated', para facilitar
	* quaisquer tipos de atualização.
	*/
	createDialog: function(){
		oCore.Dialog.create(this.hElements.get('dialog'), {
			title: 'Carrinho de compras',
			content: '',
			width: 580,
			position: [120, 'center'],
			statusBar: _globalSiteNome,
			
			onRender: (function(){
				this.getCmp('content').setStyle('padding:1px 2px;');
			}).bind(this),
			
			onOpen: (function(){
				this.getItensCarrinho();
			}).bind(this),
			
			onClose: (function(){
				oCore.Dialog.updateContent(this.hElements.get('dialog'), '');
			}).bind(this),
			
			onUpdate: (function(){
				if(!oCore.Dialog.isVisible(this.hElements.get('dialog')) || !$('itens-carrinho'))
					return;
				
				this.fireUpdatedCarrinho();
				this.numberMask();
				
				$('bt-calcular-entrega').observe('click', (function(){
					this.setCep();
				}).bind(this));
				
				/*
				- Removido em 13/10/2008 -
				$('cep_entrega').observe('change', (function(){
					this.setCep();
				}).bind(this));
				
				$('formapagto').observe('change', (function(){
					this.setFormaPagto();
				}).bind(this));
				*/
			}).bind(this)
		});
		
		this.getCmp('dialog')
			.observe('carrinho:updated', (function(){
				this.atualizarTotais();
			}).bind(this))
			.observe('carrinho:qtdupdated', (function(){
				this.setCep();
				// this.setFormaPagto();
			}).bind(this));
	},
	
	openDialog: function(){
		sDialog = this.hElements.get('dialog');
		
		if(!oCore.Dialog.isRendered(sDialog))
			this.createDialog();
		
		oCore.Dialog.open(sDialog);
	},
	
	closeDialog: function(){
		oCore.Dialog.close(this.hElements.get('dialog'));
	},
	
	numberMask: function(){
		var oContent = this.getCmp('content');
		var aFields	 = $A( [$('cep_entrega')] );
		
		oCore.arrayPush(aFields, oContent.select('input[name=qtd]'))
			.flatten()
			.invoke('observe', 'keypress', function(oEvent){
				var iTecla	= oEvent.which || oEvent.keyCode;
				var iLimite = ('cep_entrega' == this.identify()) ? 8 : 3;
				
				switch(iTecla){
					case Event.KEY_BACKSPACE:
					case Event.KEY_DELETE:
					case Event.KEY_LEFT:
					case Event.KEY_RIGHT:
					case Event.KEY_HOME:
					case Event.KEY_END:
					case Event.KEY_TAB:
						return true;
					
					case Event.KEY_RETURN:
						if('cep_entrega' == $(this).identify()){
							oCarrinhoCompras.setCep();
							$('formapagto').focus();
						}
						else{
							oEvent.stop();
						}
						break;
					
					default:
						if((48 > iTecla || 57 < iTecla) || iLimite <= $F(this).length)
							oEvent.stop();
						break;
				}
		});
		
		delete oContent;
		delete aFields;
	},
	
	//---------------------------------------------------------------------------
    
	showMensagem: function(sType, sMsg){
		var oMsgElement = $(this.hElements.get('msg'));
		
		$w('success warning error').each(function(sClassName){
			oMsgElement.removeClassName(sClassName);
		});
		
		oMsgElement.addClassName(sType).update(sMsg).show();
		this.fireUpdatedDialog();
		
		this.hTimeout.set('id', setTimeout('oCarrinhoCompras.hideMensagem();', this.hTimeout.get('time')));
	},
	
	hideMensagem: function(){
		$(this.hElements.get('msg')).update('').hide();
		this.fireUpdatedDialog();
		
		clearTimeout(this.hTimeout.get('id'));
	},
	
	//---------------------------------------------------------------------------
    
	getItensCarrinho: function(){
		xajax_getItensCarrinho();
	},
	
	atualizarTotais: function(){
		xajax_atualizarTotais();
	},
	
	add: function(iCodtamcor){
		xajax_addItem(iCodtamcor);
	},
	
	rem: function(iCodtamcor, oElement){
		$(oElement).up('tr').remove();
		xajax_remItem(iCodtamcor);
		
		this.fireUpdatedDialog();
	},
	
	setQtd: function(iCodtamcor, iQtd){
		xajax_setQtd(iCodtamcor, iQtd);
	},
	
	setFormaPagto: function(){
		var iCodformapagto = $F('formapagto');
		
		if(!iCodformapagto.empty())
			xajax_setFormaPagto($F('formapagto'));
	},
	
	setCep: function(){
		var hForm = $('frmCarrinho').serialize(true);
		
		var sCep = hForm['cep_entrega'];
		var iTipoEntrega = hForm['tipo_entrega'];
		
		if(sCep.empty())
			return;
		
		$('info-aguarde-cep').setStyle('display:inline;');
		xajax_setCep(sCep, iTipoEntrega);
	},
	
	//---------------------------------------------------------------------------
    
	finalizar: function(){
		location.href = _globalEnderecoNormal + 'compras/check_carrinho.php';
	},
	
	//---------------------------------------------------------------------------
    
	fireUpdatedCarrinho: function(){
		this.getCmp('dialog').fire('carrinho:updated');
	},
	
	fireQtdUpdatedCarrinho: function(){
		this.getCmp('dialog').fire('carrinho:qtdupdated');
	},
	
	fireUpdatedDialog: function(){
		this.getCmp('dialog').fire('dialog:updated');
	}
	
};
