// -----------------------------------------------------------------------------------------------------------
// Common
//
function addOnLoadCallback(func) {
    Event.observe(window, 'load', func, false);
}

function BusEvent() {
    this.eventListeners = new Array();
}
BusEvent.prototype.addListener = function(eventListener) {
    this.eventListeners.push(eventListener);
};
BusEvent.prototype.removeListener = function(eventListener) {
    var i = 0;
    while (i < this.eventListeners.length) {
        if (this.eventListeners[i] == eventListener) {
            this.eventListeners.splice(i, 1);
        }
        else {
            i++;
        }
    }
};
BusEvent.prototype.fire = function(args) {
    for (var i = 0; i < this.eventListeners.length; i++) {
        this.eventListeners[i](args);
    }
};

var EMPTY_FUNCTION = function() {
    return false;
};
function disableForm(selector) {
    $$(selector + 'form').each(function(formNode) {
        formNode.onsubmit = EMPTY_FUNCTION;
        formNode.disable();
    });
    $$(selector + 'input.submitButton').each(function(node) {
        node.setStyle({opacity: 0.5, cursor: "wait"});
    });
}

// -----------------------------------------------------------------------------------------------------------
// Cart management
//

function displayCart() {
    updateCart('cartDisplayAction.action', null, null);
}
// Empty function to force load event
//   (necessary when the user use the back button)
function resetCartOnce() {
}
function addToCart(ref) {
    updateCart("cartAddAction.action",
               "rowId=" + ref,
               function() {
                   new Growl({title:'Caddie',
                                 message:"L'article <b>" + ref + "</b> est ajout&eacute; au caddie",
                                 icon : "styles/default/images/cart/cart-addNotification.png"
                             }).smoke();
               });
}
function addToCartNoNotif(ref) {
    updateCart("cartAddAction.action", "rowId=" + ref, null);
}
function removeToCartNoNotif(ref) {
    updateCart("cartRemoveAction.action", "rowId=" + ref, null);
}
function updateCart(action, actionParams, successFunc) {
    var params = '?now=' + new Date().getTime();
    if (actionParams) {
        params += "&" + actionParams;
    }

    new Insertion.Top('cart_content',
                      "<div id='cartUpdate' class='cartUpdate' style='display:none'>Mise &agrave; jour du caddie..</div>");

    Effect.Appear('cartUpdate');

    if (successFunc) {
        new Ajax.Updater('cart_content', action + params, { method: 'get', onSuccess: successFunc });
    }
    else {
        new Ajax.Updater('cart_content', action + params, { method: 'get' });
    }
}

// -----------------------------------------------------------------------------------------------------------
// Account Managements Listener
//
var onAccountLoginBus = new BusEvent();
// -----------------------------------------------------------------------------------------------------------
// Account Managements
//
var modalBox;

function closeAccountBox() {
    modalBox.closeDialog();
}

function displayMyAccount() {
    modalBox = new ModalBox(ModalBox.Themes.gray);
    modalBox.userState = null;

    modalBox.setCloseListener(function() {
        if (modalBox.userState) {
            onAccountLoginBus.fire(modalBox.userState);
        }
    });

    modalBox.showDialog($('accountBox'), 670, 360);
    $("accountBoxContent").innerHTML
          = waitingMessage("Chargement de mon compte....", "display:none;color:#555555;");
    Effect.Appear($("accountBoxContent").down());
    updateAccountBox("account/ajaxMyAccount.action", null);
}

function declareAccountForm(action, formId) {
    var submitId = formId.id + "submit";
    $(formId).down("input.submitButton").id = submitId;

    $(formId).onsubmit = function() {
        new Insertion.After(submitId, waitingMessage("Connexion...", "display:none;color:#555555;position:absolute;"));
        new Effect.Appear($(submitId).next());
        updateAccountBox(action, $(formId).serialize());
        disableForm('#accountBoxContent ');
        return false;
    };
}

function updateAccountBox(action, actionParams) {
    var params = '?now=' + new Date().getTime();
    var type = 'get';
    if (actionParams) {
        params += "&" + actionParams;
        type = 'post';
    }
    new Ajax.Updater('accountBoxContent', action + params, { method: type, evalScripts:true });
}

function waitingMessage(message, style) {
    return "<span style='" + style + "'>"
                 + "<img style='position:relative;top:3px;' src='styles/default/images/loading.gif'/>"
                 + message + "</span>";
}

function actionMessageBeautifier() {
    var settings = {
        tl: { radius: 10 },
        tr: { radius: 10 },
        bl: false,
        br: false,
        antiAlias: true,
        autoPad: false
    };
    var curvy = new curvyCorners(settings, "actionMessage");
    curvy.applyCornersToAll();
    Effect.Appear($$("div.actionMessage")[0]);
}
