// # -*- coding: utf-8 -*-   
var allowCartFlash=true;
var $dialog =$("<div id='cartDialog'></div>").dialog({ autoOpen: false, width: 680, height: 320, modal: true });

function cartProdSelect(k,flash) {
	allowCartFlash = (flash == null) ? true : flash;
	$("#dropbox").html("Cargando..."); 
	$("#cartDialog").load('/lib/php/carrito/carrito.php',{a:'l',k:k}); 
	$dialog.dialog("option", "title", "Seleccione su producto"); 
	$dialog.dialog("open"); 
}

function cartUnds(k,unds, flash) {
	allowCartFlash = (flash == null) ? true : flash;
	$.getJSON("/lib/php/carrito/carrito.php",{a:"m",k:k,unds:unds}, 
		function(res){
				document.getElementById("p"+res.k).cells[3].innerHTML = parseFloat(res.sT).toFixed(2) + " &euro;";
				cartRefresh(res.g,res.bG,res.iG, res.b,res.i,res.p);
				allowCartFlash=true;
			} 
	);
}

function cartAdd(idp,unds) {
	$.getJSON("/lib/php/carrito/carrito.php",{a:"u",k:0,idp:idp,unds:unds}, function(res){cartSet(res.nProd,res.nProdDiff,res.pv);cartFlash();} );
}

function cartProdSelected(k,idp) {
	$.getJSON("/lib/php/carrito/carrito.php",{a:"u",k:k,idp:idp}, function(res) {
		
		if ( res.idp>0 ) {
			var row = document.getElementById("p"+res.k);
			if ( row ) {
				row.cells[1].innerHTML ='<a href="?f='+res.fDescr+'" onmouseover="Tip(ttProd('+res.idp+'),BALLOON,true)" onmouseout="UnTip()">'+res.descr+'</a>';
				row.cells[1].onclick = eval("var f=function() { cartProdSelect("+res.k+");return false; };f;");
				row.cells[3].innerHTML = parseFloat(parseFloat(res.pv)*1.16).toFixed(2) + " &euro;";
			} else {
				// Añadir elemento...
/*				
				row.cells[4].childNodes[0].onclick=eval("var f=function(){ cartDel("+res.idp+");};f");
*/				
			}
			cartRefresh(res.g,res.gB,res.gI,res.sB,res.sI,res.idp);
			allowCartFlash=true;
		}
		
		}
	);
	$dialog.dialog("close");
}


function cartDel(k) {
	if ( confirm("Confirme que desea quitar del carrito ese producto.") )
		$.getJSON("/lib/php/carrito/carrito.php", {a:"d",k:k}, function(res) { if (res.k>=0) {document.getElementById("p"+res.k).parentNode.deleteRow( document.getElementById("p"+res.k).rowIndex ); cartRefresh(res.g,res.bG,res.iG,res.b,res.i,res.p);} });
}

function cartRefresh(grupo,gB,gI, sB, sI, p) {
	document.getElementById("miCarritoPVP_"+grupo).innerHTML = parseFloat(gB+gI).toFixed(2) + " &euro;";
	
	if ( document.getElementById("cartB") ) document.getElementById("cartB").innerHTML = parseFloat(sB).toFixed(2) + " &euro;";
	if ( document.getElementById("cartI") ) document.getElementById("cartI").innerHTML = parseFloat(sI).toFixed(2) + " &euro;";
	if ( document.getElementById("cartT") ) document.getElementById("cartT").innerHTML = parseFloat(sB+sI).toFixed(2) + " &euro;";
	
	
	cartSet(0,0,sB);
	cartFlash();
}

function cartSet(nProd,nProdDiff,pv) {
	if ( pv>0 ) {
		$("#carrito").addClass(".carritoLleno");
		document.getElementById("carrito").innerHTML="<small>"+nProd+" Productos en su carrito</small>"+
							  "<p style=\"margin-left:60px\">Diferentes: "+nProdDiff+
							  "<br />Total: </p><div style=\"text-align:right;\">"+parseFloat(pv).toFixed(2)+" &euro;</div>"+
							  "<p style=\"text-align:right;\"><br /><a href=\"/verCarrito\" style=\"color:yellow\">ver carrito</a></p>";
	} else {
		$("#carrito").removeClass(".carritoLleno");
	}
}

function cartFlash() {
	if ( !allowCartFlash ) return;
	
	
	var t = jQuery('#carrito');

	absolute = false;
	
        t.css({
            position:    absolute ? 'absolute' : 'fixed', 
            left:        '50%', 
            top:        '50%', 
            zIndex:        '99'
        }).css({
            marginLeft:    '-' + (t.outerWidth() / 2) + 'px', 
            marginTop:    '-' + (t.outerHeight() / 2) + 'px'
        });

        if (absolute) {
            t.css({
                marginTop:    parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
                marginLeft:    parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
            });
        }
//    t.show('explode', 1000, function(){ setTimeout( function(){ $("#carrito").css({position:'absolute',visibility:'hidden',marginLeft:-45,marginTop:0}); } , 1500);  }  );
    t.show('explode', { pieces: 9 }, 1000, function(){ setTimeout( function(){ $("#carrito").css({position:'absolute',visibility:'hidden',marginLeft:-45,marginTop:0}); } , 1500);  }  );
}

