$(document).ready(function() {
	// there's the gallery and the trash
	var $gallery = $('#gallery'), $carro = $('#compra_loja');

	// let the gallery items be draggable
	$('a.imgProdutoList, .moveCarrinho',$gallery).draggable({
		revert: 'invalid', // when not dropped, the item will revert back to its initial position
		helper: 'clone',
		cursor: 'move',
		start: function (event, ui) {
			$('div.ajaxLoaderDrop').show();
		}
	});

	// let the trash be droppable, accepting the gallery items
	$carro.droppable({
		accept: '#gallery a.imgProdutoList, .moveCarrinho',
		drop: function(ev, ui) {
			$('div.ajaxLoaderDrop').show();
			deleteImage(ui.draggable);
		}
	});

	// image deletion function
	function deleteImage($item) {
	
		var cor_nobrinde = $item.parent().find(".colorSet img:first").attr("rel");
		$.ajax({
			type: "POST",
			url: "/loja/ajaxServer.php",
			data: ({idprod : $item.attr('id'), cor: cor_nobrinde, req : 'verificaCarro'}),
			success: function(msg){
				 //if(msg=="true"){
					$carro.append($('.intermedio')).fadeIn(function(){
						$('.intermedio').animate({ width: '70px' }).find('img').animate({ height: '60px' });
					});
					$carro.load('/catalogo/droppable_carrinho.php?onlyItems=1', function() {
						$('div.ajaxLoaderDrop').hide();
					});
				//}
			}
		});
	}
});

function removeProdCarro(id,opcionais){
	$.ajax({
		type: "POST",
		url: "/loja/ajaxServer.php",
		data: ({idprod : id, opcionais: opcionais , req : 'removeCarro'}),
		success: function(msg){ 
			$('.pcarro-'+id).fadeOut(500,function(){ $(this).remove(); });
			$('#compra_loja').load('/catalogo/droppable_carrinho.php?onlyItems=1');
		}
	});
}

function selTipoImpressao(obj) {
	if (obj.value == "more") {
		$(obj).load('/catalogo/detalhes_produto.ajax.php?a=tiposimpressao&id=' + $('#calc_id_produto').val());
	} else {
		$('#input_tipo_impressao').val(obj.value);
	}
}

function swapColorSet(from, to) {
	var newCor = [ 	$(from).children("img").css("background-color"), 
					$(from).children("img").attr("rel"), 
					$(from).children("img").attr("title") ];
	var oldCor = [ 	$(to).children("img").css("background-color"), 
					$(to).children("img").attr("rel"), 
					$(to).children("img").attr("title") ];

	$(from).children("img")
		.attr("rel", oldCor[1])
		.attr("title", oldCor[2])
		.css("background-color", oldCor[0]);
	$(to).children("img")
		.attr("rel", newCor[1])
		.attr("title", newCor[2])
		.css("background-color", newCor[0]);
	
	$(from).closest("div[id*='multi_corpick_']").toggle();
}
