/**
* -------------------------------------------------------------------------------
* item.js
* 	Utilizado na página /produtos/item/index.jphp
* -------------------------------------------------------------------------------
*/
var oProduto = {
	
	/**
	* Array com as informações deste produto
	*/
	aFotos: [],
	aCoresTamanhos: [],
	aFormasPagto: [],
	/**
	* Adiciona as cores e tamanhos do produto ao 'buffer'
	*/
	storeCorTamanho: function(iCodtamcor, sTamanho, sCor, sSit, color){
        var Corhash = '';
        if(sCor != ''){
            Corhash = '/';
        }
        
		this.aCoresTamanhos.push($H({
			codtamcor: iCodtamcor,
			tamanho: sTamanho,
			cor: sCor+Corhash,
            situacao: sSit,
            color: color
		}));
	},
	
	/**
	* Adiciona as formas de pagamento ao 'buffer'
	*/
	storeFormaPagto: function(iCodforma, sDescricao){
		this.aFormasPagto.push($H({
			codforma: iCodforma,
			descricao: sDescricao
		}));
	},
	
	/**
	* Adiciona fotos do produto no 'buffer'
	*/
	storeFoto: function(iCodfoto, sImgTagPq, sImgTagGr){
		this.aFotos.push($H({
			codfoto: iCodfoto,
			pq: sImgTagPq,
			gr: sImgTagGr
		}));
	},
	
	/**
	* Insere todas as fotos do produto, exceto a que código for igual a
	* 'iCodfotoPrincipal'
	*/
	setFotos: function(){
		if( !$('thumbs') )
			return;
		
		var iCodfotoPrincipal = arguments[0] || 0;
		var sId;
		
		$A(this.aFotos).each(function(hFoto){
			sId = 'foto-produto-' + hFoto.get('codfoto');
			
			if(!$(sId)){
				$('thumbs').insert( new Element('a', {id: sId, href: '#'}) );
				$(sId).addClassName('foto').update(unescape(hFoto.get('pq')));
				
				$(sId).observe('click', function(oEvent){
					oEvent.stop();
					oProduto.setFotoPrincipal(hFoto.get('codfoto'));
				});
			}
			
			if(iCodfotoPrincipal != hFoto.get('codfoto'))
				$(sId).show();
			else
				$(sId).hide();
		});
	},
	
	/**
	* Atualiza a foto principal na página do produto
	*/
	setFotoPrincipal: function(iCodfoto){
		if( !$('foto-principal') )
			return;
		
		var aFotos	= $A(this.aFotos);
		var sImgTag = null;
        
		if( 0 == aFotos.size() ){
			this.semIlustracao();
			return;
		}
		
		aFotos.each(function(hFoto){
			if(iCodfoto == hFoto.get('codfoto')){
				sImgTag = hFoto.get('gr');
			}
		});
		
		$('foto-principal').update(unescape(sImgTag));
		this.setFotos(iCodfoto);
	},
	
	/**
	* Ajusta o tamanhos do quadro das miniaturas
	*/
	setThumbsFrame: function(){
		if( !$('thumbs') )
			return;
		
		var aChildren = $('thumbs').childElements();
		var iChildren = aChildren.size() - 1; //a foto principal, mesmo estando no objeto, não conta pois ela está com 'display:none'
		
		if(0 == iChildren)
			return;
		
		var iThumbWidth = aChildren.first().getWidth() + ((oUtils.browser.ie6) ? 4 : 0); //+4 no IE6 para corrigir erros da borda, que não é contabilizada
		var iMargins	= iChildren * 8;
		var iWidth		= (iChildren * iThumbWidth) + iMargins;
		
		$('thumbs').setStyle('width: ' + iWidth.toString() + 'px; margin-left: -' + (iWidth/2).toString() + 'px');
	},
	
	semIlustracao: function(){
		if($('foto-principal') && $('thumbs')){
			$('foto-principal').addClassName('imagem-indisponivel');
			$('thumbs').remove();
		}
	},
	
	replaceCarrinhoImage: function(oElement){
		$(oElement).down().writeAttribute('src', _globalEnderecoImages + 'botoes/nocarrinho.jpg');
	},
	
	//---------------------------------------------------------------------------
    
	setSelectBox: function(){
		oCore.selectFieldData('cores-tamanhos', false, {
			collection: this.aCoresTamanhos,
			template: '#{cor}#{tamanho}#{situacao}',
			value: '#{codtamcor}'
		});
		
		oCore.selectFieldData('formas-pagamento', false, {
			collection: this.aFormasPagto,
			template: '#{descricao}',
			value: '#{codforma}'
		});
	},
	
	//---------------------------------------------------------------------------
	
	indiqueAmigo: function(hInfo){
		var sIdDialog = 'indique-amigo';
		hInfo = $H(hInfo);
        
		hInfo.set('descricao', unescape(hInfo.get('descricao')));
        var selection = {codproduto: hInfo.get('codproduto') , descricao: hInfo.get('descricao')};
		var sContent = ( new Template(xajax__js_htmlFile('html/indique_amigo')) ).evaluate(selection);
        
		oCore.Dialog.create(sIdDialog, {
			title: 'Indique para um amigo',
			content: sContent,
			buttons: {
				btEnviar: {
					label: 'Enviar mensagem',
					command: function(){
						hValidate = {
							f_nome_remetente: 'vazio',
							f_email_remetente: 'vazio|email',
							f_nome_destinatario: 'vazio',
							f_email_destinatario: 'vazio|email'
						};
                        
						if( oValidaForm.validate('frmAmigo', hValidate) ){
							xajax_enviarIndiqueAmigo(xajax.getFormValues('frmAmigo'));
                        }
					}
				},
				
				btCancelar: {
					label: 'Cancelar',
					command: 'fnClose'
				}
			},
			
			onRender: function(){
				oCore.Dialog.open(sIdDialog);
			},
			
			onOpen: function(){
				$('f_nome_remetente').focus();
			},
			
			onClose: function(){
				document.forms['frmAmigo'].reset();
			}
		});
	},
	
	//---------------------------------------------------------------------------
    
	avisarDisponibilidade: function(hInfo){
		var sIdDialog = 'avisar-disponibilidade';
		hInfo = $H(hInfo);
		
		hInfo.set('descricao', unescape(hInfo.get('descricao')));

		var sContent = ( new Template(xajax__js_htmlFile('html/avisar_disponibilidade')) ).evaluate({descricao: hInfo.get('descricao'), codtamcor: hInfo.get('codtamcor')});

        oCore.Dialog.create(sIdDialog, {
			title: 'Avisar disponibilidade',
			content: sContent,
			buttons: {
				btEnviar: {
					label: 'Enviar solicitação',
					command: function(){
						hValidate = {
							f_nome: 'vazio',
							f_cpf: 'vazio|cpf',
							f_email: 'vazio|email',
							f_telefone: 'vazio' 
						};
						
						if( oValidaForm.validate('frmAvisarDisponibilidade', hValidate) )
							xajax_enviarAvisarDisponibilidade(xajax.getFormValues('frmAvisarDisponibilidade'));
					}
				},
				
				btCancelar: {
					label: 'Cancelar',
					command: 'fnClose'
				}
			},
			
			onRender: function(){
				oMascaras.start('frmAvisarDisponibilidade', {
					f_cpf: 'cpf',
					f_telefone: 'telefone'
				});
				
				oCore.Dialog.open(sIdDialog);
			},
			
			onOpen: function(){
				$('f_nome').focus();
			},
			
			onClose: function(){
				document.forms['frmAvisarDisponibilidade'].reset();
			}
		});
	}
	
};
