// Muda a Imagem
function MudaImg(img,cod){
	var id = "c_"+cod;
	document.images[id].src="data/thumb_"+img
}

// Mostra info
function MI(cod,m){
	var id = "m_"+cod;
	if(m) Element.hide(id);
	else Element.show(id);
}

// Adicionar Produto
function AddProduto(cod,quant_minima){
	var form = document.post;
	// quantidade
	var nome = "q_"+cod;
	var quantidade = form[nome].value;
	quantidade = quantidade.replace(/\D/gi,"");
	if(!quantidade){
		alert("Digite apenas números na quantidade");
		return false;
	}
	quantidade = parseInt(quantidade);
	if(quantidade<quant_minima){
		alert("A quantidade mínima é "+quant_minima);
		return false;
	}
	if(quant_minima>=1000){
		if(quantidade%100){
			alert("A quantidade precisa ser múltipla de 1000");
			return false;
		}
	}
	// pega a cor
	var valor = "";
	var tem = 1;
	for(var i=0;i<form.elements.length;i++){
		chave = form.elements[i];
		if(chave.type!="radio") continue;
		if(chave.name!="cor["+cod+"]") continue;
		tem++;
		if(!chave.checked) continue;
		valor = chave.value;
	}
	if(!valor && tem){
		alert("Por favor, selecione uma cor");
		return false;
	}
	// envia
	var opt = {
		postBody: "t="+T+"&modo=add&produto="+cod+"&cor="+valor+"&quantidade="+quantidade,
		method: 'post',
		onSuccess: function(t){
			$('budget').innerHTML=Decode(t.responseText);
		}
	}
	new Ajax.Request('products.php', opt);
	return false;
}

// fecha a janela
function Fechar(modo){
	if(modo) window.opener.location.href='products.php?t='+T+'&modo=confirma';
	window.close();
}

// Remover valor do cesto
function RemVal(cod){
	var opt = {
		postBody: "t="+T+"&modo=remove&remover="+cod,
		method: 'post',
		onSuccess: function(t){
			$('budget').innerHTML=Decode(t.responseText);
		}
	}
	new Ajax.Request('products.php', opt);
	return false;
}

// Zoom na foto
function Zoom(cod){
	var id = "c_"+cod;
	var end = document.images[id].src;
	var end = end.replace(/(.*)thumb_/,"");
	window.open("products.php?t="+T+"&modo=foto&foto="+end,"foto","width=100,height=100,left=100,top=100,status=0");
	return false;
}


function Decode(str){
	if(!str) return "";
	var n, strCode, strDecode = "";
	for (n = 0; n < str.length; n++){
		if (str.charAt(n) == "%"){
			strCode = str.charAt(n + 1) + str.charAt(n + 2);
			strDecode += String.fromCharCode(parseInt(strCode, 16));
			n += 2;
		} else {
			strDecode += str.charAt(n);
		}
	}
	return strDecode;
}

// Remover valor do cesto na finalizacao
function RemValConfirma(cod){
	var form = document.post;
	form.target="_self";
	form.modo.value="confirma";
	form.remover.value=cod;
	form.submit();
}

// Envia a ficha
function Enviar(){
	var tam = window.width;
	var alt = window.height;
	if(!tam){
		var tam = screen.width;
		var alt = screen.height;
	}
	window.open("products.php?t="+T,"ficha","width="+tam+",height="+alt+",left=0,top=0,status=0,scrollbars=1");
	document.post.submit();
	location.href='products.php?t='+T+'&modo=finalizar';
}


var Mover = Class.create();
Mover.prototype = {
	speed	: 1,
	ativo	: true,
	tempo	: 0,
	obj		: new Object(),
	// Inicia
	initialize: function(){},
	// init
	Init: function(){
		if(this.Criar()) this.Direita();
	},
	getInnerXY: function() {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		return [ myWidth, myHeight ];
	},
	Criar: function(){
		var linha = "";
		var ms;
		var tam = 0;
		var mx = "";
		if(!msgs.length) return false;
		for(var i=0;i<msgs.length;i++){
			ms = msgs[i];
			if(linha){
				linha += " | ";
				tam += 3;
			}
			if(ms[1]) mx = "<a href="+ms[1]+">"+ms[0]+"</a>";
			else mx = ms[0];
			tam += (ms[0].length*8)+30;
			linha += mx;
		}
		// se o espaco for menor, entao completa com mais campos
		var espaco = this.getInnerXY();
		var ntam = tam*2;
		var mais = "";
		for(var i=2;(espaco[0]+500)>ntam;i++){
			mais += "<div id='texto"+i+"'></div>";
			ntam += tam;
		}
		$('ticket').innerHTML="<div id=\"texto0\"></div><div id=\"texto1\"></div>"+mais;
		var left = 0;
		for(var x=0;x<i;x++){
			$('texto'+x).innerHTML=linha;
			Element.setStyle('texto'+x,{'width': tam+'px', 'left': left+'px'});
			this.obj[x] = $('texto'+x);
			left += tam;
		}
		this.total = x;
		this.dim = tam;
		return true;
	},
	// Move para direita
	Direita: function(){
		if(this.ativo){
			if(!this.temp) this.temp = 0;
			this._MoveDireita();
			this.temp += this.speed;
		}
		this.tempo=setTimeout("Mover.Direita()",20);
	},
	// Move para direita
	_MoveDireita: function(){
		var esq = 0;
		var obj = "";
		for(var i=0;i<this.total;i++){
			obj = this.obj[i];
			var esq = parseInt(obj.style.left);
			if(esq*(-1)>this.dim) esq = this.dim*(this.total-1);
			obj.style.left = esq-this.speed+"px";
		}
	},
	// Pausa
	Pausa: function(modo){
		this.ativo = (modo) ? true : false;
	}
};
Mover = new Mover();

