function IsSelectionned ( photo_id )
{
    photo = document.getElementById ( photo_id );
    if ( Element.hasClassName ( photo, 'selected' ) )
        return true;
    else
        return false;
}

function ChangeSelection ( photo_id )
{
    photo_html_id = 'photo_' + photo_id ;
    if ( IsSelectionned ( photo_html_id ) )
        action = 'remove';
    else
        action = 'add';


    Element.addClassName(photo_html_id, 'selecting');

    new Ajax.Updater(
        'cart',
        '/cart/' + action + '?photo_id=' + photo_id,
            {
                asynchronous:true,
                method:'get',
                onSuccess: function() { doEffects(photo_id, photo_html_id); }
            }
    );
    return false;
}

function doEffects ( photo_id, photo_html_id  )
{
    // on trouve quelle est la couleur de fond
    background_color = convertBgColor( getBgColor('tools1') ) ;

    new Effect.Highlight('cart_fx', {
                    startcolor:'#FF5B03',
                    endcolor: background_color,
                    restorecolor: background_color,
                    queue: 'start'});

    new switchState( photo_html_id );
}


function switchState ( photo_html_id )
{
    // removing spinner
    Element.removeClassName(photo_html_id, 'selecting');

    if ( IsSelectionned (photo_html_id ) )
    {
        Element.removeClassName(photo_html_id, 'selected');
        if ( $('preview_' + photo_html_id ) )
        {
            Element.removeClassName('preview_' + photo_html_id, 'selected');
            Element.addClassName('remove_from_cart', 'hidden');
            Element.removeClassName('add_to_cart', 'hidden');
        }

    }
    else
    {
        Element.addClassName(photo_html_id, 'selected');
        if ( $('preview_' + photo_html_id ) )
        {
            Element.addClassName('preview_' + photo_html_id, 'selected');
            Element.addClassName('add_to_cart', 'hidden');
            Element.removeClassName('remove_from_cart', 'hidden');
        }
    }
}


/* outils pour retrouver la couleur de fond d'un élément */

/*  Autor 2002 Kristof Lipfert, Duesseldorf, modifié par François Simond  */

/* Kristof Lipfert (c) 2002 Düsseldorf */
function getBgColor ( doc_el )
{
    if(window.getComputedStyle)
        return window.getComputedStyle( document.getElementById(doc_el), "" ).getPropertyValue('background-color');
    else if(document.getElementById && document.getElementById(doc_el).currentStyle)
        return document.getElementById(doc_el).currentStyle.backgroundColor;
    else if(document.ids)
        return document.layers[doc_el].bgColor;
    else if(document.all)
        return document.all[doc_el].style.backgroundColor;
}

function convertBgColor(Farbe)
{
    if(document.ids)Farbe="#00000".substr(0,(7-Farbe.toString(16).length))+Farbe.toString(16);

    else if(Farbe.indexOf('rgb')>-1)
    {
        Farbe=Farbe.split("(")[1].split(")")[0].split(",");
        for(i=0;i<3;i++){
            Farbe[i]=(Farbe[i]*1).toString(16);
        }
    Farbe="#"+Farbe.join("");
    }

    return Farbe;
}
