/*
ProtoJmfr - Handles the request to add or remove items from the jmfr cart
Because we cant know if a item is already in the jmfr cart in the DOM we handle this
on the server side.
 
When a user clicks on any jmfr button the the products list or any list where jmfr is located
a onclick event send a toogle request via ajax to the jmfrview.php
the jmfrview.php then returns a state to the ProtoJmfr
 
The different States is
added = the products was sucessfully added to the jmfr view
removed = the product was already in the jmfrview and because of that it was removed
full = the jmfrview limit is reached and products needs to be removed from the jmfrview
*/

if (typeof Effect == 'undefined')
    throw ("protojmfr.js requires including script.aculo.us' effects.js library!");

if (!window.ProtoJmfr)
    var ProtoJmfr = new Object();

ProtoJmfr.Methods = {

    options: {
        method: 'get',
        jmfrUrl: '/AjaxPages/JmfrView.aspx', // Changed by Martin A
        params: {}
    },

    doEffects: function() {
        new Effect.Appear('fadejmfr', { queue: 'front', duration: 0.4 });
        new Effect.Pulsate('fadejmfr', { queue: 'end', pulses: 1, duration: 0.5 });
        new Effect.Fade('fadejmfr', { queue: 'end', duration: 0.4 });
    },

    toogleItem: function(stockcode) {
        var cacheBuster = new Date().getTime();

        var pars = cacheBuster + '&toogleitem=' + stockcode;

        var self = this;

        this.itemRequest = new Ajax.Request(this.options.jmfrUrl, { method: this.options.method.toLowerCase(), parameters: pars,
            onSuccess: function(transport) {

                if (transport.responseText == "added") {
                    if ($('jmfrSmall' + stockcode)) ($('jmfrSmall' + stockcode)).setStyle({ backgroundPosition: '0px -15px' });
                    if ($('jmfrLarge' + stockcode)) ($('jmfrLarge' + stockcode)).setStyle({ backgroundPosition: '0px -15px' });
                    if ($('jmfrAccessories' + stockcode)) ($('jmfrAccessories' + stockcode)).setStyle({ backgroundPosition: '0px -15px' });
                    self.doEffects();
                }

                if (transport.responseText == "removed") {
                    if ($('jmfrSmall' + stockcode)) ($('jmfrSmall' + stockcode)).setStyle({ backgroundPosition: '0px -0px' });
                    if ($('jmfrLarge' + stockcode)) ($('jmfrLarge' + stockcode)).setStyle({ backgroundPosition: '0px -0px' });
                    if ($('jmfrAccessories' + stockcode)) ($('jmfrAccessories' + stockcode)).setStyle({ backgroundPosition: '0px -0px' });
                    self.doEffects();
                }

                if (transport.responseText == "full") {
                    Modalbox.show('/AjaxPages/JmfrMax.aspx', { title: "CAPTECH WEBSHOP" }); // Changed by Martin A
                }

            } .bind(this),
            onException: function(instance, exception) {

                throw ('ProtoJmfr Loading Error: ' + exception);
            }
        });
    }
}

Object.extend(ProtoJmfr, ProtoJmfr.Methods);