document.observe('dom:loaded', function() {
    var tooltip = TooltipManager.addTooltipByDom("tooltip", "tooltip_container", {delayOver: 0, delayOut: 0,className: 'tooltip_skin', shiftY:-30, shiftX:10});
});
Event.observe(window, 'load', function() {
    equalHeightByGroup($$(".mea-product-compatible span.sub-2-cols span.reception-header"),2);
});

// enabled/disabled address field group
var EnableAdressFieldGroup = Class.create();
EnableAdressFieldGroup.prototype = {
    initialize : function(group){
        var fieldGroup = new Array($(group+':postcode'),$(group+':city'),$(group+':street1'),$(group+':street2'));
        this.fieldGroup = fieldGroup;
        
        fieldGroup.each(function(element){
            var index = fieldGroup.indexOf(element);

            if(fieldGroup.indexOf(element) != 0) {
                element.disabled = true;
                element.up("span.input-box").addClassName("disabled");
            }

            element.observe("keyup", function() {
                if(element.id.indexOf('postcode')!=-1 
                && element.getValue().length==5
                && fieldGroup.indexOf(element) != fieldGroup.length -1) {
                    fieldGroup[index+1].disabled = false;
                    fieldGroup[index+1].up("span.input-box").removeClassName("disabled");
                }
                else if(element.id.indexOf('postcode')==-1 
                && element.getValue()!=""
                && fieldGroup.indexOf(element) != fieldGroup.length -1) {
                    fieldGroup[index+1].disabled = false;
                    fieldGroup[index+1].up("span.input-box").removeClassName("disabled");
                }
                else {
                    for(i = index;i<fieldGroup.length;i++) {
                        if(fieldGroup[i+1]) {
                            fieldGroup[i+1].disabled = true;
                            fieldGroup[i+1].up("span.input-box").addClassName("disabled");
                            fieldGroup[i+1].clear();
                        }
                    }
                }
            });
        });
    },

    update : function() {
        this.fieldGroup.each(function(element){
            element.disabled = false;
            element.up("span.input-box").removeClassName("disabled");
        });
    }
}

// Decoder Pagination
var DecoderPagination = Class.create();
DecoderPagination.prototype = {
    initialize : function(pagination, classSelector){
        obj = this;
        var brandsList = $$(classSelector);
        brandsListPagination = brandsList.inGroupsOf(pagination);
        obj.page = 0;

        obj.displayPagination(obj.page);
        obj.displayPaginationButton(obj.page);

        $("next").observe('click',function(){
            var page = obj.page+1;
            if($R(0, brandsListPagination.length-1).include(page)){
                obj.displayPagination(page);
            }
            obj.displayPaginationButton(page);
        });

        $("before").observe('click',function(){
            var page = obj.page-1;
            if($R(0, brandsListPagination.length-1).include(page)){
                obj.displayPagination(page);
            }
            obj.displayPaginationButton(page);
        });
    },
    displayPagination: function(currentPage) {
        brandsListPagination.each(function(group, index){
            if(index == currentPage){
                group.each(function(brand){
                    if(brand!=null)brand.style.display = "";
                });
            }
            else{
                group.each(function(brand){
                    if(brand!=null)brand.style.display = "none";
                });
            }
            obj.page = currentPage;
        });
    },
    displayPaginationButton: function(currentPage) {
        (currentPage == brandsListPagination.length-1) ? $("next").style.display = "none" : $("next").style.display = "block";
        (currentPage == 0) ? $("before").style.display = "none" : $("before").style.display = "block";
    }
}


// Enabled button "Continue" depending the checkbox state (ie : step checkoutcart, step coordinate/payment).
function enabledButtonWithCheckbox(checkbox,target){
    $(checkbox).observe('click', function() {
        if($(checkbox).checked == true) {
            $(target).removeClassName('disabled');
            $(target).disabled = false;
        }
        else {
            $(target).addClassName('disabled');
            $(target).disabled = true;
        }
    });
}

// Expand link hit area.
function expandLinkHitArea(){
    $$(".expand-link-hit-area").each(function(elt){
        $(elt).observe('click', function(event){
            // Redirect only on a left click.
            if(Event.isLeftClick(event)) {
                // Get the last link href value and set this value to the redirect.
                $(document).location.href = $(elt).down('a').readAttribute('href');
            }
        });
    });
}

// show modal with iframe content.
function showList(url, title) {
    win = new Window({showEffectOptions: {duration:0.2}, hideEffectOptions: {duration:0.2}, title: title, width:682, height:420,url: url, destroyOnClose:true, resizable:false, minimizable:false, maximizable:false, draggable:false, zIndex:4000});
    win.showCenter(true);
}

// show modal with ajax content.
function showModale(request, title) {
    win = new Window({showEffectOptions: {duration:0.2}, hideEffectOptions: {duration:0.2}, title: title, width:682, height:420,destroyOnClose:true, resizable:false, minimizable:false, maximizable:false, draggable:false, zIndex:4000, onClose : function() {$('overlay_modal').remove();}});
    win.setAjaxContent(request, {
        onLoading: function(response) {
            // A la fermeture (avant affichage) de la fenêtre modale, on annule la requête Ajax.
            win.setCloseCallback(function() {
                response.transport.abort();
                return true;
            });
        }
    });
    win.showCenter(true);
}

// show modal with content in page.
function showModaleContentInPage(id, title){
    win = new Window({showEffectOptions: {duration:0.2}, hideEffectOptions: {duration:0.2}, title: title, width:682, height:420,destroyOnClose:true, resizable:false, minimizable:false, maximizable:false, draggable:false, zIndex:4000});
    win.setContent(id);
    win.showCenter(true);
}

/* Set element height to the same value (tallest) */
function equalHeight(elementsToFixe){
    /* Find the tallest value element */
    var heightToSet = elementsToFixe.max(function(element) {
        return parseInt(element.getStyle('height').replace('px',''));
    });

    /* Set the tallest value height to every elements */
    elementsToFixe.invoke("setStyle", {
        height: heightToSet + 'px'
    });
}

function equalHeightByGroup(elementsToFixe, groupSize){
    var elementsToFixeGroup = elementsToFixe.inGroupsOf(groupSize, {  });
    elementsToFixeGroup.each(function(group){
        equalHeight(group);
    });
}
