// FUNCOES QUE CRIAM O DRAG AND DROP DO CARRINHO.
var $count = 0;

$(function() {
	// there's the gallery and the trash
	var $carrinho = $('#carrinho'), $produtos = $('#produtos');
	
	// let the gallery items be draggable
	$('li',$produtos).draggable({
		cancel: 'a.ui-icon',// clicking an icon won't initiate dragging
		revert: 'invalid', // when not dropped, the item will revert back to its initial position
		helper: 'clone',
		cursor: 'move', 
		addClasses: true,
		frameClass: 'teste'
	});
	
	// let the trash be droppable, accepting the product items
	$carrinho.droppable({
		accept: '#produtos > div li',
		drop: function(ev, ui) {
			carrinho(ui.draggable);
		}
	});

	// let the gallery be droppable as well, accepting items from the trash
	$produtos.droppable({
		accept: '#carrinho li',
		activeClass: 'produtos-ativo',
		drop: function(ev, ui) {
			removeCarrinho(ui.draggable);
		}
	});
	
	function carrinho($item) {
		
		var remover_produto = "<a href='javscript:' title='Remover Produto' id='remover_produto' alt='Remover Produto'>&nbsp;</a>";
		
		//$texto = "teste de escrita";
		
		//$item.attr("onmousemove","showToolTip(event,'" + $texto + "', 'bubble_tooltip', 'bubble_tooltip_content'); return false").attr("onmouseout","hideToolTip('bubble_tooltip')");
		
		$item.fadeOut(function() {
			var $list = $('ul',$carrinho).length ? $('ul',$carrinho) : $('<ul/>').appendTo($carrinho);
			$item.find('img').css('height', '96px');
			$item.append(remover_produto).appendTo($list).fadeIn("fast");
		});
		
		$count = $count + 1;
		$('#qtd_produtos').find('span').remove();
		$('#qtd_produtos').append("<span id='qtd_produtos'>"+$count+"</span>");
		
		if($count > 0)
			$carrinho.animate({'background-color':'#fff'});
	}

	// image recycle function
	function removeCarrinho($item) {
		
		$item.find('#remover_produto').remove();
		
		$div = "div #div_" + $item.attr("id");
		
		$item.fadeOut(function() {
			$item.find('img').css('height','127px').end().appendTo($div).fadeIn("fast");
		});
		
		//hideToolTip('bubble_tooltip');
		//$item.attr("onmousemove","").attr("onmouseout","");
		
		$count = $count - 1;
		$('#qtd_produtos').find('span').remove();
		$('#qtd_produtos').append("<span id='qtd_produtos'>"+$count+"</span>");
		
		if($count == 0)
			$carrinho.css('background-color', '');
	}
	
	// resolve the icons behavior with event delegation
	$('ul.produtos > div li').click(function(ev) {
		var $item = $(this);
		var $target = $(ev.target);

		if ($target.is('#remover_produto')) {
			removeCarrinho($item);
		}
		
		if ($target.is('ul.produtos > div li > img')) {
			carrinho($item);
		}

		return false;
	});
});

// CRIA O TOOLTIP (O BALAO DE INFORMACOES)
function showToolTip(e,text,id1, id2){
	if(document.all)e = event;

	var obj = document.getElementById(id1);
	var obj2 = document.getElementById(id2);
	obj2.innerHTML = text;
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0;
	var leftPos = e.clientX - 100;
	if(leftPos<0)leftPos = 0;
	obj.style.left = leftPos + 'px';
	obj.style.top = e.clientY - obj.offsetHeight -1 + st + 'px';
}

// ESCONDE O TOOLTIP
function hideToolTip(id)
{
	document.getElementById(id).style.display = 'none';
}

// REORGANIZA OS CAMPOS NECESSÁRIOS PARA O PAGSEGURO DO FORM DO CARRINHO DE ACORDO COM A QTD DE ELEMENTOS ADICIONADOS
var $id_elemento = 0;
function organiza_dados(formulario) {
	
	var elementos = formulario.elements;
  	
	for(var i = 0; i < elementos.length; i++){
		
		
		//alert(elementos[i].getAttribute("name") + " valor: " + elementos[i].getAttribute("value"));
		
		if(elementos[i].getAttribute("name").substring(0,7) == "item_id") {
			$id_elemento == 0 ? $id_elemento = i + 1 : $id_elemento = $id_elemento + 1;
			
			elementos[i].setAttribute("name", "item_id_" + $id_elemento);
			//elementos[i].setAttribute("value", $id_elemento);
		}
		if(elementos[i].getAttribute("name").substring(0,10) == "item_descr") {
			elementos[i].setAttribute("name", "item_descr_" + $id_elemento);
		}
		if(elementos[i].getAttribute("name").substring(0,10) == "item_quant") {
			elementos[i].setAttribute("name", "item_quant_" + $id_elemento);
		}
		if(elementos[i].getAttribute("name").substring(0,10) == "item_valor") {
			elementos[i].setAttribute("name", "item_valor_" + $id_elemento);
		}
		
	}
	return true; 
}

// FUNCAO QUE ABRE O POPUP
function abre_popup(URL, width, height, scrollbar) {

   config=""
   config+="toolbar=no,";
   config+="resizable=no," 
   config+="scrollbars="+scrollbar+","
   config+="width="+width+","
   config+="height="+height
   var window=open(URL,"",config);
   window.focus();
   window.moveTo(window.parent.screenX+100, window.parent.screenY+100);

}

function suporte(){
	
	config=""
	config+="toolbar=no,";
	config+="resizable=no," 
	config+="scrollbars=no,"
	config+="width=540,"
	config+="height=430"
	var window=open("atendimento/suporte.php?page=indexCliente.php","suporte.php",config);
	window.focus();
}

function abre_link($link) {
	$('#conteudo_pagina').load($link + '.php');
}
