// Common namespace
if(typeof MISS == 'undefined')
    MISS = {}

MISS.context ='';

MISS.setContext = function(path) {
    MISS.context = path;
}

MISS.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
}

MISS.parseCoupon = function() {
    var lastSiteClicked;
    var clipboard=null;

    $$(".coupon").each(function(element){
        var selecteur=element.id;
        var data = selecteur.split("-");
        var couponId=data[0];
        var siteId=data[1];

        var divYesVote = $$("#"+selecteur + " .couponOk")[0];
        var divNoVote = $$("#"+selecteur + " .couponWrong")[0];

        if(divYesVote) {
            divYesVote.onclick = function(){
                new Ajax.Request(MISS.context+'/ajax/coupon.jsp?command=voteyes&cid='+couponId, {
                    onSuccess: function(response) {
                        alert('Merci de votre contribution');
                        window.location = this.location;
                    }
                });
            }
        }

        if(divNoVote) {
            divNoVote.onclick = function(){
                new Ajax.Request(MISS.context+'/ajax/coupon.jsp?command=voteno&cid='+couponId, {
                    onSuccess: function(response) {
                         alert('Merci de votre contribution');
                         window.location.reload(true) ;
                    }
                });
            }
        }

        Event.observe(element, 'mouseover', function(event) {
            var divCode = $$("#"+couponId+"_p span")[0];
            var codeLink = $$("#dest-"+couponId)[0];
            var divCodeValueContainer = $$("#"+couponId+"_p .codeUncovered")[0];
            var divCodeValue = $$("#"+couponId+"_p #"+couponId+"Value")[0];
            Event.observe(divCode,"mouseover",function(event) {
                if(clipboard!= null) clipboard.destroy();
                clipboard=new ZeroClipboard.Client;
                var j=divCode.positionedOffset();
                clipboard.setText(divCodeValue.innerHTML);
                clipboard.glue(divCode);
                clipboard.addEventListener("onMouseOver",function() {
                    divCode.className = "codeCoverEn" ;
                });
                clipboard.addEventListener("onMouseOut",function() {
                    divCode.className = "codeCover" ;
                });
                clipboard.addEventListener("onComplete",function() {
                    divCode.setStyle({backgroundColor:"#FEBF01",borderColor:"#DBA100"});
                    divCodeValueContainer.show();
                    divCode.hide();
                    if(lastSiteClicked!=siteId) {
                        lastSiteClicked=siteId;
                        if(!window.open(MISS.context+"/redirect.jsp?cid="+couponId,"merchantWindow")) {
                            window.location=MISS.context+"/redirect.jsp?cid="+couponId+".html";
                        }
                    }
                    window.location = codeLink.href;
                });
                clipboard.addEventListener("mouseout",function() {
                    clipboard.destroy()
                });
            });
            Event.stopObserving(element,"mouseover");
        })
    });
}

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+$/,"");
}

// 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);
            }
        });
    }
})

