// Common namespace
if(typeof Common == 'undefined')
    Common = {}

Builder.dump();

Common.changeDivContent = function(div_id, target_url) {
    var url = target_url;
    new Ajax.Updater(div_id, url );
}

Common.setContext = function(path) {
    Common.context = path;
}

Common.inArray = function(a,b) {
    if(b.indexOf) return b.indexOf(a);

    for(var d=0,f=b.length;d<f;d++)
        if(b[d]===a)return d;

    return -1
}

var FillOrRemoveFieldContent = Class.create({
    initialize: function(fieldId, defaultText, isPassField){
        $(fieldId).observe('focus', function(event){
            if(isPassField) $(fieldId).setAttribute('type', 'password');
            if($(fieldId).value == defaultText){
                $(fieldId).value = '';
            }
        });

        $(fieldId).observe('blur', function(event){
            if($(fieldId).value == ''){
                if(isPassField) $(fieldId).setAttribute('type', 'text');
                $(fieldId).value = defaultText  ;
            }
        })
    }
});

//Copy in clipboard
var Copy = Class.create({
    initialize: function(elementToCopyId, glueId, noticeId, noticeText){
        var elementToCopy = $(elementToCopyId);
        var notice = $(noticeId);
        var clipboard=new ZeroClipboard.Client();
        clipboard.setHandCursor( true );
        clipboard.setText('');
        clipboard.addEventListener("mouseOver", function(){
            var text = elementToCopy.innerHTML ;
            text = text.replace(/&lt;/gi,"<");
            text = text.replace(/&gt;/gi,">");
        });
        clipboard.addEventListener("mouseDown", function(){
            var text = elementToCopy.innerHTML ;
            clipboard.setText(text);
            notice.update = noticeText;
            notice.show();
        });
        clipboard.glue(glueId);
    }
})

//Copy in clipboard
var OpenCoupon   = Class.create({
    initialize: function(couponUrl, popupId, merchantUrl, merchantId, dialogText){
        couponWindow = window.open(couponUrl, popupId, 'left=0,screenX=0,width=500,height='+screen.height+',scrollbars=yes');
        merchantWindow = window.open(merchantUrl, merchantId, 'width='+(screen.width-480)+', height='+screen.height+',status=1,toolbar=1,scrollbars=1, resizable=1, screenX=480,left=480');

        if(!merchantWindow){
            couponWindow.blur();
            Dialog.alert(dialogText , {
                className:"alphacube",width:400, height:170, okLabel: "OK",
                ok:function(win) {
                    var newWin = window.open(merchantUrl, merchantId, 'width='+(screen.width-480)+', height='+screen.height+',status=1,toolbar=1,scrollbars=1, resizable=1, screenX=480,left=480');
                    couponWindow.focus();
                    newWin.focus();
                    win.close();
                }
            });
        }
    }
})


//Slide
var Slide = Class.create({
    initialize: function(fireLinkId, elementToSlideID, elementDefaultClassName){
        this.elementToSlide = $(elementToSlideID);
        this.elementClassName = elementDefaultClassName;
        this.fireLink = $(fireLinkId);
        try{
            this.fireLink.observe('click', this.play.bind(this));
        }catch(Err){}
    },

    play: function(){
        this.openedItems = document.getElementsByClassName(this.elementClassName + ' opened');
        this.openedItem = this.openedItems[0];
        if(this.elementToSlide.className == this.elementClassName){
            Effect.BlindDown(this.elementToSlide);
            this.elementToSlide.className = this.elementClassName + ' opened';
            try{
                this.openedItem.hide();
                this.openedItem.className = this.elementClassName;
            }catch(err){}

        }else if(this.elementToSlide.className == this.elementClassName + ' opened'){
            Effect.BlindUp(this.elementToSlide) ;
            this.elementToSlide.className = this.elementClassName;
        }
    }
});

var Vote = Class.create({
    initialize: function(actionUrl, referer){
         new Ajax.Request(actionUrl, {
            onSuccess: function(response){
                alert('Merci de votre contribution');
                location.reload();
            }
        });
    }
})

// Banner rotate
function loadBanner(targetId, target, bannerSize,idToUpdate, url, delay){
    new Ajax.PeriodicalUpdater(
        idToUpdate,
        url,
        {
            method : 'get',
            parameters : {
                tid : targetId,
                target: target,
                size: bannerSize
            },
            frequency: delay == null ? 10 : delay
        }
    );
}

// None rotate banner
var LoadBanner = Class.create({
    initialize: function(targetId, target, bannerSize,idToUpdate, url){
         new Ajax.Request(url, {
            method: 'post',
            parameters: {
                tid: targetId,
                target: target,
                size: bannerSize
            },
            onSuccess: function(t){
                $(idToUpdate).update(t.responseText);
            }
        });
    }
})

var BgClickable = Class.create({
    initialize: function(containerW, siteUrlTrk){
        /** Background Image Clickable **/
        this.containerWidth = containerW;
        this.mouseX = 0 ;
        this.status = true;

        var o = this;

        Event.observe(window,"load",function() {
            Event.observe($$('body')[0], "click", function(event){
                if(o.status) {
                    var e = event || window.event;
                    o.mouseX = e.clientX ;
                    bodyOfPage = $$('body')[0];
                    if(o.verifyMouseX()) {
                        window.open(siteUrlTrk);
                        o.status = true;
                    }
                }
            });

            Event.observe(document, 'mouseover', function(event){
                var e = event || window.event;
                o.mouseX = e.clientX ;
                bodyOfPage = $$('body')[0];

                if(o.verifyMouseX()) {
                    bodyOfPage.setStyle({
                        cursor: 'pointer'
                    });

                } else {
                    bodyOfPage.setStyle({
                        cursor: 'default'
                    });
                    status = true;
                }
            });
        });

    },

    _checkScrollBar: function(){
        if(document.documentElement.scrollHeight != window.innerHeight) return 16
        else return 0
    },

    verifyMouseX: function(){
        if(this.mouseX <=((this._windowSize() - this.containerWidth)/2) || this.mouseX >= ((this._windowSize()-this.containerWidth)/2) + this.containerWidth) return true;
        else return false;
    },

    _windowSize : function(){
        if (document.all)return document.documentElement.clientWidth;
        else return window.innerWidth- this._checkScrollBar();
    }
})


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

var UpdateField = Class.create({
    initialize: function(targetId, url, evalscript) {
        new Ajax.Updater(targetId, url,{ method: "get", evalScripts: evalscript });
    }
})
