function getSettings(w,h)
{
    if(screen.width)
    {
    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;
    }
    else
    {
    winl = 0;
    wint =0;
    }

    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;

    var settings = 'height=' + h + ',';
    settings += 'width=' + w + ',';
    settings += 'top=' + wint + ',';
    settings += 'left=' + winl + ',';
    //settings += ",status=no,toolbar=no,titlebar=no,menubar=no,location=no";

 return settings;
}

function passar(a)
{
   window.location.href=a;
}

/*
function obre_detall(link)
{
   window2 = window.open(link,'finestraproducte',getSettings(400,400)+'resizable=no,scrollbars=yes,toolbar=no,statusbar=no,locationbar=no');
   window2.focus();
}
*/

function detallCerca(id)
{
 $('[id^=detall]:visible').hide();
 $('#detall'+id).show();
 return false;
}


function detall(id)
{
 $('[id^=detall]:visible').hide();
 $('#detall'+id).show();
 correDetall(0);
 return false;
}

function detallTanca()
{
 $('.fletxa').hide();
 $('div[id^=detall]').hide();
 $('#cortina').hide();
}


function correDetall(quant)
{
    var duracio=750;

    var quadresProducte = $('div[id^="quadreProducte"]');
    $.each(quadresProducte,function(index,value)
    {

     var sortint=$('[id^=detall]:visible');

     if( $('#'+$(this).attr('id')+' + [id^=detall]').attr('id') ==sortint.attr('id'))
     {

	if(index+quant>=0 && index+quant<quadresProducte.length)
	{

	 var entrant= $('#'+quadresProducte[index+quant].id+' + [id^=detall]');

	 $('#cortina').show();
         $('.fletxa').hide();

	if(quant==0) // si es 0 no cal animacio
	{
	 duracio=0;
	}
	else
	{

	    if(quant>0) // dreta
	    {
		desde=$(window).width()+entrant.width();
		fins=sortint.width()*-1;
	    }
	    else // esquerra
	    {
		desde=sortint.width()*-1;
		fins=$(window).width()+entrant.width();
	    }

	    entrant.css('left',desde).css('opacity','0');

	    var p=sortint.position();

	    sortint.css('left',p.left).animate({left:fins,opacity:'0'},{duration:duracio,easing:'swing',complete:function(){$(this).hide().css('left','50%').css('opacity','1')}});


	}

	 entrant.show().animate({left: '50%',opacity:'1'},{duration:duracio,easing:'swing',complete:function(){fletxes(index,quant,quadresProducte.length)}});

	}
	 return false;
     }

    }
    )
}


function fletxes(index,quant,totalElements)
{
    var duracio=750;

	$("#fletxadreta").css('top','50%').css('left','50%').css('margin-top','-50px').css('margin-left','220px').css('opacity','0');
	$("#fletxaesquerra").css('top','50%').css('left','50%').css('margin-top','-50px').css('margin-left','-231px').css('opacity','0');

	if(index+quant+1<totalElements)
	{
	    $("#fletxadreta").show();
	}

	if(index+quant>0)
	{
	    $("#fletxaesquerra").show();
	}

	$('.fletxa').animate({opacity:'0.7'},{duration: duracio, step: function(){ if(!$('.detall:visible').size()){detallTanca();} }});
}

$(document).keydown(function(e)
{

    switch(e.keyCode)
    {
	case 39:
		correDetall(1);
	  return false;

	case 37:
		correDetall(-1);
	  return false;

	case 40:
		correDetall(4);
	  return false;

	case 38:
		correDetall(-4);
	  return false;

	case 27:
		detallTanca();
	  break;
    }


});

$("#fletxadreta").live('click',function(){correDetall(1)});
$("#fletxaesquerra").live('click',function(){correDetall(-1)});

$(".fletxa").live('mouseover',function(){$(this).css('opacity','1')});
$(".fletxa").live('mouseout',function(){$(this).css('opacity','0.7')});

$('#tancarDetall').live('click',detallTanca);

$('#tancarDetall').live('mouseover',function(){$(this).css('opacity','1').css('box-shadow','3px 3px 1px #666').css('-moz-box-shadow','3px 3px 1px #666').css('-webkit-box-shadow','3px 3px 1px #666').css('box-shadow','3px 3px 1px #66');});

$('#tancarDetall').live('mouseout',function(){$(this).css('opacity','1').css('box-shadow','').css('-moz-box-shadow','').css('-webkit-box-shadow','').css('box-shadow','')});

 
function esta_ple(d1)
{
/* Torna
        0: Camp Buit
        1: Camp Ple
*/
    if (d1.value=='') return 0; else return 1;
}

function comprovar()
{
    // verifica Nom
    with (document.cerc)
    {
	result2 = esta_ple(producte);
	if (result2 == 0)
	{
	    alert('No has omplert el camp per buscar el producte');                         
	    producte.focus();
	    return false;
	}
	else return true;
    }
}

function ves_producte(codi,pro)
{
   window.location.href="/categories.php?codi="+codi+"&pro="+pro;
}

function amplia_foto(a,x,y)
{
    w_amplia_foto= window.open('client/img/'+a,'w_amplia_foto',getSettings(x,y)+',resizable=no');
    w_amplia_foto.focus();
}


$(function() {

$('a[rel=lightbox]').mouseenter( function(){ $(this).css('opacity','0.7'); $(this).children("img").css('opacity','0.7').css('border','2px solid #CC3229'); }); 
$('a[rel=lightbox]').mouseleave( function(){ $(this).css('opacity','1'); $(this).children("img").css('opacity','1').css('border','2px solid white'); });

/*$('a[rel*=jqlightbox]').lightBox();*/

});

