var enviarAmigo = {
	'opened': false,
	'show': function(){
		this.opened = true;
		bgSite.show();
		
		$('seuemail').value = $('seunome').value = $('nomeemigo').value = $('emailamigo').value = "";
		
		$('detalhesEnviarAmigo').style.display = "block";
		this.alinha();
	},
	'alinha' : function(){
		if(!this.opened) return;
		var SC = getScroll();
		var VS = getDocVisibleSize();
		var SZ = getSize('detalhesEnviarAmigo');
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-(SZ.y))/2;
		$('detalhesEnviarAmigo').style.top = (newY+SC.y)+"px";
		$('detalhesEnviarAmigo').style.left = (newX+SC.x)+"px";
		bgSite.alinha();
	},
	'hide': function(){
		this.opened = false;
		$('detalhesEnviarAmigo').style.top = "-1000px";
		$('detalhesEnviarAmigo').style.lef = "-1000px";
		$('detalhesEnviarAmigo').style.display = "none";
		bgSite.hide();
	},
	'send': function(){
		if($('seunome').value.isWhite())return;
		if($('seuemail').value.isWhite() || !validateEmail($('seuemail').value))return;
		if($('nomeemigo').value.isWhite())return;
		if($('emailamigo').value.isWhite() || !validateEmail($('emailamigo').value))return;

		var ajaxEnviarAmigo = new ajax({
			'method': 'POST',
			'url': '/enviarAmigo/',
			'oncomplete': function() {
				enviarAmigo.hide();
				delete ajaxEnviarAmigo;				
			}
		});
		
		ajaxEnviarAmigo.push('seunome', $('seunome').value);
		ajaxEnviarAmigo.push('seuemail', $('seuemail').value);
		ajaxEnviarAmigo.push('nomeemigo', $('nomeemigo').value);
		ajaxEnviarAmigo.push('emailamigo', $('emailamigo').value);
		ajaxEnviarAmigo.push('idProd', PRODUTO.id);
		ajaxEnviarAmigo.open();
	}
}

///////////////////////////////////////////////////
// PRODUTO OBJECT
///////////////////////////////////////////////////
var produto = function(){}

produto.prototype = {
	'block': false,	
	'add': function(){
		var obj = arguments[0];
		var local = arguments[1] || this;
		for (var k in obj) local[k] = obj[k];
		if(!this.temCor && !this.temVoltagem && !this.temTamanhos){
			this.loadDataByAtt();
		}else{
			if(this.temCor){
				if(this.corId){
					this.trocarCor(this.corId, true);
				}
			}
			if(this.temVoltagem == true && this.temCor == false && this.temTamanhos == false){
				this.loadDataByAtt();
			}
		}
	},
	'galeria': {
		'name':'PRODUTO',
		'opened':false,
		'data': {},
		'media': function(id){
			$('contentFotoMedia').style.display = "none";
			$('loadingImg').style.display = "block";
		
			var imgLoader = new Image();
			imgLoader.onload = function(){
				$('contentFotoMedia').src = this.src;
				$('loadingImg').style.display = "none";
				$('contentFotoMedia').style.display = "block";
			}
			imgLoader.src = this.data.fotos.fotomedia[id];
		},
		'grande': function(id){
			$('detalhesGaleriaContentFotoGrande').style.display = "none";
			$('detalhesGaleriaLoadingImg').style.display = "block";
		
			var imgLoaderGrande = new Image();
			imgLoaderGrande.onload = function(){
				$('detalhesGaleriaContentFotoGrande').src = this.src;
				$('detalhesGaleriaContentFotoGrande').style.display = "block";
				$('detalhesGaleriaLoadingImg').style.display = "none";
			}
			imgLoaderGrande.src = this.data.fotos.fotogrande[id];
		},
		'open': function(){
			bgSite.show();
			
			$('detalhesGaleriaTitulo').innerHTML = $('tituloPreco').innerHTML;
			
			$('detalhesGaleria').style.display = "block";			
			
			var strRetornoFotos = '';
			for(var i=0; i<this.data.fotos.fotopequena.length; i++){
				var f = this.data.fotos.fotopequena[i];
				strRetornoFotos += '<img src="'+f+'" width="57" height="42" style="margin-left:10px; cursor:pointer;  border:solid 1px #E3E3E3;" onclick="'+this.name+'.galeria.grande(\''+i+'\');" />';
			}
			$('detalhesGaleriaContentThumbs').innerHTML = strRetornoFotos;
			
			this.opened = true;
			
			this.alinha();
			
			this.grande(0);
		},
		'alinha': function(){
			if(!this.opened) return;
			var SC = getScroll();
			var VS = getDocVisibleSize();
			var SZ = getSize('detalhesGaleria');
			newX = (VS.x-SZ.x)/2;
			newY = (VS.y-(SZ.y))/2;
			$('detalhesGaleria').style.top = (newY+SC.y)+"px";
			$('detalhesGaleria').style.left = (newX+SC.x)+"px";
			bgSite.alinha();
		},
		'hide': function(){
			this.opened = false;
			
			$('detalhesGaleriaTitulo').innerHTML = "";
			 
			$('detalhesGaleria').style.top = "-1000px";
			$('detalhesGaleria').style.lef = "-1000px";
			$('detalhesGaleria').style.display = "none";
			bgSite.hide();
		},
		'onClick': function(bt){ this.hide(); }
	},
	'loadDataByAtt': function(){
		var objThis = this;
		var ajaxComprar = new ajax({
			'method': 'GET',
			'url': '/trocarCorXML/',
			'oncomplete': function() {				
				var data = objThis.galeria.data = json.decode(this.text);
				delete ajaxComprar;				
			}
		});
		ajaxComprar.push('idProd', this.id);
		ajaxComprar.push('idAtt', this.atributo);
		ajaxComprar.open();
	},
	'trocarTamanho': function(id,idFor){
		var i = 0;
		while($('tamanho'+(i))){
			$('tamanho'+(i)).className = 'normal';
			i++;
		}
		
		this.tamanhoId = id;
		$('tamanho'+(idFor)).className = 'ativo';
			
	},
	'trocarCor': function(){
		var idCor = arguments[0];
		var dataOnly = arguments[1] || false;
		
		if(dataOnly == false){
			if(this.temTamanhos) $('tamanhos').innerHTML = "";		
			if(this.temVoltagem) $('contentVoltagem').innerHTML = "";
			
			$('boxThumbFotos').innerHTML = "";
			
			this.corId = '';
			this.tamanhoId = '';
			this.voltagemId = '';
		}
		
		var objThis = this;
		var ajaxCor = new ajax({
			'method': 'GET',
			'url': '/trocarCorXML/',
			'oncomplete': function() {				
				var data = objThis.galeria.data = json.decode(this.text);
				if(dataOnly == true) return;
				
				objThis.corId = idCor;
				
				if(objThis.temTamanhos) {
					if(data.tamanhos.length > 0){
						var strRetorno = "";
						for(var i=0; i<data.tamanhos.length; i++){
							var v = data.tamanhos[i];
	
							objThis.tamanhoId = (data.tamanhos.length == 1)?v.id:objThis.tamanhoId;
							var css = (data.tamanhos.length == 1)?'ativo':'normal';
							
							strRetorno += '<div id="tamanho'+i+'" class="'+css+'" onclick="PRODUTO.trocarTamanho(\''+v.id+'\',\''+i+'\');">'+v.titulo+'</div> ';
						}
						$('tamanhos').innerHTML = strRetorno;
					}
				}
				
				if(objThis.temVoltagem) {
					if(data.voltagens.length > 0){
						var strRetorno = "";
						for(var i=0; i<data.voltagens.length; i++){
							var v = data.voltagens[i];
	
							objThis.voltagemId = (data.voltagens.length == 1)?v.id:objThis.voltagemId;
							var chk = (data.voltagens.length == 1)?' checked="checked"':'';
							
							strRetorno += '<label><input class="radio" type="radio" name="voltagem" value="'+v.id+'" id="voltagem_'+v.id+'"'+chk+' onclick="'+objThis.name+'.voltagemId = \''+v.id+'\';" /> '+v.titulo+'</label><br />';
						}
						$('contentVoltagem').innerHTML = strRetorno;
					}
				}
				
				if(data.fotos.fotopequena.length > 0){
					var strRetornoFotos = '<img src="/media/img/loja/lupa.gif" width="57" height="42" style="cursor:pointer; border:solid 1px #E3E3E3;" onclick="PRODUTO.galeria.open();" />';
					for(var i=0; i<data.fotos.fotopequena.length; i++){
						var f = data.fotos.fotopequena[i];
						strRetornoFotos += '<img src="'+f+'" width="57" height="42" style="margin-left:10px; cursor:pointer;  border:solid 1px #E3E3E3;" onclick="'+objThis.name+'.galeria.media(\''+i+'\');" />';
					}
					$('boxThumbFotos').innerHTML = strRetornoFotos;
				}
				
				objThis.galeria.media(0);
				
				delete ajaxCor;
			}
		});
		ajaxCor.push('idProd', this.id);
		ajaxCor.push('idCor', idCor);
		ajaxCor.open();
	},
	'validar': function(){
		
		if(this.temVoltagem == true){
			if(radio('voltagem') == null || this.voltagemId == null || this.voltagemId == ""){
				SA.show('Escolha uma voltagem!');
				return false;
			}
		}
		
		if(this.temCor == true){
			if(this.corId == null || this.corId == ""){
				SA.show('Escolha uma cor!');
				return false;
			}
		}
		
		if(this.temTamanhos == true){
			if(this.tamanhoId == null || this.tamanhoId == ""){
				SA.show('Escolha um tamanho!');
				return false;
			}
		}
		
		return true;
	},
	'comprar': function(){
		var objThis = this; 
		if(this.block) return;
		setAlpha('btComprar',50);
		if(this.validar() == false){
			setAlpha('btComprar',100);
			return;
		}
		var ajaxComprar = new ajax({
			'method': 'GET',
			'url': '/carrinho/incluirProdutoXml',
			'oncomplete': function() {
				setAlpha('btComprar',100);
				objThis.block = false;
				location = "/carrinhos";
				delete ajaxComprar;
			}
		});
		ajaxComprar.push('idProduto', this.id);
		ajaxComprar.push('qtd', $('qtd').value);
		
		if(!this.temCor && !this.temVoltagem && !this.temTamanhos){
			ajaxComprar.push('idAtributo', this.atributo);
		}else{
			if(this.temCor) ajaxComprar.push('idCor', this.corId);
			if(this.temVoltagem) ajaxComprar.push('idVoltagem', this.voltagemId);
			if(this.temTamanhos) ajaxComprar.push('idTamanho', this.tamanhoId);
		}
		
		this.block = true;
		ajaxComprar.open();
	}
}

var PRODUTO = new produto();

var videoViemo = {
	'opened': false,
	'show': function(id){
		this.opened = true;
		bgSite.show();
		
		$('videoVimeo').style.display = "block";
		
		vimeo = 'http://vimeo.com/moogaloop.swf?clip_id='+id+'&amp;server=vimeo.com&amp;autoplay=0&amp;fullscreen=1&amp;md5=0&amp;show_portrait=0&amp;show_title=0&amp;show_byline=0&amp;context=undefined&amp;context_id=undefined&amp;force_embed=1&amp;multimoog=&amp;color=ff9933&amp;force_info=undefined';
		
		mostraFlash("contentVideo", vimeo, 400, 302, {'scale':'noscale', 'wmode':'transparent', 'allowfullscreen':'true', 'AllowScriptAccess':'always'} );
		
		this.alinha();
	},
	'alinha' : function(){
		if(!this.opened) return;
		var SC = getScroll();
		var VS = getDocVisibleSize();
		var SZ = getSize('videoVimeo');
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-(SZ.y))/2;
		$('videoVimeo').style.top = (newY+SC.y)+"px";
		$('videoVimeo').style.left = (newX+SC.x)+"px";
		bgSite.alinha();
	},
	'hide': function(){
		this.opened = false;
		$('videoVimeo').style.top = "-1000px";
		$('videoVimeo').style.lef = "-1000px";
		$('videoVimeo').style.display = "none";
		
		bgSite.hide();
		try{ $('videoVimeo').removeChild($('videoVimeomovie')); } catch(e){}
	}
}

window.onload = function(e){}

window.onresize = function(){
	PRODUTO.galeria.alinha();
	enviarAmigo.alinha();	
}

window.onstopscroll = function(){
	PRODUTO.galeria.alinha();
	enviarAmigo.alinha();
}